Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions extension/js/common/platform/xss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ export class Xss {
'direction',
'unicode-bidi',
]);
private static readonly DANGEROUS_CSS_PROPERTIES = new Set<string>(['z-index', 'pointer-events', 'transform', 'filter', 'clip-path', 'clip']); // xss-none
// prettier-ignore
private static readonly DANGEROUS_CSS_PROPERTIES = new Set<string>(['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)
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}
Expand All @@ -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') ||
Expand Down
99 changes: 99 additions & 0 deletions test/source/tests/browser-unit-tests/unit-Xss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
`<div style="margin-top:-33px;margin-bottom:0"><b style="margin-left:72px;background-color:#31a217">signed</b></div>`,
`<b style="margin:-1px 0 0 72px">signed</b>`,
`<b style="margin-top:-2em!important">signed</b>`,
`<b style="margin-top:calc(-33px + 1px)">signed</b>`,
`<b style="margin-top:var(--m, -33px)">signed</b>`,
`<b style="margin-block-start:-33px">signed</b>`,
];
for (const dirty of negativePayloads) {
const clean = Xss.htmlSanitizeKeepBasicTags(dirty, 'IMG-KEEP');
if (hasNegativeMargin(clean)) {
throw Error(`negative margin survived sanitization: ${clean}`);
}
}
const functionPayloads = [
`<b style="margin-top:calc(1% - 200px)">signed</b>`,
`<b style="margin-top:clamp(0px, 1% - 200px, 10px)">signed</b>`,
`<b style="margin-top:min(1%, -200px)">signed</b>`,
`<b style="margin-top:max(-200px, 1%)">signed</b>`,
`<b style="margin:calc(1% - 200px) 0 0 0">signed</b>`,
];
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(`<b style="margin-top:12px;margin-left:40px;margin-right:2em">safe</b>`, '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(`<b style="margin-left:calc(100% - 40px);color:green">safe</b>`, '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(`<b style="position:relative;z-index:-1;margin-top:-33px;color:green">signed</b>`);
if (/margin-top/.test(negative)) {
throw Error(`negative margin survived sanitization: ${negative}`);
}
const customProp = Xss.htmlSanitize(`<b style="margin-top:var(--m);--m:-33px;color:green">signed</b>`);
if (/margin-top|var\(--m\)/.test(customProp)) {
throw Error(`var() margin survived sanitization: ${customProp}`);
}
const offsets = Xss.htmlSanitize(`<b style="position:relative;top:-100px;left:72px;color:green">signed</b>`);
if (/(?:^|[;\s])(?:top|right|bottom|left|inset)\s*:/.test(offsets)) {
throw Error(`displacement offset survived sanitization: ${offsets}`);
}
const functionPayloads = [
`<b style="margin-top:calc(1% - 200px)">signed</b>`,
`<b style="margin-top:clamp(0px, 1% - 200px, 10px)">signed</b>`,
`<b style="margin-top:min(1%, -200px)">signed</b>`,
`<b style="margin-top:max(-200px, 1%)">signed</b>`,
];
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(`<b style="translate:0 -200px;rotate:45deg;scale:2;offset:10px;offset-path:path('M0 0 L10 0');offset-distance:50%;offset-rotate:45deg;color:green">signed</b>`);
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 = [
Expand Down