diff --git a/packages/shared/src/themeCss.test.ts b/packages/shared/src/themeCss.test.ts index 9b63049b0e360..9aecb1c1f7931 100644 --- a/packages/shared/src/themeCss.test.ts +++ b/packages/shared/src/themeCss.test.ts @@ -32,7 +32,19 @@ describe("themeCssValueFetches", () => { describe("themeCssLoadsRemoteFiles", () => { it.each([ ['[data-chat-header]::after { content: "@import"; }', false], + ['.a::after { content: "say \\"@import\\""; }', false], + ['.a::after { content: "a\\\r\n@import"; }', false], + ['.a::after { content: "\\22\r\n@import"; }', false], + ['.a::after { content: "/*"; } /* note */ .b::after { content: "@import"; }', false], + ['/* @import "x.css"; */ .a::after { content: "*/"; }', false], + [ + ".a { background-image: url(data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%3E%3C!--/*@import*/--%3E%3C/svg%3E); }", + false, + ], ['@import "x.css";', true], + ['.a\\" {} @import "x.css"; .b\\" {}', true], + ['.a::after { content: "\\\\"; } @import "x.css";', true], + [".a { background: url(https://example.invalid/x.png); }", true], ])("%s", (css, loads) => { expect(themeCssLoadsRemoteFiles(css)).toBe(loads); }); diff --git a/packages/shared/src/themeCss.ts b/packages/shared/src/themeCss.ts index db0e86a61bc4b..6fa05e57336c9 100644 --- a/packages/shared/src/themeCss.ts +++ b/packages/shared/src/themeCss.ts @@ -3,16 +3,25 @@ import { THEME_COLOR_ROLES } from "./themePalettes.ts"; const FETCHES_BY_STRING = /image-set\(|\bsrc\(/i; // Only CSS whitespace may come first. \s also matches U+00A0, which makes the address relative. const URL_NOT_LOCAL = /url\((?![ \t\n\f\r]*["']?(?:data:|#))/i; -// Read tokens left to right, as CSS does: a url( inside a string loads nothing, and a quote in an -// unquoted url( is not a string. A quoted url() that is not data: or #id keeps `url(` for the check. -const URL_OR_STRING = - /url\((?![ \t\n\f\r]*["'])[^)]*|url\([ \t\n\f\r]*("[^"\n\r\f]*"|'[^'\n\r\f]*')|"[^"\n\r\f]*"|'[^'\n\r\f]*'/gi; +// Read tokens left to right, as CSS does: a url( or /* inside a string loads nothing, and a quote +// in an unquoted url( or after a backslash is not a string. A quoted url() that is not data: or #id +// keeps `url(` for the check. +const ESCAPE = String.raw`\\(?:(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{1,5}(?![0-9a-fA-F]))(?:\r\n|[\n\r\f])?|\r\n|[^0-9a-fA-F])`; +const STRING = String.raw`"(?:${ESCAPE}|[^"\\\n\r\f])*"|'(?:${ESCAPE}|[^'\\\n\r\f])*'`; +const URL_OR_STRING = String.raw`url\((?![ \t\n\f\r]*["'])([^)]*)|url\([ \t\n\f\r]*(${STRING})|${STRING}|${ESCAPE}`; +const VALUE_TOKENS = new RegExp(URL_OR_STRING, "gi"); +const STYLESHEET_TOKENS = new RegExp(String.raw`\/\*[\s\S]*?\*\/|${URL_OR_STRING}`, "gi"); -/** The code with its strings removed, keeping `url(` where a quoted url() can load a file. */ -function withoutStrings(code: string): string { - return code.replace(URL_OR_STRING, (token, urlString: string | undefined) => { - if (urlString !== undefined) return /^["'](?:data:|#)/i.test(urlString) ? "" : "url("; - return token.startsWith('"') || token.startsWith("'") ? "" : token; +/** + * The code with its strings removed, keeping `url(` where a quoted url() can load a file. A + * stylesheet also loses its comments, and an unquoted url() there is judged by its address too. + */ +function withoutStrings(code: string, stylesheet: boolean): string { + const tokens = stylesheet ? STYLESHEET_TOKENS : VALUE_TOKENS; + return code.replace(tokens, (token, address?: string, urlString?: string) => { + const url = stylesheet ? (address ?? urlString) : urlString; + if (url !== undefined) return /^[ \t\n\f\r]*["']?(?:data:|#)/i.test(url) ? "" : "url("; + return /^["'/]/.test(token) ? "" : token; }); } @@ -26,12 +35,12 @@ function fetchesOutsideStrings(code: string): boolean { * so every value that still has a backslash is refused. */ export function themeCssValueFetches(value: string): boolean { - return value.includes("\\") || fetchesOutsideStrings(withoutStrings(value)); + return value.includes("\\") || fetchesOutsideStrings(withoutStrings(value, false)); } /** A text check for early feedback. CSS escapes can dodge it; the client check is the guard. */ export function themeCssLoadsRemoteFiles(css: string): boolean { - const code = withoutStrings(css.replace(/\/\*[\s\S]*?\*\//g, "")); + const code = withoutStrings(css, true); return /@import/i.test(code) || fetchesOutsideStrings(code); }