Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/agent-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ Ciki 采用专表与职责分离设计,API 令牌分为两种类型(`kind`

头像文件(`avatars/`)不走引用授权。若两条均不成立或 key 形状不合法,接口一律返回 404,不泄露文件是否存在。

Embed 文件读回的 `Content-Type` 为 `application/octet-stream`,返回字节即 R2 原文件真身(无 CF challenge 注入),Agent 按字节原样重传即可。

---

## 五、画布写入规范(两步法)
Expand Down Expand Up @@ -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 分钟。
Expand Down
10 changes: 8 additions & 2 deletions worker/src/routes/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions worker/test/read-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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())
Expand All @@ -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)
})
Expand Down