fix(web-push): let a new session take over a foreign push fence (06-06)
After an account switch without logout the new session could not name the stored authority, so enable and reconcile stayed STALE_AUTHORITY. The port gains revokeForeign: it rotates a fence another fence generation holds and revokes its association, and leaves the caller's own fence alone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cd47516592
commit
996a9591c8
@@ -1310,6 +1310,26 @@ logout/account switch:
|
||||
찾아 best-effort close한다.
|
||||
6. 새 account opt-in은 기존 consent와 association 정책을 다시 평가한다.
|
||||
|
||||
logout 없이 account가 바뀐 경우(session 만료, cookie 삭제, logout 중 crash)에는
|
||||
새 session이 이전 authority를 모른다. 그래서 `revoke`를 부를 수 없고, `enable`과
|
||||
`reconcile`은 `STALE_AUTHORITY`로 막힌다. 이때는 `revokeForeign({ nextAuthority })`
|
||||
를 한 번 부른다(`src/application/ports/out/web-push-control.ts`). 다른
|
||||
`fenceGeneration`이 가진 fence를 `nextAuthority`의 `REVOKED`로 회전하고, 그 fence가
|
||||
갖던 association을 revoke하고, native subscription과 그 association의 notification을
|
||||
위 1~5 순서로 정리한다. 그 뒤 새 account의 opt-in이 가능하다. `nextAuthority`의
|
||||
`fenceGeneration`이 이미 가진 fence(같은 session의 다른 tab이 opt-in한 경우 등)는
|
||||
건드리지 않고 `inspect` 결과를 돌려준다(`push-subscription-adapter.ts`
|
||||
`revokeForeign`, 06-06). 이 연산은 저장된 authority를 증명으로 요구하지 않는다.
|
||||
그래서 BFF가 방금 확인한 현재 session의 authority로만 부른다. 오래된 session을 가진
|
||||
tab이 부르면 현재 account의 association을 revoke하게 된다.
|
||||
|
||||
Web Push를 채택하려면 server 쪽에 다음 두 가지가 있어야 한다. 비정상 전환에서
|
||||
client revoke가 늦거나 실패해도 이전 account의 push를 막는 것은 이 둘뿐이다.
|
||||
|
||||
- 짧은 association TTL: 갱신되지 않은 association은 server가 스스로 만료시킨다.
|
||||
- send-time authorization: 보낼 때마다 association의 account와 session binding이
|
||||
아직 유효한지 확인한다.
|
||||
|
||||
backend revoke가 ambiguous하면 server subscription은 짧은 association TTL,
|
||||
session/account epoch 확인과 send-time authorization으로 추가 방어한다.
|
||||
local `REVOKED` transaction이 실패하면 “late push를 반드시 drop했다”고 주장하지
|
||||
|
||||
@@ -143,33 +143,8 @@ export function createWebPushSubscriptionAdapter(
|
||||
return exclusive(
|
||||
"SUBSCRIPTION_INSPECT",
|
||||
input.signal,
|
||||
async (generation, operationSignal) => {
|
||||
const supported = supportReadiness();
|
||||
if (supported) return webPushSuccess(supported);
|
||||
const permission = permissionReadiness();
|
||||
if (permission) return webPushSuccess(permission);
|
||||
const native = await getSubscription(operationSignal, generation);
|
||||
if (!native.ok) return native;
|
||||
if (!native.value) {
|
||||
return webPushSuccess(
|
||||
unavailable("NATIVE_SUBSCRIPTION_MISSING"),
|
||||
);
|
||||
}
|
||||
if (!subscriptionUsesKey(native.value, vapidPublicKey)) {
|
||||
return webPushSuccess(
|
||||
unavailable("SUBSCRIPTION_KEY_MISMATCH"),
|
||||
);
|
||||
}
|
||||
const control = await dependencies.fenceStore.read({
|
||||
signal: operationSignal,
|
||||
});
|
||||
if (!control.ok) return control;
|
||||
return webPushSuccess(
|
||||
isActiveFor(control.value?.control ?? null, input.authority)
|
||||
? readiness("PUSH_READY")
|
||||
: unavailable("SESSION_AUTHORITY_CHANGED"),
|
||||
);
|
||||
},
|
||||
(generation, operationSignal) =>
|
||||
inspectReadiness(input.authority, generation, operationSignal),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -421,62 +396,54 @@ export function createWebPushSubscriptionAdapter(
|
||||
) {
|
||||
return webPushSuccess(unavailable("LOCAL_FENCE_UNSAFE"));
|
||||
}
|
||||
const capturedNative = await captureNativeForCleanup(
|
||||
return await revokeReceipt(
|
||||
current.value,
|
||||
input.previousAuthority,
|
||||
input.nextAuthority,
|
||||
generation,
|
||||
operationSignal,
|
||||
generation,
|
||||
);
|
||||
const previousAssociation =
|
||||
current.value.control.association.state === "UNASSOCIATED"
|
||||
? null
|
||||
: current.value.control.association.associationEpoch;
|
||||
const fenced = await dependencies.fenceStore.rotateAndRevoke({
|
||||
expectedRevision: current.value.revision,
|
||||
previousAuthority: input.previousAuthority,
|
||||
nextAuthority: input.nextAuthority,
|
||||
updatedAt: instantNow(),
|
||||
signal: operationSignal,
|
||||
});
|
||||
if (!fenced.ok) {
|
||||
if (previousAssociation) {
|
||||
await boundedBackendRevoke(previousAssociation);
|
||||
}
|
||||
return webPushSuccess(unavailable("LOCAL_FENCE_UNSAFE"));
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
|
||||
let revokeAmbiguous = false;
|
||||
if (previousAssociation) {
|
||||
const backend = await boundedBackendRevoke(
|
||||
previousAssociation,
|
||||
revokeForeign(input) {
|
||||
return exclusive(
|
||||
"SUBSCRIPTION_REVOKE",
|
||||
input.signal,
|
||||
async (generation, operationSignal) => {
|
||||
const current = await dependencies.fenceStore.read({
|
||||
signal: operationSignal,
|
||||
});
|
||||
if (!current.ok) {
|
||||
return webPushSuccess(unavailable("LOCAL_FENCE_UNSAFE"));
|
||||
}
|
||||
// 06-06. Only another session generation's fence is foreign. The
|
||||
// next authority's own fence (another tab of the same session opted
|
||||
// in, or a new release of it) keeps its association.
|
||||
if (
|
||||
!current.value ||
|
||||
current.value.control.fenceGeneration ===
|
||||
input.nextAuthority.fenceGeneration
|
||||
) {
|
||||
return await inspectReadiness(
|
||||
input.nextAuthority,
|
||||
generation,
|
||||
operationSignal,
|
||||
);
|
||||
}
|
||||
const stored = current.value.control;
|
||||
return await revokeReceipt(
|
||||
current.value,
|
||||
Object.freeze({
|
||||
fenceGeneration: stored.fenceGeneration,
|
||||
sessionBindingEpoch: stored.sessionBindingEpoch,
|
||||
releaseEpoch: stored.releaseEpoch,
|
||||
}),
|
||||
input.nextAuthority,
|
||||
generation,
|
||||
operationSignal,
|
||||
);
|
||||
revokeAmbiguous = !backend.ok;
|
||||
}
|
||||
const nativeClean = await cleanupNative(
|
||||
previousAssociation,
|
||||
generation,
|
||||
capturedNative,
|
||||
fenced.value,
|
||||
);
|
||||
observeWebPush(dependencies.observer, {
|
||||
event: "web_push_association_revoked",
|
||||
outcome:
|
||||
revokeAmbiguous || !nativeClean ? "DEGRADED" : "SUCCEEDED",
|
||||
...(revokeAmbiguous
|
||||
? { reason: "BACKEND_REVOKE_AMBIGUOUS" }
|
||||
: !nativeClean
|
||||
? { reason: "NATIVE_UNSUBSCRIBE_AMBIGUOUS" }
|
||||
: {}),
|
||||
});
|
||||
if (revokeAmbiguous) {
|
||||
return webPushSuccess(
|
||||
unavailable("BACKEND_REVOKE_AMBIGUOUS"),
|
||||
);
|
||||
}
|
||||
if (!nativeClean) {
|
||||
return webPushSuccess(
|
||||
unavailable("NATIVE_UNSUBSCRIBE_AMBIGUOUS"),
|
||||
);
|
||||
}
|
||||
return webPushSuccess(unavailable("REVOKED"));
|
||||
},
|
||||
);
|
||||
},
|
||||
@@ -544,6 +511,97 @@ export function createWebPushSubscriptionAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
async function inspectReadiness(
|
||||
authority: PushAuthoritySnapshot,
|
||||
generation: number,
|
||||
operationSignal: AbortSignal,
|
||||
): Promise<WebPushResult<WebPushReadiness>> {
|
||||
const supported = supportReadiness();
|
||||
if (supported) return webPushSuccess(supported);
|
||||
const permission = permissionReadiness();
|
||||
if (permission) return webPushSuccess(permission);
|
||||
const native = await getSubscription(operationSignal, generation);
|
||||
if (!native.ok) return native;
|
||||
if (!native.value) {
|
||||
return webPushSuccess(unavailable("NATIVE_SUBSCRIPTION_MISSING"));
|
||||
}
|
||||
if (!subscriptionUsesKey(native.value, vapidPublicKey)) {
|
||||
return webPushSuccess(unavailable("SUBSCRIPTION_KEY_MISMATCH"));
|
||||
}
|
||||
const control = await dependencies.fenceStore.read({
|
||||
signal: operationSignal,
|
||||
});
|
||||
if (!control.ok) return control;
|
||||
return webPushSuccess(
|
||||
isActiveFor(control.value?.control ?? null, authority)
|
||||
? readiness("PUSH_READY")
|
||||
: unavailable("SESSION_AUTHORITY_CHANGED"),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout order: rotate the fence to REVOKED first, then revoke the captured
|
||||
* association, unsubscribe the captured native subscription and close its
|
||||
* notifications.
|
||||
*/
|
||||
async function revokeReceipt(
|
||||
current: PushControlReceipt,
|
||||
previousAuthority: PushAuthoritySnapshot,
|
||||
nextAuthority: PushAuthoritySnapshot,
|
||||
generation: number,
|
||||
operationSignal: AbortSignal,
|
||||
): Promise<WebPushResult<WebPushReadiness>> {
|
||||
const capturedNative = await captureNativeForCleanup(
|
||||
operationSignal,
|
||||
generation,
|
||||
);
|
||||
const previousAssociation =
|
||||
current.control.association.state === "UNASSOCIATED"
|
||||
? null
|
||||
: current.control.association.associationEpoch;
|
||||
const fenced = await dependencies.fenceStore.rotateAndRevoke({
|
||||
expectedRevision: current.revision,
|
||||
previousAuthority,
|
||||
nextAuthority,
|
||||
updatedAt: instantNow(),
|
||||
signal: operationSignal,
|
||||
});
|
||||
if (!fenced.ok) {
|
||||
if (previousAssociation) {
|
||||
await boundedBackendRevoke(previousAssociation);
|
||||
}
|
||||
return webPushSuccess(unavailable("LOCAL_FENCE_UNSAFE"));
|
||||
}
|
||||
|
||||
let revokeAmbiguous = false;
|
||||
if (previousAssociation) {
|
||||
const backend = await boundedBackendRevoke(previousAssociation);
|
||||
revokeAmbiguous = !backend.ok;
|
||||
}
|
||||
const nativeClean = await cleanupNative(
|
||||
previousAssociation,
|
||||
generation,
|
||||
capturedNative,
|
||||
fenced.value,
|
||||
);
|
||||
observeWebPush(dependencies.observer, {
|
||||
event: "web_push_association_revoked",
|
||||
outcome: revokeAmbiguous || !nativeClean ? "DEGRADED" : "SUCCEEDED",
|
||||
...(revokeAmbiguous
|
||||
? { reason: "BACKEND_REVOKE_AMBIGUOUS" }
|
||||
: !nativeClean
|
||||
? { reason: "NATIVE_UNSUBSCRIBE_AMBIGUOUS" }
|
||||
: {}),
|
||||
});
|
||||
if (revokeAmbiguous) {
|
||||
return webPushSuccess(unavailable("BACKEND_REVOKE_AMBIGUOUS"));
|
||||
}
|
||||
if (!nativeClean) {
|
||||
return webPushSuccess(unavailable("NATIVE_UNSUBSCRIBE_AMBIGUOUS"));
|
||||
}
|
||||
return webPushSuccess(unavailable("REVOKED"));
|
||||
}
|
||||
|
||||
function supportReadiness(): WebPushReadiness | null {
|
||||
if (!dependencies.secureContext || !requestLock) {
|
||||
return readiness("PUSH_UNSUPPORTED");
|
||||
|
||||
@@ -26,6 +26,21 @@ export interface WebPushControlPort {
|
||||
signal?: AbortSignal;
|
||||
}>): Promise<WebPushResult<WebPushReadiness>>;
|
||||
|
||||
/**
|
||||
* 06-06. The session changed without `revoke` (expiry, cleared cookies, a
|
||||
* crash during logout), so the caller cannot name the stored authority. A
|
||||
* fence held by another `fenceGeneration` is rotated to `nextAuthority` as
|
||||
* REVOKED and the association it held is revoked, as `revoke` does. A fence
|
||||
* `nextAuthority`'s generation already holds is left alone and reported as
|
||||
* `inspect` reports it. Call it only with the authority the BFF has just
|
||||
* confirmed for the current session: a tab holding a stale session would
|
||||
* otherwise revoke the current account's association.
|
||||
*/
|
||||
revokeForeign(input: Readonly<{
|
||||
nextAuthority: PushAuthoritySnapshot;
|
||||
signal?: AbortSignal;
|
||||
}>): Promise<WebPushResult<WebPushReadiness>>;
|
||||
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { WEB_PUSH_PROTOCOLS, webPushSuccess } from "../../src/contracts/web-push.ts";
|
||||
import {
|
||||
WEB_PUSH_PROTOCOLS,
|
||||
webPushSuccess,
|
||||
type PushAuthoritySnapshot,
|
||||
} from "../../src/contracts/web-push.ts";
|
||||
import { createPushAssociationFenceStore } from "../../src/adapters/web-push/push-association-fence-store.ts";
|
||||
import {
|
||||
createWebPushSubscriptionAdapter,
|
||||
@@ -1160,3 +1164,143 @@ describe("06-02 two tabs share one native subscription and one fence", () => {
|
||||
expect(await tabs.fence()).toEqual({ ok: true, value: null });
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* 06-06. The session changed from A to B without `revoke` (expiry, cleared
|
||||
* cookies, a crash during logout). B cannot name A's stored authority, so
|
||||
* `revokeForeign` rotates a fence another fence generation holds. A fence B
|
||||
* already holds, for example because another tab of B opted in, is left alone.
|
||||
*/
|
||||
describe("06-06 account switch without logout", () => {
|
||||
async function fenceFor(owner: PushAuthoritySnapshot, associationEpoch: string) {
|
||||
const store = createPushAssociationFenceStore(
|
||||
createFakePushControlStoreDependencies(),
|
||||
);
|
||||
const prepared = await store.prepare({
|
||||
authority: owner,
|
||||
updatedAt: "2026-07-28T00:00:00.000Z",
|
||||
});
|
||||
if (!prepared.ok) throw new Error("expected prepared control");
|
||||
const active = await store.activate({
|
||||
expectedRevision: prepared.value.revision,
|
||||
authority: owner,
|
||||
associationEpoch,
|
||||
updatedAt: "2026-07-28T00:00:01.000Z",
|
||||
});
|
||||
if (!active.ok) throw new Error("expected active control");
|
||||
return store;
|
||||
}
|
||||
|
||||
function adapterOver(
|
||||
fenceStore: ReturnType<typeof createPushAssociationFenceStore>,
|
||||
) {
|
||||
const unsubscribe = vi.fn(async () => {
|
||||
current = null;
|
||||
return true;
|
||||
});
|
||||
let current: WindowPushSubscriptionFacade | null = subscription(unsubscribe);
|
||||
const revoke = vi.fn(gateway().revoke);
|
||||
const register = vi.fn(async () =>
|
||||
webPushSuccess({
|
||||
associationEpoch: "association_02",
|
||||
sessionBindingEpoch: nextAuthority.sessionBindingEpoch,
|
||||
}),
|
||||
);
|
||||
const adapter = createWebPushSubscriptionAdapter({
|
||||
secureContext: true,
|
||||
userActivationIsActive: () => true,
|
||||
permission: {
|
||||
permission: () => "granted",
|
||||
requestPermission: async () => "granted",
|
||||
},
|
||||
registration: {
|
||||
active: true,
|
||||
pushManager: {
|
||||
getSubscription: async () => current,
|
||||
subscribe: async () => {
|
||||
current = subscription();
|
||||
return current;
|
||||
},
|
||||
},
|
||||
getNotifications: async () => [],
|
||||
},
|
||||
fenceStore,
|
||||
gateway: gateway({ register, revoke }),
|
||||
vapidPublicKey,
|
||||
lockManager: navigator.locks,
|
||||
now: () => now,
|
||||
});
|
||||
return { adapter, unsubscribe, revoke, register };
|
||||
}
|
||||
|
||||
it("lets B take over A's ACTIVE fence in one call and opt in again", async () => {
|
||||
const fenceStore = await fenceFor(authority, "association_01");
|
||||
const tab = adapterOver(fenceStore);
|
||||
// Without it B is stuck: the fence still belongs to A.
|
||||
expect(
|
||||
await tab.adapter.enable({
|
||||
authority: nextAuthority,
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
).toMatchObject({ ok: false, error: { code: "STALE_AUTHORITY" } });
|
||||
|
||||
expect(
|
||||
await tab.adapter.revokeForeign({ nextAuthority }),
|
||||
).toEqual({
|
||||
ok: true,
|
||||
value: { state: "PUSH_UNAVAILABLE", reason: "REVOKED" },
|
||||
});
|
||||
expect(tab.revoke.mock.calls).toEqual([
|
||||
[{ associationEpoch: "association_01", signal: expect.any(AbortSignal) }],
|
||||
]);
|
||||
expect(tab.unsubscribe).toHaveBeenCalledOnce();
|
||||
expect(await fenceStore.read()).toMatchObject({
|
||||
ok: true,
|
||||
value: {
|
||||
control: {
|
||||
...nextAuthority,
|
||||
association: { state: "REVOKED", associationEpoch: "association_01" },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(
|
||||
await tab.adapter.enable({
|
||||
authority: nextAuthority,
|
||||
signal: new AbortController().signal,
|
||||
}),
|
||||
).toEqual({ ok: true, value: { state: "PUSH_READY" } });
|
||||
});
|
||||
|
||||
it("leaves a fence B already holds alone and reports it like inspect", async () => {
|
||||
const fenceStore = await fenceFor(nextAuthority, "association_02");
|
||||
const before = await fenceStore.read();
|
||||
const tab = adapterOver(fenceStore);
|
||||
|
||||
expect(await tab.adapter.revokeForeign({ nextAuthority })).toEqual({
|
||||
ok: true,
|
||||
value: { state: "PUSH_READY" },
|
||||
});
|
||||
expect(tab.revoke).not.toHaveBeenCalled();
|
||||
expect(tab.unsubscribe).not.toHaveBeenCalled();
|
||||
expect(await fenceStore.read()).toEqual(before);
|
||||
});
|
||||
|
||||
it("does not treat a new release of the same session as another account", async () => {
|
||||
const fenceStore = await fenceFor(authority, "association_01");
|
||||
const before = await fenceStore.read();
|
||||
const tab = adapterOver(fenceStore);
|
||||
|
||||
expect(
|
||||
await tab.adapter.revokeForeign({
|
||||
nextAuthority: { ...authority, releaseEpoch: "release_02" },
|
||||
}),
|
||||
).toEqual({
|
||||
ok: true,
|
||||
value: { state: "PUSH_UNAVAILABLE", reason: "SESSION_AUTHORITY_CHANGED" },
|
||||
});
|
||||
expect(tab.revoke).not.toHaveBeenCalled();
|
||||
expect(tab.unsubscribe).not.toHaveBeenCalled();
|
||||
expect(await fenceStore.read()).toEqual(before);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user