refactor(web-push): merge codec primitives into one module and inject the page document (Q-6.6-12)
web-push/codec-primitives.ts now owns OPAQUE_ID/validOpaqueId, isRecord,
hasExactKeys, exactRecord, validAuthority, base64UrlDecode and (to share the
alphabet) base64UrlEncode. push-codec,
push-registration-gateway, push-association-fence-store,
push-subscription-adapter and service-worker-runtime import them instead of
carrying their own copies (the fence store's control/association key checks
now use exactRecord/hasExactKeys with the same key lists). The duplicate-key
JSON scan in push-codec.ts is untouched (realtime lane, T76).
Where the copies differed, the stricter validated behavior is kept:
- validAuthority: the fence store's null guard. The gateway's copy threw a
TypeError for a null authority, so register()/reconcile() rejected instead
of returning INVALID_INPUT; they now return INVALID_INPUT (the only change a
caller can observe; characterization test was RED before the merge).
Non-record values (arrays) are also refused.
- base64UrlDecode: the gateway's decoder, which refuses a lone trailing
character (length % 4 == 1). The window adapter's copy accepted it when the
spare bits were zero; its only caller pre-checks an 87-char VAPID key, so
that difference was not reachable. `>>` vs `>>>` gave identical bytes.
- OPAQUE_ID, isRecord/hasExactKeys/exactRecord were already identical.
Page controller: PageControllerDependencies gains optional `document`; the
6-hour update check reads it (falling back to the global document), and
bootstrap/register-service-worker.ts now passes host.document through.
New adapter file: INVENTORY.md row 126 + renumber, total 137/137; detail row in
docs/reviews/adapters/05-service-worker-and-web-push.md.
Verified: characterization tests pinned the caller-visible behavior of every
copy before the merge (spare-bit keys, VAPID key, null authority in fence
store and gateway, array/extra-key backend answers, opaque ids); all pass
after, plus direct base64UrlDecode tests and an encode round-trip against
Node's base64url for lengths 0..66. Document injection test was RED
(update() ran while the injected document was hidden), green after. Gates:
check:architecture, types app/test/service-worker, lint, test:unit (only
pre-existing removal-fixture failure), test:component,
check:realtime-boundaries, check:adapter-inventory.
Realtime reference bundle (check:optional-recipes:source, already over budget
at b953142): 40139 -> 40097 gzip bytes after T89+T90.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9e70ae2d6c
commit
ec71b0b0a2
@@ -18,6 +18,13 @@ import {
|
||||
createWebPushRegistrationGateway,
|
||||
type WebPushRegistrationExecutor,
|
||||
} from "../../src/adapters/web-push/push-registration-gateway.ts";
|
||||
import { createPushAssociationFenceStore } from "../../src/adapters/web-push/push-association-fence-store.ts";
|
||||
import { createWebPushSubscriptionAdapter } from "../../src/adapters/web-push/push-subscription-adapter.ts";
|
||||
import { createFakePushControlStoreDependencies } from "../helpers/fake-push-control-repository.ts";
|
||||
import {
|
||||
base64UrlDecode,
|
||||
base64UrlEncode,
|
||||
} from "../../src/adapters/web-push/codec-primitives.ts";
|
||||
|
||||
const now = Date.parse("2026-07-28T00:00:00.000Z");
|
||||
|
||||
@@ -253,3 +260,136 @@ describe("Web Push contracts", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Q-6.6-12. The web-push files each carried their own base64url decoder,
|
||||
* opaque-id pattern, authority check and exact-record check. These pin what
|
||||
* every caller observes, so merging them into one module cannot loosen any.
|
||||
*/
|
||||
describe("web-push codec primitives as seen by their callers (Q-6.6-12)", () => {
|
||||
const authority = {
|
||||
fenceGeneration: "fence_01",
|
||||
sessionBindingEpoch: "session_01",
|
||||
releaseEpoch: "release_01",
|
||||
};
|
||||
const p256dhBytes = new Uint8Array(65);
|
||||
p256dhBytes[0] = 4;
|
||||
const material = {
|
||||
endpoint: "https://push.example.test/subscription/opaque",
|
||||
p256dh: Buffer.from(p256dhBytes).toString("base64url"),
|
||||
auth: Buffer.from(new Uint8Array(16)).toString("base64url"),
|
||||
expirationTime: null,
|
||||
};
|
||||
/** 87 chars carry 2 spare bits and 22 chars carry 4; they must be zero. */
|
||||
const withSpareBitSet = (encoded: string) =>
|
||||
`${encoded.slice(0, -1)}${encoded.endsWith("A") ? "B" : "A"}`;
|
||||
const registered: WebPushRegistrationExecutor = {
|
||||
async execute() {
|
||||
return webPushSuccess({
|
||||
protocol: WEB_PUSH_PROTOCOLS.registration,
|
||||
associationEpoch: "association_01",
|
||||
sessionBindingEpoch: "session_01",
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
it("rejects subscription keys whose spare base64url bits are set", async () => {
|
||||
const gateway = createWebPushRegistrationGateway(registered);
|
||||
for (const broken of [
|
||||
{ ...material, p256dh: withSpareBitSet(material.p256dh) },
|
||||
{ ...material, auth: withSpareBitSet(material.auth) },
|
||||
]) {
|
||||
expect(
|
||||
await gateway.register({
|
||||
material: broken,
|
||||
authority,
|
||||
idempotencyKey: "idempotency_01",
|
||||
}),
|
||||
).toMatchObject({ ok: false, error: { code: "INVALID_INPUT" } });
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects a VAPID key whose spare base64url bits are set", () => {
|
||||
const key = new Uint8Array(65);
|
||||
key[0] = 4;
|
||||
const valid = Buffer.from(key).toString("base64url");
|
||||
expect(() =>
|
||||
createWebPushSubscriptionAdapter({
|
||||
vapidPublicKey: withSpareBitSet(valid),
|
||||
} as never),
|
||||
).toThrow(TypeError);
|
||||
});
|
||||
|
||||
it("rejects a null authority as invalid input in the fence store", async () => {
|
||||
const store = createPushAssociationFenceStore(
|
||||
createFakePushControlStoreDependencies(),
|
||||
);
|
||||
expect(
|
||||
await store.prepare({
|
||||
authority: null as never,
|
||||
updatedAt: "2026-07-28T00:00:00.000Z",
|
||||
}),
|
||||
).toMatchObject({ ok: false, error: { code: "INVALID_INPUT" } });
|
||||
});
|
||||
|
||||
it("rejects a null authority as invalid input in the gateway", async () => {
|
||||
const gateway = createWebPushRegistrationGateway(registered);
|
||||
await expect(
|
||||
gateway.register({
|
||||
material,
|
||||
authority: null as never,
|
||||
idempotencyKey: "idempotency_01",
|
||||
}),
|
||||
).resolves.toMatchObject({ ok: false, error: { code: "INVALID_INPUT" } });
|
||||
await expect(
|
||||
gateway.reconcile({ material, authority: null as never }),
|
||||
).resolves.toMatchObject({ ok: false, error: { code: "INVALID_INPUT" } });
|
||||
});
|
||||
|
||||
it("rejects backend answers that are arrays or carry extra keys", async () => {
|
||||
for (const answer of [
|
||||
[WEB_PUSH_PROTOCOLS.revoke, "REVOKED"],
|
||||
{ protocol: WEB_PUSH_PROTOCOLS.revoke, state: "REVOKED", extra: 1 },
|
||||
]) {
|
||||
const gateway = createWebPushRegistrationGateway({
|
||||
execute: async () => webPushSuccess(answer),
|
||||
});
|
||||
expect(
|
||||
await gateway.revoke({ associationEpoch: "association_01" }),
|
||||
).toMatchObject({ ok: false, error: { code: "CONTRACT_REJECTED" } });
|
||||
}
|
||||
});
|
||||
|
||||
it("gives every accepted base64url string exactly one decoding", () => {
|
||||
expect([...base64UrlDecode("AQID")!]).toEqual([1, 2, 3]);
|
||||
expect([...base64UrlDecode("AQ")!]).toEqual([1]);
|
||||
expect(base64UrlDecode("")?.byteLength).toBe(0);
|
||||
// "AQIDA": a lone trailing character cannot finish a byte. The window
|
||||
// adapter's old copy accepted it because its spare bits were zero.
|
||||
for (const invalid of ["A", "AQIDA", "AR", "AQ==", "AQ+/", null, 7]) {
|
||||
expect(base64UrlDecode(invalid), String(invalid)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("encodes base64url exactly like Node and round-trips through the decoder", () => {
|
||||
for (let length = 0; length <= 66; length += 1) {
|
||||
const bytes = Uint8Array.from({ length }, (_, index) => (index * 37 + 11) & 255);
|
||||
const encoded = base64UrlEncode(bytes);
|
||||
expect(encoded).toBe(Buffer.from(bytes).toString("base64url"));
|
||||
expect([...base64UrlDecode(encoded)!]).toEqual([...bytes]);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects opaque ids outside the shared pattern", async () => {
|
||||
const gateway = createWebPushRegistrationGateway(registered);
|
||||
for (const associationEpoch of ["", "-leading", "a b", "a".repeat(129)]) {
|
||||
expect(await gateway.revoke({ associationEpoch })).toMatchObject({
|
||||
ok: false,
|
||||
error: { code: "INVALID_INPUT" },
|
||||
});
|
||||
}
|
||||
expect(
|
||||
decodeWebPushHint(encoded(hint({ notificationId: "a b" })), now),
|
||||
).toMatchObject({ ok: false });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user