diff --git a/pm-stack/aggregator/src/push.ts b/pm-stack/aggregator/src/push.ts index 97a2050ab8..a3f199e89e 100644 --- a/pm-stack/aggregator/src/push.ts +++ b/pm-stack/aggregator/src/push.ts @@ -25,17 +25,30 @@ export type PushOutcome = { ok: true; status: number } | { ok: false; gone: bool let configured = false; let configurePromise: Promise | null = null; -/** Lazily fetch the VAPID private key by name and configure web-push. */ +/** Lazily fetch the VAPID private key by name and configure web-push. + * + * Only success is memoized (#3729): when an attempt settles false — + * the secret resolving to null on a transient bws problem, or the + * configure throwing — the pending promise clears itself, so the next + * call re-resolves and can succeed without a process restart (the + * secrets layer already heals its own null cache after 5 minutes; an + * unbounded latch here defeated that). Concurrent callers still share + * the one in-flight attempt. Same retry-after-failure shape as + * createAlertsStoreGetter in alerts.ts. */ export function ensurePushConfigured(): Promise { if (configured) return Promise.resolve(true); if (!configurePromise) { - configurePromise = (async () => { + const pending = (async () => { const priv = await resolveBwsSecret(VAPID_PRIVATE_KEY_SECRET); if (!priv) return false; webpush.setVapidDetails(VAPID_SUBJECT, VAPID_PUBLIC_KEY, priv); configured = true; return true; })().catch(() => false); + configurePromise = pending; + void pending.then((ok) => { + if (!ok && configurePromise === pending) configurePromise = null; + }); } return configurePromise; } diff --git a/pm-stack/aggregator/test/push-config-3729.test.ts b/pm-stack/aggregator/test/push-config-3729.test.ts new file mode 100644 index 0000000000..dc7a1617bb --- /dev/null +++ b/pm-stack/aggregator/test/push-config-3729.test.ts @@ -0,0 +1,97 @@ +// #3729 — a failed VAPID configure attempt must not latch for the +// process lifetime. Oracle: the stubbed secrets layer's call count and +// the stubbed web-push's recorded calls — recovery is proven by the +// secret being RE-RESOLVED after a failure, not by the module's own +// return values alone. The three tests are one narrative in order: +// push.ts module state (configured / configurePromise) is the thing +// under test, so they share it deliberately. +import { describe, expect, mock, test } from "bun:test"; + +let resolveCalls = 0; +let resolveImpl: () => Promise = async () => null; +const forgotten: string[] = []; +mock.module("../src/secrets.ts", () => ({ + resolveBwsSecret: async (_name: string) => { + resolveCalls += 1; + return resolveImpl(); + }, + forgetBwsSecret: (name: string) => { + forgotten.push(name); + }, +})); + +const vapidCalls: unknown[][] = []; +let sendImpl: () => Promise<{ statusCode: number }> = async () => ({ statusCode: 201 }); +const webpushStub = { + setVapidDetails: (...args: unknown[]) => { + vapidCalls.push(args); + }, + sendNotification: async () => sendImpl(), +}; +mock.module("web-push", () => ({ default: webpushStub, ...webpushStub })); + +const { ensurePushConfigured, sendPush } = await import("../src/push.ts"); + +const target = { endpoint: "https://push.example/sub-1", p256dh: "k", auth: "a" }; + +describe("ensurePushConfigured failure latch (#3729)", () => { + test("concurrent callers share one in-flight attempt; a failed attempt is retried, not latched", async () => { + let resolveFirst!: (value: string | null) => void; + const firstResult = new Promise((resolve) => { + resolveFirst = resolve; + }); + resolveImpl = () => (resolveCalls === 1 ? firstResult : Promise.resolve("vapid-private-key-1")); + + const a = ensurePushConfigured(); + const b = ensurePushConfigured(); + const c = ensurePushConfigured(); + expect(resolveCalls).toBe(1); // one in-flight attempt, shared + resolveFirst(null); // transient bws failure + expect(await Promise.all([a, b, c])).toEqual([false, false, false]); + expect(vapidCalls.length).toBe(0); + + // bws has recovered: the next call re-resolves instead of + // returning the settled failure. + expect(await ensurePushConfigured()).toBe(true); + expect(resolveCalls).toBe(2); + expect(vapidCalls.length).toBe(1); + expect(vapidCalls[0][2]).toBe("vapid-private-key-1"); + + // Success IS memoized. + expect(await ensurePushConfigured()).toBe(true); + expect(resolveCalls).toBe(2); + }); + + test("sendPush succeeds after recovery", async () => { + sendImpl = async () => ({ statusCode: 201 }); + expect(await sendPush(target, { hello: "world" })).toEqual({ ok: true, status: 201 }); + }); + + test("401/403 rotation still forgets the secret and reconfigures; a genuinely absent key soft-fails per send", async () => { + sendImpl = async () => { + throw Object.assign(new Error("forbidden"), { statusCode: 403 }); + }; + const rotated = await sendPush(target, { hello: "world" }); + expect(rotated.ok).toBe(false); + expect(rotated.status).toBe(403); + expect(forgotten.length).toBe(1); + + // Next send reconfigures from scratch; with the key genuinely + // absent it fails soft with the existing outcome — no throw. + resolveImpl = async () => null; + const before = resolveCalls; + expect(await sendPush(target, { hello: "world" })).toEqual({ + ok: false, + gone: false, + error: "VAPID private key unavailable (bws)", + }); + expect(resolveCalls).toBeGreaterThan(before); + + // And that soft failure does not latch either: the key appearing + // makes the following send work. + resolveImpl = async () => "vapid-private-key-2"; + sendImpl = async () => ({ statusCode: 201 }); + expect(await sendPush(target, { hello: "world" })).toEqual({ ok: true, status: 201 }); + expect(vapidCalls[vapidCalls.length - 1][2]).toBe("vapid-private-key-2"); + }); +});