-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Backoffice: Add localize.htmlString() helper to prevent XSS in HTML-rendered translations #22731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
697eb9d
docs(claude): document how unsafeHTML should be used together with es…
iOvergaard a0a63ec
fix: adds escapeHTML where appropriate in order not to render html di…
iOvergaard 7291e8b
chore: removes small nitpick fallback
iOvergaard efd5463
docs(claude): fixes incorrect using of unsafeHTML
iOvergaard ea57cb6
feat(localization): add localize.htmlString() and convert call sites
iOvergaard a870d5a
chore(eslint): add no-unsafe-localize rule to flag unsafeHTML(localiz…
iOvergaard ed3d558
fix(localization): stringify htmlString args before escaping
iOvergaard 4b4f711
fix(installer-consent-element): sanitise content before rendering it
liamlaverty 6a64286
fix(dashboard-telem-element): sanitise html before rendering
liamlaverty 2268dd2
Merge remote-tracking branch 'origin/main' into v17/bugfix/escaped-de…
iOvergaard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/Umbraco.Web.UI.Client/devops/eslint/rules/no-unsafe-localize.cjs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Flags `unsafeHTML(<x>.localize.string(...))` and `unsafeHTML(<x>.localize.term(...))`. | ||
| * | ||
| * Wrapping a localized string in `unsafeHTML` leaves any interpolated args un-escaped — an XSS hazard | ||
| * when the args are user-controlled. Use `<x>.localize.htmlString(text, ...args)` instead, which | ||
| * escapes args via escapeHTML and returns a Lit unsafeHTML directive ready to inline in templates. | ||
| * | ||
| * See `docs/security.md` (XSS Prevention → Localized HTML) for the full pattern. | ||
| */ | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Disallow `unsafeHTML(<x>.localize.string|term(...))`. Use `<x>.localize.htmlString(...)` instead.', | ||
| category: 'Possible Errors', | ||
| recommended: true, | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| unsafeLocalize: | ||
| 'Avoid `unsafeHTML(...localize.{{method}}(...))` — interpolated args are not escaped (XSS hazard). Use `localize.htmlString(...)` instead.', | ||
| }, | ||
| }, | ||
| create(context) { | ||
| /** | ||
| * Returns true if the given AST node represents a `<...>.localize` member access, | ||
| * e.g. `this.localize`, `this.#localize`, `host.localize`, `this._localize`. | ||
| */ | ||
| function isLocalizeMemberAccess(node) { | ||
| if (!node || node.type !== 'MemberExpression') return false; | ||
| const prop = node.property; | ||
| if (!prop) return false; | ||
| // Match `localize` (regular) or any private/aliased identifier ending in `localize` (e.g. `#localize`, `_localize`). | ||
| if (prop.type === 'Identifier' && /localize$/i.test(prop.name)) return true; | ||
| if (prop.type === 'PrivateIdentifier' && /localize$/i.test(prop.name)) return true; | ||
| return false; | ||
| } | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| // Looking for: unsafeHTML(<inner>) | ||
| if (!node.callee || node.callee.type !== 'Identifier' || node.callee.name !== 'unsafeHTML') return; | ||
| if (node.arguments.length === 0) return; | ||
|
|
||
| const arg = node.arguments[0]; | ||
| if (!arg || arg.type !== 'CallExpression') return; | ||
|
|
||
| // <inner> must itself be a member-call: <localizeExpr>.string(...) or .term(...) | ||
| const innerCallee = arg.callee; | ||
| if (!innerCallee || innerCallee.type !== 'MemberExpression') return; | ||
| if (innerCallee.property?.type !== 'Identifier') return; | ||
|
|
||
| const method = innerCallee.property.name; | ||
| if (method !== 'string' && method !== 'term') return; | ||
|
|
||
| if (!isLocalizeMemberAccess(innerCallee.object)) return; | ||
|
|
||
| context.report({ | ||
| node, | ||
| messageId: 'unsafeLocalize', | ||
| data: { method }, | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.