fix(files,security): S-1 per-source Move authorization, S-2 session-only annotation, S-5 restore guards

S-1 (/files/move, /files/organize action=move):
- organize-guard: authorizeMoveEntities checks every source's media entities
  (declared entity plus entities of registered assets under the source);
  one-time approval still covers the whole request; any source that cannot
  be attributed to an entity only passes with an approval; denial lists every
  unauthorized item; optional non-consuming pre-check
- transfer: entities per source (directory = registered assets beneath it)
- organize: all move groups pre-checked before any group executes; any
  denial -> whole request 403 approval_required
S-2:
- api.ts: GET /files/download and POST /files/trash/purge marked
  auth:"session"; files routes attach requireAdminSession from the registry
  annotation; handler-side purge check removed, download keeps a same-rule
  defense check; permanent delete uses isAdminSession
- DEV key rule written down: equals an admin-scope key for session-only
  endpoints, still admin for Move/delete approval bypass (as authorizeMove)
- api-registry.test: session list updated; new reverse assertion that no
  non-session endpoint answers 403 to an admin-scope bearer
S-5 (trash restore):
- restore-to requires a writable managed library root (isWritableTarget)
- in-place restore rebuilds missing parents level by level without
  following symlinks (mkdirWithin), realpath-checked per level
- cross-volume restore capped by FILES_TRASH_CROSS_MAX_BYTES; cross-volume
  copies (trash in / restore out) go through fsproxy
Tests: multi-entity move 403 + listing, unattributed source, organize foreign
asset 403, download/purge 403 for DEV and admin-scope keys, restore target
not writable, symlinked parent, rebuild parents, cross-volume cap.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
rootandClaude Opus 5.5 committed 2026-09-28 00:45:56 +08:00
1 parent 9887d8b311
commit 17cf3cbf97
16 files changed
+587 -107

No files matched your search

@@ -348,4 +348,80 @@ describe.skipIf(skip)("files 写操作", () => {
expect(await readFile(join(h.lib, "A", "CopyMe.mkv"), "utf8")).toBe("copy");
expect(await exists(join(h.dl, "CopyMe.mkv"))).toBe(true);
});
it("多源 Move(验收 07 S-1):两个作品只授权一个 → 整体 403 并列出未授权项;都授权后放行;未归属作品的源只认审批", async () => {
const e1 = await seedEntity(h.db, { kind: "movie", title: "授权作品", year: 2011 });
const e2 = await seedEntity(h.db, { kind: "movie", title: "未授权作品", year: 2012 });
await mkdir(join(h.lib, "S1"));
await mkdir(join(h.lib, "S1dst"));
await mkdir(join(h.lib, "S1", "Two"));
await writeFile(join(h.lib, "S1", "One.2011.mkv"), "one");
await writeFile(join(h.lib, "S1", "Two", "Two.2012.mkv"), "two");
await seedAsset(h.db, h, { entityId: e1, rel: "S1/One.2011.mkv" });
await seedAsset(h.db, h, { entityId: e2, rel: "S1/Two/Two.2012.mkv" });
const g1 = await h.db
.insertInto("automation_grant")
.values({ action: "organize.execute", scope: JSON.stringify({ mediaEntityIds: [e1] }), granted_by: "test" })
.returning("id")
.executeTakeFirstOrThrow();
const body = {
items: [
{ root: h.libRoot, path: "S1/One.2011.mkv" },
{ root: h.libRoot, path: "S1/Two" },
],
target: { root: h.libRoot, path: "S1dst" },
mode: "move",
};
const run = async () => {
const dry = await h.api("POST", "/files/move", { ...body, idempotencyKey: key(), dryRun: true }, EXEC_KEY);
expect(dry.status).toBe(200);
return h.api(
"POST",
"/files/move",
{ ...body, idempotencyKey: key(), dryRun: false, planId: dry.body.data.planId },
EXEC_KEY,
);
};
const denied = await run();
expect(denied.status).toBe(403);
expect(denied.body.error.message).toMatch(/^approval_required:/);
expect(denied.body.error.message).toContain("S1/Two");
expect(denied.body.error.message).toContain(e2);
expect(denied.body.error.message).not.toContain("S1/One.2011.mkv(");
// 整体拒绝:两个源都没动
expect(await exists(join(h.lib, "S1", "One.2011.mkv"))).toBe(true);
expect(await exists(join(h.lib, "S1", "Two", "Two.2012.mkv"))).toBe(true);
const audit = await h.db
.selectFrom("audit_event")
.select(["payload"])
.where("action", "=", "files.move_denied")
.orderBy("at", "desc")
.executeTakeFirst();
expect(JSON.stringify(audit?.payload)).toContain(e2);
// 未登记文件(无法归属作品)混入:即使其余都有授权也拒绝
await writeFile(join(h.lib, "S1", "Loose.mkv"), "loose");
const g2 = await h.db
.insertInto("automation_grant")
.values({ action: "organize.execute", scope: JSON.stringify({ mediaEntityIds: [e2] }), granted_by: "test" })
.returning("id")
.executeTakeFirstOrThrow();
const loose = { ...body, items: [...body.items, { root: h.libRoot, path: "S1/Loose.mkv" }] };
const ldry = await h.api("POST", "/files/move", { ...loose, idempotencyKey: key(), dryRun: true }, EXEC_KEY);
const ldenied = await h.api(
"POST",
"/files/move",
{ ...loose, idempotencyKey: key(), dryRun: false, planId: ldry.body.data.planId },
EXEC_KEY,
);
expect(ldenied.status).toBe(403);
expect(ldenied.body.error.message).toContain("S1/Loose.mkv");
expect(ldenied.body.error.message).toContain("一次性审批");
// 两个作品都被覆盖 → 放行
const ok = await run();
expect(ok.status).toBe(200);
expect(await exists(join(h.lib, "S1dst", "One.2011.mkv"))).toBe(true);
expect(await exists(join(h.lib, "S1dst", "Two", "Two.2012.mkv"))).toBe(true);
await h.db.updateTable("automation_grant").set({ status: "revoked" }).where("id", "in", [g1.id, g2.id]).execute();
});
});