From ca90e4483b30cbab2a28e160fea39c6aaade0632 Mon Sep 17 00:00:00 2001 From: bili-agent Date: Tue, 27 Oct 2026 21:00:49 +0800 Subject: [PATCH] fix: blog detail route rejected non-ASCII slugs, falling back to overview (#4957) The #/blog/ route regex only admitted [A-Za-z0-9_-], but slugs keep whatever the filename carried after the date prefix (CJK, dots, spaces) and the list href is percent-encoded (%XX), so clicking any such article fell through to the overview page. Relax the capture to (.+), mirroring the sessions route above; loadBlog already does exact id matching with a not-found back-link card. --- src/web/client.ts | 2 +- tests/web-blog-route.test.ts | 39 ++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/web/client.ts b/src/web/client.ts index f15f5ac4bc..b6d1cb7b4c 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -4320,7 +4320,7 @@ if (d.keepalive) kv(parts, t("det.keepalive"), d.keepalive.tierLabel + " · " + if (name === "logs" && hq) applyLogFilter(hq); } else { const ses = hash.match(new RegExp("^#/sessions(?:/(.+))?$")) || hash.match(new RegExp("^#/session/(.+)$")); - const blg = hash.match(new RegExp("^#/blog/([A-Za-z0-9_-]+)$")); + const blg = hash.match(new RegExp("^#/blog/(.+)$")); if (ses) { name = "sessions"; if (ses[1]) detailId = decodeURIComponent(ses[1]); diff --git a/tests/web-blog-route.test.ts b/tests/web-blog-route.test.ts index 7f290105bc..373ae4232c 100644 --- a/tests/web-blog-route.test.ts +++ b/tests/web-blog-route.test.ts @@ -36,7 +36,17 @@ const mkHarnessEl = (): HarnessEl => ({ const FEED = { items: [{ id: "demo", kind: "post", date: "2026-10-08", title: "Demo post", titleZh: "演示", excerpt: "ex", html: "

hi

", htmlZh: "

你好

" }] }; -async function runClient(hash: string): Promise<{ els: Map; blogHits: number; unhandled: unknown[] }> { +// #4957: blog slugs keep whatever the filename carried after the date prefix +// (blogfeed.ts strips only `YYYY-MM-DD_`), so ids can contain dots, spaces and +// CJK text — and the list href is percent-encoded, so it also contains `%`. +const NONASCII_FEED = { + items: [ + { id: "v0.1.208·压缩上线", kind: "post", date: "2026-10-20", title: "Dot and middot id", titleZh: "", excerpt: "ex", html: "

dot

", htmlZh: "" }, + { id: "深入浅出上下文压缩", kind: "post", date: "2026-10-21", title: "CJK id", titleZh: "", excerpt: "ex", html: "

cjk

", htmlZh: "" }, + ], +}; + +async function runClient(hash: string, feed: unknown = FEED): Promise<{ els: Map; blogHits: number; unhandled: unknown[] }> { const unhandled: unknown[] = []; const onRej = (r: unknown) => unhandled.push(r); process.on("unhandledRejection", onRej); @@ -62,7 +72,7 @@ async function runClient(hash: string): Promise<{ els: Map; b fetch: async (url: string | URL | Request) => { if (String(url).endsWith("/blog")) { blogHits += 1; - return { ok: true, status: 200, json: async () => FEED }; + return { ok: true, status: 200, json: async () => feed }; } return { ok: true, status: 200, json: async () => ({}) }; }, @@ -113,6 +123,31 @@ test("unknown slug degrades to a back link without a crash", async () => { assert.ok(!list.innerHTML.includes("post-body"), "no article body for an unknown slug"); }); +test("blog ids containing non-ASCII or dots route to the article detail, not overview (#4957)", async () => { + // The hash is exactly what the index row href produces (encodeURIComponent of + // the id): dots survive literal, CJK becomes %XX sequences. The old route + // regex only admitted [A-Za-z0-9_-] so both fell back to overview. + for (const item of NONASCII_FEED.items) { + const hash = "#/blog/" + encodeURIComponent(item.id); + const r = await runClient(hash, NONASCII_FEED); + assert.equal(r.unhandled.length, 0, `${hash} raised unhandled rejection: ${r.unhandled.join("; ")}`); + assert.equal(r.blogHits, 1, `${hash} fetches the feed once`); + assert.ok(r.els.get("page-blog") && !r.els.get("page-blog")!.hidden, `${hash} shows the blog section`); + assert.ok(r.els.get("page-overview")!.hidden, `${hash} must NOT fall back to the overview page`); + const list = r.els.get("bl-list")!; + assert.ok(list.innerHTML.includes("post-body"), `${hash} renders the article body`); + } +}); + +test("unknown non-ASCII blog slug degrades to the back-link card without a crash", async () => { + const hash = "#/blog/" + encodeURIComponent("不存在的文章.md"); + const r = await runClient(hash, NONASCII_FEED); + assert.equal(r.unhandled.length, 0, `client raised unhandled rejection: ${r.unhandled.join("; ")}`); + const list = r.els.get("bl-list")!; + assert.ok(list.innerHTML.includes(''), "back link present for an unknown non-ASCII slug"); + assert.ok(!list.innerHTML.includes("post-body"), "no article body for an unknown slug"); +}); + test("default and other hashes do NOT activate the blog view or hit the blog endpoint", async () => { for (const hash of ["#/overview", "#/logs"]) { const r = await runClient(hash);