From 7c1da27a2c57fb1f514a9569032fb04d55dc8b65 Mon Sep 17 00:00:00 2001 From: dog Date: Tue, 27 Oct 2026 21:02:06 +0800 Subject: [PATCH] fix: pin sandbox CSP + nosniff on every blog asset response (#4958) --- src/web/blogfeed.ts | 24 +++++++++++++++----- tests/web-blogfeed.test.ts | 45 ++++++++++++++++++++++++++++++++++++-- tests/web-routing.test.ts | 7 +++++- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/src/web/blogfeed.ts b/src/web/blogfeed.ts index fcd654dbe4..494e7627dc 100644 --- a/src/web/blogfeed.ts +++ b/src/web/blogfeed.ts @@ -40,6 +40,19 @@ let rootOverride: string | undefined; let probedRoot: string | undefined; let state: { items: BlogItem[] } | undefined; +/** Defense-in-depth on EVERY blog-asset response (#4958): asset bytes can + * come from GitHub master (remote overlay, #4906) and .svg is served as + * image/svg+xml — DIRECTLY NAVIGATED, that is active content on the panel + * origin (which also hosts the config editor and its provider API keys), so + * an embedded "; + registerRemoteBlogFeed({ items: [], assets: new Map([["paper/figures/remote.svg", { buf: Buffer.from(evilSvg), type: "image/svg+xml" }]]) }); + const overlay = fakeRes(); + handleBlogAssetGet(overlay.res, "/__bili/blog-assets/paper/figures/remote.svg"); + assert.equal(overlay.status, 200); + assert.equal(overlay.headers["content-security-policy"], "sandbox"); + assert.equal(overlay.headers["x-content-type-options"], "nosniff"); + assert.match(String(overlay.headers["content-type"]), /^image\/svg\+xml/); + assert.equal(overlay.body, evilSvg, "body bytes unchanged by the header fix"); + + // On-disk fallback branch keeps the same pair with its bytes/type intact. + const disk = fakeRes(); + handleBlogAssetGet(disk.res, "/__bili/blog-assets/paper/figures/p.png"); + assert.equal(disk.status, 200); + assert.equal(disk.headers["content-security-policy"], "sandbox"); + assert.equal(disk.headers["x-content-type-options"], "nosniff"); + assert.match(String(disk.headers["content-type"]), /^image\/png/); + assert.equal(disk.body, "PAPERPNG"); + + // 404s from the asset route carry the pair too — every response of the route. + const miss = fakeRes(); + handleBlogAssetGet(miss.res, "/__bili/blog-assets/paper/figures/nope.png"); + assert.equal(miss.status, 404); + assert.equal(miss.headers["content-security-policy"], "sandbox"); + assert.equal(miss.headers["x-content-type-options"], "nosniff"); +}); + test("handleBlogGet answers 200 JSON always; broken manifest degrades to posts-only", () => { const warm = fakeRes(); handleBlogGet(warm.res); @@ -139,6 +171,15 @@ test("handleBlogGet answers 200 JSON always; broken manifest degrades to posts-o assert.ok(data.items.every((i) => i.kind === "post")); }); +test("the sandbox/nosniff pair stays scoped to the asset route — the blog feed JSON route does not gain it (#4958)", () => { + const feed = fakeRes(); + handleBlogGet(feed.res); + assert.equal(feed.status, 200); + assert.equal(feed.headers["content-security-policy"], undefined); + assert.equal(feed.headers["x-content-type-options"], undefined); + assert.match(String(feed.headers["content-type"]), /^application\/json/); +}); + test("a root without content degrades to an empty list instead of throwing", () => { _setBlogRootForTest(join(outsideDir, "does-not-exist")); assert.deepEqual(getBlogFeed(), { items: [] }); diff --git a/tests/web-routing.test.ts b/tests/web-routing.test.ts index 918500b28d..b2818fafa5 100644 --- a/tests/web-routing.test.ts +++ b/tests/web-routing.test.ts @@ -65,7 +65,12 @@ test("Web UI exposes upstream controls without inline handlers", async () => { const port = (proxy.address() as { port: number }).port; const base = `http://127.0.0.1:${port}`; try { - const ui = await (await fetch(`${base}/__bili/`)).text(); + const uiRes = await fetch(`${base}/__bili/`); + const ui = await uiRes.text(); + // #4958 negative direction: the sandbox CSP + nosniff pair is scoped to + // the blog ASSET route only — the panel HTML route must not gain it. + assert.equal(uiRes.headers.get("content-security-policy"), null); + assert.equal(uiRes.headers.get("x-content-type-options"), null); assert.match(ui, /Codex(ChatGPT 登录)/); assert.match(ui, /上游网络/); assert.match(ui, /Fork me on GitHub/);