fix: remove string eval from gettext - #382
Merged
Merged
Conversation
codingLogan
marked this pull request as ready for review
September 11, 2026 18:42
codingLogan
requested review from
Jameson13B,
ash-wright123,
mwclemy and
wesrisenmay-mx
as code owners
September 11, 2026 18:42
wesrisenmay-mx
approved these changes
Sep 11, 2026
wesrisenmay-mx
left a comment
Collaborator
There was a problem hiding this comment.
I'm good with this IF we fast follow up on something that isn't a monkey patch.
|
🎉 This PR is included in version 2.38.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patches around gettext's unsafe-eval issue, and there is no update from our current version available: https://www.npmjs.com/package/gettext.js
Summary of Changes
Fixes an issue where loading plural translations in Spanish (
es) or French Canadian (fr-CA) causes the widget to crash with anEvalErrorunder hardened Content Security Policy (CSP) headers disallowing'unsafe-eval':Root Cause
In
gettext.js, plural formula strings (e.g.,nplurals=2; plural=(n!=1);) are compiled dynamically vianew Function("n", ...). When translated pluralmessages exist in the dictionary,
gettext.jsinvokes this function constructor, triggering the CSP violation.Solution
gettext.js@2.0.3viapatch-package:new Function("n", ...)call insidegetPluralFuncwith a lightweight, safe AST-based formula evaluator (parsePluralExprandevalPluralExpr).{ nplurals, plural }).n != 1,n > 1, ternary? :, modulo%, compound logic) in pure JavaScript with zeroevalordynamic code execution.
console.log('>>> Plural form:', ...)output from the library.patch-packagetodevDependenciesand a"postinstall": "patch-package"script topackage.json.Intl-test.tsxsimulating strict CSP enforcement (globalThis.Functionblocked to throwEvalError) to verifySpanish and French plural translations evaluate cleanly without violating CSP.