Skip to content

fix(server): allow Shiki WASM in CSP - #4435

Merged
wbxl2000 merged 2 commits into
mainfrom
fix/web-csp-wasm-highlight
Oct 26, 2026
Merged

wbxl2000 merged 2 commits into
mainfrom
fix/web-csp-wasm-highlight

Conversation

@kimi-agent-bot

Copy link
Copy Markdown
Collaborator

Requirement or Bug

修复非回环 Kimi Web 页面中 Shiki 代码高亮因 WebAssembly 被 CSP 拦截而失效的问题。

Bug Reproduction Steps

  1. 以非回环地址启动 kimi web,使页面带上服务端 CSP。
  2. 打开使用 Shiki 的代码或 diff 视图。
  3. 动态加载的 Shiki chunk 成功,但 WebAssembly.instantiate() 被 default-src 'self' 拦截。
  4. 组件捕获异常并清空 tokens,页面退化为无色纯文本。

Root Cause

Shiki 默认使用 Oniguruma 的 WASM 正则引擎。CSP 未声明 script-src,因此脚本策略回退到 default-src 'self',不包含 WASM 编译所需的 'wasm-unsafe-eval'。本次精确声明 WASM 权限,保留 JavaScript eval 和 inline script 的禁止策略,是根本修复。

Code Changes

 Content-Security-Policy
-  script-src: 未声明,回退到 default-src 'self'
+  script-src 'self' 'wasm-unsafe-eval'
  • 不添加 'unsafe-eval'、'unsafe-inline' 或其他脚本来源。
  • 更新 securityHeaders、securityExposure 和 hostExposure.e2e 的 CSP 契约。
  • 新增测试,明确允许 WASM 且仍禁止 JavaScript eval。
  • 增加 patch changeset。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
Shiki/WASM 初始化 非回环页面中的 WASM 编译被 CSP 阻止,代码高亮退化为纯文本 允许同源应用所需的 WASM 编译,Shiki 恢复着色 使用 LAN、反向代理或其他非回环绑定的 Web 用户 不需要;沿用既有 Shiki 实现
JavaScript eval 和 inline script 被 CSP 禁止 继续禁止 依赖严格脚本 CSP 的部署 不需要
回环页面 不下发这份 CSP 不变 本机 127.0.0.1 Web 用户 不需要

验证:

  • securityHeaders.test.ts、securityExposure.test.ts、hostExposure.e2e.test.ts:3 个文件、17 条用例通过。
  • pnpm --filter @moonshot-ai/kap-server run typecheck 通过。
  • 同一 HighlightedCode 生产构建对照:修复前 0 个着色 token 并记录 WASM CompileError;修复后 47 个着色 token,WASM 错误消失。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete; no behavior is removed and no default is flipped.
  • Ran gen-changesets skill and included the approved patch changeset.
  • Ran gen-docs skill, or this PR needs no doc update. (No user documentation change required)

Requested by qer via Mira.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T11:42:26.592284Z a4377f0 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@changeset-bot

changeset-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a4377f0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@a4377f0
npx https://pkg.pr.new/@moonshot-ai/kimi-code@a4377f0

commit: a4377f0

sailist
sailist previously approved these changes Oct 26, 2026
@wbxl2000
wbxl2000 merged commit 70a661c into main Oct 26, 2026
16 checks passed
@wbxl2000
wbxl2000 deleted the fix/web-csp-wasm-highlight branch October 26, 2026 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants