diff --git a/src/adapters/web-push/push-subscription-adapter.ts b/src/adapters/web-push/push-subscription-adapter.ts index adda9277..59ffebd4 100644 --- a/src/adapters/web-push/push-subscription-adapter.ts +++ b/src/adapters/web-push/push-subscription-adapter.ts @@ -343,6 +343,29 @@ export function createWebPushSubscriptionAdapter( return webPushFailure("ABORTED", "SUBSCRIPTION_RECONCILE"); } if (reconciled.value.state === "ABSENT") { + // R-6.7. Only a local ACTIVE association (the server lost it) is + // re-registered. A REVOKED or UNASSOCIATED fence with a leftover + // native subscription is a failed logout cleanup or an unfinished + // opt-in; registering it would sign the current account up without + // its own consent, so the native subscription is removed instead. + const association = prepared.value.control.association; + if (association.state !== "ACTIVE") { + const nativeClean = await cleanupNative( + association.state === "UNASSOCIATED" + ? null + : association.associationEpoch, + generation, + Object.freeze({ captured: true, subscription: native.value }), + prepared.value, + ); + return webPushSuccess( + unavailable( + nativeClean + ? "BACKEND_ASSOCIATION_MISSING" + : "NATIVE_UNSUBSCRIBE_AMBIGUOUS", + ), + ); + } return await registerAndActivate({ subscription: native.value, prepared: prepared.value, diff --git a/tests/unit/web-push-subscription-adapter.test.ts b/tests/unit/web-push-subscription-adapter.test.ts index 04bfcb9d..1c01f76f 100644 --- a/tests/unit/web-push-subscription-adapter.test.ts +++ b/tests/unit/web-push-subscription-adapter.test.ts @@ -828,3 +828,118 @@ describe("Web Push window subscription adapter", () => { }); }); }); + +/** + * R-6.7. A server ABSENT answer is re-registered only when this browser's + * local association was ACTIVE (the server lost it). A REVOKED or + * UNASSOCIATED fence with a leftover native subscription must not sign the + * current account up without its own opt-in. + */ +describe("Web Push reconcile with a server ABSENT association (R-6.7)", () => { + function reconcileAdapter( + fenceStore: ReturnType, + native: WindowPushSubscriptionFacade, + register: WebPushRegistrationGateway["register"], + ) { + return createWebPushSubscriptionAdapter({ + secureContext: true, + userActivationIsActive: () => false, + permission: { + permission: () => "granted", + requestPermission: async () => "granted", + }, + registration: { + active: true, + pushManager: { + getSubscription: async () => native, + subscribe: async () => native, + }, + getNotifications: async () => [], + }, + fenceStore, + gateway: gateway({ + register, + reconcile: async () => webPushSuccess({ state: "ABSENT" as const }), + }), + vapidPublicKey, + now: () => now, + idempotencyKeyFactory: () => "idempotency_02", + }); + } + + it("re-registers when the local association is ACTIVE (crash recovery)", async () => { + const fenceStore = await activeFenceStore(); + const unsubscribe = vi.fn(async () => true); + const register = vi.fn(async () => + webPushSuccess({ + associationEpoch: "association_02", + sessionBindingEpoch: "session_01", + }), + ); + const adapter = reconcileAdapter( + fenceStore, + subscription(unsubscribe), + register, + ); + + expect(await adapter.reconcile({ authority })).toEqual({ + ok: true, + value: { state: "PUSH_READY" }, + }); + expect(register).toHaveBeenCalledOnce(); + expect(unsubscribe).not.toHaveBeenCalled(); + }); + + /** Logout rotated the fence to the next account, but unsubscribe failed. */ + async function revokedForNextAccount() { + const fenceStore = await activeFenceStore(); + const current = await fenceStore.read(); + if (!current.ok || !current.value) throw new Error("expected control"); + const revoked = await fenceStore.rotateAndRevoke({ + expectedRevision: current.value.revision, + previousAuthority: authority, + nextAuthority, + updatedAt: "2026-07-28T00:00:02.000Z", + }); + if (!revoked.ok) throw new Error("expected revoked control"); + return fenceStore; + } + + it.each([ + { state: "REVOKED", fenceStore: revokedForNextAccount }, + { + state: "UNASSOCIATED", + fenceStore: async () => + createPushAssociationFenceStore( + createFakePushControlStoreDependencies(), + ), + }, + ])( + "unsubscribes instead of registering when the local association is $state", + async ({ fenceStore }) => { + const unsubscribe = vi.fn(async () => true); + // The backend would accept the next account's registration. + const register = vi.fn(async () => + webPushSuccess({ + associationEpoch: "association_02", + sessionBindingEpoch: nextAuthority.sessionBindingEpoch, + }), + ); + const adapter = reconcileAdapter( + await fenceStore(), + subscription(unsubscribe), + register, + ); + + expect(await adapter.reconcile({ authority: nextAuthority })).toEqual({ + ok: true, + value: { + state: "PUSH_UNAVAILABLE", + reason: "BACKEND_ASSOCIATION_MISSING", + }, + }); + expect(register).not.toHaveBeenCalled(); + expect(unsubscribe).toHaveBeenCalledOnce(); + }, + ); +});