diff --git a/worker/src/lib/migrations.ts b/worker/src/lib/migrations.ts index 822e8f3..705281b 100644 --- a/worker/src/lib/migrations.ts +++ b/worker/src/lib/migrations.ts @@ -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}` 占位)拼装,不写死前缀。 diff --git a/worker/src/routes/me.ts b/worker/src/routes/me.ts index 477dba8..3aeac2d 100644 --- a/worker/src/routes/me.ts +++ b/worker/src/routes/me.ts @@ -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) diff --git a/worker/src/routes/twofa-manage.ts b/worker/src/routes/twofa-manage.ts index 49edc0d..c9008b2 100644 --- a/worker/src/routes/twofa-manage.ts +++ b/worker/src/routes/twofa-manage.ts @@ -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) diff --git a/worker/test/passkey-name-totp-label.test.ts b/worker/test/passkey-name-totp-label.test.ts index 36888dc..3eda0ae 100644 --- a/worker/test/passkey-name-totp-label.test.ts +++ b/worker/test/passkey-name-totp-label.test.ts @@ -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 { @@ -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 } })