From c65afd670ba8be6cbd3051bb193acf76dc5f9453 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 11:46:50 -0600 Subject: [PATCH] fix(aggregator): retry failed VAPID setup --- pm-stack/aggregator/src/push.ts | 6 +- pm-stack/aggregator/test/push.test.ts | 84 +++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 pm-stack/aggregator/test/push.test.ts diff --git a/pm-stack/aggregator/src/push.ts b/pm-stack/aggregator/src/push.ts index 97a2050ab8..f5623977ed 100644 --- a/pm-stack/aggregator/src/push.ts +++ b/pm-stack/aggregator/src/push.ts @@ -29,13 +29,17 @@ let configurePromise: Promise | null = null; export function ensurePushConfigured(): Promise { if (configured) return Promise.resolve(true); if (!configurePromise) { - configurePromise = (async () => { + const attempt = (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 = attempt; + void attempt.then((ok) => { + if (!ok && configurePromise === attempt) configurePromise = null; + }); } return configurePromise; } diff --git a/pm-stack/aggregator/test/push.test.ts b/pm-stack/aggregator/test/push.test.ts new file mode 100644 index 0000000000..6885733e5e --- /dev/null +++ b/pm-stack/aggregator/test/push.test.ts @@ -0,0 +1,84 @@ +import { expect, mock, test } from "bun:test"; + +type WebPushArgs = [ + subscription: { endpoint: string; keys: { p256dh: string; auth: string } }, + payload: string, + options: { TTL: number; urgency: string }, +]; + +const lookups: string[] = []; +const forgotten: string[] = []; +const configurations: Array<{ subject: string; publicKey: string; privateKey: string }> = []; +let resolveSecret: (name: string) => Promise = async () => null; +let sendNotification: (...args: WebPushArgs) => Promise<{ statusCode: number }> = async () => ({ statusCode: 201 }); + +mock.module("web-push", () => ({ + default: { + setVapidDetails: (subject: string, publicKey: string, privateKey: string) => { + configurations.push({ subject, publicKey, privateKey }); + }, + sendNotification: (...args: WebPushArgs) => sendNotification(...args), + }, +})); + +mock.module("../src/secrets.ts", () => ({ + resolveBwsSecret: (name: string) => { + lookups.push(name); + return resolveSecret(name); + }, + forgetBwsSecret: (name: string) => forgotten.push(name), +})); + +const pushModule = import("../src/push.ts"); + +test("VAPID setup retries failures, shares in-flight work, and resets on key rotation", async () => { + const push = await pushModule; + const target = { endpoint: "https://push.example/sub", p256dh: "public", auth: "auth" }; + const payload = { title: "test" }; + const missing = { ok: false, gone: false, error: "VAPID private key unavailable (bws)" } as const; + + const initialSecrets: Array = [null, null, "initial-key"]; + resolveSecret = async () => initialSecrets.shift() ?? null; + + expect(await push.sendPush(target, payload)).toEqual(missing); + expect(await push.sendPush(target, payload)).toEqual(missing); + expect(lookups).toHaveLength(2); + expect(await push.ensurePushConfigured()).toBe(true); + expect(lookups).toHaveLength(3); + expect(configurations.map(({ privateKey }) => privateKey)).toEqual(["initial-key"]); + + expect(await push.ensurePushConfigured()).toBe(true); + expect(lookups).toHaveLength(3); + expect(await push.sendPush(target, payload)).toEqual({ ok: true, status: 201 }); + + sendNotification = async () => { + throw Object.assign(new Error("unauthorized"), { statusCode: 401 }); + }; + expect(await push.sendPush(target, payload)).toMatchObject({ ok: false, status: 401 }); + expect(forgotten).toEqual([push.VAPID_PRIVATE_KEY_SECRET]); + + let resolveDeferred!: (secret: string | null) => void; + resolveSecret = async () => new Promise((resolve) => { + resolveDeferred = resolve; + }); + const first = push.ensurePushConfigured(); + const second = push.ensurePushConfigured(); + expect(lookups).toHaveLength(4); + resolveDeferred("rotated-key"); + await expect(Promise.all([first, second])).resolves.toEqual([true, true]); + expect(configurations.map(({ privateKey }) => privateKey)).toEqual(["initial-key", "rotated-key"]); + + sendNotification = async () => { + throw Object.assign(new Error("forbidden"), { statusCode: 403 }); + }; + expect(await push.sendPush(target, payload)).toMatchObject({ ok: false, status: 403 }); + expect(forgotten).toEqual([push.VAPID_PRIVATE_KEY_SECRET, push.VAPID_PRIVATE_KEY_SECRET]); + + resolveSecret = async () => "rotated-again-key"; + expect(await push.ensurePushConfigured()).toBe(true); + expect(configurations.map(({ privateKey }) => privateKey)).toEqual([ + "initial-key", + "rotated-key", + "rotated-again-key", + ]); +});