doxa-maps/library/packet/sha384.js
Doxa Maps 5d3b2cf95d Pinned files: a page that is not secure is told so in words
A browser gives crypto.subtle only to a secure page (https, or http://localhost). Opened any other way, for example a dev server reached over plain http by its LAN address from a phone, every pinned file was refused with a bare "reading 'digest'": the packet printed in English and page 1 fell back to its stored sheet. sha384() now refuses with a sentence that names the cause. check:pinned-fetch-fresh arm D2 removes crypto.subtle and requires that sentence; its self-test plants the unguarded call.
2026-10-03 08:58:09 +00:00

37 lines
2.3 KiB
JavaScript

// The Subresource-Integrity form of a text's SHA-384 ("sha384-<base64>"), via WebCrypto, so a file fetched at
// print time can be compared with the pin baked into the bundle (poster-render.integrity.js) before it is used.
// The same value pin-poster-render.mjs computes with node:crypto.
// ★ ONLY ON A SECURE PAGE. A browser gives crypto.subtle only to a secure page (https, or http://localhost). Opened any
// other way (a dev server reached over plain http by its LAN address, from a phone on the same Wi-Fi), every pinned
// file was refused with a bare "reading 'digest'": the packet printed in English and page 1 fell back to its stored
// sheet, with nothing saying why. Say it in words.
export async function sha384(text) {
const subtle = globalThis.crypto && globalThis.crypto.subtle
if (!subtle) throw new Error('pinned files can only be checked on a secure page (https, or http://localhost); this page is not one')
const d = new Uint8Array(await subtle.digest('SHA-384', new TextEncoder().encode(text)))
let bin = ''
for (const b of d) bin += String.fromCharCode(b)
return `sha384-${btoa(bin)}`
}
// ★ A PINNED FILE THAT FAILS ITS PIN IS FETCHED ONCE MORE, FRESH. The loaders fetch with cache: 'force-cache' (fast,
// and it works offline once cached), which returns ANY cached copy, however old. After a re-pin, a reader who had
// printed before kept the previous file: it failed its pin on every print, and the print fell back without a word
// (the stored poster for page 1, English for the packet text) until the browser evicted it. So a copy that is not
// served or does not match is fetched ONCE more with cache: 'reload', which skips the HTTP cache and refreshes it,
// and only that answer decides. Resolves with the verified text; rejects with the last reason (err.integrity is
// true when the bytes were wrong rather than missing).
export async function fetchPinned(url, pin) {
let last = null
for (const cache of ['force-cache', 'reload']) {
try {
const res = await fetch(url, { cache })
if (!res.ok) { last = new Error(`not served (${res.status})`); continue }
const text = await res.text()
const got = await sha384(text)
if (got === pin) return text
last = Object.assign(new Error(`failed its integrity check (expected ${pin}, got ${got})`), { integrity: true })
} catch (e) { last = e }
}
throw last
}