Skip to content

fix: remove string eval from gettext - #382

Merged
wesrisenmay-mx merged 4 commits into
masterfrom
lr/CT-2455/translation-eval-fix
Sep 11, 2026
Merged

wesrisenmay-mx merged 4 commits into
masterfrom
lr/CT-2455/translation-eval-fix

Conversation

@codingLogan

@codingLogan codingLogan commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

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 an EvalError under hardened Content Security Policy (CSP) headers disallowing 'unsafe-eval':

 EvalError: Evaluating a string as JavaScript violates the following Content Security Policy directive because 'unsafe-eval' is not an allowed source of
 script: ...

Root Cause

In gettext.js, plural formula strings (e.g., nplurals=2; plural=(n!=1);) are compiled dynamically via new Function("n", ...). When translated plural
messages exist in the dictionary, gettext.js invokes this function constructor, triggering the CSP violation.

Solution

  • Patched gettext.js@2.0.3 via patch-package:
    • Replaced the unsafe new Function("n", ...) call inside getPluralFunc with a lightweight, safe AST-based formula evaluator (parsePluralExpr and
      evalPluralExpr).
    • Preserved the original variable names and signature ({ nplurals, plural }).
    • Evaluates standard GNU gettext plural formulas (n != 1, n > 1, ternary ? :, modulo %, compound logic) in pure JavaScript with zero eval or
      dynamic code execution.
    • Removed accidental console.log('>>> Plural form:', ...) output from the library.
  • Automated Patching: Added patch-package to devDependencies and a "postinstall": "patch-package" script to package.json.
  • Testing: Added unit tests in Intl-test.tsx simulating strict CSP enforcement (globalThis.Function blocked to throw EvalError) to verify
    Spanish and French plural translations evaluate cleanly without violating CSP.

@codingLogan
codingLogan marked this pull request as ready for review September 11, 2026 18:42
@codingLogan codingLogan self-assigned this Sep 11, 2026

@wesrisenmay-mx wesrisenmay-mx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with this IF we fast follow up on something that isn't a monkey patch.

@wesrisenmay-mx
wesrisenmay-mx merged commit b9510cc into master Sep 11, 2026
7 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.38.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants