From fada67d32c98c632cf69deb233c0562cef7545f1 Mon Sep 17 00:00:00 2001 From: martgil <46025304+martgil@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:31:08 +0800 Subject: [PATCH 1/4] fix(xss): drop negative margins from sanitized CSS Allowlisted negative margins let untrusted message content escape its container and repaint trust UI. Both CSS sanitizers now remove margin declarations with negative lengths, with regression tests for shorthand, calc(), !important and logical-property variants. --- extension/js/common/platform/xss.ts | 5 ++- .../tests/browser-unit-tests/unit-Xss.js | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/extension/js/common/platform/xss.ts b/extension/js/common/platform/xss.ts index 8dbfa7b00cd..7249ebf0889 100644 --- a/extension/js/common/platform/xss.ts +++ b/extension/js/common/platform/xss.ts @@ -145,6 +145,7 @@ export class Xss { 'unicode-bidi', ]); private static readonly DANGEROUS_CSS_PROPERTIES = new Set(['z-index', 'pointer-events', 'transform', 'filter', 'clip-path', 'clip']); // xss-none + private static readonly NEGATIVE_CSS_LENGTH = /-\s*(?:\d|\.)/; public static sanitizeRender = (selector: string | HTMLElement | JQuery, dirtyHtml: string) => { // browser-only (not on node) @@ -430,8 +431,9 @@ export class Xss { const style = document.createElement('span').style; style.cssText = css; for (const property of Array.from(style)) { + const lower = property.toLowerCase(); const value = style.getPropertyValue(property).trim(); - if (!this.ALLOWED_EMAIL_CSS_PROPERTIES.has(property.toLowerCase()) || !value) { + if (!this.ALLOWED_EMAIL_CSS_PROPERTIES.has(lower) || !value || (lower.startsWith('margin') && this.NEGATIVE_CSS_LENGTH.test(value))) { style.removeProperty(property); } } @@ -451,6 +453,7 @@ export class Xss { if (lower === 'position' && (['fixed', 'absolute', 'sticky'].includes(value) || value.includes('var('))) { style.removeProperty(property); } else if ( + (lower.startsWith('margin') && this.NEGATIVE_CSS_LENGTH.test(value)) || this.DANGEROUS_CSS_PROPERTIES.has(lower) || // xss-none lower === 'backdrop-filter' || lower.startsWith('mask') || diff --git a/test/source/tests/browser-unit-tests/unit-Xss.js b/test/source/tests/browser-unit-tests/unit-Xss.js index fc7fba66981..71bcba4dfd8 100644 --- a/test/source/tests/browser-unit-tests/unit-Xss.js +++ b/test/source/tests/browser-unit-tests/unit-Xss.js @@ -146,6 +146,40 @@ BROWSER_UNIT_TEST_NAME(`pgp badges stay above message content`); return 'pass'; })(); +BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitizeKeepBasicTags strips negative margins`); +(async () => { + const payloads = [ + `
signed
`, + `signed`, + `signed`, + `signed`, + `signed`, + ]; + for (const dirty of payloads) { + const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP'); + if (/margin[^;"]*-\s*[\d.]/.test(clean)) { + throw Error(`negative margin survived sanitization: ${clean}`); + } + } + const safe = Xss.htmlSanitizeKeepBasicTags(`safe`, 'IMG-KEEP'); + if (!/margin-top:\s*12px/.test(safe)) { + throw Error(`positive margin was removed: ${safe}`); + } + return 'pass'; +})(); + +BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitize strips negative margins`); +(async () => { + const clean = Xss.htmlSanitize(`signed`); + if (/-\s*33px/.test(clean)) { + throw Error(`negative margin survived sanitization: ${clean}`); + } + if (!/color:\s*green/.test(clean)) { + throw Error(`safe color was removed: ${clean}`); + } + return 'pass'; +})(); + BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitizeKeepBasicTags rejects encoded logout image paths`); (async () => { const urls = [ From 875df5b59b35696e826e0653a41dec96dd46e207 Mon Sep 17 00:00:00 2001 From: martgil <46025304+martgil@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:13:35 +0800 Subject: [PATCH 2/4] fix(xss): harden CSS sanitizer against var() and offset displacement Strip var()/attr()/env() margins and positioning offsets, and stop stripping valid positive calc() margins. Extends regression tests with the bypass variants found in review. --- extension/js/common/platform/xss.ts | 13 ++++-- .../tests/browser-unit-tests/unit-Xss.js | 42 ++++++++++++++----- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/extension/js/common/platform/xss.ts b/extension/js/common/platform/xss.ts index 7249ebf0889..e893ede7041 100644 --- a/extension/js/common/platform/xss.ts +++ b/extension/js/common/platform/xss.ts @@ -144,8 +144,10 @@ export class Xss { 'direction', 'unicode-bidi', ]); - private static readonly DANGEROUS_CSS_PROPERTIES = new Set(['z-index', 'pointer-events', 'transform', 'filter', 'clip-path', 'clip']); // xss-none - private static readonly NEGATIVE_CSS_LENGTH = /-\s*(?:\d|\.)/; + // prettier-ignore + private static readonly DANGEROUS_CSS_PROPERTIES = new Set(['z-index', 'pointer-events', 'transform', 'filter', 'clip-path', 'clip', 'top', 'right', 'bottom', 'left', 'inset', 'inset-block-start', 'inset-block-end', 'inset-inline-start', 'inset-inline-end']); // xss-none + private static readonly NEGATIVE_CSS_LENGTH = /(?:^|\s|\()-(?:\d|\.)/; + private static readonly CSS_FUNCTION_VALUE = /\b(?:var|attr|env)\(/i; public static sanitizeRender = (selector: string | HTMLElement | JQuery, dirtyHtml: string) => { // browser-only (not on node) @@ -419,6 +421,9 @@ export class Xss { } }; + private static isUnsafeMargin = (property: string, value: string): boolean => + property.toLowerCase().startsWith('margin') && (Xss.NEGATIVE_CSS_LENGTH.test(value) || Xss.CSS_FUNCTION_VALUE.test(value)); + /** * Sanitize a CSS style string by keeping only explicitly allowed properties. * Uses the browser's native CSS parser for robustness (handles comments, @@ -433,7 +438,7 @@ export class Xss { for (const property of Array.from(style)) { const lower = property.toLowerCase(); const value = style.getPropertyValue(property).trim(); - if (!this.ALLOWED_EMAIL_CSS_PROPERTIES.has(lower) || !value || (lower.startsWith('margin') && this.NEGATIVE_CSS_LENGTH.test(value))) { + if (!this.ALLOWED_EMAIL_CSS_PROPERTIES.has(lower) || !value || this.isUnsafeMargin(property, value)) { style.removeProperty(property); } } @@ -453,7 +458,7 @@ export class Xss { if (lower === 'position' && (['fixed', 'absolute', 'sticky'].includes(value) || value.includes('var('))) { style.removeProperty(property); } else if ( - (lower.startsWith('margin') && this.NEGATIVE_CSS_LENGTH.test(value)) || + this.isUnsafeMargin(property, value) || this.DANGEROUS_CSS_PROPERTIES.has(lower) || // xss-none lower === 'backdrop-filter' || lower.startsWith('mask') || diff --git a/test/source/tests/browser-unit-tests/unit-Xss.js b/test/source/tests/browser-unit-tests/unit-Xss.js index 71bcba4dfd8..071c823cf8a 100644 --- a/test/source/tests/browser-unit-tests/unit-Xss.js +++ b/test/source/tests/browser-unit-tests/unit-Xss.js @@ -148,34 +148,54 @@ BROWSER_UNIT_TEST_NAME(`pgp badges stay above message content`); BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitizeKeepBasicTags strips negative margins`); (async () => { + const hasNegativeMargin = html => { + for (const [, style] of html.matchAll(/style="([^"]*)"/g)) { + for (const decl of style.split(';')) { + const parts = decl.split(':'); + if (parts[0].trim().toLowerCase().startsWith('margin') && /(?:^|\s|\()-[\d.]/.test(parts.slice(1).join(':'))) { + return true; + } + } + } + return false; + }; const payloads = [ `
signed
`, `signed`, `signed`, - `signed`, + `signed`, + `signed`, `signed`, ]; for (const dirty of payloads) { const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP'); - if (/margin[^;"]*-\s*[\d.]/.test(clean)) { + if (hasNegativeMargin(clean)) { throw Error(`negative margin survived sanitization: ${clean}`); } } - const safe = Xss.htmlSanitizeKeepBasicTags(`safe`, 'IMG-KEEP'); - if (!/margin-top:\s*12px/.test(safe)) { - throw Error(`positive margin was removed: ${safe}`); + const safe = Xss.htmlSanitizeKeepBasicTags(`safe`, 'IMG-KEEP'); + if (!/margin-top:\s*12px/.test(safe) || !/margin-left:\s*calc\(100% - 40px\)/.test(safe) || hasNegativeMargin(safe)) { + throw Error(`valid margins were altered: ${safe}`); } return 'pass'; })(); -BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitize strips negative margins`); +BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitize strips negative margins and displacement offsets`); (async () => { - const clean = Xss.htmlSanitize(`signed`); - if (/-\s*33px/.test(clean)) { - throw Error(`negative margin survived sanitization: ${clean}`); + const negative = Xss.htmlSanitize(`signed`); + if (/margin-top/.test(negative)) { + throw Error(`negative margin survived sanitization: ${negative}`); + } + const customProp = Xss.htmlSanitize(`signed`); + if (/margin-top|var\(--m\)/.test(customProp)) { + throw Error(`var() margin survived sanitization: ${customProp}`); + } + const offsets = Xss.htmlSanitize(`signed`); + if (/(?:^|[;\s])(?:top|right|bottom|left|inset)\s*:/.test(offsets)) { + throw Error(`displacement offset survived sanitization: ${offsets}`); } - if (!/color:\s*green/.test(clean)) { - throw Error(`safe color was removed: ${clean}`); + if (!/color:\s*green/.test(negative) || !/color:\s*green/.test(customProp) || !/color:\s*green/.test(offsets)) { + throw Error(`safe color was removed`); } return 'pass'; })(); From e90b1b5f1d526319ff5c26b2a951f1a23d9bdacc Mon Sep 17 00:00:00 2001 From: martgil Date: Fri, 20 Nov 2026 11:11:14 +0800 Subject: [PATCH 3/4] fix(xss): reject function-based margins that can resolve negative --- extension/js/common/platform/xss.ts | 5 ++- .../tests/browser-unit-tests/unit-Xss.js | 42 ++++++++++++++++--- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/extension/js/common/platform/xss.ts b/extension/js/common/platform/xss.ts index e893ede7041..984d0f49d5c 100644 --- a/extension/js/common/platform/xss.ts +++ b/extension/js/common/platform/xss.ts @@ -147,7 +147,6 @@ export class Xss { // prettier-ignore private static readonly DANGEROUS_CSS_PROPERTIES = new Set(['z-index', 'pointer-events', 'transform', 'filter', 'clip-path', 'clip', 'top', 'right', 'bottom', 'left', 'inset', 'inset-block-start', 'inset-block-end', 'inset-inline-start', 'inset-inline-end']); // xss-none private static readonly NEGATIVE_CSS_LENGTH = /(?:^|\s|\()-(?:\d|\.)/; - private static readonly CSS_FUNCTION_VALUE = /\b(?:var|attr|env)\(/i; public static sanitizeRender = (selector: string | HTMLElement | JQuery, dirtyHtml: string) => { // browser-only (not on node) @@ -421,8 +420,10 @@ export class Xss { } }; + // Function-based margins (calc/min/max/clamp/var/...) can resolve to a negative + // length depending on layout, e.g. calc(1% - 200px), so none of them are safe to keep. private static isUnsafeMargin = (property: string, value: string): boolean => - property.toLowerCase().startsWith('margin') && (Xss.NEGATIVE_CSS_LENGTH.test(value) || Xss.CSS_FUNCTION_VALUE.test(value)); + property.toLowerCase().startsWith('margin') && (Xss.NEGATIVE_CSS_LENGTH.test(value) || value.includes('(')); /** * Sanitize a CSS style string by keeping only explicitly allowed properties. diff --git a/test/source/tests/browser-unit-tests/unit-Xss.js b/test/source/tests/browser-unit-tests/unit-Xss.js index 071c823cf8a..58db5f0bb9f 100644 --- a/test/source/tests/browser-unit-tests/unit-Xss.js +++ b/test/source/tests/browser-unit-tests/unit-Xss.js @@ -159,7 +159,18 @@ BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitizeKeepBasicTags strips negative margins`); } return false; }; - const payloads = [ + const retainedMargins = html => { + const retained = []; + for (const [, style] of html.matchAll(/style="([^"]*)"/g)) { + for (const decl of style.split(';')) { + if (decl.split(':')[0].trim().toLowerCase().startsWith('margin')) { + retained.push(decl.trim()); + } + } + } + return retained; + }; + const negativePayloads = [ `
signed
`, `signed`, `signed`, @@ -167,16 +178,33 @@ BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitizeKeepBasicTags strips negative margins`); `signed`, `signed`, ]; - for (const dirty of payloads) { + for (const dirty of negativePayloads) { const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP'); if (hasNegativeMargin(clean)) { throw Error(`negative margin survived sanitization: ${clean}`); } } - const safe = Xss.htmlSanitizeKeepBasicTags(`safe`, 'IMG-KEEP'); - if (!/margin-top:\s*12px/.test(safe) || !/margin-left:\s*calc\(100% - 40px\)/.test(safe) || hasNegativeMargin(safe)) { + const functionPayloads = [ + `signed`, + `signed`, + `signed`, + `signed`, + ]; + for (const dirty of functionPayloads) { + const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP'); + const retained = retainedMargins(clean); + if (retained.length) { + throw Error(`function-based margin survived sanitization: ${clean} (${retained.join(', ')})`); + } + } + const safe = Xss.htmlSanitizeKeepBasicTags(`safe`, 'IMG-KEEP'); + if (!/margin-top:\s*12px/.test(safe) || !/margin-left:\s*40px/.test(safe) || !/margin-right:\s*2em/.test(safe) || hasNegativeMargin(safe)) { throw Error(`valid margins were altered: ${safe}`); } + const positiveCalc = Xss.htmlSanitizeKeepBasicTags(`safe`, 'IMG-KEEP'); + if (retainedMargins(positiveCalc).length) { + throw Error(`function-based margin survived sanitization: ${positiveCalc}`); + } return 'pass'; })(); @@ -194,7 +222,11 @@ BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitize strips negative margins and displacemen if (/(?:^|[;\s])(?:top|right|bottom|left|inset)\s*:/.test(offsets)) { throw Error(`displacement offset survived sanitization: ${offsets}`); } - if (!/color:\s*green/.test(negative) || !/color:\s*green/.test(customProp) || !/color:\s*green/.test(offsets)) { + const mathFunction = Xss.htmlSanitize(`signed`); + if (/margin-top/.test(mathFunction)) { + throw Error(`function-based margin survived sanitization: ${mathFunction}`); + } + if (!/color:\s*green/.test(negative) || !/color:\s*green/.test(customProp) || !/color:\s*green/.test(offsets) || !/color:\s*green/.test(mathFunction)) { throw Error(`safe color was removed`); } return 'pass'; From 0079d131cb4f219eca50c0adf793fa38532a7fd5 Mon Sep 17 00:00:00 2001 From: martgil Date: Fri, 20 Nov 2026 11:21:49 +0800 Subject: [PATCH 4/4] fix(xss): block translate/scale/rotate/offset displacement in htmlSanitize --- extension/js/common/platform/xss.ts | 2 +- .../tests/browser-unit-tests/unit-Xss.js | 23 +++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/extension/js/common/platform/xss.ts b/extension/js/common/platform/xss.ts index 984d0f49d5c..400937ba364 100644 --- a/extension/js/common/platform/xss.ts +++ b/extension/js/common/platform/xss.ts @@ -145,7 +145,7 @@ export class Xss { 'unicode-bidi', ]); // prettier-ignore - private static readonly DANGEROUS_CSS_PROPERTIES = new Set(['z-index', 'pointer-events', 'transform', 'filter', 'clip-path', 'clip', 'top', 'right', 'bottom', 'left', 'inset', 'inset-block-start', 'inset-block-end', 'inset-inline-start', 'inset-inline-end']); // xss-none + private static readonly DANGEROUS_CSS_PROPERTIES = new Set(['z-index', 'pointer-events', 'transform', 'translate', 'rotate', 'scale', 'offset', 'offset-path', 'offset-distance', 'offset-rotate', 'offset-anchor', 'offset-position', 'filter', 'clip-path', 'clip', 'top', 'right', 'bottom', 'left', 'inset', 'inset-block-start', 'inset-block-end', 'inset-inline-start', 'inset-inline-end']); // xss-none private static readonly NEGATIVE_CSS_LENGTH = /(?:^|\s|\()-(?:\d|\.)/; public static sanitizeRender = (selector: string | HTMLElement | JQuery, dirtyHtml: string) => { diff --git a/test/source/tests/browser-unit-tests/unit-Xss.js b/test/source/tests/browser-unit-tests/unit-Xss.js index 58db5f0bb9f..3ef9cc5ef46 100644 --- a/test/source/tests/browser-unit-tests/unit-Xss.js +++ b/test/source/tests/browser-unit-tests/unit-Xss.js @@ -189,11 +189,12 @@ BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitizeKeepBasicTags strips negative margins`); `signed`, `signed`, `signed`, + `signed`, ]; for (const dirty of functionPayloads) { const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP'); const retained = retainedMargins(clean); - if (retained.length) { + if (retained.some(decl => decl.includes('('))) { throw Error(`function-based margin survived sanitization: ${clean} (${retained.join(', ')})`); } } @@ -222,11 +223,23 @@ BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitize strips negative margins and displacemen if (/(?:^|[;\s])(?:top|right|bottom|left|inset)\s*:/.test(offsets)) { throw Error(`displacement offset survived sanitization: ${offsets}`); } - const mathFunction = Xss.htmlSanitize(`signed`); - if (/margin-top/.test(mathFunction)) { - throw Error(`function-based margin survived sanitization: ${mathFunction}`); + const functionPayloads = [ + `signed`, + `signed`, + `signed`, + `signed`, + ]; + for (const dirty of functionPayloads) { + const clean = Xss.htmlSanitize(dirty); + if (/margin-(?:top|right|bottom|left|block-start|block-end|inline-start|inline-end)\s*:/.test(clean)) { + throw Error(`function-based margin survived sanitization: ${clean}`); + } + } + const transforms = Xss.htmlSanitize(`signed`); + if (/(?:^|[;\s])(?:translate|rotate|scale|offset|offset-path|offset-distance|offset-rotate|offset-anchor|offset-position)\s*:/.test(transforms)) { + throw Error(`transform/offset displacement survived sanitization: ${transforms}`); } - if (!/color:\s*green/.test(negative) || !/color:\s*green/.test(customProp) || !/color:\s*green/.test(offsets) || !/color:\s*green/.test(mathFunction)) { + if (!/color:\s*green/.test(negative) || !/color:\s*green/.test(customProp) || !/color:\s*green/.test(offsets) || !/color:\s*green/.test(transforms)) { throw Error(`safe color was removed`); } return 'pass';