[PR #2590] [MERGED] feat(chat): apply character creator-notes CSS in chat (card theming) #5677

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2590
Author: @kolacheee
Created: 6/16/2026
Status: ✅ Merged
Merged: 6/16/2026
Merged by: @SpicyMarinara

Base: staging ← Head: feat/staging-cardcss-injection


📝 Commits (5)

  • e6fe37a fix(chat): harden card CSS sanitizer against escape-sequence evasion
  • d74d73f feat(chat): apply character creator-notes CSS in chat (card theming)
  • cff5399 feat(chat): default card CSS mode to disabled (opt-in)
  • 423017a Merge branch 'staging' into feat/staging-cardcss-injection
  • 95161f2 fix(chat): inject card CSS unlayered so it can override message styling

📊 Changes

10 files changed (+870 additions, -3 deletions)

View changed files

📝 packages/client/src/components/chat/ChatArea.tsx (+20 -0)
📝 packages/client/src/components/chat/ChatMessage.tsx (+32 -1)
📝 packages/client/src/components/chat/ChatRoleplaySurface.tsx (+5 -1)
📝 packages/client/src/components/chat/ChatSettingsDrawer.tsx (+82 -0)
📝 packages/client/src/components/chat/ConversationMessage.tsx (+1 -0)
📝 packages/client/src/components/chat/ConversationView.tsx (+5 -1)
➕ packages/client/src/components/chat/CreatorNotesCssInjector.tsx (+117 -0)
➕ packages/client/src/lib/card-css.ts (+578 -0)
➕ packages/client/src/lib/creator-notes-css.ts (+23 -0)
📝 packages/shared/src/types/chat.ts (+7 -0)

📄 Description

Linked issue

Closes #2579

Stacked on #2586 (card-CSS sanitizer hardening). Until #2586 merges, this PR's diff includes that commit; it will be rebased onto staging once #2586 lands. The two are otherwise independent — this feature ships its own self-contained card-CSS sanitizer (lib/card-css.ts).

Why this change

Character cards can ship custom CSS in their creator_notes (a long-standing card convention), but staging has no way to apply it — so imported cards that rely on creator-notes styling render plain. This ports the creator-notes card-CSS feature (source: #1748) so a card's embedded styling actually shows in chat, with a per-chat control over how aggressively it applies and a hardened sanitizer so untrusted card CSS can't exfiltrate data, escape its scope, or override the app theme.

What changed

  • lib/card-css.ts — sanitizes + scopes untrusted card CSS: strips network/script vectors, removes :has() / position: fixed / history-probing, blocks theme-token overrides and !important, namespaces @keyframes and @font-face (anti-collision salted), recurses into @media, canonicalizes CSS escape sequences so the literal-text guards can't be evaded, and rewrites every selector under a per-card scope. Plus filterCssByMode for @chat-mode surface targeting.
  • lib/creator-notes-css.ts — lifts <style> blocks out of creator_notes.
  • CreatorNotesCssInjector — for the chat's active characters, extracts → filters → scopes → injects one <style id="marinara-card-css"> into <head>, unlayered so its scoped selectors override the app's own message styling (the app sets some message styles with unlayered rules that beat any @layer — wrapping card CSS in a layer silently neutered most theming; the sanitizer + per-card scope still contain it). Mounted in ChatArea for the roleplay + conversation surfaces.
  • ChatMetadata.cardCssMode — new per-chat tri-state: disabled (default — card styling is opt-in per chat) / exclusive (each character's CSS only styles their own messages) / chat (whole area).
  • Card Theming control — a segmented selector in the chat settings drawer, shown only when an active character actually ships creator-notes CSS.
  • Scope markers — .mari-card-css + data-chat-mode on the roleplay + conversation surface containers; data-card-css={characterId} on the message wrappers.

Game mode is intentionally out of scope here (its surface and narration log have a distinct structure and stacking); a follow-up can extend the same markers to it.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Already run (automated): client typecheck (tsc -b), client lint (eslint), and server typecheck all pass. Full pnpm check and the in-app checks below still need a human.

Please manually verify in the app:

  • Add a <style>…</style> block to a character's Creator Notes (e.g. <style>.mari-message-bubble { border: 2px solid hotpink; }</style>), open a chat with that character, and confirm the Card Theming section appears in chat settings and the styling shows.
  • Switch the mode: Disabled removes all card styling; Exclusive styles only that character's messages; Chat styles the whole area. Confirm the mode persists across a reload (it is per-chat metadata).
  • In a 2+ character chat, confirm Exclusive keeps each card's CSS to its own messages (no bleed between characters).
  • Security spot-checks — confirm these are neutralized: a url(http://…) background (no outbound request), a --primary: red override (app theme unchanged), a position: fixed overlay, a :has() probe, and an escaped evasion such as po\73ition: fixed.
  • Confirm a normal chat with no card CSS is visually unchanged, and that the roleplay, conversation, and visual-novel surfaces all behave.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

To be added: screenshots of the Card Theming control and a card's CSS applied in chat (disabled / exclusive / chat).


🔄 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/2590 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 6/16/2026 **Status:** ✅ Merged **Merged:** 6/16/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `feat/staging-cardcss-injection` --- ### 📝 Commits (5) - [`e6fe37a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e6fe37a266b5e65ce1bcc835dda22183bb011b68) fix(chat): harden card CSS sanitizer against escape-sequence evasion - [`d74d73f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d74d73fa289c07b5ad2e4cfd00f6d78e219ec847) feat(chat): apply character creator-notes CSS in chat (card theming) - [`cff5399`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cff53994989b16b0b6ed9af9fe6a4329df861620) feat(chat): default card CSS mode to disabled (opt-in) - [`423017a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/423017a993bd34e086eebf46bd4b054f57e42594) Merge branch 'staging' into feat/staging-cardcss-injection - [`95161f2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/95161f282f45f2e15d511663183e40013f013918) fix(chat): inject card CSS unlayered so it can override message styling ### 📊 Changes **10 files changed** (+870 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/chat/ChatArea.tsx` (+20 -0) 📝 `packages/client/src/components/chat/ChatMessage.tsx` (+32 -1) 📝 `packages/client/src/components/chat/ChatRoleplaySurface.tsx` (+5 -1) 📝 `packages/client/src/components/chat/ChatSettingsDrawer.tsx` (+82 -0) 📝 `packages/client/src/components/chat/ConversationMessage.tsx` (+1 -0) 📝 `packages/client/src/components/chat/ConversationView.tsx` (+5 -1) ➕ `packages/client/src/components/chat/CreatorNotesCssInjector.tsx` (+117 -0) ➕ `packages/client/src/lib/card-css.ts` (+578 -0) ➕ `packages/client/src/lib/creator-notes-css.ts` (+23 -0) 📝 `packages/shared/src/types/chat.ts` (+7 -0) </details> ### 📄 Description ## Linked issue Closes #2579 > Stacked on #2586 (card-CSS sanitizer hardening). Until #2586 merges, this PR's diff includes that commit; it will be rebased onto `staging` once #2586 lands. The two are otherwise independent — this feature ships its own self-contained card-CSS sanitizer (`lib/card-css.ts`). ## Why this change Character cards can ship custom CSS in their `creator_notes` (a long-standing card convention), but staging has no way to apply it — so imported cards that rely on creator-notes styling render plain. This ports the creator-notes card-CSS feature (source: #1748) so a card's embedded styling actually shows in chat, with a per-chat control over how aggressively it applies and a hardened sanitizer so untrusted card CSS can't exfiltrate data, escape its scope, or override the app theme. ## What changed - **`lib/card-css.ts`** — sanitizes + scopes untrusted card CSS: strips network/script vectors, removes `:has()` / `position: fixed` / history-probing, blocks theme-token overrides and `!important`, namespaces `@keyframes` and `@font-face` (anti-collision salted), recurses into `@media`, canonicalizes CSS escape sequences so the literal-text guards can't be evaded, and rewrites every selector under a per-card scope. Plus `filterCssByMode` for `@chat-mode` surface targeting. - **`lib/creator-notes-css.ts`** — lifts `<style>` blocks out of `creator_notes`. - **`CreatorNotesCssInjector`** — for the chat's active characters, extracts → filters → scopes → injects one `<style id="marinara-card-css">` into `<head>`, **unlayered** so its scoped selectors override the app's own message styling (the app sets some message styles with unlayered rules that beat any `@layer` — wrapping card CSS in a layer silently neutered most theming; the sanitizer + per-card scope still contain it). Mounted in `ChatArea` for the roleplay + conversation surfaces. - **`ChatMetadata.cardCssMode`** — new per-chat tri-state: `disabled` (default — card styling is opt-in per chat) / `exclusive` (each character's CSS only styles their own messages) / `chat` (whole area). - **Card Theming control** — a segmented selector in the chat settings drawer, shown only when an active character actually ships creator-notes CSS. - **Scope markers** — `.mari-card-css` + `data-chat-mode` on the roleplay + conversation surface containers; `data-card-css={characterId}` on the message wrappers. Game mode is intentionally out of scope here (its surface and narration log have a distinct structure and stacking); a follow-up can extend the same markers to it. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Already run (automated): client typecheck (`tsc -b`), client lint (`eslint`), and server typecheck all pass. Full `pnpm check` and the in-app checks below still need a human. Please manually verify in the app: - Add a `<style>…</style>` block to a character's **Creator Notes** (e.g. `<style>.mari-message-bubble { border: 2px solid hotpink; }</style>`), open a chat with that character, and confirm the **Card Theming** section appears in chat settings and the styling shows. - Switch the mode: **Disabled** removes all card styling; **Exclusive** styles only that character's messages; **Chat** styles the whole area. Confirm the mode persists across a reload (it is per-chat metadata). - In a 2+ character chat, confirm **Exclusive** keeps each card's CSS to its own messages (no bleed between characters). - Security spot-checks — confirm these are neutralized: a `url(http://…)` background (no outbound request), a `--primary: red` override (app theme unchanged), a `position: fixed` overlay, a `:has()` probe, and an escaped evasion such as `po\73ition: fixed`. - Confirm a normal chat with no card CSS is visually unchanged, and that the roleplay, conversation, and visual-novel surfaces all behave. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) To be added: screenshots of the Card Theming control and a card's CSS applied in chat (disabled / exclusive / chat). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:24 +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#5677
No description provided.