Skip to content

Commit 99a960e

Browse files
fix: refreshSecrets takes over a key from a pending write, so a failed write's read-back cannot hide the loaded secret
1 parent 763d346 commit 99a960e

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,19 @@ export class ContextProxy {
478478
})
479479
}
480480

481+
/**
482+
* Loads one secret into the cache. A write of the key that started while the secret was being read has its own
483+
* outcome, so the (older) value that was read is dropped. Otherwise the load takes over the key from a write that is
484+
* still pending, so that a failure of that write cannot replace what was just loaded.
485+
*/
486+
private async refreshSecret(key: SecretStateKey) {
487+
const owner = this.secretWriteOwner.get(key)
488+
const value = await this.originalContext.secrets.get(key)
489+
if (this.secretWriteOwner.get(key) !== owner) return
490+
this.secretWriteOwner.delete(key)
491+
this.secretCache[key] = value
492+
}
493+
481494
/**
482495
* Refresh secrets from storage and update cache
483496
* This is useful when you need to ensure the cache has the latest values
@@ -486,7 +499,7 @@ export class ContextProxy {
486499
const promises = [
487500
...SECRET_STATE_KEYS.map(async (key) => {
488501
try {
489-
this.secretCache[key] = await this.originalContext.secrets.get(key)
502+
await this.refreshSecret(key)
490503
} catch (error) {
491504
logger.error(
492505
`Error refreshing secret ${key}: ${error instanceof Error ? error.message : String(error)}`,
@@ -495,7 +508,7 @@ export class ContextProxy {
495508
}),
496509
...GLOBAL_SECRET_KEYS.map(async (key) => {
497510
try {
498-
this.secretCache[key] = await this.originalContext.secrets.get(key)
511+
await this.refreshSecret(key)
499512
} catch (error) {
500513
logger.error(
501514
`Error refreshing global secret ${key}: ${error instanceof Error ? error.message : String(error)}`,

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,44 @@ describe("ContextProxy", () => {
444444
expect(proxy["stateWriteOwner"].size).toBe(1)
445445
})
446446

447+
it("does not let a failed write whose read-back times out hide a secret that refreshSecrets loaded", async () => {
448+
vi.useFakeTimers()
449+
try {
450+
mockSecrets.store.mockRejectedValue(new Error("keychain locked"))
451+
await proxy.initialize()
452+
let readBackSettles: (value: string) => void = () => {}
453+
mockSecrets.get
454+
.mockImplementationOnce(() => new Promise((resolve) => (readBackSettles = resolve))) // the read-back, queued first
455+
.mockResolvedValue("loaded-key") // what the refresh reads
456+
457+
const failed = expect(proxy.storeSecret("openAiApiKey", "new-key")).rejects.toThrow("keychain locked")
458+
await vi.advanceTimersByTimeAsync(0) // the store has failed and the read-back has started
459+
await proxy.refreshSecrets()
460+
expect(proxy.getSecret("openAiApiKey")).toBe("loaded-key")
461+
await vi.advanceTimersByTimeAsync(5_000) // the read-back times out
462+
await failed
463+
464+
expect(proxy.getSecret("openAiApiKey")).toBe("loaded-key")
465+
expect(proxy["secretWriteOwner"].size).toBe(0)
466+
readBackSettles("late")
467+
} finally {
468+
vi.useRealTimers()
469+
}
470+
})
471+
472+
it("does not replace a secret written while refreshSecrets was reading it", async () => {
473+
await proxy.initialize()
474+
let finishRead: (value: string) => void = () => {}
475+
mockSecrets.get.mockImplementationOnce(() => new Promise((resolve) => (finishRead = resolve)))
476+
477+
const refreshing = proxy.refreshSecrets()
478+
await proxy.storeSecret("openAiApiKey", "written-key") // starts and succeeds during the read
479+
finishRead("older-key")
480+
await refreshing
481+
482+
expect(proxy.getSecret("openAiApiKey")).toBe("written-key")
483+
})
484+
447485
it("still reports the failure when the secret cannot be read back either", async () => {
448486
mockSecrets.store.mockRejectedValue(new Error("keychain locked"))
449487
await proxy.initialize()

0 commit comments

Comments
 (0)