app

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

commit 601b9b59d003f72d0f01987625ec8e20ae3c98d5
parent 687c5f9e3baee70c74044b46e8b86b43a23f9fdb
Author: triesap <tyson@radroots.org>
Date:   Wed, 14 Oct 2026 12:21:30 +0000

harvestcircle_web: Verify original probe recipient readback

- Bind exact recipient requests to separate permissions and the original probe action.
- Verify signed artifact fields while retaining bounded SDK-observed wire bytes.
- Persist genuine readback facts through exact native lineage and retryable cleanup.
- Cover hostile AUTH, budgets, disposal, provenance and shared ordinary workflows.

Diffstat:
Mweb/src/lib/nostr/private-pool.ts | 470++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mweb/src/lib/nostr/private-publisher.ts | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Mweb/src/lib/persistence/inbox-probes.ts | 41++++++++++++++++++++++++++++++++++-------
Mweb/src/lib/persistence/private-records.ts | 45+++++++++++++++++++++++++++++++++++++++++----
Aweb/tests/e2e/harness/inbox-probe-readback.ts | 142+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/e2e/inbox-probe-readback.spec.ts | 1285+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/provenance/output.test.mjs | 3++-
Mweb/tests/provenance/source-boundaries.test.mjs | 2+-
Aweb/tests/unit/inbox-probe-readback.test.ts | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/unit/private-access-records.test.ts | 17++++++++++++++++-
Mweb/tools/check-output.mjs | 20+++++++++++---------
Mweb/tools/source-boundaries.mjs | 2+-
12 files changed, 2254 insertions(+), 42 deletions(-)

