// The verification chain, one step at a time. // // Detection is deliberately all-or-nothing: the popup says verified or not and // moves on. This module keeps the reasoning, so a reader who asks "how do you // know?" can see each link: the record that was fetched, the home address it // claims, the well-known answer at that address, and whether the two agree. // // The probes are the same ones detection makes, and the same cache backs them; // this adds no new host and no new permission. import { probeWellKnown, wellKnownUrl } from './detection' import type { PubInfo } from './types' export type VerifyStatus = 'pass' | 'fail' export interface VerifyStep { id: 'record' | 'home' | 'well-known' | 'match' label: string detail: string status: VerifyStatus /** An http(s) URL a reader can open, never an at-uri. */ href?: string } export interface VerifyReport { steps: VerifyStep[] verified: boolean } export interface VerifyOptions { /** The popup's Refresh: ask the site again instead of using its cache. */ force?: boolean /** Injected for tests; the real probe is detection's cached one. */ probe?: (base: string, force?: boolean) => Promise } function parseUrl(value: string): URL | undefined { try { const url = new URL(value) return url.protocol === 'https:' || url.protocol === 'http:' ? url : undefined } catch { return undefined } } function endpointFor(home: URL): string | undefined { try { return wellKnownUrl(home.href) } catch { return undefined } } export async function verifyPublication( pub: PubInfo, pageUrl: string, options: VerifyOptions = {}, ): Promise { const probe = options.probe ?? probeWellKnown const page = parseUrl(pageUrl) const home = parseUrl(pub.record.url) const steps: VerifyStep[] = [] steps.push({ id: 'record', label: 'Publication record', detail: `${pub.record.name} was read from ${pub.pds}.`, status: 'pass', }) const sameOrigin = !!home && !!page && home.origin === page.origin steps.push({ id: 'home', label: 'Home address', detail: !home ? 'The record has no usable http(s) home address.' : sameOrigin ? `${home.origin} is this page's origin.` : `${home.origin} is not this page's origin (${page?.origin ?? 'unknown'}).`, status: sameOrigin ? 'pass' : 'fail', ...(home ? { href: home.href } : {}), }) const endpoint = home ? endpointFor(home) : undefined let answer: string | null = null if (home) { try { answer = await probe(home.href, options.force) } catch (err) { console.debug('[substandard] verification probe failed', home.href, err) answer = null } } steps.push({ id: 'well-known', label: 'Site well-known', detail: !endpoint ? 'There is no endpoint to ask.' : answer ? `The site answered with ${answer}.` : 'The site gave no publication answer.', status: answer ? 'pass' : 'fail', ...(endpoint ? { href: endpoint } : {}), }) const matched = !!answer && answer === pub.uri steps.push({ id: 'match', label: 'Record matches the answer', detail: matched ? 'The site names this exact record.' : answer ? `The site names ${answer}, not this record.` : 'There was nothing to compare.', status: matched ? 'pass' : 'fail', }) return { steps, verified: steps.every((step) => step.status === 'pass'), } }