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.
This commit is contained in:
Buco7854 committed 2026-06-23 19:25:18 +00:00
1 parent 3685e0c476
commit 96021b7479
4 files changed
+26 -18

No files matched your search

+1
View File
@@ -9,6 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@csstools/css-tokenizer": "^3.0.4",
"@grpc/grpc-js": "^1.14.3", "@grpc/grpc-js": "^1.14.3",
"@grpc/proto-loader": "^0.8.0", "@grpc/proto-loader": "^0.8.0",
"@pulsecron/pulse": "1.6.8", "@pulsecron/pulse": "1.6.8",
+1
View File
@@ -31,6 +31,7 @@
"node": ">=20" "node": ">=20"
}, },
"dependencies": { "dependencies": {
"@csstools/css-tokenizer": "^3.0.4",
"@grpc/grpc-js": "^1.14.3", "@grpc/grpc-js": "^1.14.3",
"@grpc/proto-loader": "^0.8.0", "@grpc/proto-loader": "^0.8.0",
"@pulsecron/pulse": "1.6.8", "@pulsecron/pulse": "1.6.8",
+7 -16
View File
@@ -1,23 +1,14 @@
// Normalize the CSS the way a browser does before matching, so obfuscated forms import { isTokenString, isTokenURL, tokenize } from "@csstools/css-tokenizer";
// (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) : "�";
});
// An external reference is @import, or a resource function (url, image-set, // Absolute http(s) or protocol-relative target; data: and relative URLs stay allowed.
// image, cross-fade) whose target starts with an http(s) scheme or is const EXTERNAL_TARGET = /^\s*(?:https?:|\/\/)/i;
// 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;
// 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 => { export const cssReferencesExternalResource = (css?: string | null): boolean => {
if (typeof css !== "string" || css === "") { if (typeof css !== "string" || css === "") {
return false; return false;
} }
return EXTERNAL_REFERENCE.test(decodeForScan(css));
return tokenize({ css }).some((token) => (isTokenURL(token) || isTokenString(token)) && EXTERNAL_TARGET.test(token[4].value));
}; };
+17 -2
View File
@@ -47,12 +47,27 @@ describe("cssReferencesExternalResource", () => {
expect(cssReferencesExternalResource("body{background:url(http://evil.example/x) !!!}")).toBe(true); expect(cssReferencesExternalResource("body{background:url(http://evil.example/x) !!!}")).toBe(true);
}); });
it("flags url() obfuscated with an inline comment", () => { it("flags references obfuscated with inline comments", () => {
expect(cssReferencesExternalResource('body{background:url(/*c*/ "http://evil.example/x")}')).toBe(true); 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", () => { it("flags url() obfuscated with CSS escapes", () => {
expect(cssReferencesExternalResource("body{background:\\75 rl(http://evil.example/x)}")).toBe(true); expect(cssReferencesExternalResource("body{background:\\75 rl(http://evil.example/x)}")).toBe(true);
expect(cssReferencesExternalResource('\\40 import "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);
});
}); });