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.
15 lines
666 B
TypeScript
15 lines
666 B
TypeScript
import { isTokenString, isTokenURL, tokenize } from "@csstools/css-tokenizer";
|
|
|
|
// 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 tokenize({ css }).some((token) => (isTokenURL(token) || isTokenString(token)) && EXTERNAL_TARGET.test(token[4].value));
|
|
};
|