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 { 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, 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 { 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 | 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; }