From 3e37fbf0ed108d92d0ce809bcccb0d1b30ba0e10 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 22:24:55 +0900 Subject: [PATCH] fix(storage): delete a partition's IndexedDB dataset when an authorized purge cannot open it (05-03) LOGOUT and ACCOUNT_DELETION purged row by row and so needed an open that a corrupt budget or binding row always rejected, leaving personal rows on the device. After authority approval, a purge that finds the dataset rejected by admission now deletes that partition's own database and reports it; healthy datasets and other partitions are untouched. Co-Authored-By: Claude Opus 5.5 --- .../browser-file-storage-recovery.md | 19 ++- src/adapters/platform/indexeddb-connection.ts | 6 +- .../storage/indexeddb/indexeddb-runtime.ts | 53 +++++- .../browser-file-storage/indexeddb-port.ts | 7 + .../indexeddb-runtime.spec.ts | 127 ++++++++++++++- tests/helpers/memory-indexeddb.ts | 57 ++++++- tests/unit/indexeddb-runtime.test.ts | 153 ++++++++++++++++++ 7 files changed, 412 insertions(+), 10 deletions(-) diff --git a/docs/operations/browser-file-storage-recovery.md b/docs/operations/browser-file-storage-recovery.md index 6a583216..7a90213c 100644 --- a/docs/operations/browser-file-storage-recovery.md +++ b/docs/operations/browser-file-storage-recovery.md @@ -36,6 +36,9 @@ primitive를 coordinator 완료 증거로 사용하지 않는다. incident 중에도 다음 작업은 금지한다. - 자동 또는 일괄 `deleteDatabase()` + - 예외는 하나다. authority가 승인한 LOGOUT/ACCOUNT_DELETION이 손상된 + governance 때문에 dataset을 열 수 없을 때, runtime이 그 partition의 DB + 하나만 지운다(§5 "database 전체 삭제" 참고). - origin의 모든 `caches.keys()` 삭제 - user-authored/unsynced IndexedDB 또는 OPFS data 자동 purge - schema version downgrade @@ -248,7 +251,21 @@ remote tab 강제 종료, database 삭제, 무한 reload는 금지한다. proof를 운영자가 수동 생성·재사용하지 않음 database 전체 삭제는 data owner가 export/recovery와 영향 scope를 승인한 별도 -action이다. +action이다. runtime이 스스로 DB를 지우는 경우는 하나뿐이다(05-03). + +- 조건: `enforceLifecycleBatch`의 action이 `LOGOUT` 또는 `ACCOUNT_DELETION`이고, + `authorizeLifecycle`이 proof를 내준 뒤, open이 `POLICY_REJECTED` 또는 + `CORRUPT_DATA`로 실패했을 때(budget/binding 행 손상, 바뀐 policy). 행 단위 + purge는 open이 필요해서 이 상태에서는 영영 돌 수 없다 + (`src/adapters/storage/indexeddb/indexeddb-runtime.ts` `deleteUnadmittedDataset`). +- 범위: 세 opaque token에서 만든 그 partition의 DB 이름 하나 + (`indexeddb-governance.ts` `indexedDbPhysicalDatabaseName`). 다른 partition과 + origin의 다른 DB는 건드리지 않는다. +- 결과: 지워지면 receipt `COMPLETE`와 `datasetDeleted: true`(행 수는 세지 + 않는다). 다른 context가 연결을 닫지 않아 blocked deadline이 지나면 `BLOCKED` + / `RELOAD_OTHER_CONTEXTS`를 반환한다. 이때 삭제 요청은 브라우저에 살아 있어 + 나중에 적용될 수 있으므로 다른 tab을 닫고 같은 purge를 다시 호출한다. +- governance가 정상이면 지금처럼 행 단위 purge만 하고 DB는 지우지 않는다. 성공 판정: diff --git a/src/adapters/platform/indexeddb-connection.ts b/src/adapters/platform/indexeddb-connection.ts index 29015b51..b7476442 100644 --- a/src/adapters/platform/indexeddb-connection.ts +++ b/src/adapters/platform/indexeddb-connection.ts @@ -605,9 +605,9 @@ export function createIndexedDbConnection( /** * In this module because it is the same `IDBOpenDBRequest` state machine as - * `openIndexedDbDatabase`, not because three subsystems need it — only - * `indexeddb-checkpoint-store.ts` deletes a database, and nothing here asks the - * other three to start. Leaving it out would leave a third hand-written copy of + * `openIndexedDbDatabase`. Two callers delete a database: + * `indexeddb-checkpoint-store.ts`, and the IndexedDB runtime's authorized + * LOGOUT/ACCOUNT_DELETION purge when its dataset fails admission. Leaving it out would leave a third hand-written copy of * the settle-once blocked-deadline latch eight lines away from the kernel's. * * There is deliberately no `signal`: the request cannot be cancelled after diff --git a/src/adapters/storage/indexeddb/indexeddb-runtime.ts b/src/adapters/storage/indexeddb/indexeddb-runtime.ts index ad4e4926..d4bbc4ec 100644 --- a/src/adapters/storage/indexeddb/indexeddb-runtime.ts +++ b/src/adapters/storage/indexeddb/indexeddb-runtime.ts @@ -24,6 +24,7 @@ import { import { snapshotAbortTimers } from "../../platform/abortable-operation.ts"; import { createIndexedDbConnection, + deleteIndexedDbDatabase, openIndexedDbDatabase, type IndexedDbTranslate, } from "../../platform/indexeddb-connection.ts"; @@ -2500,7 +2501,9 @@ export function createIndexedDbRuntime( cutoffEpochMs, ); } else { - result = await purgePartitionRecords(input, deadline); + result = + (await deleteUnadmittedDataset(input.signal)) ?? + (await purgePartitionRecords(input, deadline)); } return observeResult( operation, @@ -2509,6 +2512,54 @@ export function createIndexedDbRuntime( ); } + /** + * 05-03. A LOGOUT or ACCOUNT_DELETION purge must not depend on the dataset + * passing admission: a corrupt governance row rejects every open, so no row + * could ever be purged one by one. The authority has already approved this + * purge, and the database name comes from this partition's own tokens, so + * deleting the whole database touches no other partition. Returns `null` + * when a connection is ready for the row-by-row purge. + */ + async function deleteUnadmittedDataset( + signal: AbortSignal | undefined, + ): Promise | null> { + if (connection.current()) return null; + const opened = await open(signal); + if (opened.ok) return null; + if ( + (opened.error.code !== "POLICY_REJECTED" && + opened.error.code !== "CORRUPT_DATA") || + disposed || + typeof factory?.deleteDatabase !== "function" + ) { + return opened; + } + const deleted = await deleteIndexedDbDatabase({ + factory, + databaseName, + translate: translateFor("INDEXEDDB_WRITE"), + blockedTimeoutMs, + timers, + }); + if (!deleted.ok) return deleted; + if (deleted.value.kind === "BLOCKED_DEADLINE") { + // The request is still live and may yet land; the next call finds out. + return browserDataFailure("BLOCKED", "INDEXEDDB_WRITE", { + retryable: true, + recovery: "RELOAD_OTHER_CONTEXTS", + }); + } + return browserDataSuccess( + Object.freeze({ + state: "COMPLETE" as const, + scannedRows: 0, + deletedRows: 0, + budgetExhausted: false, + datasetDeleted: true as const, + }), + ); + } + function close(): void { if (disposed) return; disposed = true; diff --git a/src/application/ports/browser-file-storage/indexeddb-port.ts b/src/application/ports/browser-file-storage/indexeddb-port.ts index cf71d6f3..9c81a893 100644 --- a/src/application/ports/browser-file-storage/indexeddb-port.ts +++ b/src/application/ports/browser-file-storage/indexeddb-port.ts @@ -144,6 +144,13 @@ export type IndexedDbLifecycleBatchReceipt = Readonly<{ scannedRows: number; deletedRows: number; budgetExhausted: boolean; + /** + * 05-03. Present when an authorized LOGOUT or ACCOUNT_DELETION found the + * dataset's governance rows unreadable, so no row could be purged one by + * one, and deleted the partition's whole database instead. Rows are then + * not counted. + */ + datasetDeleted?: true; }>; export type IndexedDbLifecycleAuthorityRequest = Readonly<{ diff --git a/tests/browser-capabilities/indexeddb-runtime.spec.ts b/tests/browser-capabilities/indexeddb-runtime.spec.ts index feeba8c7..775d5f59 100644 --- a/tests/browser-capabilities/indexeddb-runtime.spec.ts +++ b/tests/browser-capabilities/indexeddb-runtime.spec.ts @@ -23,12 +23,14 @@ async function startLifecycleRuntime( name: string, targetVersion: 1 | 2, blockedTimeoutMs = 1_000, + partitioned = false, ): Promise { await page.evaluate( async ({ currentDatabaseName, currentTargetVersion, currentBlockedTimeoutMs, + currentPartitioned, }) => { const modulePath = "/src/adapters/storage/indexeddb/indexeddb-runtime.ts"; @@ -38,24 +40,31 @@ async function startLifecycleRuntime( type LifecycleValue = Readonly<{ value: string }>; const [authorityToken, namespaceToken, partitionToken] = currentDatabaseName.slice("ca-idb-v1:".length).split("."); + const accountScope = currentPartitioned + ? ("OPAQUE_PARTITION" as const) + : ("ORIGIN_SHARED" as const); const scope = { authorityToken, namespaceToken, partitionToken, - accountScope: "ORIGIN_SHARED" as const, + accountScope, }; const storagePolicy = { owner: "platform-storage", namespace: "indexeddb-browser-lifecycle", classification: "INTERNAL" as const, authority: "SERVER" as const, - accountScope: "ORIGIN_SHARED" as const, + accountScope, retention: { kind: "EXPLICIT_DELETE" as const }, softBudgetBytes: 1_000_000, hardBudgetBytes: 2_000_000, evictionPriority: "SYNCED_COPY" as const, - logoutAction: "KEEP_ORIGIN_SHARED" as const, - accountDeletionAction: "KEEP_ORIGIN_SHARED" as const, + logoutAction: currentPartitioned + ? ("PURGE_PARTITION" as const) + : ("KEEP_ORIGIN_SHARED" as const), + accountDeletionAction: currentPartitioned + ? ("PURGE_PARTITION" as const) + : ("KEEP_ORIGIN_SHARED" as const), pressureAction: "RETAIN" as const, unavailableFallback: "ONLINE_ONLY" as const, }; @@ -184,6 +193,7 @@ async function startLifecycleRuntime( currentDatabaseName: name, currentTargetVersion: targetVersion, currentBlockedTimeoutMs: blockedTimeoutMs, + currentPartitioned: partitioned, }, ); } @@ -1067,3 +1077,112 @@ test("fails a blocked v2 upgrade closed, then closes its late connection after t await blockerPage.close(); } }); + +test("05-03 deletes a partition's dataset for an authorized ACCOUNT_DELETION when its budget row is corrupt", async ({ + page, +}) => { + const name = databaseName("corrupt-governance"); + await page.goto("/"); + await startLifecycleRuntime(page, name, 1, 1_000, true); + expect(await settleLifecycleRuntime(page)).toMatchObject({ + opened: { ok: true }, + }); + expect( + await page.evaluate(async () => { + const runtime = ( + globalThis as unknown as { + __indexedDbLifecycleRuntime: { + compareAndSwap(input: unknown): Promise; + close(): void; + }; + } + ).__indexedDbLifecycleRuntime; + const written = await runtime.compareAndSwap({ + key: "personal-1", + value: { value: "mine" }, + expectedRevision: null, + idempotencyKey: "personal-create", + }); + runtime.close(); + return written; + }), + ).toMatchObject({ ok: true }); + expect( + await page.evaluate( + async (currentDatabaseName) => + await new Promise((resolve) => { + const request = indexedDB.open(currentDatabaseName, 1); + request.onerror = () => + resolve(request.error?.name ?? "UNKNOWN_ERROR"); + request.onsuccess = () => { + const database = request.result; + const transaction = database.transaction( + "governance", + "readwrite", + ); + transaction.objectStore("governance").put({ + bindingKey: "dataset-budget", + budgetVersion: 1, + usedBytes: -5, + receiptCount: 1, + }); + transaction.oncomplete = () => { + database.close(); + resolve("CORRUPTED"); + }; + transaction.onabort = () => { + database.close(); + resolve("ABORTED"); + }; + }; + }), + name, + ), + ).toBe("CORRUPTED"); + + await startLifecycleRuntime(page, name, 1, 1_000, true); + expect(await settleLifecycleRuntime(page)).toMatchObject({ + opened: { ok: false, error: { code: "POLICY_REJECTED" } }, + }); + const purged = await page.evaluate(async () => { + const runtime = ( + globalThis as unknown as { + __indexedDbLifecycleRuntime: { + enforceLifecycleBatch(input: unknown): Promise; + }; + } + ).__indexedDbLifecycleRuntime; + return await runtime.enforceLifecycleBatch({ + action: "ACCOUNT_DELETION", + maxRows: 100, + maxDurationMs: 5_000, + }); + }); + expect(purged).toMatchObject({ + ok: true, + value: { state: "COMPLETE", datasetDeleted: true }, + }); + const remaining = await page.evaluate(async (currentDatabaseName) => { + ( + globalThis as unknown as { + __indexedDbLifecycleRuntime?: { close(): void }; + } + ).__indexedDbLifecycleRuntime?.close(); + // A fresh open of a deleted database runs its upgrade from version 0. + return await new Promise((resolve, reject) => { + const request = indexedDB.open(currentDatabaseName); + let oldVersion = -1; + request.onupgradeneeded = (event) => { + oldVersion = event.oldVersion; + }; + request.onsuccess = () => { + request.result.close(); + const deletion = indexedDB.deleteDatabase(currentDatabaseName); + deletion.onsuccess = () => resolve(oldVersion); + deletion.onerror = () => reject(deletion.error); + }; + request.onerror = () => reject(request.error); + }); + }, name); + expect(remaining).toBe(0); +}); diff --git a/tests/helpers/memory-indexeddb.ts b/tests/helpers/memory-indexeddb.ts index 7eae6491..a8746881 100644 --- a/tests/helpers/memory-indexeddb.ts +++ b/tests/helpers/memory-indexeddb.ts @@ -29,8 +29,12 @@ type DatabaseState = { stores: Map; version: number; latest: FakeDatabase | null; + /** Connections handed out by a successful open, closed or not. */ + connections: FakeDatabase[]; /** Transactions that have not finished, in creation order. */ transactions: FakeTransaction[]; + /** A `deleteDatabase` waiting for the open connections to close. */ + pendingDelete: (() => void) | null; }; function storeOf(state: DatabaseState, name: string): StoreState { @@ -314,9 +318,10 @@ class FakeDatabase { close(): void { this.closed = true; + this.state.pendingDelete?.(); } - fireVersionChange(newVersion: number): void { + fireVersionChange(newVersion: number | null): void { this.onversionchange?.({ oldVersion: this.version, newVersion, @@ -887,6 +892,8 @@ export class MemoryIndexedDbFactory { readonly factory: IDBFactory = { open: (name: string, version?: number) => this.open(name, version ?? 1) as unknown as IDBOpenDBRequest, + deleteDatabase: (name: string) => + this.deleteDatabase(name) as unknown as IDBOpenDBRequest, cmp: compareKeys, } as IDBFactory; @@ -958,6 +965,51 @@ export class MemoryIndexedDbFactory { return request; } + /** + * Like a browser: open connections get `versionchange` (new version + * `null`); if any stays open the request reports `blocked` and completes + * once the last one closes. The name then holds an empty version-0 + * database, so `hasStore` answers false. + */ + deleteDatabase(name: string): MutableOpenRequest { + const request: MutableOpenRequest = { + ...createRequest(), + transaction: null, + onblocked: null, + onupgradeneeded: null, + }; + queueMicrotask(() => { + const state = this.databaseNamed(name); + const oldVersion = state.version; + const finish = () => { + if (state.connections.some((connection) => !connection.closed)) { + return; + } + state.pendingDelete = null; + state.stores = new Map(); + state.version = 0; + state.connections = []; + request.onsuccess?.({ + oldVersion, + newVersion: null, + } as unknown as Event); + }; + for (const connection of state.connections) { + if (!connection.closed) connection.fireVersionChange(null); + } + if (state.connections.some((connection) => !connection.closed)) { + state.pendingDelete = finish; + request.onblocked?.({ + oldVersion, + newVersion: null, + } as IDBVersionChangeEvent); + return; + } + finish(); + }); + return request; + } + blockNextOpen(): void { this.shouldBlockNextOpen = true; } @@ -1056,7 +1108,9 @@ export class MemoryIndexedDbFactory { stores: new Map(), version: 0, latest: null, + connections: [], transactions: [], + pendingDelete: null, }; this.databases.set(name, state); } @@ -1101,6 +1155,7 @@ export class MemoryIndexedDbFactory { state.version = targetVersion; } database.version = state.version; + state.connections.push(database); request.result = database as unknown as IDBDatabase; request.onsuccess?.(new Event("success")); } diff --git a/tests/unit/indexeddb-runtime.test.ts b/tests/unit/indexeddb-runtime.test.ts index dbe64ac8..6103db7e 100644 --- a/tests/unit/indexeddb-runtime.test.ts +++ b/tests/unit/indexeddb-runtime.test.ts @@ -1453,3 +1453,156 @@ describe("IndexedDB runtime", () => { expect(hardMemory.readRaw("records", "x")).toBeUndefined(); }); }); + +describe("05-03 a LOGOUT or ACCOUNT_DELETION purge survives corrupt governance", () => { + const partitionScope = { + ...TEST_SCOPE, + accountScope: "OPAQUE_PARTITION" as const, + }; + const otherScope = { + ...partitionScope, + partitionToken: "partitiontoken_002", + }; + const partitionPolicy = { + ...TEST_STORAGE_POLICY, + accountScope: "OPAQUE_PARTITION" as const, + logoutAction: "PURGE_PARTITION" as const, + accountDeletionAction: "PURGE_PARTITION" as const, + }; + const batch = { maxRows: 100, maxDurationMs: 1_000 } as const; + + /** + * Another partition and this one each hold a record in one factory; this + * partition's budget row is then corrupted, so a fresh runtime cannot pass + * admission (E1). + */ + async function corruptedPartition( + authorizeLifecycle: IndexedDbRuntimeDependencies< + Item, + Item, + Query + >["authorizeLifecycle"] = () => ({ + authorized: true, + proofToken: "authorityproof_001", + }), + ) { + const memory = new MemoryIndexedDbFactory(); + const other = createIndexedDbRuntime( + dependencies(memory, { + scope: otherScope, + storagePolicy: partitionPolicy, + }), + ); + expect(await other.open()).toMatchObject({ ok: true }); + await other.compareAndSwap({ + key: "kept", + value: { label: "other", rank: 1 }, + expectedRevision: null, + idempotencyKey: "kept-create", + }); + const writer = createIndexedDbRuntime( + dependencies(memory, { + scope: partitionScope, + storagePolicy: partitionPolicy, + }), + ); + expect(await writer.open()).toMatchObject({ ok: true }); + await writer.compareAndSwap({ + key: "personal-1", + value: { label: "mine", rank: 1 }, + expectedRevision: null, + idempotencyKey: "personal-create", + }); + writer.close(); + memory.seed("governance", { + bindingKey: "dataset-budget", + budgetVersion: 1, + usedBytes: -5, + receiptCount: 1, + }); + const deleteDatabase = vi.spyOn(memory.factory, "deleteDatabase"); + const fresh = createIndexedDbRuntime( + dependencies(memory, { + scope: partitionScope, + storagePolicy: partitionPolicy, + authorizeLifecycle, + }), + ); + expect(await fresh.open()).toMatchObject({ + ok: false, + error: { code: "POLICY_REJECTED" }, + }); + return { memory, other, fresh, deleteDatabase }; + } + + it.each(["LOGOUT", "ACCOUNT_DELETION"] as const)( + "deletes the partition's whole dataset for an authorized %s and leaves other partitions alone", + async (action) => { + const { memory, other, fresh, deleteDatabase } = + await corruptedPartition(); + + expect( + await fresh.enforceLifecycleBatch({ action, ...batch }), + ).toMatchObject({ + ok: true, + value: { state: "COMPLETE", datasetDeleted: true }, + }); + expect(deleteDatabase).toHaveBeenCalledTimes(1); + expect(deleteDatabase).toHaveBeenCalledWith( + indexedDbPhysicalDatabaseName(partitionScope), + ); + expect(memory.hasStore("records")).toBe(false); + expect(await other.read("kept")).toMatchObject({ + ok: true, + value: { value: { label: "other", rank: 1 } }, + }); + }, + ); + + it("keeps the dataset when the authority does not approve", async () => { + const { memory, fresh, deleteDatabase } = await corruptedPartition( + () => ({ authorized: false }), + ); + + expect( + await fresh.enforceLifecycleBatch({ action: "ACCOUNT_DELETION", ...batch }), + ).toMatchObject({ ok: false, error: { code: "POLICY_REJECTED" } }); + expect(deleteDatabase).not.toHaveBeenCalled(); + expect(memory.readRaw("records", "personal-1")).toBeDefined(); + }); + + it("purges row by row, never the database, when governance is healthy", async () => { + const memory = new MemoryIndexedDbFactory(); + const deleteDatabase = vi.spyOn(memory.factory, "deleteDatabase"); + const runtime = createIndexedDbRuntime( + dependencies(memory, { + scope: partitionScope, + storagePolicy: partitionPolicy, + }), + ); + expect(await runtime.open()).toMatchObject({ ok: true }); + await runtime.compareAndSwap({ + key: "personal-1", + value: { label: "mine", rank: 1 }, + expectedRevision: null, + idempotencyKey: "personal-create", + }); + runtime.close(); + const fresh = createIndexedDbRuntime( + dependencies(memory, { + scope: partitionScope, + storagePolicy: partitionPolicy, + }), + ); + + const purged = await fresh.enforceLifecycleBatch({ + action: "LOGOUT", + ...batch, + }); + expect(purged).toMatchObject({ ok: true, value: { state: "COMPLETE" } }); + expect(purged.ok && purged.value).not.toHaveProperty("datasetDeleted"); + expect(deleteDatabase).not.toHaveBeenCalled(); + expect(memory.hasStore("records")).toBe(true); + expect(memory.readRaw("records", "personal-1")).toBeUndefined(); + }); +});