diff --git a/web/src/lib/nostr/private-pool.ts b/web/src/lib/nostr/private-pool.ts @@ -1,3 +1,5 @@ +import { RelayClosedError } from 'applesauce-relay/relay'; +import { inboxProbeReadbackMatches } from '../persistence/private-records.ts'; import { privateInboxQueries } from './inbox-queries.ts'; import { createScopedRelayPool } from './pool-factory.ts'; import { NEVER } from 'rxjs'; @@ -8,7 +10,10 @@ import { type PrivateDeliveryRole, inboxProbePublicationSnapshot, takeInboxProbePublication, - type InboxProbePublication + type InboxProbePublication, + inboxProbeReadbackSnapshot, + takeInboxProbeReadback, + type InboxProbeReadback } from './private-publisher.ts'; import { takeInboxAuthAdmission, @@ -56,6 +61,38 @@ export type InboxProbeArchiveAttemptResult = Readonly<{ actionId?: string; attempt?: number; }>; +export type InboxProbeReadbackAttemptResult = Readonly<{ + status: + | 'readback' + | 'empty' + | 'refused' + | 'error' + | 'budget' + | 'unknown' + | 'timed_out' + | 'stopped'; + role?: 'access_readback'; + origin?: string; + eventId?: string; + actionId?: string; + attempt?: number; + deliveries?: number; +}>; +const probeReadEvidence = new WeakMap< + InboxProbeReadbackAttemptResult, + Readonly<{ + repository: PrivateStorageRepository; + acknowledgement: import('../persistence/inbox-probes.ts').InboxProbeAcknowledgement; + predecessorWire: string; + receiptWire: string; + }> +>(); +export function inboxProbeReadbackAttemptEvidence( + result: InboxProbeReadbackAttemptResult +) { + const evidence = probeReadEvidence.get(result); + return evidence && { ...evidence }; +} type PublicationAttemptResult = PrivateGiftWrapAttemptResult | InboxProbeArchiveAttemptResult; const probeAttemptEvidence = new WeakMap< @@ -159,6 +196,16 @@ type Owner = { ) => boolean, retainPublicationCleanup?: (cleanup: () => void) => () => void ): Promise<PublicationAttemptResult>; + readProbe( + permission: InboxProbeReadback, + signal: AbortSignal, + scopeCurrent?: () => boolean, + retainConnection?: ( + origin: string, + cleanup: ScopedConnectionCleanup + ) => boolean, + retainCleanup?: (cleanup: () => void) => () => void + ): Promise<InboxProbeReadbackAttemptResult>; auth( origin: string, admission: InboxAuthAdmission, @@ -316,6 +363,23 @@ function scopedPool( ) : Promise.resolve({ status: 'stopped' }); }, + readProbe(permission, signal) { + const view = inboxProbeReadbackSnapshot(permission); + return current() && view && readOrigins.includes(view.origin) + ? base.readProbe( + permission, + AbortSignal.any([signal, life.signal]), + current, + retainConnection, + (cleanup) => { + probePublicationStops.set(cleanup, true); + return () => { + probePublicationStops.delete(cleanup); + }; + } + ) + : Promise.resolve({ status: 'stopped' }); + }, async auth(origin, admission) { if (!current() || !origins.includes(origin)) return undefined; const connection = await base.auth( @@ -501,10 +565,15 @@ export function getPrivatePool( const connections = new Map<string, Connection>(); function probeRelayAuthReady( relay: ReturnType<typeof sdk.relay>, - origin: string + origin: string, + role: 'publish' | 'read' = 'publish' ) { let required: boolean | undefined; - const observation = relay.authRequiredForPublish$.subscribe((value) => { + const observation = ( + role === 'read' + ? relay.authRequiredForRead$ + : relay.authRequiredForPublish$ + ).subscribe((value) => { required = value; }); observation.unsubscribe(); @@ -1019,6 +1088,388 @@ export function getPrivatePool( publishing = false; } }, + async readProbe( + permission, + signal, + scopeCurrent = () => true, + retainConnection, + retainCleanup + ) { + const view = inboxProbeReadbackSnapshot(permission); + if (!view) return { status: 'stopped' }; + function stopped() { + return ( + closed || + cleanupRequired || + publicationCleanupRequired || + signal.aborted || + !scopeCurrent() || + !capture.current() || + authRetiring.has(view!.origin) || + authFailed.has(view!.origin) + ); + } + if ( + publishing || + stopped() || + view.owner !== capture.owner || + !readOrigins.includes(view.origin) || + typeof window === 'undefined' || + !navigator.locks?.request + ) + return { status: 'stopped' }; + publishing = true; + try { + const previous = connections.get(view.origin); + if (previous) { + if (previous.retiring()) return { status: 'stopped' }; + const observation = previous.reserve(); + try { + if ( + !observation.current() || + !probeRelayAuthReady(observation.relay, view.origin, 'read') + ) + return { status: 'stopped' }; + } finally { + observation.close(); + } + } + if (!(await recheckPrivateSession(session)) || stopped()) + return { status: 'stopped' }; + return await navigator.locks.request( + 'harvestcircle:owner:' + capture.owner, + { mode: 'exclusive', ifAvailable: true }, + async (lock) => { + if (!lock || stopped()) return { status: 'stopped' as const }; + const admitted = await takeInboxProbeReadback( + permission, + session, + policy, + view.origin + ); + if (!admitted || stopped() || !admitted.current()) + return { status: 'stopped' as const }; + const connection = leaseConnection(view.origin); + if ( + !connection.current() || + !probeRelayAuthReady(connection.relay, view.origin, 'read') || + stopped() || + !admitted.current() + ) { + connection.close(); + return { status: 'stopped' as const }; + } + const network = admitted.beginNetwork(); + if (!network) { + connection.close(); + return { status: 'stopped' as const }; + } + return await new Promise<InboxProbeReadbackAttemptResult>( + (resolve) => { + const budget = finiteBudget(); + let ended = false, + attempted = false, + installing = false, + releasing = false, + retainedConnection = false, + release = () => {}, + forgetCleanup = () => {}, + pending: + InboxProbeReadbackAttemptResult['status'] | undefined, + observedWire: string | undefined, + timer: ReturnType<typeof setTimeout> | undefined, + fenceTimer: ReturnType<typeof setTimeout> | undefined; + const pageStarted = performance.now(); + let lastClock = pageStarted; + function stale() { + return ( + stopped() || !admitted!.current() || !connection.current() + ); + } + function remainingPage() { + const now = performance.now(); + if ( + !Number.isFinite(now) || + now < lastClock || + !Number.isFinite(pageStarted) + ) + return 0; + lastClock = now; + return Math.max( + 0, + PRIVATE_TRANSPORT_BUDGETS.pageMilliseconds - + (now - pageStarted) + ); + } + function expired() { + return remainingPage() <= 0 || network!.remaining() <= 0; + } + function disposeOriginal() { + if (installing || releasing) + throw Error('private_probe_read_cleanup_pending'); + releasing = true; + try { + release(); + if (!retainedConnection) connection.close(); + publicationStops.delete(stop); + publicationFailures.delete(stop); + publicationCleanupRequired = publicationFailures.size !== 0; + forgetCleanup(); + } finally { + releasing = false; + } + } + function retainFailure() { + publicationStops.set(stop, true); + publicationFailures.set(stop, true); + publicationCleanupRequired = true; + forgetCleanup = retainCleanup?.(stop) ?? (() => {}); + } + function finish( + status: InboxProbeReadbackAttemptResult['status'] + ) { + if (ended) return; + if (installing) { + pending ??= status; + return; + } + ended = true; + network!.finish(); + if (timer !== undefined) clearTimeout(timer); + if (fenceTimer !== undefined) clearTimeout(fenceTimer); + signal.removeEventListener('abort', onAbort); + try { + disposeOriginal(); + } catch { + retainFailure(); + status = 'unknown'; + } + if (!attempted) status = 'stopped'; + const result: InboxProbeReadbackAttemptResult = { + status, + role: 'access_readback', + origin: admitted!.origin, + eventId: admitted!.eventId, + deliveries: budget.count(), + ...(attempted + ? { + actionId: network!.actionId, + attempt: network!.attempt + } + : {}) + }; + if ( + attempted && + status === 'readback' && + observedWire !== undefined + ) { + probeReadEvidence.set(result, { + repository: admitted!.repository, + acknowledgement: admitted!.acknowledgement, + predecessorWire: admitted!.predecessorWire, + receiptWire: JSON.stringify({ + actionId: network!.actionId, + role: 'access_readback', + origin: admitted!.origin, + eventId: admitted!.eventId, + attempt: network!.attempt, + status: 'readback', + observedAtMilliseconds: Date.now(), + readbackWire: observedWire + }) + }); + } + resolve(result); + } + // Abort dispatch cannot propagate a synchronous cleanup exception. + // Explicit retained Stop still reports pending original installation. + function onAbort() { + finish('stopped'); + } + function stop() { + if (ended) { + disposeOriginal(); + return; + } + finish('stopped'); + if (installing) + throw Error('private_probe_read_cleanup_pending'); + } + function fence() { + if (ended) return; + if (stale()) { + finish('stopped'); + return; + } + if (expired()) { + finish('timed_out'); + return; + } + fenceTimer = setTimeout(fence, 50); + } + try { + publicationStops.set(stop, true); + forgetCleanup = retainCleanup?.(stop) ?? (() => {}); + signal.addEventListener('abort', onAbort, { once: true }); + if (stale() || expired()) { + finish(stale() ? 'stopped' : 'timed_out'); + return; + } + connection.start(); + retainedConnection = + retainConnection?.(view.origin, { + current: connection.current, + close: connection.close + }) ?? false; + if ( + stale() || + !probeRelayAuthReady(connection.relay, view.origin, 'read') + ) { + finish('stopped'); + return; + } + if (expired()) { + finish('timed_out'); + return; + } + installing = true; + attempted = true; + // Exact original self artifact/recipient only. SDK owns transport; + // no caller filter, socket, key or auto AUTH/retry port is exposed. + const subscription = sdk + .req( + [view.origin], + [ + { + kinds: [1059], + ids: [admitted.eventId], + '#p': [admitted.owner], + limit: 1 + } + ], + { + waitForAuth: false, + reconnect: false, + resubscribe: false + } + ) + .subscribe({ + next(message) { + if (ended || pending !== undefined) return; + if (stale()) { + finish('stopped'); + return; + } + if (expired()) { + finish('timed_out'); + return; + } + if ( + message.type !== 'EVENT' && + message.from !== view.origin && + message.from !== view.origin + '/' + ) { + finish('error'); + return; + } + if (message.type === 'EVENT') { + const wire = JSON.stringify(message.event); + if ( + typeof wire !== 'string' || + !boundedUtf8(wire, budget.remainingBytes()) + ) { + budget.charge(budget.remainingBytes() + 1); + finish('budget'); + return; + } + if ( + !budget.charge( + new TextEncoder().encode(wire).length + ) + ) { + finish('budget'); + return; + } + // Charge delivered work before source or signature rejection. + if ( + message.from !== view.origin && + message.from !== view.origin + '/' + ) { + finish('error'); + return; + } + if ( + !boundedUtf8( + wire, + PRIVATE_TRANSPORT_BUDGETS.envelopeBytes + ) + ) + return; + if ( + inboxProbeReadbackMatches( + { + eventId: admitted.eventId, + wire: JSON.stringify(admitted.event) + }, + admitted.owner, + wire + ) + ) { + observedWire = wire; + finish('readback'); + } + } else if (message.type === 'EOSE') finish('empty'); + else if (message.type === 'CLOSED') finish('refused'); + else if (message.type === 'ERROR') + finish( + message.error instanceof RelayClosedError + ? 'refused' + : 'error' + ); + }, + error: (error: unknown) => + finish( + stale() + ? 'stopped' + : error instanceof RelayClosedError + ? 'refused' + : 'error' + ), + complete: () => finish(stale() ? 'stopped' : 'empty') + }); + release = () => subscription.unsubscribe(); + installing = false; + if (pending !== undefined) { + finish(pending); + return; + } + if (stale()) { + finish('stopped'); + return; + } + if (expired()) { + finish('timed_out'); + return; + } + timer = setTimeout( + () => finish(stale() ? 'stopped' : 'timed_out'), + Math.min(remainingPage(), network.remaining()) + ); + fence(); + } catch { + installing = false; + finish(stale() ? 'stopped' : 'unknown'); + } + } + ); + } + ); + } catch { + return { status: 'stopped' }; + } finally { + publishing = false; + } + }, async auth( origin, admission, @@ -1963,6 +2414,19 @@ export function publishInboxProbeArchiveAttempt( return Promise.resolve({ status: 'stopped' }); } } +export function readInboxProbeAttempt( + pool: PrivatePool, + permission: InboxProbeReadback, + signal: AbortSignal +): Promise<InboxProbeReadbackAttemptResult> { + try { + if (!inboxProbeReadbackSnapshot(permission)) + return Promise.resolve({ status: 'stopped' }); + return ownerOf(pool).readProbe(permission, signal); + } catch { + return Promise.resolve({ status: 'stopped' }); + } +} export function beginPrivateAuthConnection( pool: PrivatePool, origin: string, diff --git a/web/src/lib/nostr/private-publisher.ts b/web/src/lib/nostr/private-publisher.ts @@ -519,6 +519,7 @@ export function captureInboxProbePublicationAction( } } type ProbePermission = { + role: 'access_archive' | 'access_readback'; action: ProbeAction; acknowledgement: InboxProbeAcknowledgement; wire: string; @@ -531,18 +532,21 @@ type ProbePermission = { take(): boolean; }; const probePermissions = new WeakMap<InboxProbePublication, ProbePermission>(); -export function captureInboxProbePublication( +declare const probeReadbackBrand: unique symbol; +export type InboxProbeReadback = Readonly<{ [probeReadbackBrand]: true }>; +const probeReadbacks = new WeakMap<InboxProbeReadback, ProbePermission>(); +function captureProbePermission( repository: PrivateStorageRepository, session: PrivateSession, policy: RelayPolicy, acknowledgement: InboxProbeAcknowledgement, action: InboxProbePublicationAction, origin: string, - review: unknown -): InboxProbePublication | undefined { + role: ProbePermission['role'] +): ProbePermission | undefined { if ( typeof window === 'undefined' || - review !== 'reviewed_inbox_probe_publication' + (role !== 'access_archive' && role !== 'access_readback') ) return undefined; try { @@ -563,12 +567,17 @@ export function captureInboxProbePublication( !captured.current() || captured.original !== probeImmutable(binding.record) || !binding.record.self || - !binding.record.binding.archiveTargets.includes(origin) + !( + role === 'access_archive' + ? binding.record.binding.archiveTargets + : binding.record.binding.readTargets + ).includes(origin) ) return undefined; if ( !readRelayPolicy(policy).inbox.some( - ({ origin: allowed, write }) => allowed === origin && write + ({ origin: allowed, read, write }) => + allowed === origin && (role === 'access_archive' ? write : read) ) ) return undefined; @@ -600,8 +609,8 @@ export function captureInboxProbePublication( probeImmutable(fresh.record) === captured!.original ); } - const token = Object.freeze({}) as InboxProbePublication; - probePermissions.set(token, { + return { + role, action: captured, acknowledgement, wire: artifact.wire, @@ -616,12 +625,71 @@ export function captureInboxProbePublication( consumed = true; return true; } - }); - return token; + }; } catch { return undefined; } } +export function captureInboxProbePublication( + repository: PrivateStorageRepository, + session: PrivateSession, + policy: RelayPolicy, + acknowledgement: InboxProbeAcknowledgement, + action: InboxProbePublicationAction, + origin: string, + review: unknown +): InboxProbePublication | undefined { + if (review !== 'reviewed_inbox_probe_publication') return undefined; + const saved = captureProbePermission( + repository, + session, + policy, + acknowledgement, + action, + origin, + 'access_archive' + ); + if (!saved) return undefined; + const token = Object.freeze({}) as InboxProbePublication; + probePermissions.set(token, saved); + return token; +} +export function captureInboxProbeReadback( + repository: PrivateStorageRepository, + session: PrivateSession, + policy: RelayPolicy, + acknowledgement: InboxProbeAcknowledgement, + action: InboxProbePublicationAction, + origin: string, + review: unknown +): InboxProbeReadback | undefined { + if (review !== 'reviewed_inbox_probe_readback') return undefined; + const saved = captureProbePermission( + repository, + session, + policy, + acknowledgement, + action, + origin, + 'access_readback' + ); + if (!saved) return undefined; + const token = Object.freeze({}) as InboxProbeReadback; + probeReadbacks.set(token, saved); + return token; +} +export function inboxProbeReadbackSnapshot(permission: InboxProbeReadback) { + const saved = probeReadbacks.get(permission); + return saved?.current() + ? { + owner: saved.owner, + command: saved.command, + role: 'access_readback' as const, + origin: saved.origin, + eventId: saved.eventId + } + : undefined; +} export function inboxProbePublicationSnapshot( permission: InboxProbePublication ) { @@ -636,13 +704,12 @@ export function inboxProbePublicationSnapshot( } : undefined; } -export async function takeInboxProbePublication( - permission: InboxProbePublication, +async function takeProbePermission( + saved: ProbePermission | undefined, session: PrivateSession, policy: RelayPolicy, origin: string ) { - const saved = probePermissions.get(permission); if ( !saved || saved.action.session !== session || @@ -679,13 +746,42 @@ export async function takeInboxProbePublication( current: () => saved.current(), owner: saved.owner, command: saved.command, - role: 'access_archive' as const, + role: saved.role, origin, eventId: saved.eventId, networkMetered: true, beginNetwork: () => - saved.action.meter.begin('access_archive', origin, saved.eventId, () => + saved.action.meter.begin(saved.role, origin, saved.eventId, () => saved.current() ) }; } + +export async function takeInboxProbePublication( + permission: InboxProbePublication, + session: PrivateSession, + policy: RelayPolicy, + origin: string +) { + const admitted = await takeProbePermission( + probePermissions.get(permission), + session, + policy, + origin + ); + return admitted && { ...admitted, role: 'access_archive' as const }; +} +export async function takeInboxProbeReadback( + permission: InboxProbeReadback, + session: PrivateSession, + policy: RelayPolicy, + origin: string +) { + const admitted = await takeProbePermission( + probeReadbacks.get(permission), + session, + policy, + origin + ); + return admitted && { ...admitted, role: 'access_readback' as const }; +} diff --git a/web/src/lib/persistence/inbox-probes.ts b/web/src/lib/persistence/inbox-probes.ts @@ -1,6 +1,8 @@ import { inboxProbeArchiveAttemptEvidence, - type InboxProbeArchiveAttemptResult + type InboxProbeArchiveAttemptResult, + inboxProbeReadbackAttemptEvidence, + type InboxProbeReadbackAttemptResult } from '../nostr/private-pool.ts'; import { inboxProbePolicyMatches } from '../nostr/inbox-access.ts'; import type { RelayPolicy } from '../config/relays.ts'; @@ -413,14 +415,15 @@ export type InboxProbeArchiveCommitResult = Readonly<{ disposition: 'original_protected'; }>; const appliedArchiveEvidence = new WeakMap< - InboxProbeArchiveAttemptResult, + InboxProbeArchiveAttemptResult | InboxProbeReadbackAttemptResult, Lineage >(); // Only actual SDK attempt observations can advance memory-only provenance. // This bookkeeping can settle after owner loss, but cannot authorize traffic. -export async function commitInboxProbeArchiveAttempt( +async function commitProbeAttempt( repository: PrivateStorageRepository, - result: InboxProbeArchiveAttemptResult + result: InboxProbeArchiveAttemptResult | InboxProbeReadbackAttemptResult, + role: 'access_archive' | 'access_readback' ): Promise<InboxProbeArchiveCommitResult> { const refuse = ( status: InboxProbeArchiveCommitResult['status'] @@ -428,7 +431,14 @@ export async function commitInboxProbeArchiveAttempt( status, disposition: 'original_protected' }); - const effect = inboxProbeArchiveAttemptEvidence(result); + const effect = + role === 'access_archive' + ? inboxProbeArchiveAttemptEvidence( + result as InboxProbeArchiveAttemptResult + ) + : inboxProbeReadbackAttemptEvidence( + result as InboxProbeReadbackAttemptResult + ); if (!effect || effect.repository !== repository) return refuse('invalid'); const saved = acknowledgements.get(effect.acknowledgement), lineage = saved?.lineage; @@ -486,9 +496,13 @@ export async function commitInboxProbeArchiveAttempt( return refuse('invalid_record'); const last = next.receipts[next.receipts.length - 1]; if ( - last.role !== 'access_archive' || + last.role !== role || last.eventId !== lineage.record().self!.eventId || - !lineage.record().binding.archiveTargets.includes(last.origin) + !( + role === 'access_archive' + ? lineage.record().binding.archiveTargets + : lineage.record().binding.readTargets + ).includes(last.origin) ) return refuse('invalid_record'); const committed = await commitPrivateRecord( @@ -528,3 +542,16 @@ export async function commitInboxProbeArchiveAttempt( lineage.finish(); } } + +export function commitInboxProbeArchiveAttempt( + repository: PrivateStorageRepository, + result: InboxProbeArchiveAttemptResult +): Promise<InboxProbeArchiveCommitResult> { + return commitProbeAttempt(repository, result, 'access_archive'); +} +export function commitInboxProbeReadbackAttempt( + repository: PrivateStorageRepository, + result: InboxProbeReadbackAttemptResult +): Promise<InboxProbeArchiveCommitResult> { + return commitProbeAttempt(repository, result, 'access_readback'); +} diff --git a/web/src/lib/persistence/private-records.ts b/web/src/lib/persistence/private-records.ts @@ -16,7 +16,10 @@ import { inboxOperatorQualificationSnapshot, validateInboxOperatorQualification } from '../config/inbox-operators.ts'; -import { incomingGiftWrapTags } from '../nostr/incoming-wire.ts'; +import { + incomingGiftWrapTags, + parseIncomingLayer +} from '../nostr/incoming-wire.ts'; import type { InboxRouteSnapshot } from '../messaging/inbox-routing.ts'; import { LOCAL_PERSISTENCE_BUDGETS, @@ -544,10 +547,43 @@ function probeBinding(value: unknown): value is PrivateAccessProbeBinding { targets(value.readTargets, 'read') && targets(value.archiveTargets, 'write') ); } +// Pure bounded metadata comparison only. This does not prove a relay effect, +// original websocket bytes, nonce decryption, readiness or live ACK lineage. +export function inboxProbeReadbackMatches( + self: PrivateCiphertextArtifact, + owner: string, + observedWire: unknown +): boolean { + if ( + !artifact(self, owner) || + parseIncomingLayer( + observedWire, + PRIVATE_TRANSPORT_BUDGETS.envelopeBytes + ) === undefined + ) + return false; + const original = verifyEnvelope(self.wire), + observed = verifyEnvelope(observedWire), + before = original.ok && verifiedEnvelopeSnapshot(original.value), + after = observed.ok && verifiedEnvelopeSnapshot(observed.value); + if (!before || !after) return false; + const core = (event: typeof before) => + JSON.stringify([ + event.id, + event.pubkey, + event.kind, + event.created_at, + event.tags, + event.content, + event.sig + ]); + return core(before) === core(after); +} function probeReceipt( value: unknown, self: PrivateCiphertextArtifact, - binding: PrivateAccessProbeBinding + binding: PrivateAccessProbeBinding, + owner: string ): value is PrivateAccessProbeReceipt { if ( !exactLocalFields(value, [ @@ -582,7 +618,7 @@ function probeReceipt( value.role === 'access_readback' && binding.readTargets.includes(value.origin) && (value.status === 'readback' || value.status === 'nonce_verified') && - value.readbackWire === self.wire + inboxProbeReadbackMatches(self, owner, value.readbackWire) ); } // Strict local storage codec only, never installed-account/effect authorization, @@ -704,7 +740,8 @@ export function decodePrivateRecord( probeReceipt( fact, fields.self as PrivateCiphertextArtifact, - fields.binding as PrivateAccessProbeBinding + fields.binding as PrivateAccessProbeBinding, + owner ) )) ) diff --git a/web/tests/e2e/harness/inbox-probe-readback.ts b/web/tests/e2e/harness/inbox-probe-readback.ts @@ -0,0 +1,142 @@ +import { finalize, tap, map } from 'rxjs'; +import { RelayPool } from 'applesauce-relay/pool'; +import { makePublicationFixture } from './inbox-probe-publication.ts'; +import { captureInboxProbeReadback } from '../../../src/lib/nostr/private-publisher.ts'; +import { + getPrivatePool, + readInboxProbeAttempt, + closePrivatePool, + type PrivatePool, + type InboxProbeReadbackAttemptResult +} from '../../../src/lib/nostr/private-pool.ts'; +import { commitInboxProbeReadbackAttempt } from '../../../src/lib/persistence/inbox-probes.ts'; +export * from './inbox-probe-publication.ts'; +export async function makeReadbackFixture() { + const f = await makePublicationFixture(); + let pool: PrivatePool | undefined, + last: InboxProbeReadbackAttemptResult | undefined; + const consumer = Object.freeze({}); + function permission( + origin = 'wss://inbox.example.org', + review: unknown = 'reviewed_inbox_probe_readback' + ) { + const ack = f.acknowledgement(), + action = f.action(); + return ack && action + ? captureInboxProbeReadback( + f.repository, + f.session, + f.policy, + ack, + action, + origin, + review + ) + : undefined; + } + function acquire() { + pool ??= getPrivatePool( + f.session, + f.policy, + f.snapshot()!.record.binding.readTargets, + consumer, + 'access' + ); + if (!pool) throw Error('actual original access scope'); + return pool; + } + return { + ...f, + readPermission: permission, + readPool: acquire, + async read(token = permission(), signal = new AbortController().signal) { + if (!token) return { status: 'stopped' as const }; + last = await readInboxProbeAttempt(acquire(), token, signal); + return last; + }, + lastRead: () => last, + appendRead: (result = last) => + result + ? commitInboxProbeReadbackAttempt(f.repository, result) + : Promise.resolve({ status: 'invalid' as const }), + installWrongReadSource() { + const original = Object.getOwnPropertyDescriptor( + RelayPool.prototype, + 'req' + )?.value as RelayPool['req']; + RelayPool.prototype.req = function ( + ...args: Parameters<RelayPool['req']> + ) { + return original + .apply(this, args) + .pipe( + map((message) => + message.type === 'EVENT' + ? { ...message, from: 'wss://foreign.example.org' } + : message + ) + ); + }; + return () => { + RelayPool.prototype.req = original; + }; + }, + installReadInstallation(onInstallation: () => void) { + const original = Object.getOwnPropertyDescriptor( + RelayPool.prototype, + 'req' + )?.value as RelayPool['req']; + let fired = false; + RelayPool.prototype.req = function ( + ...args: Parameters<RelayPool['req']> + ) { + return original.apply(this, args).pipe( + tap((message) => { + if (!fired && message.type === 'OPEN') { + fired = true; + onInstallation(); + } + }) + ); + }; + return () => { + RelayPool.prototype.req = original; + }; + }, + installReadTeardownFailure(onTeardown?: () => void) { + const original = Object.getOwnPropertyDescriptor( + RelayPool.prototype, + 'req' + )?.value as RelayPool['req']; + let failing = true, + faults = 0; + RelayPool.prototype.req = function ( + ...args: Parameters<RelayPool['req']> + ) { + return original.apply(this, args).pipe( + finalize(() => { + if (failing) { + faults++; + onTeardown?.(); + throw Error('actual original REQ teardown failure'); + } + }) + ); + }; + return { + faults: () => faults, + restore: () => { + failing = false; + RelayPool.prototype.req = original; + } + }; + }, + stopRead: () => { + if (pool) closePrivatePool(pool); + }, + close() { + if (pool) closePrivatePool(pool); + f.close(); + } + }; +} diff --git a/web/tests/e2e/inbox-probe-readback.spec.ts b/web/tests/e2e/inbox-probe-readback.spec.ts @@ -0,0 +1,1285 @@ +import { test, expect } 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-probe-readback.ts'; +declare global { + interface Window { + rb: typeof Fixture; + rbFixture: Awaited<ReturnType<typeof Fixture.makeReadbackFixture>>; + rbPending?: Promise<unknown>; + rbAbort?: AbortController; + rbOtherPool?: Fixture.PrivatePool; + rbRestore?: () => void; + rbAdvancePage?: () => void; + rbAdvanceOnSocket?: 'network' | 'page'; + rbClock?: number; + rbNetworkClock?: ReturnType<Window['rbFixture']['installNetworkClock']>; + rbDelayClose?: boolean; + rbFailClose?: boolean; + rbInvalidateOnSocket?: boolean; + rbNestedCleanup?: number; + rbNestedFailure?: number; + rbHeldClose?: (() => void)[]; + } +} +let server: Awaited<ReturnType<typeof createStaticHarness>>, + sockets: WebSocketServer, + endpoint: string, + bundle: string; +let readMode = 'exact'; +const archived = new Map<string, Record<string, unknown>>(); +let head = '', + response: 'accepted' | 'refused' | 'unknown' | 'wrong' | 'auth_required' = + 'accepted', + auth = false; +const frames: { origin: string; frame: unknown[] }[] = [], + physical = new Set<WebSocket>(); +let peak = 0, + privatePeak = 0; +const privatePhysical = new Set<WebSocket>(); +const privateConnections = new Map<string, number>(); +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 loorback fixture'); + endpoint = 'ws://127.0.0.1:' + address.port; + sockets.on('connection', (socket, request) => { + physical.add(socket); + peak = Math.max(peak, physical.size); + socket.on('close', () => physical.delete(socket)); + const origin = new URL(request.url!, 'http://127.0.0.1').searchParams.get( + 'origin' + )!; + if (origin !== 'wss://discovery.example.org') { + privatePhysical.add(socket); + privateConnections.set(origin, (privateConnections.get(origin) ?? 0) + 1); + privatePeak = Math.max(privatePeak, privatePhysical.size); + socket.on('close', () => privatePhysical.delete(socket)); + } + if (auth && origin === 'wss://inbox.example.org') + socket.send(JSON.stringify(['AUTH', 'probe-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[]; + frames.push({ origin, frame }); + if (frame[0] === 'REQ') { + const filter = frame[2] as { kinds?: number[]; ids?: string[] }; + if (filter.kinds?.includes(10050) && head) + socket.send(JSON.stringify(['EVENT', frame[1], JSON.parse(head)])); + if (filter.kinds?.includes(1059)) { + if (readMode === 'refused' || readMode === 'auth_required') { + socket.send( + JSON.stringify([ + 'CLOSED', + frame[1], + readMode === 'auth_required' + ? 'auth-required: source fixture' + : 'blocked source fixture' + ]) + ); + return; + } + if (readMode === 'unknown') return; + const original = archived.get(filter.ids?.[0] ?? ''); + if (original && readMode !== 'empty') { + const event: Record<string, unknown> = + readMode === 'metadata' + ? { + sig: original.sig, + content: original.content, + tags: original.tags, + created_at: original.created_at, + kind: original.kind, + pubkey: original.pubkey, + id: original.id, + observation: { fixture: true } + } + : { ...original }; + if (readMode === 'wrong_id') event.id = 'f'.repeat(64); + if (readMode === 'wrong_sig') { + event.sig = '0'.repeat(128); + event.verified = true; + } + if (readMode === 'wrong_recipient') + event.tags = [['p', 'f'.repeat(64)]]; + if (readMode === 'wrong_content') + event.content = String(event.content) + 'a'; + if (readMode === 'oversized' || readMode === 'flood_bytes') + event.content = 'x'.repeat( + readMode === 'oversized' ? 40000 : 31000 + ); + if (readMode === 'flood_deliveries') + event.content = String(event.content) + 'a'; + let wire = JSON.stringify(['EVENT', frame[1], event]); + if (readMode === 'sdk_duplicate') + wire = wire.replace('"kind":1059', '"kind":1059,"kind":1059'); + if (readMode === 'sdk_numeric_alias') + wire = wire.replace('"kind":1059', '"kind":1.059e3'); + for (let n = 0; n < (readMode.startsWith('flood_') ? 501 : 1); n++) + socket.send(wire); + } + } + socket.send(JSON.stringify(['EOSE', frame[1]])); + } else if (frame[0] === 'AUTH') { + socket.send( + JSON.stringify(['OK', (frame[1] as { id: string }).id, true, '']) + ); + } else if (frame[0] === 'EVENT' && response !== 'unknown') { + archived.set( + (frame[1] as { id: string }).id, + frame[1] as Record<string, unknown> + ); + socket.send( + JSON.stringify([ + 'OK', + response === 'wrong' + ? 'f'.repeat(64) + : (frame[1] as { id: string }).id, + response !== 'refused' && response !== 'auth_required', + response === 'auth_required' + ? 'auth-required: source fixture' + : response === 'refused' + ? 'blocked: source fixture' + : '' + ]) + ); + } + }); + }); + const result = await build({ + configFile: false, + logLevel: 'silent', + build: { + write: false, + minify: false, + lib: { + entry: fileURLToPath( + new URL('./harness/inbox-probe-readback.ts', import.meta.url) + ), + name: 'rb', + formats: ['iife'] + } + } + }); + const output = Array.isArray(result) ? result[0] : result; + if (!('output' in output)) throw Error('compiled fixture'); + const chunks = output.output.filter((x) => x.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 = ''; + readMode = 'exact'; + archived.clear(); + response = 'accepted'; + auth = false; + peak = 0; + privatePeak = 0; + privateConnections.clear(); + frames.length = 0; + await page.addInitScript( + ({ endpoint }) => { + const Native = window.WebSocket; + window.WebSocket = class extends Native { + constructor(url: string | URL, protocols?: string | string[]) { + const origin = new URL(String(url)).origin; + if ( + ![ + 'wss://discovery.example.org', + 'wss://inbox.example.org', + 'wss://peer.example.org', + 'wss://archive.example.org' + ].includes(origin) + ) + throw Error('unapproved fixture origin'); + super(endpoint + '/?origin=' + encodeURIComponent(origin), protocols); + if ( + origin === 'wss://inbox.example.org' && + window.rbAdvanceOnSocket + ) { + if (window.rbAdvanceOnSocket === 'network') + window.rbNetworkClock!.advance(45001); + else window.rbAdvancePage!(); + } + if ( + window.rbInvalidateOnSocket && + origin === 'wss://inbox.example.org' + ) + window.rb.closeInboxAccessController( + window.rbFixture.controller()! + ); + } + override close(code?: number, reason?: string) { + if (window.rbFailClose) + throw Error('actual controlled disposal failure'); + if (window.rbDelayClose) + (window.rbHeldClose ??= []).push(() => super.close(code, reason)); + else super.close(code, reason); + } + }; + }, + { endpoint } + ); + await page.goto(server.url + '/search'); + await page.addScriptTag({ content: bundle }); + head = await page.evaluate(async (multi) => { + window.rbFixture = await window.rb.makeReadbackFixture(); + return window.rbFixture.preferenceWire( + multi + ? [ + 'wss://inbox.example.org', + 'wss://peer.example.org', + 'wss://archive.example.org' + ] + : ['wss://inbox.example.org'] + ); + }, testInfo.title.includes('three-origin')); + await page.evaluate(async () => { + const f = window.rbFixture; + await f.capture(); + if ( + (await f.reserve()).status !== 'reserved' || + (await f.prepare()) !== 'wrapped' || + (await f.commitOriginal()).status !== 'prepared' + ) + throw Error('genuine cold native original setup'); + }); +}); +test.afterEach(async ({ page }) => { + if (!page.isClosed()) + await page.evaluate(() => { + window.rbRestore?.(); + window.rbFailClose = false; + window.rbDelayClose = false; + for (const release of window.rbHeldClose ?? []) release(); + window.rbFixture.close(); + }); +}); +function privateEvents() { + return frames.filter( + (x) => x.origin !== 'wss://discovery.example.org' && x.frame[0] === 'EVENT' + ); +} +function privateReads() { + return frames.filter( + (x) => x.origin !== 'wss://discovery.example.org' && x.frame[0] === 'REQ' + ); +} + +async function publishOriginal(page: import('@playwright/test').Page) { + return page.evaluate(async () => { + const f = window.rbFixture, + result = await f.publish(); + await f.append(result); + return result; + }); +} +test('cold actual archived artifact is fetched by exact recipient REQ; original PUB and readback share action and immutable native lineage', async ({ + page +}) => { + expect(privateReads()).toHaveLength(0); + const published = await publishOriginal(page); + readMode = 'metadata'; + const observed = await page.evaluate(async () => { + const f = window.rbFixture, + before = f.snapshot()!.record, + action = f.action(), + token = f.readPermission()!, + result = await f.read(token); + const forged = await f.appendRead({ ...result }), + saved = await f.appendRead(result), + duplicate = await f.appendRead(result), + recommit = await f.commitOriginal(); + const second = await f.read(); + await f.appendRead(second); + const fourth = await f.publish(); + return { + before, + result, + forged, + saved, + duplicate, + recommit: recommit.status, + second, + fourth, + same: f.action() === action, + row: f.snapshot()!.record, + readEvidence: !!window.rb.inboxProbeReadbackAttemptEvidence(result), + archiveEvidence: !!window.rb.inboxProbeArchiveAttemptEvidence( + result as unknown as Parameters< + typeof window.rb.inboxProbeArchiveAttemptEvidence + >[0] + ), + readiness: window.rb.inboxAccessControllerSnapshot(f.controller()!) + ?.readiness + }; + }); + expect(observed.result).toMatchObject({ + status: 'readback', + attempt: 2, + actionId: published.actionId, + deliveries: 1 + }); + expect(observed.second).toMatchObject({ + status: 'readback', + attempt: 3, + actionId: published.actionId + }); + expect(observed.fourth).toEqual({ status: 'stopped' }); + expect(observed.same).toBe(true); + expect(observed.forged.status).toBe('invalid'); + expect(observed.saved.status).toBe('saved'); + expect(observed.duplicate.status).toBe('existing'); + expect(observed.recommit).toBe('existing'); + expect(observed.readEvidence).toBe(true); + expect(observed.archiveEvidence).toBe(false); + expect(observed.readiness).toBe('unavailable'); + expect(observed.row.self).toEqual(observed.before.self); + expect(observed.row.receipts).toHaveLength(3); + const receipt = observed.row.receipts[1]; + expect(receipt.status).toBe('readback'); + expect(receipt.readbackWire).not.toBe(observed.before.self!.wire); + expect(JSON.parse(receipt.readbackWire!)).toMatchObject({ + ...(JSON.parse(observed.before.self!.wire) as Record<string, unknown>), + observation: { fixture: true } + }); + for (const read of privateReads()) + expect(read.frame[2]).toEqual({ + kinds: [1059], + ids: [observed.row.self!.eventId], + '#p': [observed.row.owner], + limit: 1 + }); + expect(privateEvents()).toHaveLength(1); + expect(privateReads()).toHaveLength(2); + expect(privateConnections.get('wss://inbox.example.org')).toBe(1); + expect(privatePeak).toBe(1); + expect(peak).toBeGreaterThan(0); +}); +for (const mode of [ + 'empty', + 'refused', + 'wrong_id', + 'wrong_sig', + 'wrong_recipient', + 'wrong_content', + 'oversized', + 'flood_deliveries', + 'flood_bytes', + 'sdk_duplicate', + 'sdk_numeric_alias' +]) + test(`actual SDK ${mode} preserves truthful bounded observed readback disposition`, async ({ + page + }) => { + await publishOriginal(page); + readMode = mode; + const observed = await page.evaluate(async () => { + const f = window.rbFixture, + result = await f.read(), + appended = await f.appendRead(result); + return { + result, + appended, + row: f.snapshot()!.record, + proof: !!window.rb.inboxProbeReadbackAttemptEvidence(result) + }; + }); + const positive = mode.startsWith('sdk_'); + expect(observed.result.status).toBe( + positive + ? 'readback' + : mode.startsWith('flood_') + ? 'budget' + : mode === 'refused' + ? 'refused' + : 'empty' + ); + expect(observed.proof).toBe(positive); + expect(observed.appended.status).toBe(positive ? 'saved' : 'invalid'); + expect(observed.row.receipts).toHaveLength(positive ? 2 : 1); + if (mode === 'flood_deliveries') + expect(observed.result.deliveries).toBe(501); + if (mode === 'flood_bytes') { + expect(observed.result.deliveries).toBeGreaterThan(200); + expect(observed.result.deliveries).toBeLessThan(500); + } + if (positive) { + const wire = observed.row.receipts[1].readbackWire!; + expect(wire).not.toContain('1.059e3'); + expect(wire.match(/"kind"/g)).toHaveLength(1); + } + }); +test('separate genuine publication/read permissions reject cross-family calls before REQ or extra fresh-key work', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.rbFixture, + api = window.rb, + before = f.counts(), + publication = f.permission()!, + read = f.readPermission()!; + const wrongRead = await api.readInboxProbeAttempt( + f.readPool(), + publication as unknown as Parameters<typeof api.readInboxProbeAttempt>[1], + new AbortController().signal + ); + const wrongWrite = await api.publishInboxProbeArchiveAttempt( + f.pool(), + read as unknown as Parameters< + typeof api.publishInboxProbeArchiveAttempt + >[1], + new AbortController().signal + ); + const copied = await f.read({ ...read }), + policy = api.captureInboxProbeReadback( + f.repository, + f.session, + f.copiedPolicy(), + f.acknowledgement()!, + f.action()!, + 'wss://inbox.example.org', + 'reviewed_inbox_probe_readback' + ); + return { + wrongRead, + wrongWrite, + copied, + policy: !!policy, + before, + after: f.counts() + }; + }); + expect(result.wrongRead).toEqual({ status: 'stopped' }); + expect(result.wrongWrite).toEqual({ status: 'stopped' }); + expect(result.copied).toEqual({ status: 'stopped' }); + expect(result.policy).toBe(false); + expect(result.after).toEqual(result.before); + expect(privateReads()).toHaveLength(0); + expect(privateEvents()).toHaveLength(0); +}); +for (const mode of ['alias', 'forged_fact', 'removed', 'corrupt_tail']) + test(`actual native ${mode} blocks read before original REQ`, async ({ + page + }) => { + await publishOriginal(page); + const result = await page.evaluate(async (mode) => { + const f = window.rbFixture, + token = f.readPermission()!, + rows = await f.stored(), + own = rows[0]; + if (mode === 'removed') await f.removeOriginal(); + else if (mode === 'corrupt_tail') + await f.inject([ + { owner: f.owner, id: crypto.randomUUID(), wire: '{bad' } + ]); + else if (mode === 'alias') + await f.inject([{ ...own, wire: own.wire + '\n' }]); + else { + const row = JSON.parse( + own.wire + ) as import('../../src/lib/persistence/private-records.ts').PrivateAccessProbe; + await f.inject([ + { + ...own, + wire: JSON.stringify({ + ...row, + revision: row.revision + 1, + receipts: [ + ...row.receipts, + { ...row.receipts[0], actionId: crypto.randomUUID() } + ] + }) + } + ]); + } + const before = await f.stored(), + read = await f.read(token); + return { + read, + unchanged: JSON.stringify(await f.stored()) === JSON.stringify(before) + }; + }, mode); + expect(result.read).toEqual({ status: 'stopped' }); + expect(result.unchanged).toBe(true); + expect(privateReads()).toHaveLength(0); + }); +test('provider waiting stays outside original network meter and does not regenerate crypto or artifact', async ({ + page +}) => { + const pub = await publishOriginal(page); + readMode = 'exact'; + const before = await page.evaluate(() => { + const f = window.rbFixture; + window.rbNetworkClock = f.installNetworkClock(); + window.rbRestore = () => window.rbNetworkClock!.restore(); + f.mode('hold_key'); + const before = f.counts(); + window.rbPending = f.read(); + return before; + }); + await expect + .poll(() => page.evaluate(() => window.rbFixture.waiting())) + .toBe('key'); + expect(privateReads()).toHaveLength(0); + await page.evaluate(() => { + window.rbNetworkClock!.advance(90000); + window.rbFixture.mode('normal'); + window.rbFixture.settle(); + }); + expect(await page.evaluate(async () => window.rbPending)).toMatchObject({ + status: 'readback', + attempt: 2, + actionId: pub.actionId + }); + const after = await page.evaluate(() => window.rbFixture.counts()); + expect(after.encrypts).toBe(before.encrypts); + expect(after.decrypts).toBe(before.decrypts); + expect(after.signs).toBe(before.signs); +}); +test('actual learned read AUTH requirement refuses before subsequent key prompt, then explicit original AUTH permits the same action', async ({ + page +}) => { + auth = true; + await publishOriginal(page); + readMode = 'auth_required'; + const first = await page.evaluate(() => window.rbFixture.read()); + expect(first).toMatchObject({ status: 'refused', attempt: 2 }); + const denied = await page.evaluate(async () => { + const f = window.rbFixture, + before = f.counts(); + f.mode('hold_key'); + const result = await f.read(); + return { result, before, after: f.counts(), waiting: f.waiting() }; + }); + expect(denied.result).toEqual({ status: 'stopped' }); + expect(denied.after).toEqual(denied.before); + expect(denied.waiting).toBe(''); + expect(privateReads()).toHaveLength(1); + readMode = 'exact'; + await page.evaluate(() => { + const f = window.rbFixture; + f.mode('normal'); + return window.rb.authenticateInboxAccess( + f.controller()!, + 'wss://inbox.example.org', + 'reviewed_inbox_connection_auth' + ); + }); + await expect + .poll(() => + page.evaluate( + () => + window.rb.inboxAccessControllerSnapshot( + window.rbFixture.controller()! + )?.auth[0]?.state + ) + ) + .toBe('challenge'); + expect( + await page.evaluate(() => + window.rb.respondInboxAccessAuthentication( + window.rbFixture.controller()!, + 'wss://inbox.example.org', + 'reviewed_inbox_auth_response' + ) + ) + ).toBe('accepted'); + expect(await page.evaluate(() => window.rbFixture.read())).toMatchObject({ + status: 'readback', + attempt: 3, + actionId: first.actionId + }); + expect(frames.filter((x) => x.frame[0] === 'AUTH')).toHaveLength(1); + expect(privateReads()).toHaveLength(2); +}); +test('actual shared forty-five-second network allowance spans archive and recipient readback without reset', async ({ + page +}) => { + await publishOriginal(page); + readMode = 'unknown'; + await page.evaluate(() => { + const f = window.rbFixture; + window.rbNetworkClock = f.installNetworkClock(); + window.rbRestore = () => window.rbNetworkClock!.restore(); + window.rbPending = f.read(); + }); + await expect.poll(() => privateReads().length).toBe(1); + await page.evaluate(() => window.rbNetworkClock!.advance(45001)); + expect(await page.evaluate(async () => window.rbPending)).toMatchObject({ + status: 'timed_out', + attempt: 2 + }); + readMode = 'exact'; + expect(await page.evaluate(() => window.rbFixture.read())).toEqual({ + status: 'stopped' + }); + expect(await page.evaluate(() => window.rbFixture.publish())).toEqual({ + status: 'stopped' + }); + expect(privateReads()).toHaveLength(1); + expect(privateEvents()).toHaveLength(1); +}); +test('actual Stop sends original CLOSE and retains ciphertext with no readback fact or readiness', async ({ + page +}) => { + await publishOriginal(page); + readMode = 'unknown'; + await page.evaluate(() => { + window.rbAbort = new AbortController(); + window.rbPending = window.rbFixture.read(undefined, window.rbAbort.signal); + }); + await expect.poll(() => privateReads().length).toBe(1); + await page.evaluate(() => window.rbAbort!.abort()); + expect(await page.evaluate(async () => window.rbPending)).toMatchObject({ + status: 'stopped', + attempt: 2 + }); + await expect + .poll( + () => + frames.filter( + (x) => + x.origin === 'wss://inbox.example.org' && x.frame[0] === 'CLOSE' + ).length + ) + .toBeGreaterThan(0); + expect( + await page.evaluate(() => window.rbFixture.snapshot()!.record.receipts) + ).toHaveLength(1); +}); +test('actual readback evidence settles only against original lineage after owner loss and cannot restore traffic', async ({ + page +}) => { + await publishOriginal(page); + const result = await page.evaluate(async () => { + const f = window.rbFixture, + result = await f.read(); + window.rb.closeInboxAccessController(f.controller()!); + return { + result, + append: await f.appendRead(result), + later: await f.read(), + rows: await f.stored() + }; + }); + expect(result.result.status).toBe('readback'); + expect(result.append.status).toBe('saved'); + expect(result.later).toEqual({ status: 'stopped' }); + expect( + ( + JSON.parse( + result.rows[0].wire + ) as import('../../src/lib/persistence/private-records.ts').PrivateAccessProbe + ).receipts + ).toHaveLength(2); + expect(privateReads()).toHaveLength(1); +}); +for (const nested of [false, true]) + test(`actual original REQ teardown ${nested ? 'reentrant Stop' : 'failure'} retains retry custody and another original archive scope`, async ({ + page + }) => { + await publishOriginal(page); + const result = await page.evaluate(async (nested) => { + const f = window.rbFixture; + window.rbNestedFailure = 0; + const failure = f.installReadTeardownFailure( + nested + ? () => { + try { + f.stopRead(); + } catch { + window.rbNestedFailure!++; + } + } + : undefined + ); + window.rbRestore = failure.restore; + const read = await f.read(), + required = window.rb.privatePoolCleanupRequired(f.readPool()), + faults = failure.faults(); + failure.restore(); + f.stopRead(); + const retainedArchive = await f.publish(); + return { + read, + required, + faults, + nested: window.rbNestedFailure, + retainedArchive + }; + }, nested); + expect(result.read).toMatchObject({ status: 'unknown', attempt: 2 }); + expect(result.required).toBe(true); + expect(result.faults).toBeGreaterThan(0); + if (nested) expect(result.nested).toBeGreaterThan(0); + expect(result.retainedArchive).toMatchObject({ + status: 'accepted', + attempt: 3 + }); + expect(privateConnections.get('wss://inbox.example.org')).toBe(1); + expect(privateEvents()).toHaveLength(2); + expect(privateReads()).toHaveLength(1); + }); + +for (const mode of ['aborted', 'uncertain'] as const) + test(`actual native readback ${mode} CAS preserves original evidence and exact idempotent retry`, async ({ + page + }) => { + await publishOriginal(page); + const result = await page.evaluate(async (mode) => { + const f = window.rbFixture, + read = await f.read(), + before = await f.stored(), + uncertain = + mode === 'uncertain' ? f.installUncertainCommit() : undefined, + restore = + mode === 'aborted' + ? f.installNativeAbort() + : () => uncertain!.restore(); + let first: Awaited<ReturnType<typeof f.appendRead>>; + try { + first = await f.appendRead(read); + } finally { + restore(); + } + const afterFirst = await f.stored(), + second = await f.appendRead(read), + third = await f.appendRead(read); + return { + first, + before, + afterFirst, + second, + third, + row: f.snapshot()!.record, + native: uncertain?.counts() + }; + }, mode); + expect(result.first.status).toBe( + mode === 'aborted' ? 'aborted' : 'reconciled' + ); + if (mode === 'aborted') expect(result.afterFirst).toEqual(result.before); + expect(result.second.status).toBe( + mode === 'aborted' ? 'saved' : 'existing' + ); + expect(result.third.status).toBe('existing'); + expect(result.row.receipts).toHaveLength(2); + if (mode === 'uncertain') + expect(result.native).toMatchObject({ + nativeComplete: 1, + nativeAbort: 0, + putSuccess: 1, + abortRefused: 1 + }); + }); +test('native full hundred-row namespace supports only existing original readback growth and serial duplicate evidence', async ({ + page +}) => { + await publishOriginal(page); + const result = await page.evaluate(async () => { + const f = window.rbFixture; + await f.fillCapacity(99); + const read = await f.read(), + concurrent = await Promise.all([f.appendRead(read), f.appendRead(read)]), + final = await f.appendRead(read); + return { + read, + concurrent, + final, + rows: (await f.stored()).length, + row: f.snapshot()!.record + }; + }); + expect(result.read.status).toBe('readback'); + expect(result.rows).toBe(100); + expect(result.concurrent.map((x) => x.status).sort()).toEqual([ + 'busy', + 'saved' + ]); + expect(result.final.status).toBe('existing'); + expect(result.row.receipts).toHaveLength(2); +}); +test('native eight-MiB joint namespace refuses observed readback growth without adopting metadata or losing original ciphertext', async ({ + page +}) => { + await publishOriginal(page); + const result = await page.evaluate(async () => { + const f = window.rbFixture, + actual = await f.read(), + original = await f.stored(), + template = JSON.parse( + original[0].wire + ) as import('../../src/lib/persistence/private-records.ts').PrivateAccessProbe, + id = crypto.randomUUID(), + fact = { + ...template.receipts[0], + actionId: crypto.randomUUID(), + observedAtMilliseconds: 1 + }; + const row = (count: number) => ({ + owner: f.owner, + id, + wire: JSON.stringify({ + ...template, + id, + receipts: Array.from({ length: count }, () => fact) + }) + }); + const bytes = (rows: typeof original) => + rows.reduce( + (sum, x) => sum + new TextEncoder().encode(JSON.stringify(x)).length, + 0 + ); + const empty = bytes([...original, row(0)]), + step = bytes([row(1)]) - bytes([row(0)]), + limit = 8 * 1024 * 1024; + await f.inject([row(Math.floor((limit - empty + 1) / (step + 1)))]); + const before = await f.stored(), + appended = await f.appendRead(actual); + return { + appended, + unchanged: JSON.stringify(await f.stored()) === JSON.stringify(before), + headroom: limit - bytes(before), + row: f.snapshot()!.record, + evidence: !!window.rb.inboxProbeReadbackAttemptEvidence(actual) + }; + }); + expect(result.headroom).toBeGreaterThanOrEqual(0); + expect(result.appended.status).toBe('capacity'); + expect(result.unchanged).toBe(true); + expect(result.evidence).toBe(true); + expect(result.row.receipts).toHaveLength(1); +}); +test('actual read page expires at existing fifteen-second bound before the shared action allowance', async ({ + page +}) => { + await publishOriginal(page); + readMode = 'unknown'; + await page.evaluate(() => { + const original = performance.now.bind(performance); + let offset = 0; + performance.now = () => + /at remainingPage(?:\d+)? \(/.test(new Error().stack ?? '') + ? original() + offset + : original(); + window.rbRestore = () => { + performance.now = original; + }; + window.rbAdvancePage = () => { + offset = 15001; + }; + window.rbPending = window.rbFixture.read(); + }); + await expect.poll(() => privateReads().length).toBe(1); + await page.evaluate(() => window.rbAdvancePage!()); + expect(await page.evaluate(async () => window.rbPending)).toMatchObject({ + status: 'timed_out', + attempt: 2 + }); + await page.evaluate(() => window.rbRestore!()); + readMode = 'exact'; + expect(await page.evaluate(() => window.rbFixture.read())).toMatchObject({ + status: 'readback', + attempt: 3 + }); +}); +for (const stop of ['abort', 'scope'] as const) + test(`actual original SDK OPEN-time ${stop} retains installing cleanup with no uncaught page error`, async ({ + page + }) => { + await publishOriginal(page); + const errors: string[] = []; + page.on('pageerror', (error) => errors.push(error.message)); + const result = await page.evaluate(async (stop) => { + const f = window.rbFixture, + abort = new AbortController(); + let pending = 0; + const restore = f.installReadInstallation(() => { + if (stop === 'abort') abort.abort(); + else + try { + f.stopRead(); + } catch { + pending++; + } + }); + window.rbRestore = restore; + const read = await f.read(undefined, abort.signal); + restore(); + if (stop === 'scope') f.stopRead(); + return { read, pending, archive: await f.publish() }; + }, stop); + expect(result.read).toMatchObject({ status: 'stopped', attempt: 2 }); + expect(result.archive).toMatchObject({ status: 'accepted', attempt: 3 }); + if (stop === 'scope') expect(result.pending).toBeGreaterThan(0); + expect(errors).toEqual([]); + }); +test('known original C7 AUTH signer wait consumes no read key prompt, network meter or recipient REQ', async ({ + page +}) => { + auth = true; + await publishOriginal(page); + await page.evaluate(() => + window.rb.authenticateInboxAccess( + window.rbFixture.controller()!, + 'wss://inbox.example.org', + 'reviewed_inbox_connection_auth' + ) + ); + await expect + .poll(() => + page.evaluate( + () => + window.rb.inboxAccessControllerSnapshot( + window.rbFixture.controller()! + )?.auth[0]?.state + ) + ) + .toBe('challenge'); + await page.evaluate(() => { + const f = window.rbFixture; + window.rbNetworkClock = f.installNetworkClock(); + window.rbRestore = () => window.rbNetworkClock!.restore(); + f.mode('hold_sign'); + window.rbPending = window.rb.respondInboxAccessAuthentication( + f.controller()!, + 'wss://inbox.example.org', + 'reviewed_inbox_auth_response' + ); + }); + await expect + .poll(() => page.evaluate(() => window.rbFixture.waiting())) + .toBe('sign'); + const denied = await page.evaluate(async () => { + const before = window.rbFixture.counts(); + return { + read: await window.rbFixture.read(), + before, + after: window.rbFixture.counts() + }; + }); + expect(denied.read).toEqual({ status: 'stopped' }); + expect(denied.after).toEqual(denied.before); + expect(privateReads()).toHaveLength(0); + await page.evaluate(() => { + window.rbNetworkClock!.advance(90000); + window.rbFixture.mode('normal'); + window.rbFixture.settle(); + }); + expect(await page.evaluate(async () => window.rbPending)).toBe('accepted'); + expect(await page.evaluate(() => window.rbFixture.read())).toMatchObject({ + status: 'readback', + attempt: 2 + }); +}); +test('three-origin original PUB and REQ action has nine total admissions and at most three original physical connections', async ({ + page +}) => { + const origins = [ + 'wss://inbox.example.org', + 'wss://peer.example.org', + 'wss://archive.example.org' + ]; + const result = await page.evaluate(async (origins) => { + const f = window.rbFixture, + action = f.action(), + results: Awaited<ReturnType<typeof f.read>>[] = []; + for (const origin of origins) { + const pub = await f.publish(f.permission(origin)); + await f.append(pub); + } + for (let round = 0; round < 2; round++) + for (const origin of origins) { + const read = await f.read(f.readPermission(origin)); + results.push(read); + await f.appendRead(read); + await f.commitOriginal(); + } + return { + results, + extra: await f.read(f.readPermission(origins[2])), + same: f.action() === action, + row: f.snapshot()!.record + }; + }, origins); + expect(result.same).toBe(true); + expect(result.extra).toEqual({ status: 'stopped' }); + expect(result.results.every((x) => x.status === 'readback')).toBe(true); + expect(new Set(result.results.map((x) => x.actionId)).size).toBe(1); + expect(result.row.receipts).toHaveLength(9); + expect(privatePeak).toBe(3); + expect(privateReads()).toHaveLength(6); + expect(privateEvents()).toHaveLength(3); + for (const origin of origins) expect(privateConnections.get(origin)).toBe(1); +}); +for (const clock of [NaN, Infinity, -1, 0]) + test(`actual read meter invalid clock ${String(clock)} permanently refuses original action without recipient traffic`, async ({ + page + }) => { + await publishOriginal(page); + const result = await page.evaluate(async (clock) => { + const f = window.rbFixture, + controlled = f.installNetworkClock(); + window.rbRestore = controlled.restore; + controlled.set(clock); + const read = await f.read(), + retry = await f.read(); + return { read, retry, action: !!f.action(), row: f.snapshot()!.record }; + }, clock); + expect(result.read).toEqual({ status: 'stopped' }); + expect(result.retry).toEqual({ status: 'stopped' }); + expect(result.action).toBe(false); + expect(privateReads()).toHaveLength(0); + expect(result.row.receipts).toHaveLength(1); + }); + +for (const clock of ['network', 'page'] as const) + test(`actual original socket startup exhausts ${clock} deadline before REQ or attempted read evidence`, async ({ + page + }) => { + const result = await page.evaluate(async (clock) => { + const f = window.rbFixture; + if (clock === 'network') { + window.rbNetworkClock = f.installNetworkClock(); + window.rbRestore = () => window.rbNetworkClock!.restore(); + } else { + const original = performance.now.bind(performance); + let offset = 0; + performance.now = () => + /at remainingPage(?:\d+)? \(/.test(new Error().stack ?? '') + ? original() + offset + : original(); + window.rbRestore = () => { + performance.now = original; + }; + window.rbAdvancePage = () => { + offset = 15001; + }; + } + window.rbAdvanceOnSocket = clock; + const read = await f.read(); + return { + read, + proof: !!window.rb.inboxProbeReadbackAttemptEvidence(read), + row: f.snapshot()!.record + }; + }, clock); + expect(result.read).toMatchObject({ status: 'stopped' }); + expect(result.read.attempt).toBeUndefined(); + expect(result.proof).toBe(false); + expect(result.row.receipts).toHaveLength(0); + expect(privateReads()).toHaveLength(0); + expect(privateEvents()).toHaveLength(0); + }); +test('actual original socket startup closing controller refuses before recipient REQ', async ({ + page +}) => { + const result = await page.evaluate(async () => { + window.rbInvalidateOnSocket = true; + const f = window.rbFixture, + read = await f.read(); + return { read, rows: await f.stored() }; + }); + expect(result.read).toMatchObject({ status: 'stopped' }); + expect(result.read.attempt).toBeUndefined(); + expect(privateReads()).toHaveLength(0); + expect( + JSON.parse(result.rows[0].wire) as Record<string, unknown> + ).toHaveProperty('family', 'private_access_probe'); +}); +test('actual same SDK request with foreign observation source cannot mint readback provenance', async ({ + page +}) => { + await publishOriginal(page); + const result = await page.evaluate(async () => { + const f = window.rbFixture, + restore = f.installWrongReadSource(); + window.rbRestore = restore; + const read = await f.read(); + restore(); + return { + read, + append: await f.appendRead(read), + row: f.snapshot()!.record + }; + }); + expect(result.read.status).toBe('error'); + expect(result.read.deliveries).toBe(1); + expect(result.append.status).toBe('invalid'); + expect(result.row.receipts).toHaveLength(1); +}); + +for (const status of ['readback', 'nonce_verified'] as const) + test(`handwritten strictly valid ${status} labels cannot replace original read permission or admit lineage`, async ({ + page + }) => { + await publishOriginal(page); + const result = await page.evaluate(async (status) => { + const f = window.rbFixture, + token = f.readPermission()!, + original = (await f.stored())[0], + row = JSON.parse( + original.wire + ) as import('../../src/lib/persistence/private-records.ts').PrivateAccessProbe, + fake = { + ...row, + revision: row.revision + 1, + receipts: [ + ...row.receipts, + { + actionId: crypto.randomUUID(), + origin: 'wss://inbox.example.org', + role: 'access_readback', + eventId: row.self!.eventId, + attempt: 1, + status, + observedAtMilliseconds: Date.now(), + readbackWire: row.self!.wire + } + ] + }, + wire = JSON.stringify(fake); + const valid = window.rb.decodePrivateRecord(wire, row.owner, row.id).ok; + await f.inject([{ ...original, wire }]); + const read = await f.read(token), + commit = await f.commitOriginal(); + return { + valid, + read, + commit: commit.status, + unchanged: (await f.stored())[0].wire === wire, + live: !!f.snapshot() + }; + }, status); + expect(result.valid).toBe(true); + expect(result.read).toEqual({ status: 'stopped' }); + expect(result.commit).toBe('conflict'); + expect(result.unchanged).toBe(true); + expect(result.live).toBe(false); + expect(privateReads()).toHaveLength(0); + }); +for (const fault of ['held', 'failed'] as const) + test(`actual original read-only ${fault} physical close blocks replacement until actual original retry and close`, async ({ + page + }) => { + const first = await page.evaluate(() => window.rbFixture.read()); + expect(first.status).toBe('empty'); + await expect.poll(() => privatePhysical.size).toBe(1); + const stopped = await page.evaluate((fault) => { + const f = window.rbFixture; + window.rbDelayClose = fault === 'held'; + window.rbFailClose = fault === 'failed'; + let failed = false; + try { + f.stopRead(); + } catch { + failed = true; + } + let replacement = false; + try { + replacement = !!window.rb.getPrivatePool( + f.session, + f.policy, + ['wss://inbox.example.org'], + Object.freeze({}), + 'access' + ); + } catch { + /* Actual original cleanup pending. */ + } + return { + failed, + replacement, + required: window.rb.privatePoolCleanupRequired(f.readPool()) + }; + }, fault); + expect(stopped.required).toBe(true); + expect(stopped.replacement).toBe(false); + if (fault === 'failed') expect(stopped.failed).toBe(true); + await page.evaluate(() => { + window.rbDelayClose = false; + window.rbFailClose = false; + for (const release of window.rbHeldClose ?? []) release(); + window.rbFixture.stopRead(); + }); + await expect.poll(() => privatePhysical.size).toBe(0); + await expect + .poll(() => + page.evaluate(() => + window.rb.privatePoolCleanupRequired(window.rbFixture.readPool()) + ) + ) + .toBe(false); + const next = await page.evaluate(async () => { + const f = window.rbFixture; + window.rbOtherPool = window.rb.getPrivatePool( + f.session, + f.policy, + ['wss://inbox.example.org'], + Object.freeze({}), + 'access' + )!; + const result = await window.rb.readInboxProbeAttempt( + window.rbOtherPool, + f.readPermission()!, + new AbortController().signal + ); + window.rb.closePrivatePool(window.rbOtherPool); + return result; + }); + expect(next).toMatchObject({ + status: 'empty', + attempt: 2, + actionId: first.actionId + }); + expect(privateReads()).toHaveLength(2); + expect(privateConnections.get('wss://inbox.example.org')).toBe(2); + }); +test('three-origin failed recipient cleanup preserves other original archive connection and scope', async ({ + page +}) => { + const result = await page.evaluate(async () => { + const f = window.rbFixture; + const first = await f.publish(); + await f.append(first); + const other = await f.publish(f.permission('wss://peer.example.org')); + await f.append(other); + const failure = f.installReadTeardownFailure(); + window.rbRestore = failure.restore; + const read = await f.read(); + failure.restore(); + f.stopRead(); + const next = await f.publish(f.permission('wss://peer.example.org')); + return { read, next, other, before: first.actionId }; + }); + expect(result.read).toMatchObject({ status: 'unknown', attempt: 2 }); + expect(result.next).toMatchObject({ + status: 'accepted', + attempt: 2, + actionId: result.other.actionId + }); + expect(result.next.actionId).toBe(result.before); + expect(privateConnections.get('wss://peer.example.org')).toBe(1); + expect(privateEvents()).toHaveLength(3); +}); diff --git a/web/tests/provenance/output.test.mjs b/web/tests/provenance/output.test.mjs @@ -613,7 +613,8 @@ import { auditOutput } from '../../tools/check-output.mjs'; assert.ok((await f.audit()).length > 0); for (const producer of [ 'src/lib/persistence/private-storage.ts', - 'src/lib/persistence/inbox-probes.ts' + 'src/lib/persistence/inbox-probes.ts', + 'src/lib/persistence/private-records.ts' ]) { for (const change of ['changed', 'missing']) { await t.test( diff --git a/web/tests/provenance/source-boundaries.test.mjs b/web/tests/provenance/source-boundaries.test.mjs @@ -18,7 +18,7 @@ import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { test } from 'node:test'; -test('CR007 and CR008-S4B1 exact private SDK removal cannot become an arbitrary remove capability', async () => { +test('CR007 and CR008-S4B exact private SDK removal cannot become an arbitrary remove capability', async () => { await fixture(async ({ directory, put, execute }) => { await isolateScriptDependencies(directory); await cp(path.join(root, 'src'), path.join(directory, 'src'), { diff --git a/web/tests/unit/inbox-probe-readback.test.ts b/web/tests/unit/inbox-probe-readback.test.ts @@ -0,0 +1,143 @@ +import { afterEach, expect, it, vi } from 'vitest'; +import { finalizeEvent, generateSecretKey } from 'applesauce-core/helpers'; +import { nip44 } from 'applesauce-core/helpers/encryption'; +import { inboxProbeReadbackMatches } from '../../src/lib/persistence/private-records.ts'; +import type { InboxProbeAcknowledgement } from '../../src/lib/persistence/inbox-probes.ts'; +import type { PrivateStorageRepository } from '../../src/lib/persistence/private-storage.ts'; +import type { PrivateSession } from '../../src/lib/runtime/private-session.ts'; +import type { RelayPolicy } from '../../src/lib/config/relays.ts'; +import type { + InboxProbeReadback, + InboxProbePublicationAction +} from '../../src/lib/nostr/private-publisher.ts'; +import type { + PrivatePool, + InboxProbeReadbackAttemptResult +} from '../../src/lib/nostr/private-pool.ts'; +afterEach(() => vi.unstubAllGlobals()); +for (const browser of [false, true]) + it(`cold ${browser ? 'browser-shaped' : 'SSR'} readback families reject forged inputs before any effects`, async () => { + vi.resetModules(); + let effects = 0; + const trap = () => { + effects++; + throw Error('forged effects'); + }; + if (browser) { + vi.stubGlobal('window', { + get nostr() { + return trap(); + } + }); + vi.stubGlobal('navigator', { + get locks() { + return trap(); + } + }); + vi.stubGlobal('WebSocket', trap); + } + const p = await import('../../src/lib/nostr/private-publisher.ts'), + t = await import('../../src/lib/nostr/private-pool.ts'), + s = await import('../../src/lib/persistence/inbox-probes.ts'); + for (const fake of [ + null, + undefined, + 0, + false, + '', + Symbol(), + {}, + { current: () => true, status: 'readback' } + ]) { + expect( + p.captureInboxProbeReadback( + {} as PrivateStorageRepository, + {} as PrivateSession, + {} as RelayPolicy, + fake as InboxProbeAcknowledgement, + fake as InboxProbePublicationAction, + 'wss://inbox.example.org', + 'reviewed_inbox_probe_readback' + ) + ).toBeUndefined(); + expect( + p.inboxProbeReadbackSnapshot(fake as InboxProbeReadback) + ).toBeUndefined(); + expect( + await t.readInboxProbeAttempt( + {} as PrivatePool, + fake as InboxProbeReadback, + new AbortController().signal + ) + ).toEqual({ status: 'stopped' }); + expect( + t.inboxProbeReadbackAttemptEvidence( + fake as InboxProbeReadbackAttemptResult + ) + ).toBeUndefined(); + expect( + await s.commitInboxProbeReadbackAttempt( + {} as PrivateStorageRepository, + fake as InboxProbeReadbackAttemptResult + ) + ).toEqual({ status: 'invalid', disposition: 'original_protected' }); + } + expect(effects).toBe(0); + }); +it('pure readback comparison preserves bounded alternate serialization and unsigned metadata with independently identical signed core', () => { + const owner = + '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', + key = generateSecretKey(), + conversation = nip44.v2.utils.getConversationKey(key, owner); + try { + const e = finalizeEvent( + { + kind: 1059, + created_at: 1700000000, + tags: [['p', owner]], + content: nip44.v2.encrypt( + 'Structural source fixture, no nonce proof', + conversation + ) + }, + key + ), + self = { eventId: e.id, wire: JSON.stringify(e) }; + const alternate = { + sig: e.sig, + content: e.content, + tags: e.tags, + created_at: e.created_at, + kind: e.kind, + pubkey: e.pubkey, + id: e.id, + observed: { source: 'fixture' } + }; + for (const wire of [ + self.wire, + self.wire + '\n', + JSON.stringify(alternate, null, 2) + ]) + expect(inboxProbeReadbackMatches(self, owner, wire)).toBe(true); + for (const wire of [ + null, + undefined, + {}, + JSON.stringify({ ...e, kind: 14 }), + JSON.stringify({ ...e, sig: '0'.repeat(128), verified: true }), + JSON.stringify({ ...e, id: 'f'.repeat(64) }), + JSON.stringify({ ...e, content: e.content + 'a' }), + JSON.stringify({ ...e, tags: [['p', 'f'.repeat(64)]] }), + self.wire.replace('"kind":1059', '"kind":1059,"kind":1059'), + self.wire.replace('"kind":1059', '"kind":1.059e3'), + self.wire + ' '.repeat(32768) + ]) + expect(inboxProbeReadbackMatches(self, owner, wire)).toBe(false); + expect(inboxProbeReadbackMatches(self, 'f'.repeat(64), self.wire)).toBe( + false + ); + } finally { + conversation.fill(0); + key.fill(0); + } +}); diff --git a/web/tests/unit/private-access-records.test.ts b/web/tests/unit/private-access-records.test.ts @@ -310,7 +310,6 @@ describe('CR008 S2 strict separate probe journal codec', () => { for (const patch of [ { origin: archiveOrigin }, { readbackWire: null }, - { readbackWire: row.self!.wire + '\n' }, { status: 'accepted' }, { status: 'nonce_verified', readbackWire: artifact().wire } ]) @@ -322,6 +321,22 @@ describe('CR008 S2 strict separate probe journal codec', () => { }).ok ).toBe(false); }); + it('retains harmless serialized observation bytes for probe readback metadata without effect authority', () => { + const row = prepared(), + wire = row.self!.wire + '\n'; + const observed = { + ...row, + revision: 2, + receipts: [{ ...fact(row, 'readback'), readbackWire: wire }] + }; + const result = decode(observed); + expect(result.ok).toBe(true); + if (!result.ok) throw Error('bounded signed-equivalent observation'); + expect(privateRecordSnapshot(result.value, owner, command)).toEqual( + observed + ); + expect(privateRecordSettlement(observed)).toBe('protected'); + }); it('rejects plaintext, unknown/missing fields and duplicate aliases at every nested level', () => { const row = prepared(); for (const key of [ diff --git a/web/tools/check-output.mjs b/web/tools/check-output.mjs @@ -319,13 +319,13 @@ async function operationalFlagsAdmission(web) { 'src/lib/nostr/self-recovery-reader.ts': 'b15be31d13499b6e397f8b8dc691076e2b41b4157bf62013e5105cdd03875fa6', 'src/lib/nostr/private-publisher.ts': - 'f7032c8a138adf70331b690cbf88eaac695facc9fd58bb0f12f9461e46edd51b', + '2f939dda3504b760dbf3758632cda6ce38d3e16c6d142b814e40a0e295a77d90', 'src/lib/nostr/private-pool.ts': - '4d5f5c671aefc5439ed2d4f1e7619e1a348598f98a81862ee8aed41ed03df286', + 'c5d7884b33ea8dcde6aea495c92e7a53db12cf650f4476f2b66adc4165e771c2', 'src/lib/nostr/inbox-ingress.ts': 'f860a094a8ba767585124c795d44b7620064f73a5b3a0170041c581c0fc64ecb', 'src/lib/persistence/inbox-probes.ts': - '857c2cf7e5cbf9253ad4b60b0f0e9a84cd99a1648d3b05d8e3013c695ac227fa', + 'b4d3089bcf08a5c9ac3a99dfcf8e2c2e2160a2c8996c19fccb2670614f7b4d7a', 'src/lib/nostr/inbox-access.ts': 'd39f218b66fb51833e962b20152a2df81adf3097ba434a4cf2172ee4a7c8e3bc', 'src/lib/nostr/seal-builder.ts': @@ -401,7 +401,7 @@ async function operationalFlagsAdmission(web) { 'src/lib/persistence/local-public-hides.ts': '4f0e64ed0fabbe9f86014a0512e7dbd3c81711f28ca2c626e2dafa5cda349f83', 'src/lib/persistence/private-records.ts': - '2e5947e19df337ac1a7cd76b0f2cd6ec11cdb85d3fd38128e127fa8e3f30723a', + '821ef96740cf644bff4ffe5c7816c6aa097aa419bd16b8b301809b963294dafe', 'src/lib/persistence/private-sends.ts': '6696f4179370cad373c06debf57b919598d53b3d25b6d2a6054a557608e4eb9c', 'src/lib/persistence/private-storage.ts': @@ -597,13 +597,13 @@ async function privacyDataAdmission(web) { 'src/lib/nostr/self-recovery-reader.ts': 'b15be31d13499b6e397f8b8dc691076e2b41b4157bf62013e5105cdd03875fa6', 'src/lib/nostr/private-publisher.ts': - 'f7032c8a138adf70331b690cbf88eaac695facc9fd58bb0f12f9461e46edd51b', + '2f939dda3504b760dbf3758632cda6ce38d3e16c6d142b814e40a0e295a77d90', 'src/lib/nostr/private-pool.ts': - '4d5f5c671aefc5439ed2d4f1e7619e1a348598f98a81862ee8aed41ed03df286', + 'c5d7884b33ea8dcde6aea495c92e7a53db12cf650f4476f2b66adc4165e771c2', 'src/lib/nostr/inbox-ingress.ts': 'f860a094a8ba767585124c795d44b7620064f73a5b3a0170041c581c0fc64ecb', 'src/lib/persistence/inbox-probes.ts': - '857c2cf7e5cbf9253ad4b60b0f0e9a84cd99a1648d3b05d8e3013c695ac227fa', + 'b4d3089bcf08a5c9ac3a99dfcf8e2c2e2160a2c8996c19fccb2670614f7b4d7a', 'src/lib/nostr/inbox-access.ts': 'd39f218b66fb51833e962b20152a2df81adf3097ba434a4cf2172ee4a7c8e3bc', 'src/lib/nostr/seal-builder.ts': @@ -673,7 +673,7 @@ async function privacyDataAdmission(web) { 'src/lib/persistence/local-public-hides.ts': '4f0e64ed0fabbe9f86014a0512e7dbd3c81711f28ca2c626e2dafa5cda349f83', 'src/lib/persistence/private-records.ts': - '2e5947e19df337ac1a7cd76b0f2cd6ec11cdb85d3fd38128e127fa8e3f30723a', + '821ef96740cf644bff4ffe5c7816c6aa097aa419bd16b8b301809b963294dafe', 'src/lib/persistence/private-sends.ts': '6696f4179370cad373c06debf57b919598d53b3d25b6d2a6054a557608e4eb9c', 'src/lib/persistence/private-storage.ts': @@ -2166,10 +2166,12 @@ async function inboxSetupAdmission(web) { /** @param {string} web */ async function inboxProbeStorageAdmission(web) { const producers = { + 'src/lib/persistence/private-records.ts': + '821ef96740cf644bff4ffe5c7816c6aa097aa419bd16b8b301809b963294dafe', 'src/lib/persistence/private-storage.ts': 'ba4a61dd1fa08fc89d00adac3c258c1f6ae7e5c1b15165facffe76ddd71d5e91', 'src/lib/persistence/inbox-probes.ts': - '857c2cf7e5cbf9253ad4b60b0f0e9a84cd99a1648d3b05d8e3013c695ac227fa' + 'b4d3089bcf08a5c9ac3a99dfcf8e2c2e2160a2c8996c19fccb2670614f7b4d7a' }; for (const [name, pin] of Object.entries(producers)) { const bytes = await readOwned(web, name); diff --git a/web/tools/source-boundaries.mjs b/web/tools/source-boundaries.mjs @@ -1664,7 +1664,7 @@ export async function auditSource(root) { const relayRemovalTrusted = files.has(relayRemovalFile) && createHash('sha256').update(files.get(relayRemovalFile)).digest('hex') === - '4d5f5c671aefc5439ed2d4f1e7619e1a348598f98a81862ee8aed41ed03df286'; + 'c5d7884b33ea8dcde6aea495c92e7a53db12cf650f4476f2b66adc4165e771c2'; if (files.has(relayRemovalFile) && !relayRemovalTrusted) complain( relayRemovalFile,