Skip to content

[PR] Consistently apply sandbox CSP + nosniff to blog asset responses, blocking same-origin SVG script execution in the panel | 博客资产响应统一加 sandbox CSP + nosniff,堵住面板同源 SVG 执行脚本 - #4970

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-27_blog-asset-sandbox-headers
Oct 27, 2026

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

(ai: zhipuai-lb/glm-5.3)

Fixes #4958

问题

GET /__bili/blog-assets/<rel> 把博客图片直接从面板同源服务。其中 SVG 以 image/svg+xml 类型响应,且此前没有任何 sandbox/nosniff 头。SVG 是主动内容:用户把浏览器直接导航到该 URL 时,SVG 内嵌的 <script> 会在面板 origin 上执行——而这个 origin 同时挂着配置编辑器(PUT /__bili/config,含各 provider 的 API key),脚本可以读取并外传。

信任模型原本不一致:博客 markdown 按「不可信」处理(escape-first 渲染),而资产字节按「可信」直出。#4906 之前资产只能来自随包文件;#4906 之后远端 GitHub master 字节也会直出,一次坏合并(外部 PR 里一个看似无害的 figures/*.svg)= 所有已升级用户面板上的存储型 XSS。

根因

  • src/web/blogfeed.ts handleBlogAssetGet 的三处 writeHead 都只带 content-type + cache-control。
  • ASSET_TYPES 含 .svg → image/svg+xml。

修法

  • 新增 ASSET_SECURITY_HEADERS = { "content-security-policy": "sandbox", "x-content-type-options": "nosniff" },应用到该路由的每一处响应:远端 overlay 200、磁盘兜底 200、404。
  • content-type / cache-control / 响应字节保持逐字节不变;.svg 保留在 ASSET_TYPES(页内 <img> 引用不受 sandbox 头影响——src/web/markdown.ts 只允许 https?:// 或 /__bili/blog-assets/ 的 img src,而 <img> 方式加载的 SVG 本身不执行脚本,sandbox 只约束把资产当文档直接导航的场景)。
  • 零配置面变化、零新依赖。

测试

  • tests/web-blogfeed.test.ts 新增:
    • 正向:远端 overlay 路径(带 <script> 的 SVG fixture)与磁盘兜底路径的响应都带 content-security-policy: sandbox + x-content-type-options: nosniff,且 content-type 与响应字节不变;资产路由 404 也带这对头。
    • 负向:handleBlogGet(博客 JSON feed 路由)不获得这对头。
  • tests/web-routing.test.ts:真服务器断言面板 HTML 路由(GET /__bili/)不获得这对头(改动方向钉死:头只加在资产路由)。

本地验证:node --import tsx --test tests/web-blogfeed.test.ts 10/10 通过;tests/web-routing.test.ts 8/8 通过;tests/web-blog-refresh.test.ts 17/17 通过(相邻面回归);npm run typecheck 干净。完整套件以后续 CI test lanes 为准。

@ranxianglei ranxianglei changed the title 博客资产响应统一加 sandbox CSP + nosniff,堵住面板同源 SVG 执行脚本 [PR] Consistently apply sandbox CSP + nosniff to blog asset responses, blocking same-origin SVG script execution in the panel | 博客资产响应统一加 sandbox CSP + nosniff,堵住面板同源 SVG 执行脚本 Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-27_blog-asset-sandbox-headers (8a5aa3f)

Latest build — install this exact version

npm install -g billion-context@0.0.6489-pr.4970.0e2ed75

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
8a5aa3f billion-context@0.0.6489-pr.4970.0e2ed75 build
7c1da27 billion-context@0.0.6481-pr.4970.4b76827 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr4970.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei
ranxianglei merged commit 51c9861 into master Oct 27, 2026
3 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.884-master.51c9861 (rolling master tag).

npm install -g billion-context@0.0.884-master.51c9861  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

ranxianglei pushed a commit that referenced this pull request Oct 27, 2026
Follow-up merge after master advanced past c499b14 mid-resolution (#4969/#4970/#4972/#4971 blog+billing, #4544 event-loop-yield, #4678 mysql-v8-dedup, #4782 dsh-goal-subfork). Conflicts:
- src/server/prepare-responses.ts: import hunk unioned again - kept this branch's fold-reconcile import line (superset of master's: adds noteBiliToolSurface/outboundBiliSurface); master's yieldToEventLoop lines from #4528 landed in all four prepare files untouched.
- tests/golden/storage-render/{anthropic,chat,google,responses}.json: regenerated from the merged tree via scripts/update-storage-render-goldens.ts; output byte-identical to the previous round's regeneration (master's new commits did not change these golden conversations' outbound wire or persisted state) and differing from master only by this branch's image-rescale armed-state latch digests.
Verification: no conflict markers remain; targeted suites green (golden-storage-render + fold-reconcile + issue3400-image-rescale = 109/109); typecheck clean. Full-suite verdict deferred to CI on the pushed head.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant