Files

111 lines
4.0 KiB
TypeScript

// @vitest-environment node
import { readFileSync } from "node:fs";
import vm from "node:vm";
import { describe, expect, it, vi } from "vitest";
const source = readFileSync(new URL("./sw.js", import.meta.url), "utf8");
function createHarness(windowClients: Array<Record<string, unknown>> = []) {
const handlers = new Map<string, (event: Record<string, unknown>) => void>();
const showNotification = vi.fn().mockResolvedValue(undefined);
const openWindow = vi.fn().mockResolvedValue(undefined);
const matchAll = vi.fn().mockResolvedValue(windowClients);
const self = {
location: { origin: "https://app.example.test" },
registration: { showNotification },
clients: { claim: vi.fn(), matchAll, openWindow },
skipWaiting: vi.fn(),
addEventListener: (name: string, listener: (event: Record<string, unknown>) => void) => handlers.set(name, listener),
};
vm.runInNewContext(source, {
self,
caches: { keys: vi.fn().mockResolvedValue([]), open: vi.fn() },
fetch: vi.fn(),
URL,
Promise,
});
return { handlers, showNotification, openWindow, matchAll };
}
async function dispatchPush(harness: ReturnType<typeof createHarness>, payload: unknown) {
let pending: Promise<unknown> | undefined;
harness.handlers.get("push")?.({
data: { json: () => payload },
waitUntil: (promise: Promise<unknown>) => {
pending = promise;
},
});
await pending;
}
async function dispatchClick(harness: ReturnType<typeof createHarness>, href: unknown) {
let pending: Promise<unknown> | undefined;
const close = vi.fn();
harness.handlers.get("notificationclick")?.({
notification: { data: { href }, close },
waitUntil: (promise: Promise<unknown>) => {
pending = promise;
},
});
await pending;
return close;
}
describe("service worker Web Push boundary", () => {
it("renders the bounded worker payload as a visible notification with a same-origin call target", async () => {
const harness = createHarness();
await dispatchPush(harness, {
title: "HiveGrid",
body: "+49 30 123456",
data: { callId: "call_1", href: "/hivegrid-demo/calls/call_1" },
});
expect(harness.showNotification).toHaveBeenCalledWith(
"HiveGrid",
expect.objectContaining({
body: "+49 30 123456",
tag: "telephony-call:call_1",
data: { href: "/hivegrid-demo/calls/call_1" },
}),
);
});
it("ignores malformed payloads and replaces external navigation URLs with the safe app root", async () => {
const harness = createHarness();
await dispatchPush(harness, { title: "HiveGrid", data: {} });
expect(harness.showNotification).not.toHaveBeenCalled();
await dispatchPush(harness, {
title: "HiveGrid",
body: "Incoming call",
data: { href: "https://attacker.example/phish" },
});
expect(harness.showNotification).toHaveBeenCalledWith(
"HiveGrid",
expect.objectContaining({ data: { href: "/" } }),
);
});
it("focuses an already open exact call route instead of opening a duplicate tab", async () => {
const focus = vi.fn().mockResolvedValue(undefined);
const client = { url: "https://app.example.test/hivegrid-demo/calls/call_1", focus };
const harness = createHarness([client]);
const close = await dispatchClick(harness, "/hivegrid-demo/calls/call_1");
expect(close).toHaveBeenCalledOnce();
expect(focus).toHaveBeenCalledOnce();
expect(harness.openWindow).not.toHaveBeenCalled();
});
it("opens a safe relative call target and refuses protocol-relative or external click targets", async () => {
const harness = createHarness();
await dispatchClick(harness, "/hivegrid-demo/calls/call_2?source=push");
expect(harness.openWindow).toHaveBeenCalledWith("/hivegrid-demo/calls/call_2?source=push");
harness.openWindow.mockClear();
await dispatchClick(harness, "//attacker.example/phish");
await dispatchClick(harness, "https://attacker.example/phish");
expect(harness.openWindow).not.toHaveBeenCalled();
});
});