[PR #2382] [MERGED] Sanitize theme and extension CSS before injecting into document.head #5573

Closed
opened 2026-10-22 04:17:29 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2382
Author: @Romuromylus
Created: 6/5/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/theme-css-sanitize


📝 Commits (2)

  • 946f02b Sanitize theme and extension CSS before injecting into document.head
  • 863242d Sanitize theme editor live-preview CSS

📊 Changes

4 files changed (+81 additions, -15 deletions)

View changed files

📝 src/app/providers/CustomThemeInjector.tsx (+6 -2)
📝 src/features/shell/settings/components/SettingsPanel.tsx (+4 -1)
📝 src/shared/lib/chat-css.test.ts (+40 -1)
📝 src/shared/lib/chat-css.ts (+31 -11)

📄 Description

Linked issue

Closes #2365

Why this change

CustomThemeInjector assigned the active theme's CSS and each enabled extension's CSS straight to a <style> element's textContent, bypassing the card-CSS sanitizer. With the app CSP set to null, a theme or extension could ship url() beacons (exfil / IP tracking), @import, expression() / javascript:, or :visited history probes that run on every render — and an imported profile can auto-activate a theme without user intent.

What changed

  • Factor a reusable stripDangerousCss out of sanitizeChatCss covering the constructs that are unsafe regardless of scope (remote url() -> about:invalid, @import / @namespace / @font-face, expression / javascript: / vbscript: / behavior / -moz-binding, :visited), and run theme and extension CSS through it.
  • sanitizeChatCss now calls stripDangerousCss and keeps its card-only scope/theme-protection passes, so scoped card CSS is byte-for-byte unchanged (existing tests pass).
  • Theme CSS is intentionally not run through the full card sanitizer: a user-installed theme legitimately overrides theme tokens, uses !important, and positions elements, so only the always-dangerous vectors are stripped. Auto-activation of imported themes is tracked separately (#2366).

Refactor impact

Primary owner: app shell / shared lib (src/app/providers/CustomThemeInjector.tsx, src/shared/lib/chat-css.ts)

Impact areas reviewed:

  • Card-CSS scoping path unchanged; only the theme/extension injection path now sanitizes.

Boundary notes:

  • app/providers imports stripDangerousCss from shared/lib (downward dependency, allowed).

Pressure points touched:

  • none

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • pnpm vitest run src/shared/lib/chat-css.test.ts — 46 passed (all existing card-sanitization tests + 6 new stripDangerousCss tests proving url()/@import/expression/javascript:/:visited are stripped while theme tokens, !important, and position: fixed are preserved).
  • pnpm exec tsc -b — clean.

Feature Discoverability

  • N/A because this PR is a security bugfix and does not add a new thing users need to find.

Reason:

  • Hardening of the theme/extension injection path.

Docs and release impact

  • No docs changes needed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2382 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/5/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/theme-css-sanitize` --- ### 📝 Commits (2) - [`946f02b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/946f02b625d41abd937cb301f05469ce23857f58) Sanitize theme and extension CSS before injecting into document.head - [`863242d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/863242d649b1f61865a8a7962b8118b59a1b99a4) Sanitize theme editor live-preview CSS ### 📊 Changes **4 files changed** (+81 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `src/app/providers/CustomThemeInjector.tsx` (+6 -2) 📝 `src/features/shell/settings/components/SettingsPanel.tsx` (+4 -1) 📝 `src/shared/lib/chat-css.test.ts` (+40 -1) 📝 `src/shared/lib/chat-css.ts` (+31 -11) </details> ### 📄 Description ## Linked issue Closes #2365 ## Why this change `CustomThemeInjector` assigned the active theme's CSS and each enabled extension's CSS straight to a `<style>` element's `textContent`, bypassing the card-CSS sanitizer. With the app CSP set to `null`, a theme or extension could ship `url()` beacons (exfil / IP tracking), `@import`, `expression()` / `javascript:`, or `:visited` history probes that run on every render — and an imported profile can auto-activate a theme without user intent. ## What changed - Factor a reusable `stripDangerousCss` out of `sanitizeChatCss` covering the constructs that are unsafe regardless of scope (remote `url()` -> `about:invalid`, `@import` / `@namespace` / `@font-face`, `expression` / `javascript:` / `vbscript:` / `behavior` / `-moz-binding`, `:visited`), and run theme and extension CSS through it. - `sanitizeChatCss` now calls `stripDangerousCss` and keeps its card-only scope/theme-protection passes, so scoped card CSS is byte-for-byte unchanged (existing tests pass). - Theme CSS is intentionally **not** run through the full card sanitizer: a user-installed theme legitimately overrides theme tokens, uses `!important`, and positions elements, so only the always-dangerous vectors are stripped. Auto-activation of imported themes is tracked separately (#2366). ## Refactor impact Primary owner: app shell / shared lib (`src/app/providers/CustomThemeInjector.tsx`, `src/shared/lib/chat-css.ts`) Impact areas reviewed: - Card-CSS scoping path unchanged; only the theme/extension injection path now sanitizes. Boundary notes: - `app/providers` imports `stripDangerousCss` from `shared/lib` (downward dependency, allowed). Pressure points touched: - none ## Validation - [ ] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `pnpm vitest run src/shared/lib/chat-css.test.ts` — 46 passed (all existing card-sanitization tests + 6 new `stripDangerousCss` tests proving `url()`/`@import`/`expression`/`javascript:`/`:visited` are stripped while theme tokens, `!important`, and `position: fixed` are preserved). - `pnpm exec tsc -b` — clean. ## Feature Discoverability - [x] N/A because this PR is a security bugfix and does not add a new thing users need to find. Reason: - Hardening of the theme/extension injection path. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:29 +07:00
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#5573
No description provided.