app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

commit 0d9257a14e569339e87f91842677dadb9ee7b59d
parent 4fb0b806b2546c90d7029fdaeb46a372b9a7df68
Author: triesap <tyson@radroots.org>
Date:   Thu, 15 Oct 2026 05:12:22 +0000

web: Coordinate reviewed inbox access checks

- Bind original and persisted recovery checks to genuine source owners.
- Require one-use reviews for checks reconciliation and optional local cleanup.
- Preserve combined budgets real read retry and owned cleanup through source loss.
- Verify native uncertainty receive behavior current producers and browser controls.

Diffstat:
Aweb/src/lib/messaging/inbox-access-check.ts | 874+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/src/lib/messaging/inbox-access-controller.ts | 46++++++++++++++++++++++++++++++----------------
Mweb/src/lib/messaging/inbox-access-qualification.ts | 4++--
Aweb/tests/e2e/harness/inbox-access-check.ts | 683+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/e2e/inbox-access-check.spec.ts | 2365+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/provenance/output.test.mjs | 3+++
Aweb/tests/unit/inbox-access-check.test.ts | 97+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tools/check-output.mjs | 18+++++++++++++-----
8 files changed, 4067 insertions(+), 23 deletions(-)

diff --git a/web/src/lib/messaging/inbox-access-check.ts b/web/src/lib/messaging/inbox-access-check.ts @@ -0,0 +1,874 @@ +import type { RelayPolicy } from '../config/relays.ts'; +import { + inboxOperatorQualificationSnapshot, + inboxOperatorQualificationWire, + type InboxOperatorQualification +} from '../config/inbox-operators.ts'; +import { + identityMessagingOwnership, + type IdentitySession +} from '../runtime/identity-session.ts'; +import { + privateSessionOwnership, + subscribePrivateSessionClose, + type PrivateSession +} from '../runtime/private-session.ts'; +import { + privateStorageOwner, + type PrivateStorageRepository +} from '../persistence/private-storage.ts'; +import { canonicalLocalId } from '../private-handles.ts'; +import { + inboxAccessConstructionBinding, + inboxAccessControllerSourceSnapshot, + subscribeInboxAccessControllerClose, + closeInboxAccessController, + type InboxAccessController +} from './inbox-access-controller.ts'; +import { + reserveInboxProbeIdentity, + inboxProbeSnapshot, + stopInboxProbeIdentity, + type InboxProbeIdentity +} from '../nostr/inbox-access.ts'; +import { + captureInboxProbeSealOperation, + buildInboxProbeSeal, + stopInboxProbeSeal, + type InboxProbeSealOperation +} from '../nostr/seal-builder.ts'; +import { + buildInboxProbeGiftwrap, + type InboxProbeGiftwrap +} from '../nostr/giftwrap-builder.ts'; +import { + commitInboxProbe, + adoptRecoveredInboxProbe, + commitInboxProbeArchiveAttempt, + commitInboxProbeReadbackAttempt, + commitInboxProbeNonceVerification, + type InboxProbeAcknowledgement, + type InboxProbeNonceAcknowledgement +} from '../persistence/inbox-probes.ts'; +import { + captureInboxProbePublicationAction, + captureRecoveredInboxProbeRetryAction, + captureInboxProbePublication, + captureInboxProbeReadback, + type InboxProbePublicationAction +} from '../nostr/private-publisher.ts'; +import { + getPrivatePool, + closePrivatePool, + privatePoolCleanupRequired, + publishInboxProbeArchiveAttempt, + readInboxProbeAttempt, + inboxProbeArchiveAttemptEvidence, + inboxProbeReadbackAttemptEvidence, + type PrivatePool, + type InboxProbeArchiveAttemptResult, + type InboxProbeReadbackAttemptResult +} from '../nostr/private-pool.ts'; +import { + captureInboxProbeRecoveryReader, + recoverInboxProbe, + stopInboxProbeRecoveryReader, + captureInboxProbeNonceReader, + readInboxProbeNonce, + stopInboxProbeNonceReader, + captureRecoveredInboxProbeNonceReader, + readRecoveredInboxProbeNonce, + stopRecoveredInboxProbeNonceReader, + type InboxProbeRecoveryReader, + type RecoveredInboxProbe, + type InboxProbeNonceReader, + type RecoveredInboxProbeNonceReader, + type InboxProbeNonceProof, + type RecoveredInboxProbeNonceProof +} from '../nostr/self-recovery-reader.ts'; +import { + qualifyInboxAccess, + qualifyRemovedInboxAccess, + inboxAccessQualificationSnapshot, + stopInboxAccessQualification, + type InboxAccessQualification +} from './inbox-access-qualification.ts'; +import { + captureInboxProbeCleanup, + deleteReviewedInboxProbe, + stopInboxProbeCleanup, + type InboxProbeCleanup, + type InboxProbeRemovalAcknowledgement +} from '../persistence/inbox-probe-cleanup.ts'; + +declare const checkBrand: unique symbol; +export type InboxAccessCheck = Readonly<{ [checkBrand]: true }>; +declare const reviewBrand: unique symbol; +export type InboxAccessCheckReview = Readonly<{ [reviewBrand]: true }>; +export type InboxAccessCheckResult = Readonly<{ status: string }>; +type Mode = 'original' | 'recover'; +const disclosures = Object.freeze([ + 'This private inbox diagnostic may appear as a note to self or an extra event in other compatible clients.', + 'Relay operators may retain the diagnostic remotely after local removal. Local journal cleanup does not delete remote copies.', + 'After journal loss, removal or reload, the diagnostic uses ordinary bounded receive and retention costs and two-layer decryption before authenticated self-message rejection.', + 'This check is not an enquiry, reply, delivery receipt or proof that another person or client received a message.' +]); +type CheckSelection = Readonly<{ + kind: 'check'; + mode: Mode; + origin: string; + command?: string; +}>; +type Selection = CheckSelection | Readonly<{ kind: 'cleanup' | 'reconcile' }>; +type Snapshot = Readonly<{ + state: string; + busy: boolean; + cleanupRequired: boolean; + pendingSettlement: boolean; + sourceOnly: true; + mode?: Mode; + command?: string; + reason: string | null; +}>; +type Control = Readonly<{ + repository: PrivateStorageRepository; + identity: IdentitySession; + session: PrivateSession; + policy: RelayPolicy; + operator: InboxOperatorQualification; + current(): boolean; + snapshot(): Snapshot; + select( + mode: unknown, + origin: unknown, + command: unknown + ): InboxAccessCheckReview | undefined; + selectAction( + kind: 'cleanup' | 'reconcile' + ): InboxAccessCheckReview | undefined; + reviewCurrent(selection: Selection): boolean; + run(selection: CheckSelection): Promise<InboxAccessCheckResult>; + cleanup(review: unknown): Promise<InboxAccessCheckResult>; + reconcile(review: unknown): Promise<InboxAccessCheckResult>; + qualification(): InboxAccessQualification | undefined; + stop(): boolean; +}>; +const controls = new WeakMap<InboxAccessCheck, Control>(); +const currentChecks = new WeakMap<InboxAccessController, InboxAccessCheck>(); +const reviews = new WeakMap< + InboxAccessCheckReview, + Readonly<{ + control: Control; + selection: Selection; + }> +>(); +function applied(status: string) { + return status === 'saved' || status === 'existing' || status === 'reconciled'; +} + +// Capture is source metadata only. Actual C7 association precedes passive +// session observation; this coordinator owns the exact opaque operator binding. +export function createInboxAccessCheck( + repository: PrivateStorageRepository, + identity: IdentitySession, + session: PrivateSession, + policy: RelayPolicy, + access: InboxAccessController, + operator: InboxOperatorQualification +): InboxAccessCheck | undefined { + if (typeof window === 'undefined') return undefined; + try { + const source = inboxAccessConstructionBinding(access, session, policy); + if (!source || privateStorageOwner(repository) !== source.owner) + return undefined; + const publicOwner = identityMessagingOwnership(identity), + owner = privateSessionOwnership(session), + declaration = inboxOperatorQualificationSnapshot(operator), + operatorWire = inboxOperatorQualificationWire(operator, policy); + if ( + !publicOwner?.current() || + !owner?.current() || + publicOwner.owner !== source.owner || + owner.owner !== source.owner || + publicOwner.session !== source.session || + owner.session !== source.session || + !declaration || + declaration.authority === 'unconfigured' || + !operatorWire || + ![...source.readTargets, ...source.archiveTargets].every((origin) => { + const row = declaration.relays.find((row) => row.origin === origin); + return ( + row && + row.auth !== 'unsupported' && + row.recipientRead === 'supported' && + row.namedArchiveAck === 'supported' && + row.exactReadback === 'supported' && + row.retention === 'operator_defined' + ); + }) + ) + return undefined; + const prior = currentChecks.get(access), + previous = prior && controls.get(prior); + if (previous) + return previous.repository === repository && + previous.identity === identity && + previous.session === session && + previous.policy === policy && + previous.operator === operator && + previous.current() + ? prior + : undefined; + const token = Object.freeze({}) as InboxAccessCheck; + const cancellation = new AbortController(); + const archive = new Map<string, InboxProbeArchiveAttemptResult>(); + const listeners = new Map<symbol, () => void>(); + let stopped = false, + busy = false, + cleanupRequired = false, + state = 'not_checked', + reason: string | null = null, + mode: Mode | undefined, + readOrigin: string | undefined, + command: string | undefined, + selectedReview: InboxAccessCheckReview | undefined, + original: InboxProbeIdentity | undefined, + seal: InboxProbeSealOperation | undefined, + wrap: InboxProbeGiftwrap | undefined, + recovery: InboxProbeRecoveryReader | undefined, + recovered: RecoveredInboxProbe | undefined, + acknowledgement: InboxProbeAcknowledgement | undefined, + action: InboxProbePublicationAction | undefined, + pool: PrivatePool | undefined, + pendingArchive: + | Readonly<{ origin: string; result: InboxProbeArchiveAttemptResult }> + | undefined, + pendingRead: InboxProbeReadbackAttemptResult | undefined, + read: InboxProbeReadbackAttemptResult | undefined, + nonceReader: InboxProbeNonceReader | undefined, + recoveredNonceReader: RecoveredInboxProbeNonceReader | undefined, + nonceProof: + InboxProbeNonceProof | RecoveredInboxProbeNonceProof | undefined, + nonce: InboxProbeNonceAcknowledgement | undefined, + qualified: InboxAccessQualification | undefined, + cleanup: InboxProbeCleanup | undefined, + removal: InboxProbeRemovalAcknowledgement | undefined; + function current() { + if (stopped) return false; + const fresh = + source!.current() && + publicOwner!.current() && + owner!.current() && + inboxOperatorQualificationWire(operator, policy) === operatorWire; + if (!fresh && !stopped) { + revoke(); + closeOwn(); + } + return fresh && !stopped; + } + function qualification() { + return current() && + !busy && + !cleanupRequired && + qualified && + inboxAccessQualificationSnapshot(qualified) + ? qualified + : undefined; + } + function result(status: string): InboxAccessCheckResult { + reason = status === 'qualified' ? null : status; + state = + stopped || !current() + ? 'stopped' + : status === 'qualified' + ? 'qualified' + : 'needs_action'; + return { status }; + } + function observeCleanup() { + cleanupRequired = + !!pendingArchive || + !!pendingRead || + (!!pool && privatePoolCleanupRequired(pool)) || + !!inboxAccessControllerSourceSnapshot(access)?.cleanupRequired; + return !cleanupRequired; + } + function revoke() { + stopped = true; + state = 'stopped'; + cancellation.abort(); + if (selectedReview) reviews.delete(selectedReview); + selectedReview = undefined; + if (qualified) stopInboxAccessQualification(qualified); + qualified = undefined; + if (nonceReader) stopInboxProbeNonceReader(nonceReader); + if (recoveredNonceReader) + stopRecoveredInboxProbeNonceReader(recoveredNonceReader); + if (recovery) stopInboxProbeRecoveryReader(recovery); + if (seal) stopInboxProbeSeal(seal); + if (original) stopInboxProbeIdentity(original); + if (cleanup) stopInboxProbeCleanup(cleanup); + } + function closeOwn() { + let failed = busy; + if (pool) { + try { + closePrivatePool(pool); + } catch { + failed = true; + } + if (privatePoolCleanupRequired(pool)) failed = true; + } + cleanupRequired = failed || !!pendingArchive || !!pendingRead; + if (!cleanupRequired) + for (const [id, off] of listeners) { + off(); + listeners.delete(id); + } + return !cleanupRequired; + } + function stop() { + revoke(); + const own = closeOwn(); + const accessClosed = closeInboxAccessController(access); + cleanupRequired = !own || !accessClosed; + return !cleanupRequired; + } + function sourceClosed() { + revoke(); + // The source is already inside its close owner. Calling close() again + // would report its legitimate reentry as a permanent cleanup failure. + if (!closeOwn()) throw Error('inbox_access_check_cleanup_required'); + } + async function settle() { + if (pendingArchive) { + const pending = pendingArchive; + // Authentic admitted outcomes must reach the historical append owner + // even after Stop/head/session loss. No new traffic is authorized here. + const saved = await commitInboxProbeArchiveAttempt( + repository, + pending.result + ); + if (!applied(saved.status)) return saved.status; + if (pending.result.status === 'accepted') + archive.set(pending.origin, pending.result); + pendingArchive = undefined; + } + if (pendingRead) { + const pending = pendingRead; + const saved = await commitInboxProbeReadbackAttempt( + repository, + pending + ); + if (!applied(saved.status)) return saved.status; + read = pending; + pendingRead = undefined; + } + return 'saved'; + } + function selectReview(selection: Selection) { + if (selectedReview) reviews.delete(selectedReview); + const review = Object.freeze({}) as InboxAccessCheckReview; + reviews.set(review, { control, selection }); + selectedReview = review; + return review; + } + function retryNonceRead(status: string) { + if (status !== 'busy') return; + // A used reader permanently owns its actual read observation. An explicit + // Retry must spend a later real READ under the SAME publication action, + // rather than recapture from that observation or reset any attempt meter. + if (nonceReader) stopInboxProbeNonceReader(nonceReader); + if (recoveredNonceReader) + stopRecoveredInboxProbeNonceReader(recoveredNonceReader); + nonceReader = undefined; + recoveredNonceReader = undefined; + read = undefined; + } + const control: Control = { + repository, + identity, + session, + policy, + operator, + current, + qualification, + stop, + snapshot() { + const fresh = current(); + return { + state: !fresh + ? 'stopped' + : qualification() + ? 'qualified' + : state === 'qualified' + ? 'needs_action' + : state, + busy, + cleanupRequired, + pendingSettlement: !!pendingArchive || !!pendingRead, + sourceOnly: true, + mode, + command, + reason + }; + }, + select(selectedMode, origin, selectedCommand) { + if ( + !current() || + busy || + cleanupRequired || + (selectedMode !== 'original' && selectedMode !== 'recover') || + typeof origin !== 'string' || + !source.readTargets.includes(origin) || + (readOrigin !== undefined && readOrigin !== origin) || + (mode && mode !== selectedMode) + ) + return undefined; + const id = + selectedMode === 'recover' + ? canonicalLocalId(selectedCommand) + : undefined; + if ( + (selectedMode === 'recover' && !id) || + (selectedMode === 'original' && selectedCommand !== undefined) || + (command && selectedMode === 'recover' && id !== command) + ) + return undefined; + return selectReview({ + kind: 'check', + mode: selectedMode, + origin, + command: id + }); + }, + reviewCurrent(selection) { + if (busy) return false; + // Historical append is allowed only for an authentic already-admitted + // pending result. It neither restores live authority nor issues proof. + if (selection.kind === 'reconcile') + return !!pendingArchive || !!pendingRead; + return current() && !cleanupRequired; + }, + selectAction(kind) { + const selection = { kind }; + return control.reviewCurrent(selection) + ? selectReview(selection) + : undefined; + }, + async reconcile(consent) { + if (consent !== 'reviewed_inbox_access_check_reconciliation') + return { status: 'invalid' }; + if (busy) return { status: 'busy' }; + busy = true; + try { + const status = await settle(); + observeCleanup(); + return { status }; + } catch { + return { status: 'unknown_completion' }; + } finally { + busy = false; + } + }, + async run(selection) { + if (!current()) return { status: 'stopped' }; + if (busy || cleanupRequired) return { status: 'busy' }; + if (qualification()) return { status: 'qualified' }; + if (mode && mode !== selection.mode) return { status: 'invalid' }; + if (readOrigin !== undefined && readOrigin !== selection.origin) + return { status: 'invalid' }; + mode ??= selection.mode; + readOrigin ??= selection.origin; + busy = true; + state = 'working'; + try { + const settled = await settle(); + if (!current()) return result('stopped'); + if (!applied(settled)) return result(settled); + if (!acknowledgement) { + if (mode === 'original') { + if (!original) { + const reserved = await reserveInboxProbeIdentity( + repository, + session, + policy, + access, + operator, + 'reviewed_inbox_probe_reservation' + ); + if ('identity' in reserved) { + original = reserved.identity; + command = inboxProbeSnapshot(original)?.id; + } + if (!current()) return result('stopped'); + if (!original) return result(reserved.status); + } + if (!current() || !original) return result('stopped'); + if (!wrap) { + seal ??= captureInboxProbeSealOperation( + session, + original, + 'reviewed_inbox_probe_seal' + ); + if (!seal) return result('unavailable'); + const sealed = await buildInboxProbeSeal(seal); + if (!current()) return result('stopped'); + if (!('seal' in sealed)) return result(sealed.status); + wrap = buildInboxProbeGiftwrap( + sealed.seal, + 'reviewed_inbox_probe_wrap' + ); + if (!wrap || !current()) return result('stopped'); + } + const committed = await commitInboxProbe( + repository, + session, + wrap, + 'reviewed_inbox_probe_commit' + ); + if ('acknowledgement' in committed) + acknowledgement = committed.acknowledgement; + if (!current()) return result('stopped'); + if (!acknowledgement) return result(committed.status); + } else { + command ??= selection.command; + if (!recovery) + recovery = captureInboxProbeRecoveryReader( + repository, + identity, + session, + policy, + access, + operator, + command, + 'reviewed_inbox_probe_recovery' + ); + if (!recovery) return result('unavailable'); + if (!recovered) { + const found = await recoverInboxProbe(recovery); + if ('proof' in found) recovered = found.proof; + if (!current()) return result('stopped'); + if (!recovered) return result(found.status); + } + const adopted = await adoptRecoveredInboxProbe( + repository, + session, + access, + policy, + operator, + recovered, + 'reviewed_original_inbox_probe_retry' + ); + if ('acknowledgement' in adopted) + acknowledgement = adopted.acknowledgement; + if (!current()) return result('stopped'); + if (!acknowledgement) return result(adopted.status); + } + } + if (!current()) return result('stopped'); + if (!action) + action = + mode === 'original' + ? captureInboxProbePublicationAction( + repository, + session, + policy, + acknowledgement, + 'reviewed_inbox_probe_network' + ) + : await captureRecoveredInboxProbeRetryAction( + repository, + session, + policy, + acknowledgement, + 'reviewed_original_inbox_probe_retry' + ); + if (!current()) return result('stopped'); + if (!action) return result('unavailable'); + const targets = new Map<string, true>(); + for (const origin of [ + ...source.readTargets, + ...source.archiveTargets + ]) + targets.set(origin, true); + pool ??= getPrivatePool( + session, + policy, + Array.from(targets.keys()), + token, + 'access', + current + ); + if (!pool || !current()) return result('stopped'); + for (const origin of source.archiveTargets) { + if (!current()) return result('stopped'); + if (archive.has(origin)) continue; + const permission = captureInboxProbePublication( + repository, + session, + policy, + acknowledgement, + action, + origin, + 'reviewed_inbox_probe_publication' + ); + if (!permission) return result('unavailable'); + const attempt = await publishInboxProbeArchiveAttempt( + pool, + permission, + cancellation.signal + ); + if (inboxProbeArchiveAttemptEvidence(attempt)) + pendingArchive = { origin, result: attempt }; + const saved = await settle(); + if (!current()) return result('stopped'); + if (!applied(saved)) return result(saved); + if (attempt.status !== 'accepted' || !archive.has(origin)) + return result(attempt.status); + } + if (!read) { + if (!current()) return result('stopped'); + const permission = captureInboxProbeReadback( + repository, + session, + policy, + acknowledgement, + action, + selection.origin, + 'reviewed_inbox_probe_readback' + ); + if (!permission) return result('unavailable'); + const attempt = await readInboxProbeAttempt( + pool, + permission, + cancellation.signal + ); + if (inboxProbeReadbackAttemptEvidence(attempt)) + pendingRead = attempt; + const saved = await settle(); + if (!current()) return result('stopped'); + if (!applied(saved)) return result(saved); + if (attempt.status !== 'readback' || !read) + return result(attempt.status); + } + if (!nonceProof) { + if (!current()) return result('stopped'); + if (mode === 'original') { + nonceReader ??= captureInboxProbeNonceReader( + repository, + identity, + session, + policy, + read, + 'reviewed_inbox_probe_nonce_decryption' + ); + if (!nonceReader) return result('unavailable'); + const decrypted = await readInboxProbeNonce(nonceReader); + if ('proof' in decrypted) nonceProof = decrypted.proof; + if (!current()) return result('stopped'); + if (!nonceProof) { + retryNonceRead(decrypted.status); + return result(decrypted.status); + } + } else { + recoveredNonceReader ??= captureRecoveredInboxProbeNonceReader( + repository, + identity, + session, + policy, + read, + 'reviewed_inbox_probe_nonce_decryption' + ); + if (!recoveredNonceReader) return result('unavailable'); + const decrypted = + await readRecoveredInboxProbeNonce(recoveredNonceReader); + if ('proof' in decrypted) nonceProof = decrypted.proof; + if (!current()) return result('stopped'); + if (!nonceProof) { + retryNonceRead(decrypted.status); + return result(decrypted.status); + } + } + } + if (!nonce) { + if (!current()) return result('stopped'); + const saved = await commitInboxProbeNonceVerification( + repository, + session, + policy, + nonceProof + ); + if (saved.acknowledgement) nonce = saved.acknowledgement; + if (!current()) return result('stopped'); + if (!nonce) return result(saved.status); + } + const issued = await qualifyInboxAccess( + repository, + session, + policy, + access, + operator, + nonce, + [...archive.values(), read], + 'reviewed_qualify_inbox_access' + ); + if (!current()) { + if (issued.qualification) + stopInboxAccessQualification(issued.qualification); + return result('stopped'); + } + if (issued.qualification) qualified = issued.qualification; + return result(issued.status); + } catch { + return result(current() ? 'unknown_completion' : 'stopped'); + } finally { + busy = false; + if (stopped) observeCleanup(); + } + }, + async cleanup(consent) { + if (consent !== 'reviewed_delete_local_inbox_probe') + return { status: 'invalid' }; + if (!current()) return { status: 'stopped' }; + if (busy || cleanupRequired) return { status: 'busy' }; + if (removal) return { status: 'removed' }; + if (!nonce || !read) return { status: 'unavailable' }; + busy = true; + try { + cleanup = await captureInboxProbeCleanup( + repository, + session, + policy, + nonce, + [...archive.values(), read], + 'reviewed_inbox_probe_cleanup_selection' + ); + if (!cleanup || !current()) return result('unavailable'); + const deleted = await deleteReviewedInboxProbe(cleanup, consent); + // A real claim invalidates the prior journal proof even on abort. + if (qualified && !inboxAccessQualificationSnapshot(qualified)) + qualified = undefined; + if (deleted.acknowledgement) removal = deleted.acknowledgement; + if (!current()) return result('stopped'); + if (!removal) return result(deleted.status); + const issued = qualifyRemovedInboxAccess( + repository, + session, + policy, + access, + operator, + removal, + 'reviewed_qualify_inbox_access' + ); + if (issued.qualification) qualified = issued.qualification; + return result( + issued.status === 'qualified' ? 'removed' : issued.status + ); + } catch { + return result('unknown_completion'); + } finally { + busy = false; + } + } + }; + controls.set(token, control); + currentChecks.set(access, token); + listeners.set( + Symbol(), + subscribeInboxAccessControllerClose(access, sourceClosed) + ); + listeners.set( + Symbol(), + subscribePrivateSessionClose(session, sourceClosed) + ); + if (!current()) { + stop(); + return undefined; + } + return token; + } catch { + return undefined; + } +} + +export function inboxAccessCheckSnapshot(token: InboxAccessCheck) { + return controls.get(token)?.snapshot(); +} +export function inboxAccessCheckQualification(token: InboxAccessCheck) { + return controls.get(token)?.qualification(); +} +export function reviewInboxAccessCheck( + token: InboxAccessCheck, + mode: unknown, + origin: unknown, + command?: unknown +) { + return controls.get(token)?.select(mode, origin, command); +} +export function inboxAccessCheckReviewSnapshot(token: InboxAccessCheckReview) { + const saved = reviews.get(token); + return saved?.control.reviewCurrent(saved.selection) + ? { ...saved.selection, disclosures: disclosures.slice(), sourceOnly: true } + : undefined; +} +export function runInboxAccessCheck( + token: InboxAccessCheckReview, + review: unknown +): Promise<InboxAccessCheckResult> { + const saved = reviews.get(token); + if ( + !saved || + saved.selection.kind !== 'check' || + review !== 'reviewed_inbox_access_check' + ) + return Promise.resolve({ status: 'invalid' }); + reviews.delete(token); + return saved.control.run(saved.selection); +} +export function cleanupInboxAccessCheck( + token: InboxAccessCheckReview, + review: unknown +): Promise<InboxAccessCheckResult> { + const saved = reviews.get(token); + if ( + !saved || + saved.selection.kind !== 'cleanup' || + review !== 'reviewed_delete_local_inbox_probe' + ) + return Promise.resolve({ status: 'invalid' }); + reviews.delete(token); + return saved.control.cleanup(review); +} +export function reviewInboxAccessCheckCleanup(token: InboxAccessCheck) { + return controls.get(token)?.selectAction('cleanup'); +} +export function reviewInboxAccessCheckReconciliation(token: InboxAccessCheck) { + return controls.get(token)?.selectAction('reconcile'); +} +export function reconcileInboxAccessCheck( + token: InboxAccessCheckReview, + review: unknown +): Promise<InboxAccessCheckResult> { + const saved = reviews.get(token); + if ( + !saved || + saved.selection.kind !== 'reconcile' || + review !== 'reviewed_inbox_access_check_reconciliation' + ) + return Promise.resolve({ status: 'invalid' }); + reviews.delete(token); + return saved.control.reconcile(review); +} +export function stopInboxAccessCheck(token: InboxAccessCheck): boolean { + return controls.get(token)?.stop() ?? false; +} +export function retryInboxAccessCheckCleanup( + token: InboxAccessCheck, + review: unknown +): boolean { + return ( + review === 'reviewed_inbox_access_check_cleanup_retry' && + (controls.get(token)?.stop() ?? false) + ); +} diff --git a/web/src/lib/messaging/inbox-access-controller.ts b/web/src/lib/messaging/inbox-access-controller.ts @@ -38,15 +38,17 @@ type Binding = Readonly<{ readTargets: string[]; archiveTargets: string[]; }>; -export type InboxAccessSnapshot = Readonly< +export type InboxAccessSourceSnapshot = Readonly< Binding & { state: string; busy: boolean; cleanupRequired: boolean; - readiness: 'unavailable'; auth: { origin: string; state: string; responses: number }[]; } >; +export type InboxAccessSnapshot = Readonly< + InboxAccessSourceSnapshot & { readiness: 'unavailable' } +>; type Control = { session: PrivateSession; subscribe(listener: () => void): () => void; @@ -55,6 +57,7 @@ type Control = { current(): boolean; closed(): boolean; cleanupRequired(): boolean; + sourceSnapshot(): InboxAccessSourceSnapshot; snapshot(): InboxAccessSnapshot; authenticate(origin: unknown, review: unknown): Promise<string>; respond(origin: unknown, review: unknown): Promise<string>; @@ -268,6 +271,22 @@ export function createInboxAccessController( busy = false; } } + // Construction and qualification consume this source projection directly. + // Presentation may later observe qualification, so it cannot be their input. + const sourceSnapshot = (): InboxAccessSourceSnapshot => ({ + ...initial, + readTargets: [...initial.readTargets], + archiveTargets: [...initial.archiveTargets], + state: closed ? 'stopped' : current() ? phase : 'stale', + busy, + cleanupRequired: + closed && (!cleaned || (!!pool && privatePoolCleanupRequired(pool))), + auth: [...auth].map(([origin, action]) => ({ + origin, + state: inboxAuthSnapshot(action)?.state ?? 'stopped', + responses: inboxAuthSnapshot(action)?.responses ?? 0 + })) + }); controllers.set(token, { session, subscribe(listener) { @@ -295,20 +314,10 @@ export function createInboxAccessController( authenticate, respond, close, + sourceSnapshot, snapshot: () => ({ - ...initial, - readTargets: [...initial.readTargets], - archiveTargets: [...initial.archiveTargets], - state: closed ? 'stopped' : current() ? phase : 'stale', - busy, - cleanupRequired: - closed && (!cleaned || (!!pool && privatePoolCleanupRequired(pool))), - readiness: 'unavailable', - auth: [...auth].map(([origin, action]) => ({ - origin, - state: inboxAuthSnapshot(action)?.state ?? 'stopped', - responses: inboxAuthSnapshot(action)?.responses ?? 0 - })) + ...sourceSnapshot(), + readiness: 'unavailable' }) }); off = subscribePrivateSessionClose(session, () => { @@ -326,6 +335,11 @@ export function inboxAccessControllerSnapshot( ): InboxAccessSnapshot | undefined { return controllers.get(token)?.snapshot(); } +export function inboxAccessControllerSourceSnapshot( + token: InboxAccessController +): InboxAccessSourceSnapshot | undefined { + return controllers.get(token)?.sourceSnapshot(); +} export function authenticateInboxAccess( token: InboxAccessController, origin: unknown, @@ -369,7 +383,7 @@ export function inboxAccessConstructionBinding( return undefined; const ownership = privateSessionOwnership(session); if (!ownership?.current() || !state.current()) return undefined; - const captured = state.snapshot(); + const captured = state.sourceSnapshot(); if ( captured.owner !== ownership.owner || !state.current() || diff --git a/web/src/lib/messaging/inbox-access-qualification.ts b/web/src/lib/messaging/inbox-access-qualification.ts @@ -6,7 +6,7 @@ import { } from '../config/inbox-operators.ts'; import { inboxAccessConstructionBinding, - inboxAccessControllerSnapshot, + inboxAccessControllerSourceSnapshot, subscribeInboxAccessControllerClose, type InboxAccessController } from './inbox-access-controller.ts'; @@ -125,7 +125,7 @@ function makeScope( ) return undefined; function current() { - const snapshot = inboxAccessControllerSnapshot(access); + const snapshot = inboxAccessControllerSourceSnapshot(access); return ( actual!.current() && owner!.current() && diff --git a/web/tests/e2e/harness/inbox-access-check.ts b/web/tests/e2e/harness/inbox-access-check.ts @@ -0,0 +1,683 @@ +import { makeRetryFixture } from './inbox-probe-retry.ts'; +import { makeFixture as makeCheckCryptoFixture } from './approved-signing.ts'; +import { + withIdlePrivateLocalStorage, + createPrivateSession, + closePrivateSession, + type PrivateSession +} from '../../../src/lib/runtime/private-session.ts'; +import { + createIdentitySession, + connectIdentity, + probeIdentityMessaging, + disconnectIdentity +} from '../../../src/lib/runtime/identity-session.ts'; +import { + installHostedProvider, + openRecoveryContext, + validateRelayPolicy, + validateInboxOperatorQualification +} from './inbox-probe-recovery.ts'; +import { inboxOperatorQualificationSnapshot } from '../../../src/lib/config/inbox-operators.ts'; +import { + createPublicView, + beginPublicViewRun, + disposePublicView +} from '../../../src/lib/runtime/public-runtime.ts'; +import { + resolveInboxPreference, + settleInboxResolver +} from '../../../src/lib/messaging/resolve-inbox.ts'; +import { captureUnlockedSession } from '../../../src/lib/messaging/unlocked-session.ts'; +import { + captureInboxSync, + startInboxSync, + stopInboxSync, + inboxSyncSnapshot, + type InboxSync +} from '../../../src/lib/messaging/inbox-sync.ts'; +import { observeQualifiedInboxAccess } from '../../../src/lib/messaging/inbox-access-qualification.ts'; +import { + openBrowserDatabase, + closeBrowserDatabase, + browserDatabaseTransaction +} from '../../../src/lib/persistence/database.ts'; +import { + captureDecryptionQueue, + runDecryptionBatch, + decryptionQueueSnapshot, + decryptionQueueConversations, + stopDecryptionQueue +} from '../../../src/lib/messaging/decryption-queue.ts'; +import { + createInboxAccessCheck, + inboxAccessCheckSnapshot, + inboxAccessCheckQualification, + inboxAccessCheckReviewSnapshot, + reviewInboxAccessCheck, + runInboxAccessCheck, + cleanupInboxAccessCheck, + reviewInboxAccessCheckCleanup, + reviewInboxAccessCheckReconciliation, + reconcileInboxAccessCheck, + stopInboxAccessCheck, + retryInboxAccessCheckCleanup, + type InboxAccessCheck, + type InboxAccessCheckReview +} from '../../../src/lib/messaging/inbox-access-check.ts'; +import { + createInboxAccessController, + closeInboxAccessController +} from '../../../src/lib/messaging/inbox-access-controller.ts'; +export * from '../../../src/lib/messaging/inbox-access-check.ts'; +export * from '../../../src/lib/messaging/inbox-access-controller.ts'; +export * from '../../../src/lib/messaging/inbox-access-qualification.ts'; +export * from './inbox-probe-retry.ts'; +export { decodePrivateRecord } from '../../../src/lib/persistence/private-records.ts'; +export function otherCheckOwner() { + const fixture = makeCheckCryptoFixture(); + try { + return fixture.owner; + } finally { + fixture.close(); + } +} +export async function makeCheckFixture(authRequired = false) { + // Only inert host/context setup and open() are reused. The new coordinator + // must perform every preparation, network, nonce and qualification step. + const base = await makeRetryFixture(); + if (authRequired) { + const wire = inboxOperatorQualificationSnapshot(base.host.qualification)!; + const operator = validateInboxOperatorQualification( + JSON.stringify({ + ...wire, + relays: wire.relays.map((relay) => + relay.origin === 'wss://inbox.example.org' + ? { ...relay, auth: 'explicit_required' } + : relay + ) + }) + ); + if (!operator) throw Error('actual explicit AUTH operator declaration'); + base.host.qualification = operator; + } + let check: InboxAccessCheck | undefined; + let selected: InboxAccessCheckReview | undefined; + let sync: InboxSync | undefined; + const views: ReturnType<typeof createPublicView>[] = []; + const alternates = new Set<() => boolean>(); + return { + ...base, + check: () => check, + review: () => selected, + async open() { + await base.open(); + const a = base.actors(); + check = createInboxAccessCheck( + base.repository, + a.identity, + a.session, + base.host.policy, + a.access, + base.host.qualification + ); + if (!check) throw Error('actual source check capture'); + }, + snapshot: () => check && inboxAccessCheckSnapshot(check), + qualification: () => check && inboxAccessCheckQualification(check), + async alternateActors() { + const identity = createIdentitySession(); + await connectIdentity(identity); + await probeIdentityMessaging(identity, 'reviewed_self_copy'); + const session = await createPrivateSession( + identity, + 'reviewed_private_session' + ); + if (!session) throw Error('actual alternate private session missing'); + const view = createPublicView(base.sharedPublicRuntime()); + const resolver = resolveInboxPreference( + view, + beginPublicViewRun(view), + base.host.owner + ); + await settleInboxResolver(resolver); + const access = createInboxAccessController( + session, + base.host.policy, + resolver + ); + if (!access) throw Error('actual alternate C7 missing'); + function close() { + const c7 = closeInboxAccessController(access!); + const actor = closePrivateSession(session!); + disconnectIdentity(identity); + disposePublicView(view); + if (c7 && actor) alternates.delete(close); + return c7 && actor; + } + alternates.add(close); + return { identity, session, access, close }; + }, + async startOwnInbox() { + const qualified = check && inboxAccessCheckQualification(check); + if (!qualified) throw Error('actual coordinator qualification missing'); + const a = base.actors(), + view = createPublicView(base.sharedPublicRuntime()); + views.push(view); + const resolver = resolveInboxPreference( + view, + beginPublicViewRun(view), + base.host.owner + ); + await settleInboxResolver(resolver); + const unlocked = captureUnlockedSession( + a.session, + 'reviewed_messages_unlock' + ); + if (!unlocked) throw Error('actual unlocked scope missing'); + sync = captureInboxSync( + base.repository, + a.session, + base.host.policy, + resolver, + unlocked, + observeQualifiedInboxAccess(qualified), + 'reviewed_foreground_inbox' + ); + return !!sync && startInboxSync(sync, 'reviewed_foreground_inbox'); + }, + syncSnapshot: () => sync && inboxSyncSnapshot(sync), + stopOwnInbox: () => { + if (sync) stopInboxSync(sync); + }, + async decryptReceived() { + const a = base.actors(), + unlocked = captureUnlockedSession( + a.session, + 'reviewed_messages_unlock' + ); + if (!unlocked) throw Error('actual explicit unlock missing'); + const queue = captureDecryptionQueue( + base.repository, + a.session, + a.identity, + unlocked, + 'reviewed_inbox_unlock' + ); + if (!queue) throw Error('actual decryption queue missing'); + try { + const result = await runDecryptionBatch( + queue, + 'reviewed_decrypt_batch', + [] + ); + return { + result, + snapshot: decryptionQueueSnapshot(queue), + rooms: decryptionQueueConversations(queue).length + }; + } finally { + stopDecryptionQueue(queue); + } + }, + async replaceCheck(freshSession = false) { + if (freshSession) await base.freshSession(); + else await base.replaceController(); + const a = base.actors(); + check = createInboxAccessCheck( + base.repository, + a.identity, + a.session, + base.host.policy, + a.access, + base.host.qualification + ); + if (!check) throw Error('actual replacement check capture'); + selected = undefined; + }, + select( + mode: unknown = 'original', + origin: unknown = 'wss://inbox.example.org', + id?: unknown + ) { + if (!check) throw Error('actual coordinator missing'); + selected = reviewInboxAccessCheck(check, mode, origin, id); + return selected && inboxAccessCheckReviewSnapshot(selected); + }, + run(review: unknown = 'reviewed_inbox_access_check') { + if (!selected) throw Error('actual source review missing'); + return runInboxAccessCheck(selected, review); + }, + cleanup(review: unknown = 'reviewed_delete_local_inbox_probe') { + if (!selected) throw Error('actual cleanup review missing'); + return cleanupInboxAccessCheck(selected, review); + }, + selectCleanup() { + if (!check) throw Error('actual coordinator missing'); + selected = reviewInboxAccessCheckCleanup(check); + return selected && inboxAccessCheckReviewSnapshot(selected); + }, + selectReconciliation() { + if (!check) throw Error('actual coordinator missing'); + selected = reviewInboxAccessCheckReconciliation(check); + return selected && inboxAccessCheckReviewSnapshot(selected); + }, + reconcile(review: unknown = 'reviewed_inbox_access_check_reconciliation') { + if (!selected) throw Error('actual reconciliation review missing'); + return reconcileInboxAccessCheck(selected, review); + }, + stop: () => !!check && stopInboxAccessCheck(check), + retryCleanup: () => + !!check && + retryInboxAccessCheckCleanup( + check, + 'reviewed_inbox_access_check_cleanup_retry' + ), + close() { + let failedAlternate = false; + for (const close of alternates) if (!close()) failedAlternate = true; + if (failedAlternate) throw Error('private_auth_cleanup_required'); + if (sync) stopInboxSync(sync); + for (const view of views) disposePublicView(view); + if (check && !stopInboxAccessCheck(check)) + throw Error('inbox_access_check_cleanup_required'); + base.close(); + } + }; +} + +export function checkNativeLoss( + phase: 'append' | 'nonce' | 'qualification', + lose: () => void +) { + const cursor = Object.getOwnPropertyDescriptor( + IDBIndex.prototype, + 'openCursor' + )!.value as IDBIndex['openCursor']; + const put = Object.getOwnPropertyDescriptor(IDBObjectStore.prototype, 'put')! + .value as IDBObjectStore['put']; + let hits = 0; + const nonce = + phase === 'nonce' + ? atCheckNonce(async () => { + armed = true; + await Promise.resolve(); + }) + : undefined; + let armed = false; + const lockRequest = Object.getOwnPropertyDescriptor( + LockManager.prototype, + 'request' + )!.value as LockManager['request']; + if (phase === 'qualification') { + navigator.locks.request = function ( + this: LockManager, + ...args: Parameters<LockManager['request']> + ) { + // Observe the REAL qualification owner's lock admission; native event + // callbacks intentionally do not retain their original async call stack. + if (/qualifyInboxAccess/.test(new Error().stack ?? '')) armed = true; + return lockRequest.apply(this, args); + } as LockManager['request']; + } + IDBIndex.prototype.openCursor = function ( + ...args: Parameters<IDBIndex['openCursor']> + ) { + const request = cursor.apply(this, args); + if ( + this.objectStore.name === 'private_sends' && + this.objectStore.transaction.mode === 'readonly' && + ((phase === 'nonce' && armed) || (phase === 'qualification' && armed)) + ) { + request.addEventListener('success', () => { + if (!hits && !request.result) { + hits++; + lose(); + } + }); + } + return request; + }; + IDBObjectStore.prototype.put = function ( + ...args: Parameters<IDBObjectStore['put']> + ) { + const request = put.apply(this, args); + const value = args[0] as { wire?: unknown }; + if ( + !hits && + phase === 'append' && + this.name === 'private_sends' && + typeof value?.wire === 'string' + ) { + const record = JSON.parse(value.wire) as { + family?: string; + receipts?: { role: string }[]; + }; + if ( + record.family === 'private_access_probe' && + record.receipts?.at(-1)?.role === 'access_archive' + ) { + request.addEventListener('success', () => { + if (!hits) { + hits++; + lose(); + } + }); + } + } + return request; + }; + return { + hits: () => hits, + restore() { + nonce?.restore(); + if (phase === 'qualification') navigator.locks.request = lockRequest; + IDBIndex.prototype.openCursor = cursor; + IDBObjectStore.prototype.put = put; + } + }; +} + +export function failCheckPreparationNative( + phase: 'R0' | 'R1', + mode: 'abort' | 'readback' | 'uncertain_readback' +) { + const put = Object.getOwnPropertyDescriptor(IDBObjectStore.prototype, 'put')! + .value as IDBObjectStore['put']; + const abort = Object.getOwnPropertyDescriptor( + IDBTransaction.prototype, + 'abort' + )!.value as IDBTransaction['abort']; + const cursor = Object.getOwnPropertyDescriptor( + IDBIndex.prototype, + 'openCursor' + )!.value as IDBIndex['openCursor']; + let selected: IDBTransaction | undefined, + candidate: { owner: string; id: string; wire: string } | undefined; + let puts = 0, + completes = 0, + aborts = 0, + abortRefusals = 0, + readbackAborts = 0; + IDBObjectStore.prototype.put = function ( + ...args: Parameters<IDBObjectStore['put']> + ) { + const request = put.apply(this, args), + value = args[0] as { owner?: unknown; id?: unknown; wire?: unknown }; + if ( + !selected && + this.name === 'private_sends' && + typeof value?.wire === 'string' + ) { + const record = JSON.parse(value.wire) as { + family?: string; + self?: unknown; + receipts?: unknown[]; + }; + if ( + record.family === 'private_access_probe' && + record.receipts?.length === 0 && + (phase === 'R0' ? record.self === null : record.self !== null) + ) { + if (typeof value.owner !== 'string' || typeof value.id !== 'string') + throw Error('actual native preparation identity missing'); + candidate = { owner: value.owner, id: value.id, wire: value.wire }; + selected = this.transaction; + selected.addEventListener('complete', () => { + completes++; + }); + selected.addEventListener('abort', () => { + aborts++; + }); + request.addEventListener('success', () => { + puts++; + if (mode === 'abort') selected!.abort(); + }); + if (mode === 'uncertain_readback') + throw Error('actual admitted native write uncertainty'); + } + } + return request; + }; + IDBTransaction.prototype.abort = function ( + ...args: Parameters<IDBTransaction['abort']> + ) { + if (this === selected && mode === 'uncertain_readback') { + abortRefusals++; + throw Error('actual admitted native abort refusal'); + } + return abort.apply(this, args); + }; + IDBIndex.prototype.openCursor = function ( + ...args: Parameters<IDBIndex['openCursor']> + ) { + const request = cursor.apply(this, args); + if ( + mode !== 'abort' && + puts && + !readbackAborts && + this.objectStore.name === 'private_sends' && + this.objectStore.transaction.mode === 'readonly' + ) { + readbackAborts++; + this.objectStore.transaction.abort(); + } + return request; + }; + return { + counts: () => ({ puts, completes, aborts, abortRefusals, readbackAborts }), + candidate: () => candidate, + restore() { + IDBObjectStore.prototype.put = put; + IDBTransaction.prototype.abort = abort; + IDBIndex.prototype.openCursor = cursor; + } + }; +} + +export function atCheckNonce(work: () => Promise<void>) { + const original = Object.getOwnPropertyDescriptor( + LockManager.prototype, + 'request' + )!.value as LockManager['request']; + let hits = 0; + navigator.locks.request = async function ( + this: LockManager, + ...args: Parameters<LockManager['request']> + ) { + if ( + !hits && + /read(?:Recovered)?InboxProbeNonce/.test(new Error().stack ?? '') + ) { + hits++; + await work(); + } + return original.apply(this, args); + } as LockManager['request']; + return { + hits: () => hits, + restore: () => { + navigator.locks.request = original; + } + }; +} + +export async function changeCheckNativeRow( + row: { owner: string; id: string; wire: string }, + mode: 'missing' | 'corrupt' | 'alias' | 'foreign' +) { + const opened = await openBrowserDatabase(); + if (opened.state !== 'ready') + throw Error('actual mutation database unavailable'); + try { + const tx = browserDatabaseTransaction( + opened.owner, + ['private_sends'], + 'readwrite' + ), + store = tx.objectStore('private_sends'); + if (mode === 'missing') store.delete([row.owner, row.id]); + else if (mode === 'corrupt') store.put({ ...row, wire: row.wire + 'x' }); + else if (mode === 'alias') store.put({ ...row, id: crypto.randomUUID() }); + else + store.put({ + ...row, + owner: row.owner === 'f'.repeat(64) ? 'e'.repeat(64) : 'f'.repeat(64) + }); + await new Promise<void>((resolve, reject) => { + tx.addEventListener('complete', () => resolve()); + tx.addEventListener('abort', () => + reject(Error('actual fixture mutation aborted')) + ); + }); + } finally { + closeBrowserDatabase(opened.owner); + } +} + +export function abortCheckCleanup() { + const remove = Object.getOwnPropertyDescriptor( + IDBObjectStore.prototype, + 'delete' + )!.value as IDBObjectStore['delete']; + let aborts = 0, + selected = false; + IDBObjectStore.prototype.delete = function ( + ...args: Parameters<IDBObjectStore['delete']> + ) { + const request = remove.apply(this, args); + if (!selected && this.name === 'private_sends') { + selected = true; + const tx = this.transaction; + tx.addEventListener('abort', () => { + aborts++; + }); + request.addEventListener('success', () => tx.abort()); + } + return request; + }; + return { + aborts: () => aborts, + restore: () => { + IDBObjectStore.prototype.delete = remove; + } + }; +} + +// Hold an ACTUAL competing owner WebLock only when the real nonce reader first +// requests admission. No callback receives a fabricated lock or idle result. +export function holdCheckNonceOwnerLock(holds: 1 | 2 = 1) { + return holdCheckNonceAdmission(undefined, holds); +} +export async function holdCheckOwnerLock(owner: string) { + let release = () => {}, + enter = () => {}; + const entered = new Promise<void>((resolve) => { + enter = resolve; + }); + const held = navigator.locks.request( + 'harvestcircle:owner:' + owner, + async (lock) => { + if (!lock) throw Error('actual fixture owner lock missing'); + enter(); + await new Promise<void>((resolve) => { + release = resolve; + }); + } + ); + await entered; + return async () => { + release(); + await held; + }; +} +export function holdCheckNonceSdk(session: PrivateSession, holds: 1 | 2 = 1) { + return holdCheckNonceAdmission(session, holds); +} +function holdCheckNonceAdmission( + session: PrivateSession | undefined, + holds: 1 | 2 +) { + const original = Object.getOwnPropertyDescriptor( + LockManager.prototype, + 'request' + )!.value as LockManager['request']; + let release: (() => void) | undefined, + holder: Promise<unknown> | undefined, + hits = 0; + navigator.locks.request = async function ( + this: LockManager, + ...args: Parameters<LockManager['request']> + ) { + if ( + hits < holds && + /read(?:Recovered)?InboxProbeNonce/.test(new Error().stack ?? '') + ) { + hits++; + await new Promise<void>((ready) => { + if (session) { + holder = withIdlePrivateLocalStorage(session, async () => { + ready(); + await new Promise<void>((resolve) => { + release = resolve; + }); + }); + return; + } + holder = original.call( + this, + args[0], + { mode: 'exclusive' }, + async (lock) => { + if (!lock) throw Error('actual nonce-stage competing lock missing'); + ready(); + await new Promise<void>((resolve) => { + release = resolve; + }); + } + ); + }); + } + return original.apply(this, args); + } as LockManager['request']; + return { + hits: () => hits, + async release() { + release?.(); + await holder; + }, + restore() { + navigator.locks.request = original; + } + }; +} + +// A new child realm uses only the parent extension's ephemeral closure and +// genuine native storage. No predecessor preparation or proof is imported. +export async function openHostedCheck(setup: { + owner: string; + policyWire: string; + operatorWire: string; + command: string; +}) { + installHostedProvider(); + const policy = validateRelayPolicy(setup.policyWire), + operator = validateInboxOperatorQualification(setup.operatorWire); + if (!policy || !operator) throw Error('actual hosted policy and operator'); + const context = await openRecoveryContext( + setup.owner, + policy, + operator, + setup.command + ); + const check = createInboxAccessCheck( + context.repository, + context.identity, + context.session, + policy, + context.access, + operator + ); + if (!check) throw Error('actual hosted coordinator capture'); + return { context, check }; +} diff --git a/web/tests/e2e/inbox-access-check.spec.ts b/web/tests/e2e/inbox-access-check.spec.ts @@ -0,0 +1,2365 @@ +import { test, expect, type Page } from '@playwright/test'; +import { build } from 'vite'; +import { once } from 'node:events'; +import { fileURLToPath } from 'node:url'; +import { WebSocketServer, type WebSocket } from 'ws'; +import { createStaticHarness } from '../integration/harness/static.ts'; +import type * as Fixture from './harness/inbox-access-check.ts'; +import type { PrivateAccessProbe } from '../../src/lib/persistence/private-records.ts'; +function probeRow(wire: string) { + return JSON.parse(wire) as PrivateAccessProbe; +} +declare global { + interface Window { + hcCheck: typeof Fixture; + hcCheckFixture: Awaited<ReturnType<typeof Fixture.makeCheckFixture>>; + hcCheckPending: ReturnType<typeof Fixture.runInboxAccessCheck>; + hcCheckCloseCalls: number; + hcHostedCheck: Awaited<ReturnType<typeof Fixture.openHostedCheck>>; + hcCheckRelease?: () => Promise<void>; + hcCheckLockReady?: Promise<void>; + hcCheckAuthPending?: Promise<string>; + hcCheckRestore?: () => void; + hcCheckOther?: Awaited< + ReturnType<Window['hcCheckFixture']['alternateActors']> + >; + hcCheckCloseMode?: 'hold' | 'fail'; + hcCheckHeldClose?: (() => void)[]; + } +} +let server: Awaited<ReturnType<typeof createStaticHarness>>, + sockets: WebSocketServer, + endpoint: string, + bundle: string, + head = ''; +let holdRead = false; +let wireFault: + 'normal' | 'refused' | 'wrong_ack' | 'disconnect' | 'missing' | 'wrong_read' = + 'normal'; +let holdArchive = false; +let releaseArchive: (() => void) | undefined; +let challenge = false; +let authMode: 'accept' | 'refuse' | 'hold' = 'accept'; +let releaseAuth: (() => void) | undefined; +const physical = new Set<WebSocket>(), + physicalOrigins = new WeakMap<WebSocket, string>(), + archived = new Map<string, unknown>(); +const frames: { origin: string; type: string; id?: string }[] = []; +const origins = [ + 'wss://inbox.example.org', + 'wss://peer.example.org', + 'wss://archive.example.org' +]; +test.beforeAll(async () => { + server = await createStaticHarness(); + sockets = new WebSocketServer({ + host: '127.0.0.1', + port: 0, + maxPayload: 65536 + }); + await once(sockets, 'listening'); + const address = sockets.address(); + if (!address || typeof address === 'string') + throw Error('actual loopback endpoint'); + endpoint = 'ws://127.0.0.1:' + address.port; + sockets.on('connection', (socket, request) => { + physical.add(socket); + socket.on('close', () => physical.delete(socket)); + const origin = new URL(request.url!, 'http://127.0.0.1').searchParams.get( + 'origin' + )!; + physicalOrigins.set(socket, origin); + if (challenge && origin === 'wss://inbox.example.org') + socket.send(JSON.stringify(['AUTH', 's5c-actual-source-challenge'])); + socket.on('message', (bytes) => { + const raw = Buffer.isBuffer(bytes) + ? bytes + : Array.isArray(bytes) + ? Buffer.concat(bytes) + : Buffer.from(bytes); + const frame = JSON.parse(raw.toString()) as unknown[]; + if (frames.length >= 10000) + throw Error('bounded source fixture frame log'); + if (frame[0] === 'REQ') { + const filter = frame[2] as { kinds?: number[]; ids?: string[] }; + frames.push({ origin, type: 'REQ', id: filter.ids?.[0] }); + if (filter.kinds?.includes(10050) && head) + socket.send(JSON.stringify(['EVENT', frame[1], JSON.parse(head)])); + if (filter.kinds?.includes(1059) && filter.ids) { + if (holdRead) return; + const event = archived.get(JSON.stringify([origin, filter.ids[0]])); + if (event && wireFault !== 'missing') + socket.send( + JSON.stringify([ + 'EVENT', + frame[1], + wireFault === 'wrong_read' + ? { ...event, id: 'f'.repeat(64) } + : event + ]) + ); + } + if (filter.kinds?.includes(1059) && !filter.ids) { + for (const [key, event] of archived) { + if ((JSON.parse(key) as [string, string])[0] === origin) + socket.send(JSON.stringify(['EVENT', frame[1], event])); + } + } + socket.send(JSON.stringify(['EOSE', frame[1]])); + } else if (frame[0] === 'AUTH') { + const event = frame[1] as { id: string }; + frames.push({ origin, type: 'AUTH', id: event.id }); + const acknowledge = () => + socket.send( + JSON.stringify([ + 'OK', + event.id, + authMode !== 'refuse', + 'controlled actual auth response' + ]) + ); + if (authMode === 'hold') releaseAuth = acknowledge; + else acknowledge(); + } else if (frame[0] === 'EVENT') { + const event = frame[1] as { id: string }; + frames.push({ origin, type: 'EVENT', id: event.id }); + archived.set(JSON.stringify([origin, event.id]), event); + const acknowledge = () => { + if (wireFault === 'disconnect') { + socket.close(); + return; + } + socket.send( + JSON.stringify([ + 'OK', + wireFault === 'wrong_ack' ? 'f'.repeat(64) : event.id, + wireFault !== 'refused', + 'controlled actual named acceptance' + ]) + ); + if (wireFault === 'wrong_ack') socket.close(); + }; + if (holdArchive) releaseArchive = acknowledge; + else acknowledge(); + } + }); + }); + const result = await build({ + configFile: false, + logLevel: 'silent', + build: { + write: false, + minify: false, + lib: { + entry: fileURLToPath( + new URL('./harness/inbox-access-check.ts', import.meta.url) + ), + name: 'hcCheck', + formats: ['iife'] + } + } + }); + const output = Array.isArray(result) ? result[0] : result; + if (!('output' in output)) throw Error('actual compiled check harness'); + const chunks = output.output.filter((item) => item.type === 'chunk'); + expect(chunks).toHaveLength(1); + bundle = chunks[0].code; +}); +test.afterAll(async () => { + for (const socket of sockets.clients) socket.terminate(); + await new Promise<void>((resolve) => sockets.close(() => resolve())); + await server.close(); +}); +test.beforeEach(async ({ page }, testInfo) => { + await expect.poll(() => physical.size).toBe(0); + head = ''; + archived.clear(); + frames.length = 0; + holdRead = false; + wireFault = 'normal'; + holdArchive = false; + releaseArchive = undefined; + challenge = false; + authMode = 'accept'; + releaseAuth = undefined; + await page.addInitScript( + ({ endpoint, origins }) => { + const Native = window.WebSocket; + window.hcCheckCloseCalls = 0; + const close = Object.getOwnPropertyDescriptor(Native.prototype, 'close')! + .value as WebSocket['close']; + window.WebSocket = class extends Native { + constructor(url: string | URL, protocols?: string | string[]) { + const origin = new URL(String(url)).origin; + if (!['wss://discovery.example.org', ...origins].includes(origin)) + throw Error('unapproved fixture origin'); + super(endpoint + '/?origin=' + encodeURIComponent(origin), protocols); + } + close(...args: Parameters<WebSocket['close']>) { + window.hcCheckCloseCalls++; + if (window.hcCheckCloseMode === 'fail') + throw Error('actual controlled physical close refusal'); + if (window.hcCheckCloseMode === 'hold') { + (window.hcCheckHeldClose ??= []).push(() => + close.apply(this, args) + ); + return; + } + return close.apply(this, args); + } + }; + }, + { endpoint, origins } + ); + await page.goto(server.url + '/search'); + await page.addScriptTag({ content: bundle }); + head = await page.evaluate( + async ({ origins, authRequired }) => { + window.hcCheckFixture = + await window.hcCheck.makeCheckFixture(authRequired); + return window.hcCheckFixture.host.preferenceWire(origins); + }, + { origins, authRequired: testInfo.title.includes('required AUTH') } + ); + await page.evaluate(() => window.hcCheckFixture.open()); +}); +test.afterEach(async ({ page }) => { + if (page.isClosed()) return; + const closed = await page.evaluate(async () => { + await window.hcCheckRelease?.(); + window.hcCheckRelease = undefined; + window.hcCheckRestore?.(); + window.hcCheckRestore = undefined; + window.hcCheckCloseMode = undefined; + for (const release of window.hcCheckHeldClose ?? []) release(); + window.hcCheckHeldClose = []; + try { + window.hcCheckFixture.close(); + return true; + } catch (error) { + if ( + error instanceof Error && + [ + 'inbox_access_check_cleanup_required', + 'private_inbox_cleanup_required', + 'private_history_cleanup_required', + 'private_auth_cleanup_required', + 'private_pool_close_pending', + 'private_pool_close_failed' + ].includes(error.message) + ) + return false; + throw error; + } + }); + if (!closed) { + await expect + .poll( + () => + [...physical].filter( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ).length + ) + .toBe(0); + await page.evaluate(() => window.hcCheckFixture.close()); + } +}); +test('cold original check shows source disclosure with no preparation or provider work before review', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + before = f.host.counts(), + rowsBefore = await f.host.stored(); + const disclosure = f.select(); + const invalid = await f.run('not_reviewed'); + return { + before, + after: f.host.counts(), + rowsBefore: rowsBefore.length, + rowsAfter: (await f.host.stored()).length, + disclosure, + invalid: invalid.status, + state: f.snapshot() + }; + }); + expect(result.rowsBefore).toBe(0); + expect(result.rowsAfter).toBe(0); + expect(result.after).toEqual(result.before); + expect(result.invalid).toBe('invalid'); + expect(JSON.stringify(result.disclosure)).toMatch(/note to self|self.note/i); + expect(JSON.stringify(result.disclosure)).toMatch( + /remote retention|relay.*retain/i + ); + expect(JSON.stringify(result.disclosure)).toMatch(/receive|decrypt/i); + expect(result.state?.sourceOnly).toBe(true); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); +}); +test('cold original coordinator performs actual journal archive read nonce and source qualification', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture; + f.select(); + const before = f.host.counts(), + result = await f.run(); + return { + status: result.status, + before, + after: f.host.counts(), + snapshot: f.snapshot(), + rows: await f.host.stored(), + qualification: + f.qualification() && + window.hcCheck.inboxAccessQualificationSnapshot(f.qualification()!) + }; + }); + expect(result.status).toBe('qualified'); + expect(result.rows).toHaveLength(1); + const row = JSON.parse(result.rows[0].wire) as { + family: string; + self: { eventId: string }; + receipts: { role: string; status: string }[]; + }; + expect(row.family).toBe('private_access_probe'); + expect( + row.receipts.filter( + (fact) => fact.role === 'access_archive' && fact.status === 'accepted' + ) + ).toHaveLength(3); + expect(row.receipts.some((fact) => fact.status === 'nonce_verified')).toBe( + true + ); + expect( + frames.filter((frame) => frame.type === 'EVENT').map((frame) => frame.id) + ).toEqual([row.self.eventId, row.self.eventId, row.self.eventId]); + expect( + new Set( + frames + .filter((frame) => frame.type === 'EVENT') + .map((frame) => frame.origin) + ).size + ).toBe(3); + expect(result.qualification?.sourceOnly).toBe(true); + expect(result.after.signs - result.before.signs).toBe(1); +}); +test('copied review never authorizes work and a consumed review cannot replay', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture; + f.select(); + const before = f.host.counts(), + copied = await window.hcCheck.runInboxAccessCheck( + { ...f.review()! }, + 'reviewed_inbox_access_check' + ); + const afterCopy = f.host.counts(), + actual = await f.run(), + replay = await f.run(); + return { + before, + afterCopy, + copied: copied.status, + actual: actual.status, + replay: replay.status + }; + }); + expect(result.copied).toBe('invalid'); + expect(result.afterCopy).toEqual(result.before); + expect(result.actual).toBe('qualified'); + expect(result.replay).toBe('invalid'); +}); +test('fresh explicit successful check review reuses genuine current qualification without new effects', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture; + f.select(); + const first = await f.run(), + qualification = f.qualification(), + before = f.host.counts(); + f.select(); + const second = await f.run(); + return { + first: first.status, + second: second.status, + same: qualification === f.qualification(), + before, + after: f.host.counts(), + rows: (await f.host.stored()).length + }; + }); + expect(result.first).toBe('qualified'); + expect(result.second).toBe('qualified'); + expect(result.same).toBe(true); + expect(result.after).toEqual(result.before); + expect(result.rows).toBe(1); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(3); +}); + +test('source review regression: another requested read origin cannot inherit cached qualification', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture; + f.select(); + const first = await f.run(), + before = f.host.counts(); + const changed = f.select('original', 'wss://archive.example.org'); + return { first: first.status, changed, before, after: f.host.counts() }; + }); + expect(result.first).toBe('qualified'); + expect(result.changed).toBeUndefined(); + expect(result.after).toEqual(result.before); +}); + +test('source review regression: bare coordinator cannot bypass reviewed cleanup selection', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + api = window.hcCheck; + f.select(); + const first = await f.run(), + before = f.host.counts(); + const cleanup = await api.cleanupInboxAccessCheck( + f.check()! as unknown as Parameters< + typeof api.cleanupInboxAccessCheck + >[0], + 'reviewed_delete_local_inbox_probe' + ); + return { + first: first.status, + cleanup: cleanup.status, + rows: (await f.host.stored()).length, + before, + after: f.host.counts() + }; + }); + expect(result.first).toBe('qualified'); + expect(result.cleanup).toBe('invalid'); + expect(result.rows).toBe(1); + expect(result.after).toEqual(result.before); +}); + +test('source review regression: bare coordinator cannot bypass reviewed reconciliation selection', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + api = window.hcCheck, + before = f.host.counts(); + const reconciled = await api.reconcileInboxAccessCheck( + f.check()! as unknown as Parameters< + typeof api.reconcileInboxAccessCheck + >[0], + 'reviewed_inbox_access_check_reconciliation' + ); + return { + status: reconciled.status, + before, + after: f.host.counts(), + rows: (await f.host.stored()).length + }; + }); + expect(result.status).toBe('invalid'); + expect(result.rows).toBe(0); + expect(result.after).toEqual(result.before); +}); + +test('source review regression: actual head loss closes the owned retained transport scope', async ({ + page +}) => { + holdRead = true; + await page.evaluate(() => { + const f = window.hcCheckFixture; + f.select(); + window.hcCheckPending = f.run(); + }); + await expect + .poll(() => frames.some((frame) => frame.type === 'REQ' && !!frame.id)) + .toBe(true); + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + before = window.hcCheckCloseCalls; + f.actors().churnHead(); + const snapshot = f.snapshot(), + after = window.hcCheckCloseCalls; + const completed = await window.hcCheckPending; + return { + before, + after, + snapshot, + status: completed.status, + qualified: !!f.qualification() + }; + }); + expect(result.status).toBe('stopped'); + expect(result.qualified).toBe(false); + expect(result.snapshot?.state).toBe('stopped'); + expect(result.after).toBeGreaterThan(result.before); + await expect + .poll( + () => + [...physical].filter( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ).length + ) + .toBe(0); +}); + +test('source review regression: explicit retry after actual nonce owner contention makes a later genuine read', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + held = window.hcCheck.holdCheckNonceOwnerLock(); + try { + f.select(); + const first = await f.run(), + command = f.snapshot()?.command; + await held.release(); + const before = f.host.counts(); + f.select(); + const second = await f.run(); + return { + first: first.status, + second: second.status, + command, + afterCommand: f.snapshot()?.command, + hits: held.hits(), + before, + after: f.host.counts(), + rows: (await f.host.stored()).length + }; + } finally { + await held.release(); + held.restore(); + } + }); + expect(result.hits).toBe(1); + expect(result.first).toBe('busy'); + expect(result.second).toBe('qualified'); + expect(result.afterCommand).toBe(result.command); + expect(result.rows).toBe(1); + expect(result.after.signs).toBe(result.before.signs); + expect(result.after.encrypts).toBe(result.before.encrypts); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(3); + expect( + frames.filter((frame) => frame.type === 'REQ' && !!frame.id) + ).toHaveLength(2); +}); + +async function seedRecovery(page: Page, freshSession = false) { + const original = await page.evaluate(async () => { + const f = window.hcCheckFixture; + f.select(); + const completed = await f.run(), + command = f.snapshot()?.command, + row = (await f.host.stored()).find( + (row) => row.id === command && row.owner === f.host.owner + ); + if (completed.status !== 'qualified' || !row) + throw Error('actual coordinator seed failed'); + if (!command) throw Error('actual coordinator command missing'); + f.stop(); + return { command, row }; + }); + await expect + .poll( + () => + [...physical].filter( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ).length + ) + .toBe(0); + await page.evaluate(async (freshSession) => { + const f = window.hcCheckFixture; + if (!f.retryCleanup()) throw Error('actual ended scope cleanup incomplete'); + await f.replaceCheck(freshSession); + }, freshSession); + return original; +} + +for (const freshSession of [false, true]) { + test(`persisted recovery through ${freshSession ? 'fresh session' : 'replacement controller'} uses exact original artifact without new signing or encryption`, async ({ + page + }) => { + const original = await seedRecovery(page, freshSession); + const result = await page.evaluate(async ({ command }) => { + const f = window.hcCheckFixture, + before = f.host.counts(); + const disclosure = f.select( + 'recover', + 'wss://inbox.example.org', + command + ); + const result = await f.run(); + return { + disclosure, + status: result.status, + before, + after: f.host.counts(), + row: (await f.host.stored())[0], + snapshot: f.snapshot(), + qualified: !!f.qualification() + }; + }, original); + expect(result.disclosure?.kind).toBe('check'); + if (!result.disclosure || !('mode' in result.disclosure)) + throw Error('actual check review metadata missing'); + expect(result.disclosure.mode).toBe('recover'); + expect(result.status).toBe('qualified'); + expect(result.qualified).toBe(true); + expect(result.snapshot?.command).toBe(original.command); + const before = probeRow(original.row.wire), + after = probeRow(result.row.wire); + expect(after.self).toEqual(before.self); + expect(after.rumorHash).toBe(before.rumorHash); + expect(after.id).toBe(before.id); + expect(result.after.signs).toBe(result.before.signs); + expect(result.after.encrypts).toBe(result.before.encrypts); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(6); + expect( + new Set( + frames + .filter((frame) => frame.type === 'EVENT') + .map((frame) => frame.id) + ).size + ).toBe(1); + }); +} + +test('persisted recovery explicit retry after actual nonce owner contention spends a later real read under the same command', async ({ + page +}) => { + const original = await seedRecovery(page, true); + const result = await page.evaluate(async ({ command }) => { + const f = window.hcCheckFixture, + held = window.hcCheck.holdCheckNonceOwnerLock(); + try { + f.select('recover', 'wss://inbox.example.org', command); + const first = await f.run(); + await held.release(); + const before = f.host.counts(); + f.select('recover', 'wss://inbox.example.org', command); + const second = await f.run(); + return { + first: first.status, + second: second.status, + hits: held.hits(), + before, + after: f.host.counts(), + command: f.snapshot()?.command, + row: (await f.host.stored())[0] + }; + } finally { + await held.release(); + held.restore(); + } + }, original); + expect(result.hits).toBe(1); + expect(result.first).toBe('busy'); + expect(result.second).toBe('qualified'); + expect(result.command).toBe(original.command); + expect(probeRow(result.row.wire).self).toEqual( + probeRow(original.row.wire).self + ); + expect(result.after.signs).toBe(result.before.signs); + expect(result.after.encrypts).toBe(result.before.encrypts); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(6); + expect( + frames.filter((frame) => frame.type === 'REQ' && !!frame.id) + ).toHaveLength(3); +}); + +for (const lane of ['original', 'recover'] as const) { + test(`${lane} optional cleanup requires its own genuine one-use review and yields independently covered removed qualification`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command }) => { + const f = window.hcCheckFixture, + api = window.hcCheck; + f.select(lane, 'wss://inbox.example.org', command); + const checked = await f.run(), + before = f.host.counts(); + const disclosure = f.selectCleanup(), + review = f.review()!; + const copied = await api.cleanupInboxAccessCheck( + { ...review }, + 'reviewed_delete_local_inbox_probe' + ); + const wrongKind = await api.runInboxAccessCheck( + review, + 'reviewed_inbox_access_check' + ); + const wrongSentinel = await f.cleanup('not_reviewed'); + const rowsBefore = (await f.host.stored()).length; + const removed = await f.cleanup(), + replay = await f.cleanup(); + return { + checked: checked.status, + disclosure, + copied: copied.status, + wrongKind: wrongKind.status, + wrongSentinel: wrongSentinel.status, + rowsBefore, + removed: removed.status, + replay: replay.status, + rowsAfter: (await f.host.stored()).length, + qualified: !!f.qualification(), + qualification: + f.qualification() && + api.inboxAccessQualificationSnapshot(f.qualification()!), + before, + after: f.host.counts() + }; + }, + { lane, command: seed?.command } + ); + expect(result.checked).toBe('qualified'); + expect(result.disclosure?.kind).toBe('cleanup'); + expect(result.copied).toBe('invalid'); + expect(result.wrongKind).toBe('invalid'); + expect(result.wrongSentinel).toBe('invalid'); + expect(result.rowsBefore).toBe(1); + expect(result.removed).toBe('removed'); + expect(result.replay).toBe('invalid'); + expect(result.rowsAfter).toBe(0); + expect(result.qualified).toBe(true); + expect(result.qualification?.sourceOnly).toBe(true); + expect(result.after.signs).toBe(result.before.signs); + expect(result.after.encrypts).toBe(result.before.encrypts); + expect(result.after.decrypts).toBe(result.before.decrypts); + }); +} + +test('genuine coordinator capture rejects copied policy, operator, C7, identity, session and repository before effects', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + api = window.hcCheck, + a = f.actors(), + before = f.host.counts(), + rows = await f.host.stored(); + type Args = Parameters<typeof api.createInboxAccessCheck>; + const actual: Args = [ + f.repository, + a.identity, + a.session, + f.host.policy, + a.access, + f.host.qualification + ]; + const copied = actual.map((_, index) => { + const args = actual.slice() as Args; + args[index] = { ...args[index] }; + return !!api.createInboxAccessCheck(...args); + }); + const policy = api.createInboxAccessCheck( + f.repository, + a.identity, + a.session, + f.host.copiedPolicy(), + a.access, + f.host.qualification + ); + const unsupported = api.createInboxAccessCheck( + f.repository, + a.identity, + a.session, + f.host.policy, + a.access, + f.host.operator(true) + ); + const unconfigured = api.createInboxAccessCheck( + f.repository, + a.identity, + a.session, + f.host.policy, + a.access, + f.host.operator(false, true) + ); + const foreignRepository = api.createInboxAccessCheck( + f.host.foreignRepository(), + a.identity, + a.session, + f.host.policy, + a.access, + f.host.qualification + ); + const otherOperator = api.createInboxAccessCheck( + f.repository, + a.identity, + a.session, + f.host.policy, + a.access, + f.host.operator() + ); + return { + copied, + policy: !!policy, + unsupported: !!unsupported, + unconfigured: !!unconfigured, + foreignRepository: !!foreignRepository, + otherOperator: !!otherOperator, + same: api.createInboxAccessCheck(...actual) === f.check(), + before, + after: f.host.counts(), + rows, + afterRows: await f.host.stored() + }; + }); + expect(result.copied).toEqual([false, false, false, false, false, false]); + expect(result.policy).toBe(false); + expect(result.unsupported).toBe(false); + expect(result.unconfigured).toBe(false); + expect(result.foreignRepository).toBe(false); + expect(result.otherOperator).toBe(false); + expect(result.same).toBe(true); + expect(result.after).toEqual(result.before); + expect(result.afterRows).toEqual(result.rows); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); +}); + +test('unsupported and malformed review targets and recovery ids remain inert before a valid selection', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + before = f.host.counts(); + const rejected = [ + f.select('other'), + f.select('original', 'wss://unapproved.example.org'), + f.select('recover', 'wss://inbox.example.org'), + f.select('recover', 'wss://inbox.example.org', 'not-a-command'), + f.select('original', 'wss://inbox.example.org', crypto.randomUUID()) + ]; + const original = f.select(), + stale = f.review()!; + f.select('original', 'wss://peer.example.org'); + const replaced = await window.hcCheck.runInboxAccessCheck( + stale, + 'reviewed_inbox_access_check' + ); + return { + rejected: rejected.map(Boolean), + original: !!original, + replaced: replaced.status, + before, + after: f.host.counts(), + rows: (await f.host.stored()).length + }; + }); + expect(result.rejected).toEqual([false, false, false, false, false]); + expect(result.original).toBe(true); + expect(result.replaced).toBe('invalid'); + expect(result.after).toEqual(result.before); + expect(result.rows).toBe(0); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); +}); + +for (const lane of ['original', 'recover'] as const) { + for (const contention of ['lock', 'SDK'] as const) { + for (const holds of [1, 2] as const) { + test(`${lane} nonce ${contention} contention ${holds === 1 ? 'retries with later real read' : 'exhausts the combined bound without renewal'}`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command, contention, holds }) => { + const f = window.hcCheckFixture, + api = window.hcCheck; + const held = + contention === 'lock' + ? api.holdCheckNonceOwnerLock(holds) + : api.holdCheckNonceSdk(f.actors().session, holds); + const outcomes: string[] = []; + try { + const before = f.host.counts(); + for (let n = 0; n < holds + 1; n++) { + f.select(lane, 'wss://inbox.example.org', command); + outcomes.push((await f.run()).status); + await held.release(); + } + const row = (await f.host.stored())[0]; + return { + outcomes, + hits: held.hits(), + before, + after: f.host.counts(), + command: f.snapshot()?.command, + row, + qualified: !!f.qualification() + }; + } finally { + await held.release(); + held.restore(); + } + }, + { lane, command: seed?.command, contention, holds } + ); + expect(result.hits).toBe(holds); + expect(result.outcomes.slice(0, holds)).toEqual( + Array(holds).fill('busy') + ); + expect(result.outcomes.at(-1)).toBe( + holds === 1 + ? 'qualified' + : lane === 'original' + ? 'stopped' + : 'unavailable' + ); + expect(result.qualified).toBe(holds === 1); + const row = JSON.parse(result.row.wire) as { + id: string; + self: { eventId: string }; + receipts: { + actionId: string; + eventId: string; + origin: string; + role: string; + status: string; + attempt: number; + }[]; + }; + const read = row.receipts + .filter( + (fact) => + fact.role === 'access_readback' && fact.status === 'readback' + ) + .at(-1)!; + const action = row.receipts.filter( + (fact) => fact.actionId === read.actionId + ); + expect(new Set(action.map((fact) => fact.eventId))).toEqual( + new Set([row.self.eventId]) + ); + expect( + action + .filter( + (fact) => + fact.origin === 'wss://inbox.example.org' && + fact.status !== 'nonce_verified' + ) + .map((fact) => [fact.role, fact.attempt]) + ).toEqual([ + ['access_archive', 1], + ['access_readback', 2], + ['access_readback', 3] + ]); + expect( + action + .filter((fact) => fact.status === 'nonce_verified') + .map((fact) => fact.attempt) + ).toEqual(holds === 1 ? [3] : []); + expect(result.command).toBe(row.id); + if (seed) expect(row.self).toEqual(probeRow(seed.row.wire).self); + expect(result.after.signs - result.before.signs).toBe( + lane === 'original' ? 1 : 0 + ); + expect(result.after.encrypts - result.before.encrypts).toBe( + lane === 'original' ? 1 : 0 + ); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength( + lane === 'original' ? 3 : 6 + ); + expect( + frames.filter((frame) => frame.type === 'REQ' && !!frame.id) + ).toHaveLength(lane === 'original' ? 2 : 3); + }); + } + } +} + +test('actual child page reload recovers and qualifies original stored ciphertext in two fresh source realms', async ({ + page +}) => { + const setup = await page.evaluate(async () => { + const f = window.hcCheckFixture, + api = window.hcCheck; + f.select(); + if ((await f.run()).status !== 'qualified') + throw Error('actual original coordinator qualification'); + const row = (await f.host.stored())[0]; + const result = { + owner: f.host.owner, + command: f.snapshot()!.command!, + policyWire: JSON.stringify(api.readRelayPolicy(f.host.policy)), + operatorWire: api.inboxOperatorQualificationWire( + f.host.qualification, + f.host.policy + )!, + row + }; + f.stop(); + return result; + }); + await expect + .poll( + () => + [...physical].filter( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ).length + ) + .toBe(0); + expect(await page.evaluate(() => window.hcCheckFixture.retryCleanup())).toBe( + true + ); + await page.evaluate((url) => { + const iframe = document.createElement('iframe'); + iframe.id = 'coordinator-reload'; + iframe.src = url; + document.body.append(iframe); + }, server.url + '/search'); + const frame = await ( + await page.locator('#coordinator-reload').elementHandle() + ).contentFrame(); + if (!frame) throw Error('actual child source realm missing'); + await frame.waitForLoadState(); + try { + for (let cycle = 0; cycle < 2; cycle++) { + if (cycle) await frame.goto(server.url + '/search'); + await frame.addScriptTag({ content: bundle }); + await frame.evaluate(async (setup) => { + window.hcHostedCheck = await window.hcCheck.openHostedCheck(setup); + }, setup); + const before = await page.evaluate(() => + window.hcCheckFixture.host.counts() + ); + const result = await frame.evaluate(async (setup) => { + const api = window.hcCheck, + { check } = window.hcHostedCheck; + const selected = api.reviewInboxAccessCheck( + check, + 'recover', + 'wss://inbox.example.org', + setup.command + ); + if (!selected) throw Error('actual hosted recovery review missing'); + const result = await api.runInboxAccessCheck( + selected, + 'reviewed_inbox_access_check' + ); + const output = { + status: result.status, + command: api.inboxAccessCheckSnapshot(check)?.command, + qualified: !!api.inboxAccessCheckQualification(check) + }; + api.stopInboxAccessCheck(check); + return output; + }, setup); + expect(result).toEqual({ + status: 'qualified', + command: setup.command, + qualified: true + }); + await expect + .poll( + () => + [...physical].filter( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ).length + ) + .toBe(0); + await frame.evaluate(() => { + const { check, context } = window.hcHostedCheck; + if (!window.hcCheck.stopInboxAccessCheck(check)) + throw Error('actual hosted physical cleanup incomplete'); + context.close(); + }); + const after = await page.evaluate(() => + window.hcCheckFixture.host.counts() + ); + expect(after.signs).toBe(before.signs); + expect(after.encrypts).toBe(before.encrypts); + const row = await page.evaluate( + async () => (await window.hcCheckFixture.host.stored())[0] + ); + expect(probeRow(row.wire).self).toEqual(probeRow(setup.row.wire).self); + expect(probeRow(row.wire).rumorHash).toBe( + probeRow(setup.row.wire).rumorHash + ); + } + } finally { + await frame.evaluate(() => { + if (window.hcHostedCheck) { + window.hcCheck.stopInboxAccessCheck(window.hcHostedCheck.check); + window.hcHostedCheck.context.close(); + } + }); + await page + .locator('#coordinator-reload') + .evaluate((element) => element.remove()); + } + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(9); + expect( + new Set( + frames.filter((frame) => frame.type === 'EVENT').map((frame) => frame.id) + ).size + ).toBe(1); +}); + +for (const lane of ['original', 'recover'] as const) { + for (const fault of [ + 'refused', + 'wrong_ack', + 'disconnect', + 'missing', + 'wrong_read' + ] as const) { + test(`${lane} actual ${fault} wire outcome cannot qualify or fabricate successful readback`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const priorEvents = frames.filter( + (frame) => frame.type === 'EVENT' + ).length; + wireFault = fault; + const result = await page.evaluate( + async ({ lane, command }) => { + const f = window.hcCheckFixture; + f.select(lane, 'wss://inbox.example.org', command); + const completed = await f.run(); + return { + status: completed.status, + qualification: !!f.qualification(), + snapshot: f.snapshot(), + rows: await f.host.stored() + }; + }, + { lane, command: seed?.command } + ); + expect(result.status).not.toBe('qualified'); + expect(result.qualification).toBe(false); + expect(result.rows).toHaveLength(1); + const row = JSON.parse(result.rows[0].wire) as { + self: { eventId: string }; + receipts: { status: string; actionId: string }[]; + }; + const added = row.receipts.slice( + seed ? probeRow(seed.row.wire).receipts.length : 0 + ); + expect(added.some((fact) => fact.status === 'nonce_verified')).toBe( + false + ); + if (fault === 'missing' || fault === 'wrong_read') { + expect(added.filter((fact) => fact.status === 'accepted')).toHaveLength( + 3 + ); + expect(added.some((fact) => fact.status === 'readback')).toBe(false); + } else { + expect(added.some((fact) => fact.status === 'accepted')).toBe(false); + expect( + frames.filter((frame) => frame.type === 'EVENT').length - priorEvents + ).toBe(1); + } + if (seed) expect(row.self).toEqual(probeRow(seed.row.wire).self); + }); + } + for (const fault of ['stop', 'head', 'session'] as const) { + for (const callback of ['key', 'cipher'] as const) { + test(`${lane} actual ${callback} callback ${fault} loss cannot continue effects or issue qualification`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const priorEvents = frames.filter( + (frame) => frame.type === 'EVENT' + ).length; + const result = await page.evaluate( + async ({ lane, command, fault, callback }) => { + const f = window.hcCheckFixture, + api = window.hcCheck; + let hits = 0; + const lose = () => { + if (hits++) return; + if (fault === 'stop') f.stop(); + else if (fault === 'head') f.actors().churnHead(); + else api.closePrivateSession(f.actors().session); + }; + const restore = + callback === 'key' + ? f.host.keyCallback(1, lose) + : f.host.cipherGetter(lose); + try { + f.select(lane, 'wss://inbox.example.org', command); + const result = await f.run(); + return { + status: result.status, + hits, + qualified: !!f.qualification(), + snapshot: f.snapshot(), + rows: await f.host.stored() + }; + } finally { + restore(); + } + }, + { lane, command: seed?.command, fault, callback } + ); + expect(result.hits).toBeGreaterThan(0); + expect(result.status).not.toBe('qualified'); + expect(result.qualified).toBe(false); + expect(result.snapshot?.state).toBe('stopped'); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength( + priorEvents + ); + if (seed) expect(result.rows).toEqual([seed.row]); + }); + } + } + for (const closeMode of ['hold', 'fail'] as const) { + test(`${lane} actual ${closeMode} physical close retains cleanup requirement until explicit retry`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command, closeMode }) => { + const f = window.hcCheckFixture; + f.select(lane, 'wss://inbox.example.org', command); + const checked = await f.run(); + window.hcCheckCloseMode = closeMode; + const stopped = f.stop(), + snapshot = f.snapshot(), + closes = window.hcCheckCloseCalls; + const selection = f.select(lane, 'wss://inbox.example.org', command); + const observed = f.snapshot(); + return { + checked: checked.status, + stopped, + snapshot, + selection, + observed, + closes, + afterCloses: window.hcCheckCloseCalls, + qualified: !!f.qualification(), + held: window.hcCheckHeldClose?.length ?? 0 + }; + }, + { lane, command: seed?.command, closeMode } + ); + expect(result.checked).toBe('qualified'); + expect(result.stopped).toBe(false); + expect(result.snapshot?.cleanupRequired).toBe(true); + expect(result.observed?.cleanupRequired).toBe(true); + expect(result.selection).toBeUndefined(); + expect(result.afterCloses).toBe(result.closes); + expect(result.qualified).toBe(false); + if (closeMode === 'hold') expect(result.held).toBeGreaterThan(0); + await page.evaluate(() => { + window.hcCheckCloseMode = undefined; + for (const release of window.hcCheckHeldClose ?? []) release(); + window.hcCheckHeldClose = []; + window.hcCheckFixture.retryCleanup(); + }); + await expect + .poll( + () => + [...physical].filter( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ).length + ) + .toBe(0); + expect( + await page.evaluate(() => window.hcCheckFixture.retryCleanup()) + ).toBe(true); + }); + } +} + +test('authentic pending archive survives Stop and requires a distinct reviewed historical reconciliation without new effects', async ({ + page +}) => { + holdArchive = true; + await page.evaluate(() => { + const f = window.hcCheckFixture; + f.select(); + window.hcCheckPending = f.run(); + }); + await expect.poll(() => !!releaseArchive).toBe(true); + await page.evaluate(() => { + // Publication correctly owns the real lock through its network result. + // Queue the competing holder, then release the actual ACK so it acquires + // before append's separate ifAvailable request; never wait out the relay. + window.hcCheckLockReady = window.hcCheck + .holdCheckOwnerLock(window.hcCheckFixture.host.owner) + .then((release) => { + window.hcCheckRelease = release; + }); + }); + holdArchive = false; + releaseArchive!(); + const pending = await page.evaluate(async () => { + await window.hcCheckLockReady; + const f = window.hcCheckFixture, + completed = await window.hcCheckPending; + const snapshot = f.snapshot(); + f.stop(); + await window.hcCheckRelease!(); + window.hcCheckRelease = undefined; + return { status: completed.status, snapshot }; + }); + expect(pending.status).toBe('busy'); + expect(pending.snapshot?.pendingSettlement).toBe(true); + const reconciled = await page.evaluate(async () => { + const f = window.hcCheckFixture, + api = window.hcCheck, + before = f.host.counts(); + const disclosure = f.selectReconciliation(), + selected = f.review()!; + const copied = await api.reconcileInboxAccessCheck( + { ...selected }, + 'reviewed_inbox_access_check_reconciliation' + ); + const wrongKind = await api.cleanupInboxAccessCheck( + selected, + 'reviewed_delete_local_inbox_probe' + ); + const result = await f.reconcile(), + replay = await f.reconcile(); + return { + disclosure, + copied: copied.status, + wrongKind: wrongKind.status, + status: result.status, + replay: replay.status, + before, + after: f.host.counts(), + snapshot: f.snapshot(), + qualified: !!f.qualification(), + rows: await f.host.stored() + }; + }); + expect(reconciled.disclosure?.kind).toBe('reconcile'); + expect(reconciled.copied).toBe('invalid'); + expect(reconciled.wrongKind).toBe('invalid'); + expect(['saved', 'existing', 'reconciled']).toContain(reconciled.status); + expect(reconciled.replay).toBe('invalid'); + expect(reconciled.after).toEqual(reconciled.before); + expect(reconciled.snapshot?.pendingSettlement).toBe(false); + expect(reconciled.qualified).toBe(false); + expect(reconciled.snapshot?.state).toBe('stopped'); + expect( + probeRow(reconciled.rows[0].wire).receipts.filter( + (fact: { status: string }) => fact.status === 'accepted' + ) + ).toHaveLength(1); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(1); +}); + +async function beginActualAuth(page: Page, mode: 'accept' | 'refuse' | 'hold') { + challenge = true; + authMode = mode; + const before = frames.filter((frame) => frame.type === 'AUTH').length; + // On a cold socket this API initially observes stopped until actual open; + // the following real challenge observation is required before responding. + expect(['waiting', 'challenge', 'stopped']).toContain( + await page.evaluate(() => + window.hcCheck.authenticateInboxAccess( + window.hcCheckFixture.actors().access, + 'wss://inbox.example.org', + 'reviewed_inbox_connection_auth' + ) + ) + ); + await expect + .poll(() => + page.evaluate( + () => + window.hcCheck + .inboxAccessControllerSourceSnapshot( + window.hcCheckFixture.actors().access + ) + ?.auth.find((row) => row.origin === 'wss://inbox.example.org') + ?.state + ) + ) + .toBe('challenge'); + await page.evaluate(() => { + window.hcCheckAuthPending = window.hcCheck.respondInboxAccessAuthentication( + window.hcCheckFixture.actors().access, + 'wss://inbox.example.org', + 'reviewed_inbox_auth_response' + ); + }); + await expect + .poll(() => frames.filter((frame) => frame.type === 'AUTH').length) + .toBe(before + 1); + if (mode !== 'hold') + expect(await page.evaluate(() => window.hcCheckAuthPending)).toBe( + mode === 'accept' ? 'accepted' : 'refused' + ); +} + +test('required AUTH pending response refuses coordinator work without a second SDK job or diagnostic effect', async ({ + page +}) => { + await beginActualAuth(page, 'hold'); + try { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + before = f.host.counts(); + f.select(); + const result = await f.run(); + return { + status: result.status, + before, + after: f.host.counts(), + rows: await f.host.stored(), + qualified: !!f.qualification() + }; + }); + expect(result.status).not.toBe('qualified'); + expect(result.qualified).toBe(false); + expect(result.after).toEqual(result.before); + expect(result.rows).toHaveLength(0); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); + expect(frames.filter((frame) => frame.type === 'AUTH')).toHaveLength(1); + } finally { + releaseAuth!(); + await page.evaluate(() => window.hcCheckAuthPending); + } +}); + +for (const lane of ['original', 'recover'] as const) { + for (const auth of ['accept', 'refuse', 'hold'] as const) { + test(`${lane} required AUTH ${auth} uses genuine C7 response and preserves source-only presentation`, async ({ + page + }) => { + let seed: Awaited<ReturnType<typeof seedRecovery>> | undefined; + if (lane === 'recover') { + await beginActualAuth(page, 'accept'); + seed = await seedRecovery(page, true); + } + await beginActualAuth(page, auth); + const inert = await page.evaluate(() => ({ + qualified: !!window.hcCheckFixture.qualification(), + readiness: window.hcCheck.inboxAccessControllerSnapshot( + window.hcCheckFixture.actors().access + )?.readiness + })); + expect(inert).toEqual({ qualified: false, readiness: 'unavailable' }); + if (auth === 'hold') { + expect( + await page.evaluate( + () => + window.hcCheck + .inboxAccessControllerSourceSnapshot( + window.hcCheckFixture.actors().access + ) + ?.auth.find((row) => row.origin === 'wss://inbox.example.org') + ?.state + ) + ).toBe('signing'); + expect(releaseAuth).toBeDefined(); + releaseAuth!(); + expect(await page.evaluate(() => window.hcCheckAuthPending)).toBe( + 'accepted' + ); + } + const result = await page.evaluate( + async ({ lane, command }) => { + const f = window.hcCheckFixture; + f.select(lane, 'wss://inbox.example.org', command); + const result = await f.run(); + return { + status: result.status, + qualified: !!f.qualification(), + readiness: window.hcCheck.inboxAccessControllerSnapshot( + f.actors().access + )?.readiness, + sourceOnly: f.snapshot()?.sourceOnly + }; + }, + { lane, command: seed?.command } + ); + expect(result.readiness).toBe('unavailable'); + expect(result.sourceOnly).toBe(true); + if (auth === 'refuse') { + expect(result.status).not.toBe('qualified'); + expect(result.qualified).toBe(false); + } else { + expect(result.status).toBe('qualified'); + expect(result.qualified).toBe(true); + const before = await page.evaluate(() => + window.hcCheckFixture.host.counts() + ); + for (const socket of physical) + if (physicalOrigins.get(socket) === 'wss://inbox.example.org') + socket.send(JSON.stringify(['AUTH', 's5c-genuine-new-challenge'])); + await expect + .poll(() => + page.evaluate( + () => + window.hcCheck + .inboxAccessControllerSourceSnapshot( + window.hcCheckFixture.actors().access + ) + ?.auth.find((row) => row.origin === 'wss://inbox.example.org') + ?.state + ) + ) + .toBe('challenge'); + expect( + await page.evaluate(() => !!window.hcCheckFixture.qualification()) + ).toBe(false); + expect( + await page.evaluate(() => window.hcCheckFixture.host.counts()) + ).toEqual(before); + } + }); + } +} + +for (const lane of ['original', 'recover'] as const) { + for (const mode of ['missing', 'corrupt', 'alias', 'foreign'] as const) { + test(`${lane} actual nonce-stage ${mode} native journal observation preserves source qualification rules`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command, mode }) => { + const f = window.hcCheckFixture, + api = window.hcCheck; + const hook = api.atCheckNonce(async () => { + const row = (await f.host.stored()).find( + (row) => + row.owner === f.host.owner && row.id === f.snapshot()?.command + ); + if (!row) + throw Error( + 'actual selected native journal missing before mutation' + ); + await api.changeCheckNativeRow(row, mode); + }); + try { + f.select(lane, 'wss://inbox.example.org', command); + const result = await f.run(); + return { + status: result.status, + hits: hook.hits(), + qualified: !!f.qualification(), + rows: await f.host.stored() + }; + } finally { + hook.restore(); + } + }, + { lane, command: seed?.command, mode } + ); + expect(result.hits).toBe(1); + if (mode === 'foreign') { + expect(result.status).toBe('qualified'); + expect(result.qualified).toBe(true); + expect(result.rows).toHaveLength(2); + expect(new Set(result.rows.map((row) => row.owner)).size).toBe(2); + } else { + expect(result.status).not.toBe('qualified'); + expect(result.qualified).toBe(false); + } + }); + } + test(`${lane} actual native admission abort cannot become qualification`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command }) => { + const f = window.hcCheckFixture, + fault = f.installCommitFault('aborted'); + try { + f.select(lane, 'wss://inbox.example.org', command); + const result = await f.run(); + const rows = await f.host.stored(); + return { + status: result.status, + counts: fault.counts(), + qualified: !!f.qualification(), + rows + }; + } finally { + fault.restore(); + } + }, + { lane, command: seed?.command } + ); + expect(result.counts.aborted).toBe(1); + expect(result.status).not.toBe('qualified'); + expect(result.qualified).toBe(false); + if (lane === 'recover') { + const reconciled = await page.evaluate(async () => { + const f = window.hcCheckFixture; + if (!f.snapshot()?.pendingSettlement) return 'no_pending'; + if (!f.selectReconciliation()) + throw Error('actual aborted attempt review missing'); + return (await f.reconcile()).status; + }); + expect(['saved', 'existing', 'reconciled']).toContain(reconciled); + } + if (seed) expect(result.rows).toEqual([seed.row]); + else { + expect(result.rows).toHaveLength(0); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); + } + }); + test(`${lane} actual reviewed cleanup abort preserves the journal and permanently invalidates the old journal qualification`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command }) => { + const f = window.hcCheckFixture, + api = window.hcCheck; + f.select(lane, 'wss://inbox.example.org', command); + const checked = await f.run(), + old = f.qualification()!, + before = await f.host.stored(), + fault = api.abortCheckCleanup(); + try { + f.selectCleanup(); + const removed = await f.cleanup(); + const after = await f.host.stored(); + return { + checked: checked.status, + status: removed.status, + aborts: fault.aborts(), + before, + after, + qualified: !!f.qualification(), + oldSnapshot: api.inboxAccessQualificationSnapshot(old), + afterSnapshot: api.inboxAccessQualificationSnapshot(old) + }; + } finally { + fault.restore(); + } + }, + { lane, command: seed?.command } + ); + expect(result.checked).toBe('qualified'); + expect(result.aborts).toBe(1); + expect(result.status).not.toBe('removed'); + expect(result.after).toEqual(result.before); + expect(result.qualified).toBe(false); + expect(result.oldSnapshot).toBeUndefined(); + expect(result.afterSnapshot).toBeUndefined(); + }); + for (const cleanup of [false, true]) { + test(`${lane} actual own InboxSync ${cleanup ? 'after reviewed cleanup retains then rejects the self message' : 'excludes the live genuine diagnostic journal'} without losing qualification`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const started = await page.evaluate( + async ({ lane, command, cleanup }) => { + const f = window.hcCheckFixture; + f.select(lane, 'wss://inbox.example.org', command); + if ((await f.run()).status !== 'qualified') + throw Error('actual coordinator qualification required'); + if (cleanup) { + f.selectCleanup(); + if ((await f.cleanup()).status !== 'removed') + throw Error('actual optional removal required'); + } + return f.startOwnInbox(); + }, + { lane, command: seed?.command, cleanup } + ); + expect(started).toBe(true); + await expect + .poll(() => + page.evaluate(() => { + const s = window.hcCheckFixture.syncSnapshot(); + return ( + !!s && s.pending === 0 && (s.excludedProbes > 0 || s.retained > 0) + ); + }) + ) + .toBe(true); + const result = await page.evaluate(async (cleanup) => { + const f = window.hcCheckFixture, + before = f.host.counts(); + const decrypted = cleanup ? await f.decryptReceived() : undefined; + return { + snapshot: f.syncSnapshot(), + qualified: !!f.qualification(), + decrypted, + before, + after: f.host.counts(), + rows: (await f.host.stored()).length + }; + }, cleanup); + expect(result.qualified).toBe(true); + if (cleanup) { + expect(result.snapshot?.retained).toBe(1); + expect(result.snapshot?.excludedProbes).toBe(0); + expect(result.decrypted?.rooms).toBe(0); + expect(result.decrypted?.snapshot.invalid).toBe(1); + expect(result.after.decrypts - result.before.decrypts).toBe(2); + expect(result.rows).toBe(0); + } else { + expect(result.snapshot?.retained).toBe(0); + expect(result.snapshot?.excludedProbes).toBeGreaterThan(0); + expect(result.after).toEqual(result.before); + expect(result.rows).toBe(1); + } + }); + } +} + +for (const lane of ['original', 'recover'] as const) { + for (const mode of [ + 'wrong_key', + 'refuse_decrypt', + 'wrong_decrypt' + ] as const) { + test(`${lane} actual nonce provider ${mode} cannot qualify or silently repeat rejected crypto`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command, mode }) => { + const f = window.hcCheckFixture, + hook = window.hcCheck.atCheckNonce(async () => { + f.host.mode(mode); + await Promise.resolve(); + }); + try { + f.select(lane, 'wss://inbox.example.org', command); + const first = await f.run(), + before = f.host.counts(); + f.host.mode('normal'); + const selection = f.select( + lane, + 'wss://inbox.example.org', + command + ); + const second = selection ? await f.run() : undefined; + return { + first: first.status, + second: second?.status, + hits: hook.hits(), + before, + after: f.host.counts(), + rows: await f.host.stored(), + qualified: !!f.qualification() + }; + } finally { + hook.restore(); + f.host.mode('normal'); + } + }, + { lane, command: seed?.command, mode } + ); + expect(result.hits).toBe(1); + expect(result.first).not.toBe('qualified'); + expect(result.second).not.toBe('qualified'); + expect(result.qualified).toBe(false); + expect(result.after).toEqual(result.before); + const oldFacts = seed ? probeRow(seed.row.wire).receipts.length : 0; + expect( + probeRow(result.rows[0].wire) + .receipts.slice(oldFacts) + .some((fact) => fact.status === 'nonce_verified') + ).toBe(false); + }); + } + for (const loss of ['stop', 'head', 'session'] as const) { + test(`${lane} actual held nonce decrypt ${loss} loss retains the admitted promise until real settlement`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + await page.evaluate( + ({ lane, command }) => { + const f = window.hcCheckFixture; + window.hcCheckRestore = window.hcCheck.atCheckNonce(async () => { + f.host.mode('hold_decrypt'); + await Promise.resolve(); + }).restore; + f.select(lane, 'wss://inbox.example.org', command); + window.hcCheckPending = f.run(); + }, + { lane, command: seed?.command } + ); + await expect + .poll(() => page.evaluate(() => window.hcCheckFixture.host.waiting())) + .toBe('decrypt'); + const during = await page.evaluate((loss) => { + const f = window.hcCheckFixture; + if (loss === 'stop') f.stop(); + else if (loss === 'head') f.actors().churnHead(); + else window.hcCheck.closePrivateSession(f.actors().session); + const result = { + snapshot: f.snapshot(), + qualified: !!f.qualification(), + counts: f.host.counts(), + waiting: f.host.waiting() + }; + f.host.settle(); + return result; + }, loss); + expect(during.snapshot?.state).toBe('stopped'); + expect(during.snapshot?.busy).toBe(true); + expect(during.qualified).toBe(false); + expect(during.waiting).toBe('decrypt'); + const after = await page.evaluate(async () => { + const f = window.hcCheckFixture, + result = await window.hcCheckPending; + f.host.mode('normal'); + return { + status: result.status, + snapshot: f.snapshot(), + counts: f.host.counts(), + qualified: !!f.qualification(), + rows: await f.host.stored() + }; + }); + expect(after.status).toBe('stopped'); + expect(after.snapshot?.busy).toBe(false); + expect(after.qualified).toBe(false); + expect(after.counts).toEqual(during.counts); + const oldFacts = seed ? probeRow(seed.row.wire).receipts.length : 0; + expect( + probeRow(after.rows[0].wire) + .receipts.slice(oldFacts) + .some((fact) => fact.status === 'nonce_verified') + ).toBe(false); + }); + } + test(`${lane} real held first key admits no reentry and Stop protects unfinished journal without a replacement artifact`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const priorEvents = frames.filter((frame) => frame.type === 'EVENT').length; + await page.evaluate( + ({ lane, command }) => { + const f = window.hcCheckFixture; + f.host.mode('hold_key'); + f.select(lane, 'wss://inbox.example.org', command); + window.hcCheckPending = f.run(); + }, + { lane, command: seed?.command } + ); + await expect + .poll(() => page.evaluate(() => window.hcCheckFixture.host.waiting())) + .toBe('key'); + const during = await page.evaluate( + async ({ lane, command }) => { + const f = window.hcCheckFixture, + api = window.hcCheck, + before = f.host.counts(), + consumed = f.review()!; + const reentry = await api.runInboxAccessCheck( + consumed, + 'reviewed_inbox_access_check' + ); + const selection = f.select(lane, 'wss://inbox.example.org', command); + f.stop(); + const snapshot = f.snapshot(), + counts = f.host.counts(); + f.host.settle(); + return { before, counts, reentry: reentry.status, selection, snapshot }; + }, + { lane, command: seed?.command } + ); + expect(during.reentry).toBe('invalid'); + expect(during.selection).toBeUndefined(); + expect(during.counts).toEqual(during.before); + expect(during.snapshot?.state).toBe('stopped'); + expect(during.snapshot?.busy).toBe(true); + const after = await page.evaluate(async () => { + const f = window.hcCheckFixture, + result = await window.hcCheckPending; + f.host.mode('normal'); + return { + status: result.status, + counts: f.host.counts(), + rows: await f.host.stored(), + qualified: !!f.qualification() + }; + }); + expect(after.status).toBe('stopped'); + expect(after.qualified).toBe(false); + expect(after.counts).toEqual(during.counts); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength( + priorEvents + ); + if (seed) expect(after.rows).toEqual([seed.row]); + else expect(probeRow(after.rows[0].wire).self).toBeNull(); + }); +} + +async function seedOrdinaryRow(page: Page, foreign = false) { + return page.evaluate(async (foreign) => { + const f = window.hcCheckFixture; + const owner = foreign ? window.hcCheck.otherCheckOwner() : f.host.owner; + const id = crypto.randomUUID(), + peer = window.hcCheck.otherCheckOwner(); + if (owner === peer || (foreign && owner === f.host.owner)) + throw Error('actual distinct public fixture owners required'); + const wire = JSON.stringify({ + schema: 1, + family: 'private_send_reservation', + owner, + id, + revision: 0, + peer, + rumorHash: 'a'.repeat(64), + createdAt: Math.floor(Date.now() / 1000) + }); + if (!window.hcCheck.decodePrivateRecord(wire, owner, id).ok) + throw Error('actual canonical schema1 ordinary reservation fixture'); + const row = { owner, id, wire }; + await f.host.inject([row]); + return row; + }, foreign); +} + +for (const selected of ['absent', 'ordinary', 'foreign'] as const) { + test(`cold recovery rejects an actual ${selected} stored selection without fallback construction or mutation`, async ({ + page + }) => { + const row = + selected === 'absent' + ? undefined + : await seedOrdinaryRow(page, selected === 'foreign'); + const result = await page.evaluate(async (id) => { + const f = window.hcCheckFixture, + before = f.host.counts(), + beforeRows = await f.host.stored(); + const command = id ?? crypto.randomUUID(); + f.select('recover', 'wss://inbox.example.org', command); + const completed = await f.run(); + return { + status: completed.status, + before, + after: f.host.counts(), + beforeRows, + afterRows: await f.host.stored(), + qualified: !!f.qualification(), + command: f.snapshot()?.command, + selected: command + }; + }, row?.id); + expect(result.status).not.toBe('qualified'); + expect(result.qualified).toBe(false); + expect(result.after).toEqual(result.before); + expect(result.afterRows).toEqual(result.beforeRows); + expect(result.command).toBe(result.selected); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); + }); +} + +for (const lane of ['original', 'recover'] as const) { + for (const cleanup of [false, true]) { + test(`${lane} ${cleanup ? 'reviewed optional cleanup' : 'qualification'} preserves exact preexisting schema1 ordinary reservation bytes`, async ({ + page + }) => { + const row = await seedOrdinaryRow(page); + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command, cleanup, id }) => { + const f = window.hcCheckFixture; + f.select(lane, 'wss://inbox.example.org', command); + const completed = await f.run(); + let removed: string | undefined; + if (cleanup) { + f.selectCleanup(); + removed = (await f.cleanup()).status; + } + const rows = await f.host.stored(); + return { + status: completed.status, + removed, + ordinary: rows.find((row) => row.id === id), + rows: rows.length, + qualified: !!f.qualification() + }; + }, + { lane, command: seed?.command, cleanup, id: row.id } + ); + expect(result.status).toBe('qualified'); + if (cleanup) expect(result.removed).toBe('removed'); + expect(result.qualified).toBe(true); + expect(result.ordinary).toEqual(row); + expect(result.rows).toBe(cleanup ? 1 : 2); + }); + } +} + +test('mismatched genuine same-owner identity private-session and C7 tuples cannot authorize effects or close either scope', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.hcCheckFixture, + api = window.hcCheck, + original = f.actors(), + other = await f.alternateActors(); + const before = f.host.counts(), + closes = window.hcCheckCloseCalls; + try { + const attempts = [ + api.createInboxAccessCheck( + f.repository, + other.identity, + original.session, + f.host.policy, + original.access, + f.host.qualification + ), + api.createInboxAccessCheck( + f.repository, + original.identity, + other.session, + f.host.policy, + other.access, + f.host.qualification + ), + api.createInboxAccessCheck( + f.repository, + other.identity, + original.session, + f.host.policy, + other.access, + f.host.qualification + ), + api.createInboxAccessCheck( + f.repository, + original.identity, + other.session, + f.host.policy, + original.access, + f.host.qualification + ) + ]; + return { + attempts: attempts.map(Boolean), + before, + after: f.host.counts(), + closes, + afterCloses: window.hcCheckCloseCalls, + original: api.inboxAccessControllerSourceSnapshot(original.access), + other: api.inboxAccessControllerSourceSnapshot(other.access), + same: + api.createInboxAccessCheck( + f.repository, + original.identity, + original.session, + f.host.policy, + original.access, + f.host.qualification + ) === f.check(), + rows: await f.host.stored() + }; + } finally { + other.close(); + } + }); + expect(result.attempts).toEqual([false, false, false, false]); + expect(result.after).toEqual(result.before); + expect(result.afterCloses).toBe(result.closes); + expect(result.original?.state).not.toBe('stopped'); + expect(result.other?.state).not.toBe('stopped'); + expect(result.same).toBe(true); + expect(result.rows).toHaveLength(0); + expect( + frames.filter((frame) => ['EVENT', 'AUTH'].includes(frame.type)) + ).toHaveLength(0); +}); + +test('coordinator Stop preserves a genuine alternate actor failed AUTH cleanup for its own explicit retry', async ({ + page +}) => { + await page.evaluate(async () => { + const f = window.hcCheckFixture; + window.hcCheckOther = await f.alternateActors(); + await window.hcCheck.authenticateInboxAccess( + window.hcCheckOther.access, + 'wss://inbox.example.org', + 'reviewed_inbox_connection_auth' + ); + }); + await expect + .poll(() => + page.evaluate( + () => + window.hcCheck + .inboxAccessControllerSourceSnapshot(window.hcCheckOther!.access) + ?.auth.find((row) => row.origin === 'wss://inbox.example.org') + ?.state + ) + ) + .toBe('waiting'); + try { + const result = await page.evaluate(() => { + const f = window.hcCheckFixture, + api = window.hcCheck, + other = window.hcCheckOther!; + window.hcCheckCloseMode = 'fail'; + const foreignClosed = api.closeInboxAccessController(other.access), + closes = window.hcCheckCloseCalls; + const bad = api.createInboxAccessCheck( + f.repository, + f.actors().identity, + other.session, + f.host.policy, + other.access, + f.host.qualification + ); + const ownStopped = f.stop(); + return { + foreignClosed, + bad: !!bad, + closes, + afterCloses: window.hcCheckCloseCalls, + ownStopped, + foreignSnapshot: api.inboxAccessControllerSourceSnapshot(other.access) + }; + }); + expect(result.foreignClosed).toBe(false); + expect(result.bad).toBe(false); + expect(result.ownStopped).toBe(true); + expect(result.afterCloses).toBe(result.closes); + expect(result.foreignSnapshot?.cleanupRequired).toBe(true); + expect( + [...physical].some( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ) + ).toBe(true); + } finally { + await page.evaluate(() => { + window.hcCheckCloseMode = undefined; + window.hcCheckOther!.close(); + }); + await expect + .poll( + () => + [...physical].filter( + (socket) => + physicalOrigins.get(socket) !== 'wss://discovery.example.org' + ).length + ) + .toBe(0); + expect(await page.evaluate(() => window.hcCheckOther!.close())).toBe(true); + } +}); + +for (const lane of ['original', 'recover'] as const) { + for (const phase of ['append', 'nonce', 'qualification'] as const) { + for (const loss of ['head', 'session', 'C7'] as const) { + test(`${lane} actual native ${phase} callback ${loss} loss revokes before subsequent effects and preserves admitted bookkeeping`, async ({ + page + }) => { + const seed = + lane === 'recover' ? await seedRecovery(page, true) : undefined; + const result = await page.evaluate( + async ({ lane, command, phase, loss }) => { + const f = window.hcCheckFixture, + api = window.hcCheck; + let atLoss: ReturnType<typeof f.snapshot> | undefined, + counts: ReturnType<typeof f.host.counts> | undefined; + const hook = api.checkNativeLoss(phase, () => { + if (loss === 'head') f.actors().churnHead(); + else if (loss === 'session') + api.closePrivateSession(f.actors().session); + else api.closeInboxAccessController(f.actors().access); + atLoss = f.snapshot(); + counts = f.host.counts(); + }); + try { + f.select(lane, 'wss://inbox.example.org', command); + const completed = await f.run(), + rows = await f.host.stored(); + return { + hits: hook.hits(), + status: completed.status, + atLoss, + counts, + after: f.host.counts(), + qualified: !!f.qualification(), + snapshot: f.snapshot(), + rows + }; + } finally { + hook.restore(); + } + }, + { lane, command: seed?.command, phase, loss } + ); + expect(result.hits).toBe(1); + expect(result.atLoss?.state).toBe('stopped'); + expect(result.atLoss?.busy).toBe(true); + expect(result.status).toBe('stopped'); + expect(result.qualified).toBe(false); + expect(result.snapshot?.state).toBe('stopped'); + expect(result.after).toEqual(result.counts); + expect(result.rows).toHaveLength(1); + const row = probeRow(result.rows[0].wire), + oldCount = seed ? probeRow(seed.row.wire).receipts.length : 0; + expect( + row.receipts + .slice(oldCount) + .filter((fact) => fact.status === 'accepted') + ).toHaveLength(phase === 'append' ? 1 : 3); + if (seed) expect(row.self).toEqual(probeRow(seed.row.wire).self); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength( + (seed ? 3 : 0) + (phase === 'append' ? 1 : 3) + ); + }); + } + } +} + +for (const mode of ['readback', 'uncertain_readback'] as const) { + test(`actual durable R0 ${mode} uncertainty protects one original reservation and explicit Retry creates no replacement identity`, async ({ + page + }) => { + const result = await page.evaluate(async (mode) => { + const f = window.hcCheckFixture, + api = window.hcCheck, + hook = api.failCheckPreparationNative('R0', mode); + const uuid = crypto.randomUUID.bind(crypto); + let uuids = 0; + crypto.randomUUID = () => { + uuids++; + return uuid(); + }; + try { + f.select(); + const first = await f.run(), + firstRows = await f.host.stored(), + counts = hook.counts(), + firstUuids = uuids, + before = f.host.counts(); + hook.restore(); + f.select(); + const second = await f.run(), + rows = await f.host.stored(); + return { + first: first.status, + second: second.status, + counts, + firstRows, + rows, + firstUuids, + uuids, + before, + after: f.host.counts(), + qualified: !!f.qualification(), + candidate: hook.candidate() + }; + } finally { + hook.restore(); + crypto.randomUUID = uuid; + } + }, mode); + expect(result.counts.puts).toBe(1); + expect(result.counts.completes).toBe(1); + expect(result.counts.readbackAborts).toBe(mode === 'readback' ? 1 : 0); + if (mode === 'uncertain_readback') + expect(result.counts.abortRefusals).toBeGreaterThan(0); + expect(result.first).not.toBe('qualified'); + expect(result.second).not.toBe('qualified'); + expect(result.firstRows).toHaveLength(1); + expect(result.rows).toEqual(result.firstRows); + expect(result.rows[0]).toEqual(result.candidate); + expect(result.uuids).toBe(result.firstUuids); + expect(result.after).toEqual(result.before); + expect(result.qualified).toBe(false); + expect(probeRow(result.rows[0].wire).self).toBeNull(); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); + }); +} + +for (const mode of ['abort', 'readback', 'uncertain_readback'] as const) { + test(`actual R1 ${mode} uncertainty sends no EVENT before genuine ACK and explicit Retry reuses exact original wrap and command`, async ({ + page + }) => { + const first = await page.evaluate(async (mode) => { + const f = window.hcCheckFixture, + hook = window.hcCheck.failCheckPreparationNative('R1', mode); + try { + f.select(); + const completed = await f.run(), + rows = await f.host.stored(); + return { + status: completed.status, + counts: hook.counts(), + candidate: hook.candidate(), + command: f.snapshot()?.command, + before: f.host.counts(), + rows, + qualified: !!f.qualification() + }; + } finally { + hook.restore(); + } + }, mode); + expect(first.status).not.toBe('qualified'); + expect(first.qualified).toBe(false); + expect(first.counts.puts).toBe(1); + expect(first.rows).toHaveLength(1); + expect(first.command).toBe(first.candidate?.id); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(0); + if (mode === 'abort') { + expect(first.counts.aborts).toBe(1); + expect(probeRow(first.rows[0].wire).self).toBeNull(); + } else { + expect(first.counts.completes).toBe(1); + expect(first.counts.readbackAborts).toBe(1); + expect(first.rows[0]).toEqual(first.candidate); + } + if (mode === 'uncertain_readback') + expect(first.counts.abortRefusals).toBeGreaterThan(0); + const second = await page.evaluate(async () => { + const f = window.hcCheckFixture; + f.select(); + const completed = await f.run(); + return { + status: completed.status, + after: f.host.counts(), + command: f.snapshot()?.command, + rows: await f.host.stored(), + qualified: !!f.qualification() + }; + }); + expect(second.status).toBe('qualified'); + expect(second.qualified).toBe(true); + expect(second.command).toBe(first.command); + expect(second.after.signs).toBe(first.before.signs); + expect(second.after.encrypts).toBe(first.before.encrypts); + expect(probeRow(second.rows[0].wire).self).toEqual( + probeRow(first.candidate!.wire).self + ); + expect(frames.filter((frame) => frame.type === 'EVENT')).toHaveLength(3); + expect( + new Set( + frames + .filter((frame) => frame.type === 'EVENT') + .map((frame) => frame.id) + ) + ).toEqual(new Set([probeRow(first.candidate!.wire).self!.eventId])); + }); +} diff --git a/web/tests/provenance/output.test.mjs b/web/tests/provenance/output.test.mjs @@ -620,6 +620,8 @@ import { auditOutput } from '../../tools/check-output.mjs'; 'src/lib/messaging/history-pagination.ts', 'src/lib/persistence/inbox-probe-cleanup.ts', 'src/lib/messaging/inbox-access-qualification.ts', + 'src/lib/messaging/inbox-access-controller.ts', + 'src/lib/messaging/inbox-access-check.ts', 'src/lib/persistence/private-records.ts' ]) { for (const change of ['changed', 'missing']) { @@ -1983,6 +1985,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; 'src/lib/persistence/inbox-probes.ts', 'src/lib/persistence/inbox-probe-cleanup.ts', 'src/lib/messaging/inbox-access-qualification.ts', + 'src/lib/messaging/inbox-access-check.ts', 'src/lib/nostr/inbox-access.ts', 'src/lib/nostr/seal-builder.ts', 'src/lib/nostr/giftwrap-builder.ts', diff --git a/web/tests/unit/inbox-access-check.test.ts b/web/tests/unit/inbox-access-check.test.ts @@ -0,0 +1,97 @@ +import { afterEach, expect, it, vi } from 'vitest'; +afterEach(() => vi.unstubAllGlobals()); +for (const browser of [false, true]) { + it(`cold ${browser ? 'browser-shaped' : 'SSR'} check and review forgeries have no effects`, async () => { + vi.resetModules(); + let effects = 0; + const trap = () => { + effects++; + throw Error('unadmitted check effect'); + }; + if (browser) { + vi.stubGlobal('window', { + get nostr() { + return trap(); + } + }); + vi.stubGlobal('navigator', { + get locks() { + return trap(); + } + }); + vi.stubGlobal('indexedDB', { open: trap }); + vi.stubGlobal('WebSocket', trap); + } + const api = await import('../../src/lib/messaging/inbox-access-check.ts'); + const access = + await import('../../src/lib/messaging/inbox-access-controller.ts'); + type Args = Parameters<typeof api.createInboxAccessCheck>; + const args = [{}, {}, {}, {}, {}, {}] as unknown as Args; + expect(api.createInboxAccessCheck(...args)).toBeUndefined(); + for (const value of [ + undefined, + null, + false, + 0, + '', + Symbol(), + {}, + { + current: () => true, + state: 'qualified', + sourceOnly: false, + mode: 'original', + disclosures: ['reviewed'], + command: 'a'.repeat(64) + } + ]) { + const token = value as Parameters<typeof api.inboxAccessCheckSnapshot>[0]; + const review = value as Parameters< + typeof api.inboxAccessCheckReviewSnapshot + >[0]; + expect(api.inboxAccessCheckSnapshot(token)).toBeUndefined(); + expect(api.inboxAccessCheckQualification(token)).toBeUndefined(); + expect(api.inboxAccessCheckReviewSnapshot(review)).toBeUndefined(); + expect( + api.reviewInboxAccessCheck(token, 'original', 'wss://inbox.example.org') + ).toBeUndefined(); + expect(api.reviewInboxAccessCheckCleanup(token)).toBeUndefined(); + expect(api.reviewInboxAccessCheckReconciliation(token)).toBeUndefined(); + expect( + (await api.runInboxAccessCheck(review, 'reviewed_inbox_access_check')) + .status + ).toBe('invalid'); + expect( + ( + await api.cleanupInboxAccessCheck( + review, + 'reviewed_delete_local_inbox_probe' + ) + ).status + ).toBe('invalid'); + expect(api.stopInboxAccessCheck(token)).toBe(false); + expect( + ( + await api.reconcileInboxAccessCheck( + review, + 'reviewed_inbox_access_check_reconciliation' + ) + ).status + ).toBe('invalid'); + expect( + api.retryInboxAccessCheckCleanup( + token, + 'reviewed_inbox_access_check_cleanup_retry' + ) + ).toBe(false); + expect( + access.inboxAccessControllerSourceSnapshot( + value as Parameters< + typeof access.inboxAccessControllerSourceSnapshot + >[0] + ) + ).toBeUndefined(); + } + expect(effects).toBe(0); + }); +} diff --git a/web/tools/check-output.mjs b/web/tools/check-output.mjs @@ -238,6 +238,8 @@ async function operationalFlagsAdmission(web) { if (!shell.includes('../runtime/capabilities.ts')) return false; /** @type {Record<string,string>} */ const producers = { + 'src/lib/messaging/inbox-access-check.ts': + '4245812c94b80ef39a1107d6c5c65b8d5775bedc9d5762db802e79a8d4d63c83', // Exact previous AccountGate remains compatibility history; the only // current alternative changes its obsolete condition copy below. 'src/lib/components/AccountGate.svelte': @@ -329,7 +331,7 @@ async function operationalFlagsAdmission(web) { 'src/lib/persistence/inbox-probe-cleanup.ts': 'bfc24be3530471cccd01d387ed2b38a6534eb6493ce850ba9711cc3cfa77e6d9', 'src/lib/messaging/inbox-access-qualification.ts': - 'f936a1507661eaa9f760b7f1b9775fac4893ad6d1f61b27c72eee0ffbf7990a3', + '769e849796486f6845850a2215775bdfb4d37df4758d52261da38709cdd445d5', 'src/lib/nostr/inbox-access.ts': '2d9ba6c6ef1e11a1296ab377fd35c4b3e6fdff22de3f97cc85cfe0fc15baa015', 'src/lib/nostr/seal-builder.ts': @@ -337,7 +339,7 @@ async function operationalFlagsAdmission(web) { 'src/lib/nostr/giftwrap-builder.ts': 'faf54dd80a0995ac94b4b994d3cfd015516e5e201a1fbb6340e13c836ce49e98', 'src/lib/messaging/inbox-access-controller.ts': - '2e0d551f1e373603422cd6ad6d17bc2f291b205fbcca9b2a3420649b1cd74975', + '5b8ecea98a087f6038f8e0d782f0028a5c899bdf756445b725f1da5761d8bc98', 'src/lib/messaging/inbox-setup-view.ts': 'e85238d20f1bbcb23599b36e67910f3df784efa43f0fa0b0b7f3da7e7fd33c79', 'src/lib/messaging/inbox-setup.ts': @@ -530,6 +532,8 @@ async function privacyDataAdmission(web) { return false; /** @type {Record<string,string>} */ const producers = { + 'src/lib/messaging/inbox-access-check.ts': + '4245812c94b80ef39a1107d6c5c65b8d5775bedc9d5762db802e79a8d4d63c83', 'src/lib/catalog/observations.ts': '7386470e698156f2480de1d6c9fcffacab507f97d2f4006df926db467327120c', 'src/lib/catalog/product-view.ts': @@ -611,7 +615,7 @@ async function privacyDataAdmission(web) { 'src/lib/persistence/inbox-probe-cleanup.ts': 'bfc24be3530471cccd01d387ed2b38a6534eb6493ce850ba9711cc3cfa77e6d9', 'src/lib/messaging/inbox-access-qualification.ts': - 'f936a1507661eaa9f760b7f1b9775fac4893ad6d1f61b27c72eee0ffbf7990a3', + '769e849796486f6845850a2215775bdfb4d37df4758d52261da38709cdd445d5', 'src/lib/nostr/inbox-access.ts': '2d9ba6c6ef1e11a1296ab377fd35c4b3e6fdff22de3f97cc85cfe0fc15baa015', 'src/lib/nostr/seal-builder.ts': @@ -619,7 +623,7 @@ async function privacyDataAdmission(web) { 'src/lib/nostr/giftwrap-builder.ts': 'faf54dd80a0995ac94b4b994d3cfd015516e5e201a1fbb6340e13c836ce49e98', 'src/lib/messaging/inbox-access-controller.ts': - '2e0d551f1e373603422cd6ad6d17bc2f291b205fbcca9b2a3420649b1cd74975', + '5b8ecea98a087f6038f8e0d782f0028a5c899bdf756445b725f1da5761d8bc98', 'src/lib/messaging/inbox-setup-view.ts': '4ec9025232bb1355fef1ba039b46ce0a2be10e02e348e1b4eaadc3d1f3c2fd84', 'src/lib/messaging/inbox-sync.ts': @@ -2174,6 +2178,10 @@ async function inboxSetupAdmission(web) { /** @param {string} web */ async function inboxProbeStorageAdmission(web) { const producers = { + 'src/lib/messaging/inbox-access-check.ts': + '4245812c94b80ef39a1107d6c5c65b8d5775bedc9d5762db802e79a8d4d63c83', + 'src/lib/messaging/inbox-access-controller.ts': + '5b8ecea98a087f6038f8e0d782f0028a5c899bdf756445b725f1da5761d8bc98', 'src/lib/persistence/inbox-envelope-repository.ts': 'beb7eeef35be8b81ff03c9080e780528e37f1cd5c0be4f7b32575b1e60994bd3', 'src/lib/nostr/inbox-ingress.ts': @@ -2191,7 +2199,7 @@ async function inboxProbeStorageAdmission(web) { 'src/lib/persistence/inbox-probe-cleanup.ts': 'bfc24be3530471cccd01d387ed2b38a6534eb6493ce850ba9711cc3cfa77e6d9', 'src/lib/messaging/inbox-access-qualification.ts': - 'f936a1507661eaa9f760b7f1b9775fac4893ad6d1f61b27c72eee0ffbf7990a3' + '769e849796486f6845850a2215775bdfb4d37df4758d52261da38709cdd445d5' }; for (const [name, pin] of Object.entries(producers)) { const bytes = await readOwned(web, name);