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:
1 parent
3685e0c476
commit
96021b7479
4 files changed
+26
-18
No files matched your search
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user