From 1643df798065199952a98b5e5dfc8ea1b0851875 Mon Sep 17 00:00:00 2001 From: mapxn <16299874+mapxn@users.noreply.github.com> Date: Sun, 29 Nov 2026 16:07:10 +0800 Subject: [PATCH] chore: checkpoint D0yJDt3ckGs9y5Lq4K7kd --- docs/agent-guide.md | 4 ++++ worker/src/routes/read.ts | 10 ++++++++-- worker/test/read-api.test.ts | 8 +++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/docs/agent-guide.md b/docs/agent-guide.md index a6edd07..c00bfec 100644 --- a/docs/agent-guide.md +++ b/docs/agent-guide.md @@ -218,6 +218,8 @@ Ciki 采用专表与职责分离设计,API 令牌分为两种类型(`kind` 头像文件(`avatars/`)不走引用授权。若两条均不成立或 key 形状不合法,接口一律返回 404,不泄露文件是否存在。 +Embed 文件读回的 `Content-Type` 为 `application/octet-stream`,返回字节即 R2 原文件真身(无 CF challenge 注入),Agent 按字节原样重传即可。 + --- ## 五、画布写入规范(两步法) @@ -397,6 +399,8 @@ window.addEventListener("message", (event) => { [PUT /api/write/pages/:id] (第二遍更新) --> 重映射页面链接与文件 Key,提交最终定稿正文 ``` +「拉取字节」一步注意:Embed(`embeds/*.html`)的读回 `Content-Type` 为 `application/octet-stream`,返回字节即 R2 真身(无 CF challenge 注入),Agent 按字节原样重传即可,不要按 HTML 解码/重编码。 + ### 11.2 并发与重试 - **频控限额**:写端点为单账号 600 次 / 5 分钟,认证失败为 20 次 / 5 分钟;读端点为单账号 3000 次 / 5 分钟。 diff --git a/worker/src/routes/read.ts b/worker/src/routes/read.ts index f02f1df..9f326ff 100644 --- a/worker/src/routes/read.ts +++ b/worker/src/routes/read.ts @@ -318,7 +318,8 @@ async function hasReferencedPageReadAccess( * B. 上传者授权:对象 customMetadata.uploaderId = 令牌主人 userId(覆盖尚未被任何页面引用的暂存文件); * 两条都不成立仍 404 不泄露存在性。 * 头像文件(avatars/ 类别)不走引用授权,维持现行口径不变。 - * - 成功:200 返回文件字节,响应头与会话端点一致(Content-Type + Content-Length + ETag + Cache-Control)。 + * - 成功:200 返回文件字节。embed(`embeds/*.html`)强制 `application/octet-stream`, + * 其余图片/场景 JSON 沿用 R2 `httpMetadata.contentType`;均带 Content-Length + ETag + Cache-Control。 */ readRoutes.get("/files/:key{.+}", async (c) => { const key = c.req.param("key") @@ -345,10 +346,15 @@ readRoutes.get("/files/:key{.+}", async (c) => { return c.json({ error: "File not found" }, 404) } + // 缺陷 #286:embed 原存 R2 的 contentType 是 text/html,CF 会对 text/html 响应注入 + // challenge 脚本,非浏览器客户端(curl/Node fetch)无 cf_clearance 必被污染。这里对 + // embed 强制 octet-stream,让读回的字节即 R2 真身;图片/场景 JSON 本就非 text/html 不受影响。 const headers = new Headers() headers.set( "Content-Type", - object.httpMetadata?.contentType ?? "application/octet-stream" + EMBED_KEY_RE.test(key) + ? "application/octet-stream" + : (object.httpMetadata?.contentType ?? "application/octet-stream") ) headers.set("Content-Length", String(object.size)) if (object.httpEtag) headers.set("ETag", object.httpEtag) diff --git a/worker/test/read-api.test.ts b/worker/test/read-api.test.ts index c46ba6a..e5946f6 100644 --- a/worker/test/read-api.test.ts +++ b/worker/test/read-api.test.ts @@ -574,7 +574,7 @@ describe("只读接口:GET /api/read/files/:key", () => { }) }) - it("本人可用 read 令牌与 write 令牌读回 embed HTML 资产(200 + 逐字节一致 + Content-Type 为 text/html 口径)", async () => { + it("本人可用 read 令牌与 write 令牌读回 embed HTML 资产(200 + 逐字节一致 + Content-Type 为 application/octet-stream 口径)", async () => { const { env } = await initializedContext() const admin = await adminSession(env) const writeTokenObj = await createApiToken(env, admin, "write") @@ -611,7 +611,7 @@ describe("只读接口:GET /api/read/files/:key", () => { `/api/read/files/${embed.key}` ) expect(readRes.status).toBe(200) - expect(readRes.headers.get("content-type")).toContain("text/html") + expect(readRes.headers.get("content-type")).toBe("application/octet-stream") expect(readRes.headers.get("content-length")).toBe(String(htmlBytes.length)) expect(readRes.headers.get("cache-control")).toContain("private") const readBytes = new Uint8Array(await readRes.arrayBuffer()) @@ -625,7 +625,9 @@ describe("只读接口:GET /api/read/files/:key", () => { `/api/read/files/${embed.key}` ) expect(writeRes.status).toBe(200) - expect(writeRes.headers.get("content-type")).toContain("text/html") + expect(writeRes.headers.get("content-type")).toBe( + "application/octet-stream" + ) const writeBytes = new Uint8Array(await writeRes.arrayBuffer()) expect(writeBytes).toEqual(htmlBytes) })