From 96021b747903e183440f92624c774c3047ddfd36 Mon Sep 17 00:00:00 2001 From: Buco7854 Date: Tue, 23 Jun 2026 19:25:18 +0000 Subject: [PATCH] refactor: detect external CSS references with the CSS Syntax tokenizer Replace the regex/escape-normalizer with @csstools/css-tokenizer (already present transitively). Tokenizing resolves comments, hex escapes, and line continuations into the real value before scanning, so obfuscated forms (a scheme split by a line continuation or hex escape) cannot slip past. Scan url-tokens and string-tokens to cover url(), quoted @import, and string-form image functions. Add regression tests for the bypasses. --- server/package-lock.json | 1 + server/package.json | 1 + server/src/utils/customCss.ts | 23 +++++++---------------- server/test/unit/utils/customCss.test.ts | 19 +++++++++++++++++-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/server/package-lock.json b/server/package-lock.json index 0f16e6b5d..5498a5549 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@csstools/css-tokenizer": "^3.0.4", "@grpc/grpc-js": "^1.14.3", "@grpc/proto-loader": "^0.8.0", "@pulsecron/pulse": "1.6.8", diff --git a/server/package.json b/server/package.json index 2ac0d47f9..8838e2c06 100755 --- a/server/package.json +++ b/server/package.json @@ -31,6 +31,7 @@ "node": ">=20" }, "dependencies": { + "@csstools/css-tokenizer": "^3.0.4", "@grpc/grpc-js": "^1.14.3", "@grpc/proto-loader": "^0.8.0", "@pulsecron/pulse": "1.6.8", diff --git a/server/src/utils/customCss.ts b/server/src/utils/customCss.ts index 0705efe46..3f38a0bd5 100644 --- a/server/src/utils/customCss.ts +++ b/server/src/utils/customCss.ts @@ -1,23 +1,14 @@ -// Normalize the CSS the way a browser does before matching, so obfuscated forms -// (inline comments, CSS escapes like "\75 rl(") can't hide an external reference. -const decodeForScan = (css: string): string => - css.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\\([0-9a-fA-F]{1,6})\s?|\\([\s\S])/g, (_match, hex: string, char: string) => { - if (!hex) { - return char; - } - const codePoint = parseInt(hex, 16); - return codePoint > 0 && codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : "�"; - }); +import { isTokenString, isTokenURL, tokenize } from "@csstools/css-tokenizer"; -// An external reference is @import, or a resource function (url, image-set, -// image, cross-fade) whose target starts with an http(s) scheme or is -// protocol-relative. Anchoring on the start of the target keeps data: and -// relative URLs allowed, including data: SVG that embeds the SVG namespace URL. -const EXTERNAL_REFERENCE = /(?:@import\b|(?:url|image-set|-webkit-image-set|image|cross-fade|-webkit-cross-fade)\s*\(\s*['"]?\s*(?:https?:|\/\/))/i; +// Absolute http(s) or protocol-relative target; data: and relative URLs stay allowed. +const EXTERNAL_TARGET = /^\s*(?:https?:|\/\/)/i; +// Tokenizing resolves comments, escapes, and line continuations so obfuscated +// targets can't hide. url-tokens cover url(); string-tokens cover @import and image-set(). export const cssReferencesExternalResource = (css?: string | null): boolean => { if (typeof css !== "string" || css === "") { return false; } - return EXTERNAL_REFERENCE.test(decodeForScan(css)); + + return tokenize({ css }).some((token) => (isTokenURL(token) || isTokenString(token)) && EXTERNAL_TARGET.test(token[4].value)); }; diff --git a/server/test/unit/utils/customCss.test.ts b/server/test/unit/utils/customCss.test.ts index 1efdb8012..bbdac7118 100644 --- a/server/test/unit/utils/customCss.test.ts +++ b/server/test/unit/utils/customCss.test.ts @@ -47,12 +47,27 @@ describe("cssReferencesExternalResource", () => { expect(cssReferencesExternalResource("body{background:url(http://evil.example/x) !!!}")).toBe(true); }); - it("flags url() obfuscated with an inline comment", () => { - expect(cssReferencesExternalResource('body{background:url(/*c*/ "http://evil.example/x")}')).toBe(true); + it("flags references obfuscated with inline comments", () => { + expect(cssReferencesExternalResource('body{background:/*c*/url("http://evil.example/x")}')).toBe(true); + expect(cssReferencesExternalResource('@import/*c*/"http://evil.example/x";')).toBe(true); + expect(cssReferencesExternalResource('body{background:image-set(/*c*/"http://evil.example/x" 1x)}')).toBe(true); + }); + + it("allows a comment that makes url() a bad token the browser never fetches", () => { + expect(cssReferencesExternalResource('body{background:url(/*c*/ "http://evil.example/x")}')).toBe(false); }); it("flags url() obfuscated with CSS escapes", () => { expect(cssReferencesExternalResource("body{background:\\75 rl(http://evil.example/x)}")).toBe(true); expect(cssReferencesExternalResource('\\40 import "http://evil.example/x";')).toBe(true); }); + + it("flags a scheme split by a line continuation", () => { + expect(cssReferencesExternalResource('body{background:url("htt\\\nps://evil.example/x")}')).toBe(true); + expect(cssReferencesExternalResource('body{background:url("htt\\\r\nps://evil.example/x")}')).toBe(true); + }); + + it("flags a scheme split by a hex escape", () => { + expect(cssReferencesExternalResource('body{background:url("\\68 ttps://evil.example/x")}')).toBe(true); + }); });