diff --git a/extension/js/common/platform/xss.ts b/extension/js/common/platform/xss.ts index 8dbfa7b00cd..400937ba364 100644 --- a/extension/js/common/platform/xss.ts +++ b/extension/js/common/platform/xss.ts @@ -144,7 +144,9 @@ 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 + // prettier-ignore + 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) => { // browser-only (not on node) @@ -418,6 +420,11 @@ 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) || value.includes('(')); + /** * Sanitize a CSS style string by keeping only explicitly allowed properties. * Uses the browser's native CSS parser for robustness (handles comments, @@ -430,8 +437,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 || this.isUnsafeMargin(property, value)) { style.removeProperty(property); } } @@ -451,6 +459,7 @@ export class Xss { if (lower === 'position' && (['fixed', 'absolute', 'sticky'].includes(value) || value.includes('var('))) { style.removeProperty(property); } else if ( + 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 fc7fba66981..3ef9cc5ef46 100644 --- a/test/source/tests/browser-unit-tests/unit-Xss.js +++ b/test/source/tests/browser-unit-tests/unit-Xss.js @@ -146,6 +146,105 @@ BROWSER_UNIT_TEST_NAME(`pgp badges stay above message content`); return 'pass'; })(); +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 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`, + `signed`, + `signed`, + `signed`, + ]; + for (const dirty of negativePayloads) { + const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP'); + if (hasNegativeMargin(clean)) { + throw Error(`negative margin survived sanitization: ${clean}`); + } + } + const functionPayloads = [ + `signed`, + `signed`, + `signed`, + `signed`, + `signed`, + ]; + for (const dirty of functionPayloads) { + const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP'); + const retained = retainedMargins(clean); + if (retained.some(decl => decl.includes('('))) { + 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'; +})(); + +BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitize strips negative margins and displacement offsets`); +(async () => { + 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}`); + } + 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(transforms)) { + throw Error(`safe color was removed`); + } + return 'pass'; +})(); + BROWSER_UNIT_TEST_NAME(`Xss.htmlSanitizeKeepBasicTags rejects encoded logout image paths`); (async () => { const urls = [