From 7bce61c218661f4889c53e815994a23dbd91d57f Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 24 Sep 2026 12:27:29 +0900 Subject: [PATCH] fix(web-push): re-register an ABSENT association only when locally ACTIVE (R-6.7) reconcile() re-registered every native subscription the server reported ABSENT. After an offline logout whose unsubscribe() failed, the fence is rotated to the next account with a REVOKED association while the native subscription survives; the next account's boot reconcile then registered it without that account's opt-in. When the server answers ABSENT, reconcile() now re-registers only if the local association is ACTIVE (the server lost it). For REVOKED or UNASSOCIATED it runs the existing guarded cleanupNative() (captured subscription, unchanged fence, owned notification close, bounded) and returns PUSH_UNAVAILABLE with BACKEND_ASSOCIATION_MISSING, or NATIVE_UNSUBSCRIBE_AMBIGUOUS when cleanup could not complete. Pinned first: "re-registers when the local association is ACTIVE (crash recovery)" passed before and after the change. RED before fix (resolved PUSH_READY, i.e. the next account was signed up): "unsubscribes instead of registering when the local association is REVOKED" and "... is UNASSOCIATED". Gates: check:architecture, check:types:{app,test,service-worker,recipes, node,web-worker}, lint, test:component pass. test:unit passes except the pre-existing removal-fixture.test.ts case that needs generated release evidence under artifacts/ (absent in a fresh worktree; unrelated). Co-Authored-By: Claude Opus 5.5 --- .../web-push/push-subscription-adapter.ts | 23 ++++ .../web-push-subscription-adapter.test.ts | 115 ++++++++++++++++++ 2 files changed, 138 insertions(+) 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(); + }, + ); +});