From deff678cfa6934cbad940cfca2e70128083e4435 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Wed, 25 Nov 2026 12:50:25 +0000 Subject: [PATCH] Store: CSS filter bypasses via comments in strings and vendor prefixes normalizeCss stripped /* */ without knowing about strings or escapes, so content:"/*" ... "*/" hid a url() between them from the checks, and -webkit-image-set( slipped past the function-name boundary. The checks now run with and without comment stripping and allow a vendor prefix. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- gateway/src/store.ts | 18 +++++++++++------- gateway/test/store-css.test.mjs | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 gateway/test/store-css.test.mjs diff --git a/gateway/src/store.ts b/gateway/src/store.ts index 959341c..a09a667 100644 --- a/gateway/src/store.ts +++ b/gateway/src/store.ts @@ -75,9 +75,10 @@ interface StoreTheme { // Undo what a plain pattern check would miss: comments splitting a keyword // (u/**/rl), CSS escapes (\75 rl( is url(), and case. -function normalizeCss(css: string): string { - return css - .replace(/\/\*[\s\S]*?\*\//g, "") +// Comment stripping isn't string-aware ("/*" inside content:"..."), so the +// checks run on both variants: with and without comments removed. +function normalizeCss(css: string, stripComments: boolean): string { + return (stripComments ? css.replace(/\/\*[\s\S]*?\*\//g, "") : css) .replace(/\\([0-9a-f]{1,6})\s?/gi, (_, hex: string) => { const cp = parseInt(hex, 16); return cp > 0 && cp <= 0x10ffff ? String.fromCodePoint(cp) : "\ufffd"; @@ -94,7 +95,7 @@ const FORBIDDEN_CSS = [ /@import/, /@font-face/, /@namespace/, - /(?:^|[^a-z-])(?:image-set|image|cross-fade|element|src|expression)\s*\(/, + /(?:^|[^a-z])(?:-[a-z]+-)?(?:image-set|image|cross-fade|element|src|expression)\s*\(/, /-moz-binding/, /behavior\s*:/, /javascript:/, @@ -102,7 +103,10 @@ const FORBIDDEN_CSS = [ ]; export function isSafeThemeCss(css: string): boolean { - const normalized = normalizeCss(css); + return isSafeNormalized(normalizeCss(css, true)) && isSafeNormalized(normalizeCss(css, false)); +} + +function isSafeNormalized(normalized: string): boolean { if (FORBIDDEN_CSS.some((re) => re.test(normalized))) return false; for (const match of normalized.matchAll(/url\s*\(\s*(["']?)\s*([^"')\s]*)/g)) { if (!/^data:image\/(?:png|jpeg|gif|webp|svg\+xml)[;,]/.test(match[2])) return false; @@ -372,8 +376,8 @@ async function handleStoreSubmit(req: IncomingMessage, res: ServerResponse) { const themes = await loadStoreThemes(); // Re-uploading an installed store theme unchanged would just duplicate it. - const cssKey = normalizeCss(css).replace(/\s+/g, ""); - if (themes.some((t) => normalizeCss(t.css).replace(/\s+/g, "") === cssKey)) { + const cssKey = normalizeCss(css, true).replace(/\s+/g, ""); + if (themes.some((t) => normalizeCss(t.css, true).replace(/\s+/g, "") === cssKey)) { res.writeHead(409, { "Content-Type": "application/json" }); res.end(JSON.stringify({ error: "duplicate" })); return; diff --git a/gateway/test/store-css.test.mjs b/gateway/test/store-css.test.mjs new file mode 100644 index 0000000..3ebb7f9 --- /dev/null +++ b/gateway/test/store-css.test.mjs @@ -0,0 +1,21 @@ +// Theme CSS from the Design Store must never load anything external. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { isSafeThemeCss } from "../dist/store.js"; + +test("theme CSS filter", () => { + const evil = "url(https://attacker.example/t.png)"; + for (const css of [ + `body{background:${evil}}`, + `body{background:\\75rl(https://attacker.example/t.png)}`, + `body{background:image-set("https://attacker.example/t.png" 1x)}`, + `a{content:"/*"}body{background:${evil}}b{content:"*/"}`, + `a{--x:\\/*}body{background:${evil}}b{--y:*/}`, + `body{background:-webkit-image-set("https://attacker.example/t.png" 1x)}`, + ]) assert.equal(isSafeThemeCss(css), false, css); + for (const css of [ + ":root{--bg:#123}body{color:red}a:hover{opacity:.5}", + "body{background:url(data:image/png;base64,AAAA)}", + "/* a comment */ body{color:red}", + ]) assert.equal(isSafeThemeCss(css), true, css); +});