a6724549c6f2
fix persistSlotA epoch race with lock and add regression coverage (PB-26-403, v5.0.104)
- time
- 2026-09-05 03:51 UTC · 2 months ago
- machine
- koh2
- agent
- claude-sonnet-4-6
- session
- persist slot epoch guards
- parent
- a860affc314e
↓ peach-browser-a6724549.tar.gzatlas at this save
files changed5 files
manifest.firefox.json+1 −1manifest.json+1 −1package.json+1 −1src/background/vault.test.ts+36 −1src/background/vault.ts+19 −2manifest.firefox.json+1 −1
@@ -1,7 +1,7 @@11 {22 "manifest_version": 3,33 "name": "Peach Password Manager",4- "version": "5.0.103",4+ "version": "5.0.104",55 "description": "Zero-knowledge password manager",66 "browser_specific_settings": {77 "gecko": {manifest.json+1 −1
@@ -2,7 +2,7 @@22 "manifest_version": 3,33 "minimum_chrome_version": "116",44 "name": "Peach Password Manager",5- "version": "5.0.103",5+ "version": "5.0.104",66 "description": "Zero-knowledge password manager",77 "action": {88 "default_popup": "src/popup/index.html",package.json+1 −1
@@ -1,6 +1,6 @@11 {22 "name": "peach",3- "version": "5.0.103",3+ "version": "5.0.104",44 "description": "Zero-knowledge password manager",55 "license": "GPL-3.0-only",66 "type": "module",src/background/vault.test.ts+36 −1
@@ -5,6 +5,12 @@55 66 type StorageShape = Record<string, unknown>;77 8+function deferred(): { promise: Promise<void>; resolve: () => void } {9+ let resolve!: () => void;10+ const promise = new Promise<void>((done) => { resolve = done; });11+ return { promise, resolve };12+}13+814 describe("background vault", () => {915 let storage: StorageShape;1016 let sessionStorage: StorageShape;@@ -389,10 +395,39 @@389395 expect(Number(storage.peach_last_modified_at)).toBeGreaterThanOrEqual(before);390396 });391397 398+ it("rejects a slot A persist that races with lock", async () => {399+ const vault = await import("./vault");400+ await vault.createVault("persist-epoch-pass");401+ const slotBefore = JSON.stringify(storage.peach_vault_a);402+ const readStarted = deferred();403+ const releaseRead = deferred();404+ const localGet = chrome.storage.local.get as unknown as ReturnType<typeof vi.fn>;405+ localGet.mockImplementation(async (key?: string | string[]) => {406+ if (key === "peach_vault_a") {407+ readStarted.resolve();408+ await releaseRead.promise;409+ }410+ if (!key) return storage;411+ const keys = Array.isArray(key) ? key : [key];412+ return keys.reduce<StorageShape>((result, entry) => {413+ result[entry] = storage[entry];414+ return result;415+ }, {});416+ });417+418+ const saving = vault.addEntry({ title: "stale", username: "", password: "pw", url: "" });419+ await readStarted.promise;420+ await vault.lock();421+ releaseRead.resolve();422+423+ await expect(saving).rejects.toThrow("vault_locked");424+ expect(JSON.stringify(storage.peach_vault_a)).toBe(slotBefore);425+ expect(vault.isUnlocked()).toBe(false);426+ });427+392428 it("stamps peach_last_modified_at on entry mutations through persistEntries", async () => {393429 const vault = await import("./vault");394430 await vault.createVault("stamp-entry-pass");395-396431 let previous = Number(storage.peach_last_modified_at);397432 await new Promise((resolve) => setTimeout(resolve, 1));398433 const entry = await vault.addEntry({src/background/vault.ts+19 −2
@@ -2671,16 +2671,23 @@26712671 reason: VaultSnapshot["reason"] = "local_save",26722672 ): Promise<StoredVault> {26732673 if (!derivedKey) throw new Error("vault_locked");2674+ const epoch = vaultEpoch;2675+ const throwIfStale = () => {2676+ if (epoch !== vaultEpoch || !derivedKey) throw new Error("vault_locked");2677+ };26742678 26752679 const slotA = await loadSlot(SLOT_A);2680+ throwIfStale();26762681 if (!slotA) throw new Error("missing slot A");26772682 26782683 // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures26792684 // are swallowed by takeVaultSnapshot — history must never block a save.26802685 await takeVaultSnapshot(reason);2686+ throwIfStale();26812687 26822688 const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);26832689 const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");2690+ throwIfStale();26842691 26852692 // Preserve existing recovery escrow fields.26862693 const nextSlotA: StoredVault = {@@ -2690,7 +2697,6 @@26902697 tag,26912698 // recovery_salt and recovery_wrapped_root_key are already set from loadSlot26922699 };2693- await writeSlot(SLOT_A, nextSlotA);26942700 26952701 // Slot A (the real vault) is now committed. Wrap every later write so a26962702 // mid-persist failure (e.g. a storage error after slot A landed) restores the@@ -2698,12 +2704,20 @@26982704 // wrote slot A leaves disk ahead of the in-memory rollback in addEntry/26992705 // updateEntry/deleteEntry — the entry stays silently on disk and resurfaces27002706 // as a duplicate on the next unlock (observed under storage-quota pressure).2707+ let slotACommitted = false;27012708 try {2709+ throwIfStale();2710+ await writeSlot(SLOT_A, nextSlotA);2711+ slotACommitted = true;2712+ throwIfStale();2713+27022714 // When duress is not configured, keep slot B size-matched with slot A by27032715 // regenerating its noise. When duress IS configured, leave slot B alone —27042716 // we don't have the decoy key to re-encrypt it. This is a known trade-off.27052717 if (!duressConfigured) {2718+ throwIfStale();27062719 await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2720+ throwIfStale();27072721 }27082722 // Only advance the vault version for locally-originated writes. Entries27092723 // applied from a sync peer ("sync_received") must NOT bump the counter —@@ -2711,12 +2725,15 @@27112725 // triggers a redundant outbound sync, defeating the no-op optimisation27122726 // and risking a ping-pong between two devices (SEV-2-C).27132727 if (reason !== "sync_received") {2728+ throwIfStale();27142729 await stampLocalVaultMutation();2730+ throwIfStale();27152731 }27162732 } catch (err) {27172733 // Best-effort restore — the prior slot A is smaller-or-equal, so this write27182734 // fits even when the failure was a quota overflow.2719- await writeSlot(SLOT_A, slotA).catch(() => {});2735+ if (slotACommitted) await writeSlot(SLOT_A, slotA).catch(() => {});2736+ if (epoch !== vaultEpoch || !derivedKey) throw new Error("vault_locked");27202737 throw err;27212738 }27222739