From dd79ebbb536f79c21dbc9d0111e2ee3387a10625 Mon Sep 17 00:00:00 2001 From: qer Date: Fri, 17 Jul 2026 20:09:18 +0800 Subject: [PATCH 1/2] fix(server): allow inline styles in CSP so web math renders on non-loopback binds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The security CSP fell back to default-src 'self' for styles, which strips the inline style attributes that KaTeX (injected via innerHTML) uses for all glyph positioning — formulas collapsed into overlapping characters on any non-loopback-served web UI. Shiki highlighting and Mermaid diagrams hit the same mechanism. Add style-src 'self' 'unsafe-inline'; scripts remain strictly restricted. --- .changeset/great-math-spoons.md | 5 ++ .../src/middleware/securityHeaders.ts | 8 ++- .../kap-server/test/hostExposure.e2e.test.ts | 2 +- .../kap-server/test/securityExposure.test.ts | 2 +- .../kap-server/test/securityHeaders.test.ts | 52 +++++++++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 .changeset/great-math-spoons.md create mode 100644 packages/kap-server/test/securityHeaders.test.ts diff --git a/.changeset/great-math-spoons.md b/.changeset/great-math-spoons.md new file mode 100644 index 0000000000..74e301a442 --- /dev/null +++ b/.changeset/great-math-spoons.md @@ -0,0 +1,5 @@ +--- +"@moonshot-ai/kimi-code": patch +--- + +web: Fix LaTeX formulas rendering as garbled overlapping text when the web UI is accessed over the network; the server's content security policy now allows the inline styles that math and code highlighting rely on, while scripts remain strictly restricted. diff --git a/packages/kap-server/src/middleware/securityHeaders.ts b/packages/kap-server/src/middleware/securityHeaders.ts index 6df53f7502..b1bab92d02 100644 --- a/packages/kap-server/src/middleware/securityHeaders.ts +++ b/packages/kap-server/src/middleware/securityHeaders.ts @@ -20,6 +20,12 @@ * Invariant: the served bundle must contain no inline scripts (guarded * by a kimi-web test), so plain `script-src` falling back to * `default-src 'self'` suffices. + * `style-src` needs 'unsafe-inline': KaTeX math and Shiki highlighting + * are rendered off-thread and injected via innerHTML with per-glyph + * `style="…"` attributes (KaTeX carries ALL vertical/font sizing in + * them — stripping collapses formulas into overlapping glyphs), and + * Mermaid embeds an inline