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>
102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import type { PushAuthoritySnapshot } from "../../contracts/web-push.ts";
|
|
|
|
/**
|
|
* Q-6.6-12. The validation primitives every web-push codec shares. Each file
|
|
* used to carry its own copy, and the copies had drifted: one authority check
|
|
* threw on `null`, one base64url decoder accepted a lone trailing character.
|
|
* Where they differed, the stricter behavior is the one kept here.
|
|
*/
|
|
|
|
/** Opaque ids: bounded, ASCII, starting with an alphanumeric. */
|
|
export const OPAQUE_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u;
|
|
|
|
export function validOpaqueId(value: unknown): value is string {
|
|
return typeof value === "string" && OPAQUE_ID.test(value);
|
|
}
|
|
|
|
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
}
|
|
|
|
/** `expected` must be sorted; the record's own keys must equal it exactly. */
|
|
export function hasExactKeys(
|
|
value: Record<string, unknown>,
|
|
expected: readonly string[],
|
|
): boolean {
|
|
const actual = Object.keys(value).sort();
|
|
return (
|
|
actual.length === expected.length &&
|
|
actual.every((key, index) => key === expected[index])
|
|
);
|
|
}
|
|
|
|
export function exactRecord(
|
|
value: unknown,
|
|
expected: readonly string[],
|
|
): value is Record<string, unknown> {
|
|
return isRecord(value) && hasExactKeys(value, expected);
|
|
}
|
|
|
|
/** A missing or non-object authority is invalid input, never a TypeError. */
|
|
export function validAuthority(value: unknown): value is PushAuthoritySnapshot {
|
|
return (
|
|
isRecord(value) &&
|
|
validOpaqueId(value.fenceGeneration) &&
|
|
validOpaqueId(value.sessionBindingEpoch) &&
|
|
validOpaqueId(value.releaseEpoch)
|
|
);
|
|
}
|
|
|
|
const BASE64URL_ALPHABET =
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
|
|
/**
|
|
* Unpadded base64url. Rejects non-strings, characters outside the alphabet,
|
|
* a lone trailing character (6 bits cannot finish a byte) and non-zero spare
|
|
* bits, so every accepted string has exactly one decoding.
|
|
*/
|
|
export function base64UrlDecode(value: unknown): Uint8Array<ArrayBuffer> | null {
|
|
if (typeof value !== "string") return null;
|
|
const output = new Uint8Array(
|
|
new ArrayBuffer(Math.floor((value.length * 6) / 8)),
|
|
);
|
|
let accumulator = 0;
|
|
let bitCount = 0;
|
|
let outputIndex = 0;
|
|
for (const character of value) {
|
|
const digit = BASE64URL_ALPHABET.indexOf(character);
|
|
if (digit < 0) return null;
|
|
accumulator = (accumulator << 6) | digit;
|
|
bitCount += 6;
|
|
if (bitCount >= 8) {
|
|
bitCount -= 8;
|
|
output[outputIndex] = (accumulator >>> bitCount) & 0xff;
|
|
outputIndex += 1;
|
|
}
|
|
}
|
|
return bitCount < 6 &&
|
|
outputIndex === output.length &&
|
|
(accumulator & ((1 << bitCount) - 1)) === 0
|
|
? output
|
|
: null;
|
|
}
|
|
|
|
/** Unpadded base64url with zero spare bits: the inverse of the decoder. */
|
|
export function base64UrlEncode(bytes: Uint8Array): string {
|
|
let output = "";
|
|
let accumulator = 0;
|
|
let bitCount = 0;
|
|
for (const byte of bytes) {
|
|
accumulator = (accumulator << 8) | byte;
|
|
bitCount += 8;
|
|
while (bitCount >= 6) {
|
|
bitCount -= 6;
|
|
output += BASE64URL_ALPHABET[(accumulator >>> bitCount) & 63];
|
|
}
|
|
}
|
|
if (bitCount > 0) {
|
|
output += BASE64URL_ALPHABET[(accumulator << (6 - bitCount)) & 63];
|
|
}
|
|
return output;
|
|
}
|