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
10 changes: 5 additions & 5 deletions worker/src/lib/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* 迁移的唯一定义处:单一基线 `schema_v1`(历史增量 schema_v1..v6 坍缩)。
* 迁移的唯一定义处:单一基线 `schema_v1`(历史增量 schema_v1..v6 坍缩)与增量迁移(如 schema_v7)。
*
* 舰队标准口径:迁移一律 schema_vN、基线恒 schema_v1、新增递增编号、取代功能名式。
*
* - 全新库:`schema_v1` 的 sql 按 schema.ts 的定版 DDL 一次建出全部表与索引即最终形态。
* - 生产库:已有 v1..v6 历史记录的生产库由执行器只读识别为已最新(账本已有 schema_v1,
* 直接 skipped),不重跑、不报错、不写任何数据(含账本)。
* - 重复执行:全部 `CREATE … IF NOT EXISTS`,幂等空操作(账本已记账时直接 skipped)。
* - 全新库:`schema_v1` 的 sql 按 schema.ts 的定版 DDL 一次建出全部表与索引即最终形态,后续增量迁移幂等执行。
* - 生产库:已有 v1..v6 历史记录的生产库由执行器识别基线 `schema_v1` 为已应用(直接 skipped);
* 后续增量迁移(如 `schema_v7`)避开历史编号,存量库未应用时正常执行 prepare/sql 并在成功后记账。
* - 重复执行:全部 `CREATE … IF NOT EXISTS` 与 prepare 动态探测保持幂等(账本已记账时直接 skipped)。
*
* 语句之间用 `;` 分隔,语句内部不得出现 `;`(含字符串字面量)。
* 表名/索引名一律经 tables.ts(基线迁移经 schema.ts 的 `{prefix}` 占位)拼装,不写死前缀。
Expand Down
2 changes: 1 addition & 1 deletion worker/src/routes/me.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ meRoutes.patch("/passkeys/:id", async (c) => {
return apiError(c, 404, "PASSKEY_NOT_FOUND", "Passkey not found")
}
const body = await readJsonBody<{ name?: unknown }>(c)
if (!body || typeof body !== "object") {
if (!body || typeof body !== "object" || typeof body.name !== "string") {
return apiError(c, 400, "INVALID_REQUEST", "Invalid request body")
}
const name = normalizeTag(body.name)
Expand Down
2 changes: 1 addition & 1 deletion worker/src/routes/twofa-manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ twoFactorManageRoutes.patch("/totp/label", async (c) => {
}

const body = await readJsonBody<{ label?: unknown }>(c)
if (!body || typeof body !== "object") {
if (!body || typeof body !== "object" || typeof body.label !== "string") {
return apiError(c, 400, "INVALID_REQUEST", "Invalid request body")
}
const label = normalizeTag(body.label)
Expand Down
60 changes: 44 additions & 16 deletions worker/test/passkey-name-totp-label.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,28 @@ describe("Passkey 命名与改名契约", () => {
expect(listAfter.items[0].name).toBe("Work Laptop Key")

// 缺省/非法 body 返回 400 INVALID_REQUEST,不静默清空
const invalidBodyRes = await request(env, `/api/me/passkeys/${passkeyId}`, {
method: "PATCH",
headers: { cookie }
})
expect(invalidBodyRes.status).toBe(400)
expect(((await invalidBodyRes.json()) as { code: string }).code).toBe(
"INVALID_REQUEST"
)
for (const badBody of [
undefined,
{},
{ name: 123 },
{ name: null },
{ name: [] },
{ name: {} }
]) {
const res =
badBody === undefined
? await request(env, `/api/me/passkeys/${passkeyId}`, {
method: "PATCH",
headers: { cookie }
})
: await patchJson(env, `/api/me/passkeys/${passkeyId}`, badBody, {
cookie
})
expect(res.status).toBe(400)
expect(((await res.json()) as { code: string }).code).toBe(
"INVALID_REQUEST"
)
}
const listAfterInvalid = (await (
await request(env, "/api/me/passkeys", { headers: { cookie } })
).json()) as {
Expand Down Expand Up @@ -353,14 +367,28 @@ describe("TOTP 标签与 UserView 契约", () => {
expect(await patchLabel.json()).toEqual({ success: true })

// 缺省/非法 body 请求返回 400 INVALID_REQUEST,不静默清空
const badPatch = await request(env, "/api/auth/2fa/totp/label", {
method: "PATCH",
headers: { cookie }
})
expect(badPatch.status).toBe(400)
expect(((await badPatch.json()) as { code: string }).code).toBe(
"INVALID_REQUEST"
)
for (const badBody of [
undefined,
{},
{ label: 123 },
{ label: null },
{ label: [] },
{ label: {} }
]) {
const res =
badBody === undefined
? await request(env, "/api/auth/2fa/totp/label", {
method: "PATCH",
headers: { cookie }
})
: await patchJson(env, "/api/auth/2fa/totp/label", badBody, {
cookie
})
expect(res.status).toBe(400)
expect(((await res.json()) as { code: string }).code).toBe(
"INVALID_REQUEST"
)
}

const meAfterPatch = (await (
await request(env, "/api/me", { headers: { cookie } })
Expand Down