[GH-ISSUE #1989] Card CSS sanitizer: escape sequences can bypass regex guards (needs tokenizer-level normalization) #974

Closed
opened 2026-10-21 05:24:21 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 2, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1989

Originally assigned to: @kolacheee on GitHub.

Summary

The card-CSS sanitizer in src/shared/lib/chat-css.ts (sanitizeChatCss) is regex-based. CSS identifier escape sequences (\XX hex escapes and \c literal escapes) let untrusted card CSS spell guarded tokens in a form the regexes don't match but the engine still parses. This is a pre-existing limitation of the regex approach, surfaced during review of #1812.

Known bypass vectors

  • Network exfiltration (highest priority): background: \75rl(https://tracker.test/pixel) parses as url(...) but evades the url( allowlist → external request. Same idea for @import.
  • @font-face source gate: src: l\6f cal("Inter") parses as local(...) and evades the literal local( rejection.
  • Declaration guards: po\73ition: fixed, --back\67round: red, and escaped !important evade the fixed→absolute conversion, theme-token blocklist, and !important strip respectively.

Why this wasn't fixed in #1812

Both regex-only approaches fail:

  • Decode the whole stylesheet → closes every vector but mutates benign context-sensitive escapes (escaped selector punctuation like a w-1\/2 class, escaped code points in content strings).
  • Decode only function/at-keyword tokens → preserves benign CSS but leaves seams (escaped property names, leading-whitespace at-rules).

Robust coverage needs a CSS tokenizer/parser pass: decode escapes per-token for the dangerous-construct checks, but re-emit benign tokens verbatim. That's a focused change to a security-critical function and is out of scope for the #1812 feature PR, so it was reverted there (see eee7e004) and split out here.

Suggested approach

Tokenize declarations/at-rules/selectors, run the existing guards against an escape-decoded analysis form, and reserialize preserving original benign tokens. (CodeRabbit/Bunny review threads on #1812 have detailed write-ups of each vector.)

Scope note

Card CSS is already scoped + DOMPurify-sanitized at the HTML layer; this is about the CSS sanitizer's network/anti-spoofing guards specifically. Relates to #1812.

Originally created by @kolacheee on GitHub (Jun 2, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1989 Originally assigned to: @kolacheee on GitHub. ### Summary The card-CSS sanitizer in `src/shared/lib/chat-css.ts` (`sanitizeChatCss`) is regex-based. CSS identifier **escape sequences** (`\XX` hex escapes and `\c` literal escapes) let untrusted card CSS spell guarded tokens in a form the regexes don't match but the engine still parses. This is a pre-existing limitation of the regex approach, surfaced during review of #1812. ### Known bypass vectors - **Network exfiltration (highest priority):** `background: \75rl(https://tracker.test/pixel)` parses as `url(...)` but evades the `url(` allowlist → external request. Same idea for `@import`. - **`@font-face` source gate:** `src: l\6f cal("Inter")` parses as `local(...)` and evades the literal `local(` rejection. - **Declaration guards:** `po\73ition: fixed`, `--back\67round: red`, and escaped `!important` evade the fixed→absolute conversion, theme-token blocklist, and `!important` strip respectively. ### Why this wasn't fixed in #1812 Both regex-only approaches fail: - **Decode the whole stylesheet** → closes every vector but mutates *benign* context-sensitive escapes (escaped selector punctuation like a `w-1\/2` class, escaped code points in `content` strings). - **Decode only function/at-keyword tokens** → preserves benign CSS but leaves seams (escaped property names, leading-whitespace at-rules). Robust coverage needs a **CSS tokenizer/parser pass**: decode escapes per-token for the dangerous-construct checks, but re-emit benign tokens verbatim. That's a focused change to a security-critical function and is out of scope for the #1812 feature PR, so it was reverted there (see eee7e004) and split out here. ### Suggested approach Tokenize declarations/at-rules/selectors, run the existing guards against an escape-decoded analysis form, and reserialize preserving original benign tokens. (CodeRabbit/Bunny review threads on #1812 have detailed write-ups of each vector.) ### Scope note Card CSS is already scoped + DOMPurify-sanitized at the HTML layer; this is about the CSS sanitizer's network/anti-spoofing guards specifically. Relates to #1812.
Author
Owner

@kolacheee commented on GitHub (Jun 2, 2026):

gonna work on it tomorrow 🛌

<!-- gh-comment-id:4607776601 --> @kolacheee commented on GitHub (Jun 2, 2026): gonna work on it tomorrow 🛌
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#974
No description provided.