fix(web-push): open the registration scope for a refused click (06-09)

The fallback opened `${origin}/`, outside the app under a router base path.
The scope host now passes registration.scope and the click adapter opens it,
refusing any app root outside the origin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-26 01:06:48 +09:00
co-authored by Claude Opus 5.5
parent 8c17eab668
commit dd7a44677b
5 changed files with 79 additions and 6 deletions
@@ -1486,7 +1486,10 @@ worker가 종료·재시작된 뒤에도 click을 처리할 수 있도록 이 bo
envelope만 `NotificationOptions.data`에 넣는다. click handler는 codec/size/expiry,
current local `ACTIVE` fence와 release epoch를 다시 검증한다. unknown/mismatch
(만료, fence·release 불일치, fence 읽기 실패, 모르는 route)는 notification을 닫고
요청한 route 대신 **앱 루트(`<origin>/`)를 handoff 데이터 없이 연다**(D-14).
요청한 route 대신 **앱 루트를 handoff 데이터 없이 연다**(D-14). 앱 루트는 worker의
`registration.scope`, 즉 router base path다(예: `/app/`). scope를 모르면
`<origin>/`을 쓴다(`src/adapters/web-push/service-worker-scope-host.ts`,
`inbound/notification-click-adapter.ts`의 `appRootUrl`, 06-09).
사용자의 탭이 아무 반응 없이 사라지지 않게 하되, 거절된 click의 route와
notification 정보는 페이지로 넘기지 않는다. 결과는 여전히 실패로 보고하고
(`web_push_click_dispatched` `FAILED` + reason, `nativeEffect`는 루트 창의
@@ -84,6 +84,11 @@ export function createNotificationClickAdapter(dependencies: Readonly<{
fenceStore: PushAssociationFenceStore;
clients: WorkerClientsFacade;
origin: string;
/**
* 06-09. Where a refused click opens: the registration scope, which is the
* router base path. Defaults to the origin root; must be same-origin.
*/
appRootUrl?: string;
now?: () => number;
handlerDeadlineMs?: number;
scheduler?: TimeoutScheduler;
@@ -104,6 +109,11 @@ export function createNotificationClickAdapter(dependencies: Readonly<{
throw new TypeError("Web Push click adapter configuration is invalid.");
}
const origin: string = parsedOrigin;
const parsedAppRootUrl = safeTarget(origin, dependencies.appRootUrl ?? "/");
if (!parsedAppRootUrl) {
throw new TypeError("Web Push click adapter configuration is invalid.");
}
const appRootUrl: string = parsedAppRootUrl;
return Object.freeze({
handle(event) {
@@ -246,9 +256,10 @@ export function createNotificationClickAdapter(dependencies: Readonly<{
/**
* Q-6.6-11 / D-14. A click whose data or authority was refused (expired,
* fence or release mismatch, unreadable fence, unknown route) still opens
* the app, at the same-origin root and with no handoff data, so the user's
* tap is never silently dropped. The refusal stays the result. Native
* failures (the browser refused a window) and aborts are not retried.
* the app at its root (06-09: the router base path) with no handoff data,
* so the user's tap is never silently dropped. The refusal stays the
* result. Native failures (the browser refused a window) and aborts are not
* retried.
*/
async function openAppRootIfRejected(
result: WebPushResult<void>,
@@ -265,8 +276,7 @@ export function createNotificationClickAdapter(dependencies: Readonly<{
) {
return result;
}
// `origin` is already a parsed origin, so this is its root URL.
await openWindowBounded(`${origin}/`, signal, effect).catch(() => {
await openWindowBounded(appRootUrl, signal, effect).catch(() => {
// The native call threw: whether a window opened is unknown.
});
return result;
@@ -60,6 +60,8 @@ type ServiceWorkerClientsFacade = Readonly<{
*/
export type ServiceWorkerEventHost = Readonly<{
origin: string;
/** 06-09. `registration.scope`, the app root a refused click opens. */
scope?: string;
registration: ServiceWorkerRegistrationFacade;
clients: ServiceWorkerClientsFacade;
addEventListener(type: string, listener: (event: unknown) => void): void;
@@ -118,6 +120,9 @@ export function createWebPushServiceWorkerRuntime(
},
},
origin: dependencies.host.origin,
...(dependencies.host.scope === undefined
? {}
: { appRootUrl: dependencies.host.scope }),
now: dependencies.now,
scheduler: dependencies.scheduler,
observer: dependencies.observer,
@@ -19,6 +19,7 @@ type MatchAllOptions = Parameters<
export type NativeWorkerScope = Readonly<{
location: Readonly<{ origin: string }>;
registration: Readonly<{
scope: string;
showNotification(title: string, options: unknown): Promise<void>;
}>;
clients: Readonly<{
@@ -36,6 +37,7 @@ export function createServiceWorkerScopeHost(
): ServiceWorkerEventHost {
return Object.freeze({
origin: scope.location.origin,
scope: scope.registration.scope,
registration: Object.freeze({
showNotification: (title: string, options: NotificationOptions) =>
scope.registration.showNotification(title, {
@@ -445,6 +445,59 @@ describe("Web Push worker runtime", () => {
expect(click.postMessage).not.toHaveBeenCalled();
});
// 06-09. Under a router base path the app root is the registration
// scope, not the origin root.
it("opens the registration scope under a base path", async () => {
const store = await activeFence();
const listeners = new Map<string, (event: unknown) => void>();
const openWindow = vi.fn(async () => null);
const runtime = createWebPushServiceWorkerRuntime({
fenceStore: store,
registry,
now: () => Date.parse("2026-07-28T02:00:00.000Z"),
host: {
origin: "https://app.example.test",
scope: "https://app.example.test/app/",
registration: { async showNotification() {} },
clients: { matchAll: async () => [], openWindow },
addEventListener(type, listener) {
listeners.set(type, listener);
},
removeEventListener() {},
},
});
let waited: Promise<void> | null = null;
listeners.get("notificationclick")?.({
notification: new WebIdlNotification(clickData()),
waitUntil(task: Promise<void>) {
waited = task;
},
});
await waited;
expect(openWindow.mock.calls).toEqual([
["https://app.example.test/app/"],
]);
runtime.dispose();
});
it("refuses an app root outside the origin", async () => {
expect(() =>
createNotificationClickAdapter({
fenceStore: createPushAssociationFenceStore(
createFakePushControlStoreDependencies(),
),
registry,
origin: "https://app.example.test",
appRootUrl: "https://other.example.test/app/",
clients: {
matchControlledWindowClients: async () => [],
openWindow: async () => null,
},
}),
).toThrow(TypeError);
});
it("does not retry the root after the browser refused the target window", async () => {
const store = await activeFence();
const openWindow = vi.fn(async () => null);