diff --git a/src/adapters/storage/indexeddb/indexeddb-runtime.ts b/src/adapters/storage/indexeddb/indexeddb-runtime.ts index 1a1e3c07..60f6ca9c 100644 --- a/src/adapters/storage/indexeddb/indexeddb-runtime.ts +++ b/src/adapters/storage/indexeddb/indexeddb-runtime.ts @@ -2366,11 +2366,21 @@ export function createIndexedDbRuntime( ); } - function purgePartitionRecords( + /** + * R-5.4. A partition purge (session end, logout, account deletion) exists to + * remove everything, so it deletes by key and never stops on a row it cannot + * read: failing closed here left personal data on the device for good. Rows + * that fail validation are counted and reported as one CORRUPT_DATA + * observation per batch. Per-row budget arithmetic is dropped too; the + * budget is reset to zero in the transaction that finds every store empty. + * Between batches it can only read high, which blocks writes, never admits. + */ + async function purgePartitionRecords( input: IndexedDbLifecycleBatchInput, deadline: number, ): Promise> { - return runTransaction( + let corruptRows = 0; + const result = await runTransaction( [ dependencies.recordStore, dependencies.governanceStore, @@ -2486,47 +2496,30 @@ export function createIndexedDbRuntime( walkPurge( receipts.openCursor(), ({ cursor, resume }) => { - if (!isStoredReceipt(cursor.value)) { - context.fail(corruptFailure("INDEXEDDB_WRITE")); - // `fail` aborts, so the pump has nowhere to go. - return { kind: "SUSPEND" }; - } scannedRows += 1; + if (!isStoredReceipt(cursor.value)) corruptRows += 1; const deletion = receipts.delete(cursor.primaryKey); deletion.onerror = () => context.requestFailed(deletion.error); deletion.onsuccess = () => { - const budgetRequest = governance.get( - INDEXEDDB_DATASET_BUDGET_KEY, - ); - budgetRequest.onerror = () => - context.requestFailed(budgetRequest.error); - budgetRequest.onsuccess = () => { - if ( - !isStoredDatasetBudget(budgetRequest.result) || - budgetRequest.result.receiptCount < 1 - ) { - context.fail(corruptFailure("INDEXEDDB_WRITE")); - return; - } - const storedBudgetRequest = governance.put({ - ...budgetRequest.result, - receiptCount: - budgetRequest.result.receiptCount - 1, - } satisfies StoredDatasetBudget); - storedBudgetRequest.onerror = () => - context.requestFailed( - storedBudgetRequest.error, - ); - storedBudgetRequest.onsuccess = () => { - deletedRows += 1; - resume({ kind: "CONTINUE" }); - }; - }; + deletedRows += 1; + resume({ kind: "CONTINUE" }); }; return { kind: "SUSPEND" }; }, - () => succeed("COMPLETE", false), + () => { + // Every store above is empty in this same transaction, so zero + // is exact, and a budget row that had drifted is healed. + const budgetReset = governance.put({ + bindingKey: INDEXEDDB_DATASET_BUDGET_KEY, + budgetVersion: 1, + usedBytes: 0, + receiptCount: 0, + } satisfies StoredDatasetBudget); + budgetReset.onerror = () => + context.requestFailed(budgetReset.error); + budgetReset.onsuccess = () => succeed("COMPLETE", false); + }, ); }; @@ -2534,46 +2527,21 @@ export function createIndexedDbRuntime( walkPurge( retention.openCursor(), ({ cursor, resume }) => { + scannedRows += 1; if ( !isStoredRetentionRecord( cursor.value, String(cursor.primaryKey), ) ) { - context.fail(corruptFailure("INDEXEDDB_WRITE")); - return { kind: "SUSPEND" }; + corruptRows += 1; } - scannedRows += 1; - const measuredBytes = cursor.value.measuredBytes; - const budgetRequest = governance.get( - INDEXEDDB_DATASET_BUDGET_KEY, - ); - budgetRequest.onerror = () => - context.requestFailed(budgetRequest.error); - budgetRequest.onsuccess = () => { - if ( - !isStoredDatasetBudget(budgetRequest.result) || - budgetRequest.result.usedBytes < measuredBytes - ) { - context.fail(corruptFailure("INDEXEDDB_WRITE")); - return; - } - const deletion = retention.delete(cursor.primaryKey); - deletion.onerror = () => - context.requestFailed(deletion.error); - deletion.onsuccess = () => { - const budgetWrite = governance.put({ - ...budgetRequest.result, - usedBytes: - budgetRequest.result.usedBytes - measuredBytes, - } satisfies StoredDatasetBudget); - budgetWrite.onerror = () => - context.requestFailed(budgetWrite.error); - budgetWrite.onsuccess = () => { - deletedRows += 1; - resume({ kind: "CONTINUE" }); - }; - }; + const deletion = retention.delete(cursor.primaryKey); + deletion.onerror = () => + context.requestFailed(deletion.error); + deletion.onsuccess = () => { + deletedRows += 1; + resume({ kind: "CONTINUE" }); }; return { kind: "SUSPEND" }; }, @@ -2584,66 +2552,32 @@ export function createIndexedDbRuntime( walkPurge( records.openCursor(), ({ cursor, resume }) => { - if (!isStoredRecord(cursor.value)) { - context.fail(corruptFailure("INDEXEDDB_WRITE")); - return { kind: "SUSPEND" }; - } scannedRows += 1; - const key = cursor.value.key; + const key = cursor.primaryKey; + const recordCorrupt = !isStoredRecord(cursor.value); const sidecarRequest = retention.get(key); sidecarRequest.onerror = () => context.requestFailed(sidecarRequest.error); sidecarRequest.onsuccess = () => { if ( + recordCorrupt || !isStoredRetentionRecord( sidecarRequest.result, - key, + String(key), ) ) { - context.fail(corruptFailure("INDEXEDDB_WRITE")); - return; + corruptRows += 1; } - const measuredBytes = - sidecarRequest.result.measuredBytes; - const budgetRequest = governance.get( - INDEXEDDB_DATASET_BUDGET_KEY, - ); - budgetRequest.onerror = () => - context.requestFailed(budgetRequest.error); - budgetRequest.onsuccess = () => { - if ( - !isStoredDatasetBudget(budgetRequest.result) || - budgetRequest.result.usedBytes < measuredBytes - ) { - context.fail(corruptFailure("INDEXEDDB_WRITE")); - return; - } - const recordDelete = records.delete(key); - recordDelete.onerror = () => - context.requestFailed(recordDelete.error); - recordDelete.onsuccess = () => { - const sidecarDelete = retention.delete(key); - sidecarDelete.onerror = () => - context.requestFailed(sidecarDelete.error); - sidecarDelete.onsuccess = () => { - const storedBudgetRequest = governance.put({ - bindingKey: INDEXEDDB_DATASET_BUDGET_KEY, - budgetVersion: 1, - usedBytes: - budgetRequest.result.usedBytes - - measuredBytes, - receiptCount: - budgetRequest.result.receiptCount, - } satisfies StoredDatasetBudget); - storedBudgetRequest.onerror = () => - context.requestFailed( - storedBudgetRequest.error, - ); - storedBudgetRequest.onsuccess = () => { - deletedRows += 1; - resume({ kind: "CONTINUE" }); - }; - }; + const recordDelete = records.delete(key); + recordDelete.onerror = () => + context.requestFailed(recordDelete.error); + recordDelete.onsuccess = () => { + const sidecarDelete = retention.delete(key); + sidecarDelete.onerror = () => + context.requestFailed(sidecarDelete.error); + sidecarDelete.onsuccess = () => { + deletedRows += 1; + resume({ kind: "CONTINUE" }); }; }; }; @@ -2653,6 +2587,16 @@ export function createIndexedDbRuntime( ); }, ); + if (corruptRows > 0) { + observe({ + operation: "INDEXEDDB_WRITE", + outcome: "FAILED", + schemaVersion: dependencies.schemaVersion, + countBucket: countBucket(corruptRows), + failureCode: "CORRUPT_DATA", + }); + } + return result; } async function enforceLifecycleBatch( diff --git a/tests/unit/indexeddb-runtime.test.ts b/tests/unit/indexeddb-runtime.test.ts index 792316fe..6396a37d 100644 --- a/tests/unit/indexeddb-runtime.test.ts +++ b/tests/unit/indexeddb-runtime.test.ts @@ -1240,6 +1240,127 @@ describe("IndexedDB runtime", () => { ).toMatchObject({ usedBytes: 0, receiptCount: 0 }); }); + it.each(["LOGOUT", "ACCOUNT_DELETION"] as const)( + "R-5.4 completes a %s purge past corrupt rows, reports them, and leaves other partitions alone", + async (action) => { + 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, + }; + // One factory, routed by the physical database name the runtime derives + // from its partition, the way the browser keeps databases apart. + const purged = new MemoryIndexedDbFactory(); + const other = new MemoryIndexedDbFactory(); + const otherName = indexedDbPhysicalDatabaseName(otherScope); + const factory = { + open: (name: string, version?: number) => + (name === otherName ? other : purged).factory.open(name, version), + cmp: purged.factory.cmp, + } as IDBFactory; + const observations: IndexedDbObservation[] = []; + const runtime = createIndexedDbRuntime( + dependencies(purged, { + scope: partitionScope, + storagePolicy: partitionPolicy, + factory, + nowMonotonicMilliseconds: () => 0, + observe: (event) => observations.push(event), + }), + ); + const otherRuntime = createIndexedDbRuntime( + dependencies(other, { + scope: otherScope, + storagePolicy: partitionPolicy, + factory, + nowMonotonicMilliseconds: () => 0, + }), + ); + expect(await runtime.open()).toMatchObject({ ok: true }); + expect(await otherRuntime.open()).toMatchObject({ ok: true }); + for (const key of ["healthy", "bad-record", "bad-sidecar"]) { + expect( + await runtime.compareAndSwap({ + key, + value: { label: key, rank: 1 }, + expectedRevision: null, + idempotencyKey: `${key}-create`, + }), + ).toMatchObject({ ok: true }); + } + expect( + await otherRuntime.compareAndSwap({ + key: "other-partition", + value: { label: "kept", rank: 1 }, + expectedRevision: null, + idempotencyKey: "other-create", + }), + ).toMatchObject({ ok: true }); + + // Four corrupt rows and a budget that no longer matches the data. + purged.seed("records", { key: "bad-record", codecVersion: 0 }); + purged.seed("retention", { recordKey: "bad-sidecar", measuredBytes: -1 }); + purged.seed("retention", { recordKey: "orphan-sidecar" }); + purged.seed("receipts", { idempotencyKey: "bad-receipt" }); + purged.seed("governance", { + bindingKey: "dataset-budget", + budgetVersion: 1, + usedBytes: 1, + receiptCount: 0, + }); + + const receipts = []; + for (let batch = 0; batch < 5; batch += 1) { + const receipt = await runtime.enforceLifecycleBatch({ + action, + maxRows: 2, + maxDurationMs: 1_000, + }); + receipts.push(receipt); + if (!receipt.ok || receipt.value.state === "COMPLETE") break; + } + + expect(receipts.at(-1)).toMatchObject({ + ok: true, + value: { state: "COMPLETE" }, + }); + for (const key of ["healthy", "bad-record", "bad-sidecar"]) { + expect(purged.readRaw("records", key)).toBeUndefined(); + expect(purged.readRaw("retention", key)).toBeUndefined(); + expect(purged.readRaw("receipts", `${key}-create`)).toBeUndefined(); + } + expect(purged.readRaw("retention", "orphan-sidecar")).toBeUndefined(); + expect(purged.readRaw("receipts", "bad-receipt")).toBeUndefined(); + expect(purged.readRaw("governance", "dataset-budget")).toEqual({ + bindingKey: "dataset-budget", + budgetVersion: 1, + usedBytes: 0, + receiptCount: 0, + }); + // Corruption is reported, never a reason to stop deleting. + const corrupt = observations.filter( + (event) => event.failureCode === "CORRUPT_DATA", + ); + expect(corrupt.length).toBeGreaterThan(0); + expect(observations.at(-1)).toMatchObject({ outcome: "SUCCESS" }); + + expect(other.readRaw("records", "other-partition")).toBeDefined(); + expect(await otherRuntime.read("other-partition")).toMatchObject({ + ok: true, + value: { value: { label: "kept" } }, + }); + }, + ); + it("rejects invalid measurements, byte overage, and receipt-count overage atomically", async () => { const invalidMemory = new MemoryIndexedDbFactory(); const invalidBase = dependencies(invalidMemory);