Skip to content

Commit 515cc02

Browse files
fix(config): refuse secret writes that begin inside a reset instead of letting them survive it
resetAllState waits for the secret writes that are in flight when it starts, clears both caches, drops both write claims and then deletes the keys. A storeSecret that begins after that in-flight snapshot is invisible to all of it: it puts the value into the secret cache on entry, the reset clears that cache underneath it, and the write's durable commit lands after the delete pass. The success path never touches the cache again, so the value stays out of the cache while it stays in durable storage, and the next initialize - what a restarted provider does - reads it back out and surfaces to the user a secret they had just reset away. Secret writes are therefore refused while a reset is in progress, with a SettingsWriteError naming the key, and the flag is cleared only after the reset has re-initialized so the whole window is covered. Refusing was chosen over serializing the reset: a serialized reset lets the write apply and then erases it, which discards silently a save the user just asked for, whereas a refusal leaves durable storage empty, which is what a reset promises, and hands the caller an error on a path that already reports which keys it could not store. Serialization was available and was rejected for that reason rather than omitted. Case: erases a secret whose write begins after the reset has snapshotted the pending writes. It parks the reset inside its own delete pass, starts the write in that window and reads three things: the durable double behind the secrets, this instance's cache, and the cache after a fresh initialize - the third is what makes it a security finding rather than a cache artefact. Case alone against f3ca33b: Test Files 1 failed | 9 passed (10), Tests 1 failed | 315 passed (316), with 'expected late-secret to be undefined' on the durable read. With the gate: 10 test files and 316 tests, 0 failed. Control, the gate condition disabled: 1 failed | 315 passed again, so the case measures the gate. Review row: database id 4478349061 (Major, Security & Privacy).
1 parent f3ca33b commit 515cc02

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ export class ContextProxy {
407407
* pairing a reset cannot tell a settled-but-orphaned write from a live one.
408408
*/
409409
private readonly pendingSecretWrites = new Map<string, Set<{ settle: Promise<void> }>>()
410+
// A reset is in progress: the caches are meaningless mid-reset, and a secret write begun
411+
// inside it would commit after the delete pass and survive the erase.
412+
private resetting = false
410413
/** How many writes of each secret were started. Unlike the owners, it is not cleared when a write is over. */
411414
private readonly secretWriteCount = new Map<string, number>()
412415

@@ -453,6 +456,13 @@ export class ContextProxy {
453456
}
454457

455458
storeSecret(key: SecretStateKey, value?: string) {
459+
if (this.resetting) {
460+
// Refused rather than deferred: a deferred secret write would re-store exactly the
461+
// value the user asked to erase, and a silently dropped one would lose the save.
462+
return Promise.reject(
463+
new SettingsWriteError([], [key], new Error("secret writes are refused while state is being reset")),
464+
)
465+
}
456466
// Update cache.
457467
const owner = {}
458468
this.secretWriteOwner.set(key, owner)
@@ -738,6 +748,7 @@ export class ContextProxy {
738748
* @returns A promise that resolves when all reset operations are complete
739749
*/
740750
public async resetAllState() {
751+
this.resetting = true
741752
// A secret write that is in flight at this moment has already committed nothing, but it will
742753
// commit as soon as it settles, and the deletes below cannot un-commit a write that lands
743754
// after them. Wait for the pending writes first, then end the claims, then delete, so the
@@ -761,6 +772,7 @@ export class ContextProxy {
761772
])
762773

763774
await this.initialize()
775+
this.resetting = false
764776
}
765777

766778
private static _instance: ContextProxy | null = null

‎src/core/config/__tests__/ContextProxy.spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,56 @@ describe("ContextProxy", () => {
227227
expect(await mockSecrets.get("apiKey")).toBe("newer-secret")
228228
})
229229

230+
it("erases a secret whose write begins after the reset has snapshotted the pending writes", async () => {
231+
const durable = new Map<string, string | undefined>()
232+
let deleteCalls = 0
233+
let settleFirstDelete: () => void = () => {}
234+
let markDeletePassStarted: () => void = () => {}
235+
const deletePassStarted = new Promise<void>((resolve) => {
236+
markDeletePassStarted = resolve
237+
})
238+
mockSecrets.get.mockImplementation(async (key: string) => durable.get(key))
239+
mockSecrets.store.mockImplementation(async (key: string, value: string) => {
240+
durable.set(key, value)
241+
})
242+
// Park the reset inside its own delete pass so a write can begin in the window the in-flight
243+
// snapshot cannot cover. Only the first delete is held open; the rest settle normally, so
244+
// releasing the park lets the reset finish.
245+
mockSecrets.delete.mockImplementation((key: string) => {
246+
deleteCalls++
247+
durable.delete(key)
248+
if (deleteCalls === 1) {
249+
return new Promise<undefined>((resolve) => {
250+
markDeletePassStarted()
251+
settleFirstDelete = () => resolve(undefined)
252+
})
253+
}
254+
return Promise.resolve(undefined)
255+
})
256+
257+
const reset = proxy.resetAllState()
258+
await deletePassStarted
259+
260+
// The reset has taken its in-flight snapshot, cleared both caches and dropped both write
261+
// claims, and is standing inside the delete pass. This write is invisible to all of it.
262+
const late = proxy.storeSecret("apiKey", "late-secret")
263+
settleFirstDelete()
264+
await reset
265+
await late.catch(() => undefined)
266+
267+
// All three reads matter. Durable storage is what a reset is supposed to empty, the cache is
268+
// what this provider instance shows the user, and re-initializing is what a restarted
269+
// provider does - which is how a secret that survived the reset reaches the user after the
270+
// fact, so the middle read alone would understate the finding.
271+
expect(await mockSecrets.get("apiKey")).toBeUndefined()
272+
expect(proxy.getSecret("apiKey")).toBeUndefined()
273+
await proxy.initialize()
274+
expect(proxy.getSecret("apiKey")).toBeUndefined()
275+
// The caller is told: a write the reset cannot honour is refused rather than dropped, so
276+
// the save surfaces as a failure instead of vanishing.
277+
await expect(late).rejects.toThrow()
278+
})
279+
230280
it("should bypass cache for pass-through state keys", async () => {
231281
// Setup mock return value
232282
mockGlobalState.get.mockReturnValue("pass-through-value")

0 commit comments

Comments
 (0)