app

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

commit 096b0b11a5f0586cd9b469f294bfb7a1ad1caac4
parent e331d91e9687d31cebf842aeb5aa4c91e12a4021
Author: triesap <tyson@radroots.org>
Date:   Thu,  8 Oct 2026 21:19:43 +0000

storage: add ciphertext-only private persistence records (HCP-080)

- Validate owner-scoped ciphertext records and reject duplicate JSON fields
- Enforce separate atomic outbox and received-envelope quotas without eviction
- Preserve reservation identity, exact ciphertext CAS and transaction outcomes
- Qualify hostile codecs and browser quotas with slow-host test allowances

Diffstat:
Mweb/package.json | 4++--
Mweb/playwright.config.ts | 2+-
Aweb/src/lib/persistence/private-records.ts | 416+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/src/lib/persistence/private-send-reservations.ts | 92++++++++++++++++++-------------------------------------------------------------
Aweb/src/lib/persistence/private-storage.ts | 329+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/e2e/harness/private-storage.ts | 163+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aweb/tests/e2e/private-storage.spec.ts | 415+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/provenance/output.test.mjs | 27++++++++++++++++-----------
Aweb/tests/unit/private-records.test.ts | 289++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/vitest.config.ts | 1+
10 files changed, 1653 insertions(+), 85 deletions(-)

diff --git a/web/package.json b/web/package.json @@ -14,9 +14,9 @@ "build": "node tools/check-source.mjs && node tools/build-info.mjs && vite build && node tools/check-output.mjs", "check": "node tools/check-source.mjs && svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --fail-on-warnings", "lint": "node tools/check-source.mjs && svelte-kit sync && eslint . --max-warnings 0 && prettier --check .", - "test:unit": "node tools/check-source.mjs && svelte-kit sync && vitest run --config vitest.config.ts && node --test --test-concurrency=1 --test-timeout=10000 tests/provenance/*.test.mjs", + "test:unit": "node tools/check-source.mjs && svelte-kit sync && vitest run --config vitest.config.ts && node --test --test-concurrency=1 --test-timeout=120000 tests/provenance/*.test.mjs", "test:conformance": "corepack pnpm build && node --test tests/conformance/*.test.ts", - "test:integration": "corepack pnpm build && node --test --test-timeout=10000 tests/integration/*.test.ts", + "test:integration": "corepack pnpm build && node --test --test-timeout=120000 tests/integration/*.test.ts", "test:e2e": "corepack pnpm build && playwright test", "test:conformance:oracle": "node tools/check-food-interop.mjs", "check:launch-relays": "node tools/check-source.mjs && node tools/check-launch-relays.mjs" diff --git a/web/playwright.config.ts b/web/playwright.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ fullyParallel: false, workers: 1, retries: 0, - timeout: 15_000, + timeout: 120_000, use: { browserName: 'chromium', headless: true, serviceWorkers: 'block' }, reporter: 'list' }); diff --git a/web/src/lib/persistence/private-records.ts b/web/src/lib/persistence/private-records.ts @@ -0,0 +1,416 @@ +import { canonicalPublicKey } from '../contracts/public-key.ts'; +import { exactLocalFields } from '../contracts/local-records.ts'; +import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts'; +import { canonicalLocalId } from '../private-handles.ts'; +import { safeUnsignedInteger } from '../nostr/envelope-bounds.ts'; +import { + verifyEnvelope, + verifiedEnvelopeSnapshot +} from '../nostr/verified-envelope.ts'; +import { canonicalRelayOrigin } from '../config/relays.ts'; +import { + LOCAL_PERSISTENCE_BUDGETS, + PRIVATE_TRANSPORT_BUDGETS, + RELAY_BUDGETS +} from '../config/budgets.ts'; +export type PrivateSendReservation = Readonly<{ + schema: 1; + family: 'private_send_reservation'; + owner: string; + id: string; + revision: 0; + peer: string; + rumorHash: string; + createdAt: number; +}>; +export type PrivateCiphertextArtifact = Readonly<{ + eventId: string; + wire: string; +}>; +export type PrivateSendOperation = Readonly<{ + schema: 1; + family: 'private_send_operation'; + owner: string; + id: string; + revision: number; + peer: string; + rumorHash: string; + createdAt: number; + self: PrivateCiphertextArtifact; + peerArtifact: PrivateCiphertextArtifact | null; +}>; +export type ReceivedEnvelopeRecord = Readonly<{ + schema: 1; + family: 'received_envelope'; + owner: string; + id: string; + revision: number; + outer: string; + observedAtMilliseconds: number; + sources: readonly string[]; + read: Readonly<{ rumorHash: string; atMilliseconds: number }> | null; +}>; +export type PrivateRecord = + PrivateSendReservation | PrivateSendOperation | ReceivedEnvelopeRecord; +declare const recordBrand: unique symbol; +export type PrivateRecordHandle = Readonly<{ [recordBrand]: true }>; +const records = new WeakMap< + PrivateRecordHandle, + Readonly<{ owner: string; id: string; wire: string }> +>(); +export type PrivateRecordFailure = + | 'invalid_scope' + | 'oversized' + | 'malformed' + | 'unsupported_schema' + | 'owner_mismatch' + | 'id_mismatch'; +type Result = + | Readonly<{ ok: true; value: PrivateRecordHandle }> + | Readonly<{ ok: false; reason: PrivateRecordFailure }>; +const failed = (reason: PrivateRecordFailure): Result => ({ + ok: false, + reason +}); +const hash = (value: unknown): value is string => + typeof value === 'string' && /^[0-9a-f]{64}$/.test(value); +const unsigned = (value: unknown): value is number => + safeUnsignedInteger(value) !== undefined; +const reservationFields = [ + 'schema', + 'family', + 'owner', + 'id', + 'revision', + 'peer', + 'rumorHash', + 'createdAt' +]; +// JSON.parse keeps only the last duplicate key. Retaining the original outer +// bytes therefore requires a separate syntax-only duplicate check, including +// escaped key aliases and nested local metadata. JSON validity/semantics remain +// with the ordinary parser/verifier; this scanner never canonicalizes a wire. +function uniqueJSONFields(raw: string): boolean { + type Frame = { + array: boolean; + key: boolean; + parent: Frame | undefined; + accept: (name: string) => boolean; + }; + function frame(parent: Frame | undefined, array: boolean): Frame { + const names = new Set<string>(); + return { + parent, + array, + key: !array, + accept: (name: string) => { + if (names.has(name)) return false; + names.add(name); + return true; + } + }; + } + let current: Frame | undefined; + try { + for (let index = 0; index < raw.length; index++) { + const character = raw[index]; + if (character === '"') { + let end = index + 1; + while (end < raw.length) { + if (raw[end] === '\\') { + end += 2; + continue; + } + if (raw[end] === '"') break; + end++; + } + if (end >= raw.length) return false; + if (current && !current.array && current.key) { + const name: unknown = JSON.parse(raw.slice(index, end + 1)); + if (typeof name !== 'string' || !current.accept(name)) return false; + current = { + parent: current.parent, + array: current.array, + accept: current.accept, + key: false + }; + } + index = end; + } else if (character === '{' || character === '[') { + current = frame(current, character === '['); + } else if (character === '}' || character === ']') { + if (!current) return false; + current = current.parent; + } else if (character === ',' && current && !current.array) + current = { + parent: current.parent, + array: current.array, + accept: current.accept, + key: true + }; + } + return current === undefined; + } catch { + return false; + } +} +function sendMetadata( + value: Record<string, unknown>, + owner: string, + id: string +): boolean { + const peer = canonicalPublicKey(value.peer); + return ( + value.schema === 1 && + value.owner === owner && + value.id === id && + !!peer && + peer !== owner && + hash(value.rumorHash) && + unsigned(value.createdAt) + ); +} +// Preserved metadata-only reservation admission; never plaintext or crypto proof. +export function inspectPrivateSendReservation( + raw: unknown, + expectedOwner: unknown, + expectedId: unknown +): PrivateSendReservation | undefined { + const owner = canonicalPublicKey(expectedOwner), + id = canonicalLocalId(expectedId); + if (!owner || !id || typeof raw !== 'string' || !boundedUtf8(raw, 4096)) + return undefined; + try { + const value: unknown = JSON.parse(raw); + if ( + !exactLocalFields(value, reservationFields) || + !uniqueJSONFields(raw) || + value.family !== 'private_send_reservation' || + value.revision !== 0 || + !sendMetadata(value, owner, id) + ) + return undefined; + return { + schema: 1, + family: 'private_send_reservation', + owner, + id, + revision: 0, + peer: value.peer as string, + rumorHash: value.rumorHash as string, + createdAt: value.createdAt as number + }; + } catch { + return undefined; + } +} +function outer(raw: unknown, destination: string): string | undefined { + if ( + typeof raw !== 'string' || + !boundedUtf8(raw, PRIVATE_TRANSPORT_BUDGETS.envelopeBytes) + ) + return undefined; + const proof = verifyEnvelope(raw); + if (!proof.ok) return undefined; + if (!uniqueJSONFields(raw)) return undefined; + const event = verifiedEnvelopeSnapshot(proof.value); + if ( + !event || + !exactLocalFields(event, [ + 'id', + 'pubkey', + 'created_at', + 'kind', + 'tags', + 'content', + 'sig' + ]) || + event.kind !== 1059 || + JSON.stringify(event.tags) !== JSON.stringify([['p', destination]]) + ) + return undefined; + // Structural standard-v2 preflight only. No SDK decrypt or nested admission; + // valid encoding/signature cannot prove this ciphertext's inner contents. + if ( + event.content.length < 132 || + !/^[A-Za-z0-9+/]+={0,2}$/.test(event.content) + ) + return undefined; + try { + const bytes = atob(event.content); + if ( + bytes.length < 99 || + bytes.charCodeAt(0) !== 2 || + btoa(bytes) !== event.content + ) + return undefined; + } catch { + return undefined; + } + return event.id; +} +function artifact( + value: unknown, + destination: string +): value is PrivateCiphertextArtifact { + return ( + exactLocalFields(value, ['eventId', 'wire']) && + hash(value.eventId) && + outer(value.wire, destination) === value.eventId + ); +} +// Strict local storage codec only, never installed-account/effect authorization, +// genuine sender factory proof, nested authenticity, read receipt or Send. +export function decodePrivateRecord( + raw: unknown, + expectedOwner: unknown, + expectedId: unknown +): Result { + const owner = canonicalPublicKey(expectedOwner); + if (!owner || typeof expectedId !== 'string') return failed('invalid_scope'); + const id = + canonicalLocalId(expectedId) ?? (hash(expectedId) ? expectedId : undefined); + if (!id) return failed('invalid_scope'); + if (typeof raw !== 'string') return failed('malformed'); + if (!boundedUtf8(raw, LOCAL_PERSISTENCE_BUDGETS.privateSendBytes)) + return failed('oversized'); + try { + const value: unknown = JSON.parse(raw); + if (typeof value !== 'object' || value === null || Array.isArray(value)) + return failed('malformed'); + const fields = value as Record<string, unknown>; + if (!uniqueJSONFields(raw)) return failed('malformed'); + if (fields.schema !== 1) return failed('unsupported_schema'); + if (fields.owner !== owner) return failed('owner_mismatch'); + if (fields.id !== id) return failed('id_mismatch'); + let record: PrivateRecord; + if (fields.family === 'private_send_reservation') { + const reservation = inspectPrivateSendReservation(raw, owner, id); + if (!reservation) return failed('malformed'); + record = reservation; + } else if (fields.family === 'private_send_operation') { + if ( + !canonicalLocalId(id) || + !exactLocalFields(fields, [ + ...reservationFields, + 'self', + 'peerArtifact' + ]) || + !sendMetadata(fields, owner, id) || + !unsigned(fields.revision) || + fields.revision < 1 || + !artifact(fields.self, owner) || + (fields.peerArtifact !== null && + !artifact(fields.peerArtifact, fields.peer as string)) + ) + return failed('malformed'); + record = { + schema: 1, + family: 'private_send_operation', + owner, + id, + revision: fields.revision, + peer: fields.peer as string, + rumorHash: fields.rumorHash as string, + createdAt: fields.createdAt as number, + self: { eventId: fields.self.eventId, wire: fields.self.wire }, + peerArtifact: + fields.peerArtifact === null + ? null + : { + eventId: fields.peerArtifact.eventId, + wire: fields.peerArtifact.wire + } + }; + } else if (fields.family === 'received_envelope') { + if ( + !hash(id) || + !exactLocalFields(fields, [ + 'schema', + 'family', + 'owner', + 'id', + 'revision', + 'outer', + 'observedAtMilliseconds', + 'sources', + 'read' + ]) || + !unsigned(fields.revision) || + outer(fields.outer, owner) !== id || + !unsigned(fields.observedAtMilliseconds) || + !Array.isArray(fields.sources) || + fields.sources.length === 0 || + fields.sources.length > RELAY_BUDGETS.inbox || + fields.sources.some( + (source: unknown) => canonicalRelayOrigin(source) !== source + ) || + new Set(fields.sources).size !== fields.sources.length || + (fields.read !== null && + (!exactLocalFields(fields.read, ['rumorHash', 'atMilliseconds']) || + !hash(fields.read.rumorHash) || + !unsigned(fields.read.atMilliseconds))) + ) + return failed('malformed'); + record = { + schema: 1, + family: 'received_envelope', + owner, + id, + revision: fields.revision, + outer: fields.outer as string, + observedAtMilliseconds: fields.observedAtMilliseconds, + sources: (fields.sources as unknown[]).map( + (source) => source as string + ), + read: + fields.read === null + ? null + : { + rumorHash: fields.read.rumorHash as string, + atMilliseconds: fields.read.atMilliseconds as number + } + }; + } else return failed('malformed'); + const token = Object.freeze({}) as PrivateRecordHandle; + records.set(token, { owner, id, wire: JSON.stringify(record) }); + return { ok: true, value: token }; + } catch { + return failed('malformed'); + } +} +export function privateRecordWire( + token: PrivateRecordHandle, + owner: unknown, + id: unknown +): string | undefined { + const saved = records.get(token); + return saved && saved.owner === owner && saved.id === id + ? saved.wire + : undefined; +} +export function privateRecordSnapshot( + token: PrivateRecordHandle, + owner: unknown, + id: unknown +): PrivateRecord | undefined { + const wire = privateRecordWire(token, owner, id); + return wire === undefined ? undefined : (JSON.parse(wire) as PrivateRecord); +} +// Handle identity contains no private body; callers must still match namespace. +export function privateRecordIdentity( + token: PrivateRecordHandle +): Readonly<{ owner: string; id: string }> | undefined { + const saved = records.get(token); + return saved ? { owner: saved.owner, id: saved.id } : undefined; +} +export function inspectPrivateSendRecord( + raw: unknown, + owner: unknown, + id: unknown +): PrivateSendReservation | PrivateSendOperation | undefined { + const decoded = decodePrivateRecord(raw, owner, id); + if (!decoded.ok) return undefined; + const value = privateRecordSnapshot(decoded.value, owner, id); + return value?.family !== 'received_envelope' ? value : undefined; +} diff --git a/web/src/lib/persistence/private-send-reservations.ts b/web/src/lib/persistence/private-send-reservations.ts @@ -9,16 +9,15 @@ import { browserDatabaseTransaction, type BrowserDatabase } from './database.ts'; -export type PrivateSendReservation = Readonly<{ - schema: 1; - family: 'private_send_reservation'; - owner: string; - id: string; - revision: 0; - peer: string; - rumorHash: string; - createdAt: number; -}>; +import { + inspectPrivateSendReservation, + inspectPrivateSendRecord, + type PrivateSendReservation +} from './private-records.ts'; +export { + inspectPrivateSendReservation, + type PrivateSendReservation +} from './private-records.ts'; declare const repositoryBrand: unique symbol; export type PrivateSendReservationRepository = Readonly<{ [repositoryBrand]: true; @@ -47,61 +46,6 @@ const failed = (reason: ReservationFailure): ReservationResult => ({ ok: false, reason }); -// Metadata only, never a plaintext/private-draft or encrypted-send decoder. -// Future paired-ciphertext/recovery families retain their own admission owner. -export function inspectPrivateSendReservation( - raw: unknown, - expectedOwner: unknown, - expectedId: unknown -): PrivateSendReservation | undefined { - const owner = canonicalPublicKey(expectedOwner), - id = canonicalLocalId(expectedId); - if (!owner || !id || typeof raw !== 'string' || !boundedUtf8(raw, 4096)) - return undefined; - try { - const value: unknown = JSON.parse(raw); - if ( - !exactLocalFields(value, [ - 'schema', - 'family', - 'owner', - 'id', - 'revision', - 'peer', - 'rumorHash', - 'createdAt' - ]) || - value.schema !== 1 || - value.family !== 'private_send_reservation' || - value.owner !== owner || - value.id !== id || - value.revision !== 0 - ) - return undefined; - const peer = canonicalPublicKey(value.peer), - createdAt = safeUnsignedInteger(value.createdAt); - if ( - !peer || - peer === owner || - createdAt === undefined || - typeof value.rumorHash !== 'string' || - !/^[0-9a-f]{64}$/.test(value.rumorHash) - ) - return undefined; - return { - schema: 1, - family: 'private_send_reservation', - owner, - id, - revision: 0, - peer, - rumorHash: value.rumorHash, - createdAt - }; - } catch { - return undefined; - } -} export function createPrivateSendReservationRepository( database: BrowserDatabase, expectedOwner: unknown @@ -217,17 +161,23 @@ export function reservePrivateSendReservation( refuse('corrupt_record'); return; } - const value = inspectPrivateSendReservation( - row.wire, - scope.owner, - id - ); + const value = inspectPrivateSendRecord(row.wire, scope.owner, id); if (!value) { refuse('corrupt_record'); return; } count += 1; - if (value.id === candidate.id) existing = value; + if (value.id === candidate.id) + existing = { + schema: 1, + family: 'private_send_reservation', + owner: value.owner, + id: value.id, + revision: 0, + peer: value.peer, + rumorHash: value.rumorHash, + createdAt: value.createdAt + }; if (value.rumorHash === candidate.rumorHash) collision = true; bytes += new TextEncoder().encode(JSON.stringify(row)).length; if ( diff --git a/web/src/lib/persistence/private-storage.ts b/web/src/lib/persistence/private-storage.ts @@ -0,0 +1,329 @@ +import { canonicalPublicKey } from '../contracts/public-key.ts'; +import { exactLocalFields } from '../contracts/local-records.ts'; +import { LOCAL_PERSISTENCE_BUDGETS } from '../config/budgets.ts'; +import { + browserDatabaseState, + browserDatabaseTransaction, + type BrowserDatabase +} from './database.ts'; +import { + decodePrivateRecord, + privateRecordIdentity, + privateRecordSnapshot, + privateRecordWire, + type PrivateRecordHandle, + type PrivateRecord +} from './private-records.ts'; +declare const repositoryBrand: unique symbol; +export type PrivateStorageRepository = Readonly<{ [repositoryBrand]: true }>; +export type PrivateStorageFailure = + | 'invalid_scope' + | 'invalid_record' + | 'conflict' + | 'capacity' + | 'corrupt_record' + | 'unavailable' + | 'aborted' + | 'unknown_completion'; +export type PrivateStorageResult<T> = + | Readonly<{ ok: true; value: T }> + | Readonly<{ ok: false; reason: PrivateStorageFailure }>; +type Store = 'private_sends' | 'received_envelopes'; +type Scope = Readonly<{ database: BrowserDatabase; owner: string }>; +type Row = Readonly<{ + store: Store; + record: PrivateRecord; + wire: string; + bytes: number; +}>; +const repositories = new WeakMap<PrivateStorageRepository, Scope>(); +const failed = ( + reason: PrivateStorageFailure +): PrivateStorageResult<never> => ({ ok: false, reason }); +function scopeOf(repository: PrivateStorageRepository): Scope | undefined { + const scope = repositories.get(repository); + try { + return scope && browserDatabaseState(scope.database).state === 'ready' + ? scope + : undefined; + } catch { + return undefined; + } +} +// Namespace only, not installed-account or cryptographic/effect permission. +export function createPrivateStorageRepository( + database: BrowserDatabase, + expectedOwner: unknown +): PrivateStorageRepository | undefined { + const owner = canonicalPublicKey(expectedOwner); + try { + if (!owner || browserDatabaseState(database).state !== 'ready') + return undefined; + const token = Object.freeze({}) as PrivateStorageRepository; + repositories.set(token, { database, owner }); + return token; + } catch { + return undefined; + } +} +function stored( + value: unknown, + store: Store, + owner: string, + id: unknown +): Row | undefined { + if ( + !exactLocalFields(value, ['owner', 'id', 'wire']) || + value.owner !== owner || + value.id !== id || + typeof value.wire !== 'string' + ) + return undefined; + const decoded = decodePrivateRecord(value.wire, owner, id); + if (!decoded.ok) return undefined; + const record = privateRecordSnapshot(decoded.value, owner, id); + if ( + !record || + (record.family === 'received_envelope') !== (store === 'received_envelopes') + ) + return undefined; + return { + store, + record, + wire: value.wire, + bytes: new TextEncoder().encode(JSON.stringify(value)).length + }; +} +// Each store has an independent owner-scoped scan/quota. A corrupt received +// namespace cannot reset or consume the outbox, and no uncertain row is evicted. +function transaction<T>( + scope: Scope, + store: Store, + mode: 'readonly' | 'readwrite', + finish: ( + rows: readonly Row[], + tx: IDBTransaction, + refuse: (reason: PrivateStorageFailure) => void + ) => PrivateStorageResult<T> | undefined +): Promise<PrivateStorageResult<T>> { + let tx: IDBTransaction; + try { + tx = browserDatabaseTransaction(scope.database, [store], mode); + } catch { + return Promise.resolve(failed('unavailable')); + } + const countCap = + store === 'private_sends' + ? LOCAL_PERSISTENCE_BUDGETS.unfinishedPrivateSends + : LOCAL_PERSISTENCE_BUDGETS.receivedEnvelopes; + const byteCap = + store === 'private_sends' + ? LOCAL_PERSISTENCE_BUDGETS.privateSendBytes + : LOCAL_PERSISTENCE_BUDGETS.receivedCiphertextBytes; + return new Promise((resolve) => { + let result: PrivateStorageResult<T> | undefined, + bytes = 0; + let rows = Array.from<Row>([]); + function refuse(reason: PrivateStorageFailure) { + result = failed(reason); + try { + tx.abort(); + } catch { + result = failed('unknown_completion'); + } + } + tx.addEventListener('complete', () => + resolve(result ?? failed('unknown_completion')) + ); + tx.addEventListener('abort', () => + resolve(result && !result.ok ? result : failed('aborted')) + ); + try { + const cursorRequest = tx + .objectStore(store) + .index('by_owner') + .openCursor(IDBKeyRange.only(scope.owner)); + cursorRequest.addEventListener('success', () => { + try { + const cursor = cursorRequest.result; + if (!cursor) { + const outcome = finish(rows, tx, refuse); + if (outcome) result = outcome; + return; + } + const key = cursor.primaryKey; + const row = + Array.isArray(key) && key.length === 2 && key[0] === scope.owner + ? stored(cursor.value, store, scope.owner, key[1]) + : undefined; + if (!row) { + refuse('corrupt_record'); + return; + } + rows = rows.concat(row); + bytes += row.bytes; + if (rows.length > countCap || bytes > byteCap) { + refuse('capacity'); + return; + } + cursor.continue(); + } catch { + refuse('aborted'); + } + }); + } catch { + refuse('aborted'); + } + }); +} +function validTransition( + base: PrivateRecord | undefined, + next: PrivateRecord +): boolean { + if (!base) + return ( + next.family === 'received_envelope' && + next.revision === 0 && + next.read === null + ); + if ( + base.owner !== next.owner || + base.id !== next.id || + next.revision !== base.revision + 1 + ) + return false; + if (base.family === 'received_envelope') { + return ( + next.family === base.family && + base.outer === next.outer && + base.observedAtMilliseconds === next.observedAtMilliseconds && + base.sources.every((source) => next.sources.includes(source)) + ); + } + if ( + next.family !== 'private_send_operation' || + base.peer !== next.peer || + base.rumorHash !== next.rumorHash || + base.createdAt !== next.createdAt + ) + return false; + return ( + base.family === 'private_send_reservation' || + (JSON.stringify(base.self) === JSON.stringify(next.self) && + (base.peerArtifact === null || + JSON.stringify(base.peerArtifact) === + JSON.stringify(next.peerArtifact))) + ); +} +// Local ciphertext CAS only. Factory/identity/read authorization and genuine +// durable preparation receipts belong to their workflows. No network or signer +// await can occur inside this transaction, and request success grants no credit. +export function commitPrivateRecord( + repository: PrivateStorageRepository, + handle: PrivateRecordHandle, + expectedBase: PrivateRecordHandle | null +): Promise< + PrivateStorageResult< + Readonly<{ id: string; revision: number; state: 'committed' | 'existing' }> + > +> { + const scope = scopeOf(repository), + identity = privateRecordIdentity(handle); + if (!scope) return Promise.resolve(failed('invalid_scope')); + const next = + identity && privateRecordSnapshot(handle, scope.owner, identity.id), + wire = identity && privateRecordWire(handle, scope.owner, identity.id); + const base = + identity && expectedBase !== null + ? privateRecordSnapshot(expectedBase, scope.owner, identity.id) + : undefined; + const baseWire = + identity && expectedBase !== null + ? privateRecordWire(expectedBase, scope.owner, identity.id) + : undefined; + if ( + !identity || + !next || + !wire || + (expectedBase !== null && (!base || !baseWire)) || + !validTransition(base, next) + ) + return Promise.resolve(failed('invalid_record')); + const store: Store = + next.family === 'received_envelope' + ? 'received_envelopes' + : 'private_sends'; + const row = { owner: scope.owner, id: identity.id, wire }, + bytes = new TextEncoder().encode(JSON.stringify(row)).length; + return transaction< + Readonly<{ id: string; revision: number; state: 'committed' | 'existing' }> + >(scope, store, 'readwrite', (rows, tx, refuse) => { + const previous = rows.find((row) => row.record.id === identity.id); + if (previous?.wire === wire) + return { + ok: true, + value: { id: identity.id, revision: next.revision, state: 'existing' } + }; + if ((previous?.wire ?? null) !== (baseWire ?? null)) { + refuse('conflict'); + return; + } + const countCap = + store === 'private_sends' + ? LOCAL_PERSISTENCE_BUDGETS.unfinishedPrivateSends + : LOCAL_PERSISTENCE_BUDGETS.receivedEnvelopes; + const byteCap = + store === 'private_sends' + ? LOCAL_PERSISTENCE_BUDGETS.privateSendBytes + : LOCAL_PERSISTENCE_BUDGETS.receivedCiphertextBytes; + if ( + (!previous && rows.length >= countCap) || + rows.reduce((total, row) => total + row.bytes, bytes) - + (previous?.bytes ?? 0) > + byteCap + ) { + refuse('capacity'); + return; + } + tx.objectStore(store).put(row); + return { + ok: true, + value: { id: identity.id, revision: next.revision, state: 'committed' } + }; + }); +} +export function loadPrivateRecord( + repository: PrivateStorageRepository, + store: Store, + id: unknown +): Promise<PrivateStorageResult<PrivateRecordHandle>> { + const scope = scopeOf(repository); + if (!scope) return Promise.resolve(failed('invalid_scope')); + if ( + (store !== 'private_sends' && store !== 'received_envelopes') || + typeof id !== 'string' + ) + return Promise.resolve(failed('invalid_record')); + return transaction(scope, store, 'readonly', (rows) => { + const row = rows.find((row) => row.record.id === id); + if (!row) return failed('invalid_record'); + const decoded = decodePrivateRecord(row.wire, scope.owner, id); + return decoded.ok ? decoded : failed('corrupt_record'); + }); +} +export function inspectPrivateStorage( + repository: PrivateStorageRepository, + store: Store +): Promise<PrivateStorageResult<Readonly<{ count: number; bytes: number }>>> { + const scope = scopeOf(repository); + if (!scope) return Promise.resolve(failed('invalid_scope')); + if (store !== 'private_sends' && store !== 'received_envelopes') + return Promise.resolve(failed('invalid_record')); + return transaction(scope, store, 'readonly', (rows) => ({ + ok: true, + value: { + count: rows.length, + bytes: rows.reduce((sum, row) => sum + row.bytes, 0) + } + })); +} diff --git a/web/tests/e2e/harness/private-storage.ts b/web/tests/e2e/harness/private-storage.ts @@ -0,0 +1,163 @@ +import { generateSecretKey, finalizeEvent } from 'applesauce-core/helpers'; +import { nip44 } from 'applesauce-core/helpers/encryption'; +import { + openBrowserDatabase, + browserDatabaseTransaction, + closeBrowserDatabase +} from '../../../src/lib/persistence/database.ts'; +import { + decodePrivateRecord, + privateRecordWire +} from '../../../src/lib/persistence/private-records.ts'; +import { + createPrivateStorageRepository, + commitPrivateRecord, + loadPrivateRecord, + inspectPrivateStorage +} from '../../../src/lib/persistence/private-storage.ts'; +import { + createPrivateSendReservationRepository, + reservePrivateSendReservation +} from '../../../src/lib/persistence/private-send-reservations.ts'; +export { + decodePrivateRecord, + privateRecordWire, + commitPrivateRecord, + loadPrivateRecord, + inspectPrivateStorage +}; +export const owner = + '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; +export const peer = + 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5'; +export const command = '12345678-1234-4234-8234-123456789abc'; +export const id = (index: number) => + '12345678-1234-4234-8234-' + index.toString(16).padStart(12, '0'); +// Real browser IDB; generated disposable fixture crypto only, not a genuine +// nested exchange or installed extension/operator qualification. +export async function makeFixture() { + const opened = await openBrowserDatabase(); + if (opened.state !== 'ready') throw Error('browser storage unavailable'); + const database = opened.owner, + repository = createPrivateStorageRepository(database, owner), + reservations = createPrivateSendReservationRepository(database, owner); + if (!repository || !reservations) throw Error('invalid fixture namespace'); + const repo = repository, + reservedRepo = reservations; + function reservation(index: number) { + return { + schema: 1, + family: 'private_send_reservation', + owner, + id: id(index), + revision: 0, + peer, + rumorHash: index.toString(16).padStart(64, '0'), + createdAt: 1700000000 + }; + } + function outer(destination = owner, index = 0, padded = false) { + const secret = generateSecretKey(); + let conversation: Uint8Array | undefined; + try { + conversation = nip44.v2.utils.getConversationKey(secret, destination); + const event = finalizeEvent( + { + kind: 1059, + created_at: 1700000000 + index, + tags: [['p', destination]], + content: nip44.v2.encrypt( + 'HCP080 private plaintext sentinel', + conversation + ) + }, + secret + ); + const wire = JSON.stringify(event); + // Detached valid signed raw JSON whitespace fixtures measure actual + // stored JSON UTF8 bytes; they are not factory/nested admission claims. + return { + eventId: event.id, + wire: padded + ? wire + '\n'.repeat(32768 - new TextEncoder().encode(wire).length) + : wire + }; + } finally { + conversation?.fill(0); + secret.fill(0); + } + } + function operation(index: number, padded = false) { + return { + ...reservation(index), + family: 'private_send_operation', + revision: 1, + self: outer(owner, index, padded), + peerArtifact: padded ? outer(peer, index, padded) : null + }; + } + function received(index: number, padded = false) { + const artifact = outer(owner, index, padded); + return { + schema: 1, + family: 'received_envelope', + owner, + id: artifact.eventId, + revision: 0, + outer: artifact.wire, + observedAtMilliseconds: 1700000000000, + sources: ['wss://inbox.example.org'], + read: null + }; + } + function handle(record: { owner: string; id: string }) { + const result = decodePrivateRecord( + JSON.stringify(record), + record.owner, + record.id + ); + if (!result.ok) throw Error('invalid fixture record'); + return result.value; + } + async function rows( + store: 'private_sends' | 'received_envelopes' | 'public_drafts' + ) { + const tx = browserDatabaseTransaction(database, [store], 'readonly'), + request = tx.objectStore(store).getAll(); + return await new Promise<unknown[]>((resolve, reject) => { + tx.addEventListener('complete', () => + resolve(request.result as unknown[]) + ); + tx.addEventListener('abort', () => reject(Error('inspection aborted'))); + }); + } + async function inject( + store: 'private_sends' | 'received_envelopes', + values: unknown[] + ) { + const tx = browserDatabaseTransaction(database, [store], 'readwrite'); + for (const value of values) tx.objectStore(store).put(value); + await new Promise<void>((resolve, reject) => { + tx.addEventListener('complete', () => resolve()); + tx.addEventListener('abort', () => reject(Error('injection aborted'))); + }); + } + return { + database, + repository: repo, + reservation, + operation, + received, + handle, + rows, + inject, + reserve: (index: number) => + reservePrivateSendReservation( + reservedRepo, + JSON.stringify(reservation(index)), + 1700000000, + () => true + ), + close: () => closeBrowserDatabase(database) + }; +} diff --git a/web/tests/e2e/private-storage.spec.ts b/web/tests/e2e/private-storage.spec.ts @@ -0,0 +1,415 @@ +import { expect, test, type Page } from '@playwright/test'; +import { build } from 'vite'; +import { fileURLToPath } from 'node:url'; +import { createHash } from 'node:crypto'; +import { createStaticHarness } from '../integration/harness/static.ts'; +import type * as Storage from './harness/private-storage.ts'; +declare global { + interface Window { + hcp080: typeof Storage; + hcp080Fixture: Awaited<ReturnType<typeof Storage.makeFixture>>; + } +} +let server: Awaited<ReturnType<typeof createStaticHarness>>, bundle: string; +test.beforeAll(async () => { + server = await createStaticHarness(); + const result = await build({ + configFile: false, + logLevel: 'silent', + build: { + write: false, + minify: false, + lib: { + entry: fileURLToPath( + new URL('./harness/private-storage.ts', import.meta.url) + ), + name: 'hcp080', + formats: ['iife'] + } + } + }); + const output = Array.isArray(result) ? result[0] : result; + if (!('output' in output)) throw Error('missing bundle'); + const chunks = output.output.filter((item) => item.type === 'chunk'); + expect(chunks).toHaveLength(1); + bundle = chunks[0].code; + console.log( + JSON.stringify({ + fixture: 'HCP080_REAL_IDB_CIPHERTEXT_SCHEMA_QUOTAS', + bundleSha256: createHash('sha256').update(bundle).digest('hex'), + moduleIds: Object.keys(chunks[0].modules), + qualification: + 'Controlled source-only IDB/structural signed ciphertext; no nested admission or actual operator Q' + }) + ); +}); +test.afterAll(async () => server.close()); +async function load(page: Page) { + await page.goto(server.url + '/search'); + await page.addScriptTag({ content: bundle }); + await page.evaluate(async () => { + window.hcp080Fixture = await window.hcp080.makeFixture(); + }); +} +test('durable rows contain only ciphertext/minimal metadata and preserve original reservation retry', async ({ + page +}) => { + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080; + await f.reserve(0); + const base = f.handle(f.reservation(0)), + next = f.handle(f.operation(0)); + const commit = await s.commitPrivateRecord(f.repository, next, base), + retry = await f.reserve(0); + const receive = f.received(0), + received = await s.commitPrivateRecord( + f.repository, + f.handle(receive), + null + ); + const sends = await f.rows('private_sends'), + inbox = await f.rows('received_envelopes'); + return { + commit, + retry: retry.ok && retry.state, + received: received.ok, + stored: JSON.stringify([...sends, ...inbox]), + publicCount: (await f.rows('public_drafts')).length + }; + }); + expect(result.commit.ok).toBe(true); + expect(result.retry).toBe('existing'); + expect(result.received).toBe(true); + expect(result.publicCount).toBe(0); + for (const value of [ + 'HCP080 private plaintext sentinel', + '"body"', + '"subject"', + '"contact"', + '"product"' + ]) + expect(result.stored).not.toContain(value); +}); +test('duplicate signed outer fields grant no handle and preserve existing local evidence', async ({ + page +}) => { + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080; + await f.reserve(0); + const before = JSON.stringify(await f.rows('private_sends')), + operation = f.operation(0), + received = f.received(0); + const poison = (wire: string) => + '{"\\u0063ontent":"PRIVATE_DUPLICATE_SENTINEL",' + wire.slice(1); + const values = [ + s.decodePrivateRecord( + JSON.stringify({ + ...operation, + self: { ...operation.self, wire: poison(operation.self.wire) } + }), + s.owner, + operation.id + ), + s.decodePrivateRecord( + JSON.stringify({ ...received, outer: poison(received.outer) }), + s.owner, + received.id + ) + ]; + return { + accepted: values.filter((v) => v.ok).length, + unchanged: before === JSON.stringify(await f.rows('private_sends')), + received: (await f.rows('received_envelopes')).length + }; + }); + expect(result).toEqual({ accepted: 0, unchanged: true, received: 0 }); +}); +test('conflicting ciphertext CAS preserves the original artifact and exact retries reconcile', async ({ + page +}) => { + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080; + await f.reserve(0); + const base = f.handle(f.reservation(0)), + a = f.handle(f.operation(0)), + b = f.handle(f.operation(0)); + const [first, second] = await Promise.all([ + s.commitPrivateRecord(f.repository, a, base), + s.commitPrivateRecord(f.repository, b, base) + ]); + const winner = first.ok ? a : b, + before = JSON.stringify(await f.rows('private_sends')); + const retry = await s.commitPrivateRecord(f.repository, winner, base); + return { + accepted: [first, second].filter((r) => r.ok).length, + refused: [first, second].filter((r) => !r.ok && r.reason === 'conflict') + .length, + retry: retry.ok && retry.value.state, + unchanged: before === JSON.stringify(await f.rows('private_sends')), + count: (await f.rows('private_sends')).length + }; + }); + expect(result).toEqual({ + accepted: 1, + refused: 1, + retry: 'existing', + unchanged: true, + count: 1 + }); +}); +test('owner-bound handles and loads reject cross-owner or reflected capabilities', async ({ + page +}) => { + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080, + record = f.received(0); + await s.commitPrivateRecord(f.repository, f.handle(record), null); + const foreign = { ...f.reservation(1), owner: s.peer, peer: s.owner }, + decoded = s.decodePrivateRecord( + JSON.stringify(foreign), + s.peer, + foreign.id + ); + const cross = decoded.ok + ? await s.commitPrivateRecord(f.repository, decoded.value, null) + : undefined; + const reflected = await s.commitPrivateRecord( + {} as typeof f.repository, + f.handle(record), + null + ); + const wrong = await s.loadPrivateRecord( + f.repository, + 'received_envelopes', + 'a'.repeat(64) + ); + return { + cross, + reflected, + wrong, + count: (await f.rows('received_envelopes')).length + }; + }); + expect(result.cross?.ok).toBe(false); + expect(result.reflected.ok).toBe(false); + expect(result.wrong.ok).toBe(false); + expect(result.count).toBe(1); +}); +test('unknown schema or corrupt owner records remain unchanged, never reset to an empty inbox', async ({ + page +}) => { + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080, + good = f.received(0), + bad = f.received(1); + await s.commitPrivateRecord(f.repository, f.handle(good), null); + await f.inject('received_envelopes', [ + { + owner: s.owner, + id: bad.id, + wire: JSON.stringify({ ...bad, schema: 999 }) + } + ]); + const before = JSON.stringify(await f.rows('received_envelopes')), + next = f.received(2), + result = await s.commitPrivateRecord(f.repository, f.handle(next), null); + return { + result, + unchanged: before === JSON.stringify(await f.rows('received_envelopes')) + }; + }); + expect(result.result).toEqual({ ok: false, reason: 'corrupt_record' }); + expect(result.unchanged).toBe(true); +}); +test('two actual connections race for the last send slot without evicting existing evidence', async ({ + page, + context +}) => { + await load(page); + await page.evaluate(async () => { + const f = window.hcp080Fixture; + for (let i = 0; i < 99; i++) { + const r = await f.reserve(i); + if (!r.ok) throw Error('seed failure'); + } + }); + const second = await context.newPage(); + await load(second); + const [a, b] = await Promise.all([ + page.evaluate(() => window.hcp080Fixture.reserve(100)), + second.evaluate(() => window.hcp080Fixture.reserve(101)) + ]); + expect([a, b].filter((value) => value.ok)).toHaveLength(1); + expect( + [a, b].filter((value) => !value.ok && value.reason === 'capacity') + ).toHaveLength(1); + expect( + await page.evaluate( + async () => (await window.hcp080Fixture.rows('private_sends')).length + ) + ).toBe(100); +}); +test('private-send stored UTF8 bytes refuse oversized updates and preserve their exact base', async ({ + page +}) => { + test.setTimeout(120_000); + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080; + let stopped = false, + count = 0; + for (let i = 0; i < 100; i++) { + const reservation = await f.reserve(i); + if (!reservation.ok) throw Error('reservation seed failed'); + const before = JSON.stringify(await f.rows('private_sends')); + const update = await s.commitPrivateRecord( + f.repository, + f.handle(f.operation(i, true)), + f.handle(f.reservation(i)) + ); + if (!update.ok) { + if (update.reason !== 'capacity') throw Error('wrong byte refusal'); + stopped = before === JSON.stringify(await f.rows('private_sends')); + count = i; + break; + } + } + return { + stopped, + count, + received: (await f.rows('received_envelopes')).length + }; + }); + expect(result.stopped).toBe(true); + expect(result.count).toBeGreaterThan(0); + expect(result.count).toBeLessThan(100); + expect(result.received).toBe(0); +}); +test('received count cap remains separate from a full unresolved outbox and refuses without eviction', async ({ + page +}) => { + test.setTimeout(120_000); + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080; + for (let i = 0; i < 100; i++) { + const r = await f.reserve(i); + if (!r.ok) throw Error('send seed failure'); + } + const rows = []; + for (let i = 0; i < 2000; i++) { + const r = f.received(i); + rows.push({ + owner: s.owner, + id: r.id, + wire: s.privateRecordWire(f.handle(r), s.owner, r.id) + }); + } + await f.inject('received_envelopes', rows); + const candidate = f.received(2001); + const before = JSON.stringify(await f.rows('received_envelopes')), + result = await s.commitPrivateRecord( + f.repository, + f.handle(candidate), + null + ); + return { + result, + unchanged: before === JSON.stringify(await f.rows('received_envelopes')), + sends: (await f.rows('private_sends')).length, + count: (await f.rows('received_envelopes')).length + }; + }); + expect(result.result).toEqual({ ok: false, reason: 'capacity' }); + expect(result.unchanged).toBe(true); + expect(result.sends).toBe(100); + expect(result.count).toBe(2000); +}); +test('received 48MiB byte cap refuses below count cap and preserves both independent namespaces', async ({ + page +}) => { + test.setTimeout(120_000); + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080, + rows = []; + let bytes = 0; + let candidate = f.received(0, true); + while (true) { + const wire = s.privateRecordWire( + f.handle(candidate), + s.owner, + candidate.id + )!; + const row = { owner: s.owner, id: candidate.id, wire }, + size = new TextEncoder().encode(JSON.stringify(row)).length; + if (bytes + size > 50331648) break; + rows.push(row); + bytes += size; + candidate = f.received(rows.length, true); + } + await f.inject('received_envelopes', rows); + await f.reserve(0); + const before = JSON.stringify(await f.rows('received_envelopes')), + result = await s.commitPrivateRecord( + f.repository, + f.handle(candidate), + null + ); + return { + result, + unchanged: before === JSON.stringify(await f.rows('received_envelopes')), + count: rows.length, + bytes, + sends: (await f.rows('private_sends')).length + }; + }); + expect(result.result).toEqual({ ok: false, reason: 'capacity' }); + expect(result.unchanged).toBe(true); + expect(result.count).toBeLessThan(2000); + expect(result.bytes).toBeLessThanOrEqual(50331648); + expect(result.sends).toBe(1); +}); +test('real transaction abort never acknowledges or retains a new ciphertext row', async ({ + page +}) => { + await load(page); + const result = await page.evaluate(async () => { + const f = window.hcp080Fixture, + s = window.hcp080, + r = f.received(0), + original = Reflect.get<IDBObjectStore, 'put'>( + IDBObjectStore.prototype, + 'put' + ); + IDBObjectStore.prototype.put = function (...args) { + const request = original.apply(this, args); + request.addEventListener('success', () => this.transaction.abort(), { + once: true + }); + return request; + }; + let result; + try { + result = await s.commitPrivateRecord(f.repository, f.handle(r), null); + } finally { + IDBObjectStore.prototype.put = original; + } + return { result, count: (await f.rows('received_envelopes')).length }; + }); + expect(result.result.ok).toBe(false); + expect(result.count).toBe(0); +}); diff --git a/web/tests/provenance/output.test.mjs b/web/tests/provenance/output.test.mjs @@ -21,8 +21,8 @@ import { fileURLToPath } from 'node:url'; import { generateBuildInfo } from '../../tools/build-info.mjs'; import { auditOutput } from '../../tools/check-output.mjs'; -// Five real compilations have separate120s setup/copy/compiler/audit bounds. -// Each Vite child keeps60s; admission checks keep10s within their180s group. +// Slow-host verification allows600s for each setup/copy/compiler/audit test. +// Vite children keep300s; admission checks keep120s within their1800s group. // Serial top-level tests keep compatibility work out of the mutation budget. { const source = fileURLToPath(new URL('../../../', import.meta.url)); @@ -131,7 +131,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; execFileSync( process.execPath, [path.join(source, 'web/node_modules/vite/bin/vite.js'), 'build'], - { cwd: web, stdio: 'pipe', timeout: 60000 } + { cwd: web, stdio: 'pipe', timeout: 300000 } ); active(t); } finally { @@ -142,7 +142,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; } await test( 'actual output fixture setup', - { timeout: 120000 }, + { timeout: 600000 }, async (t) => { // Compile actual current source once in isolation. No install/native subprocess. for (const name of git( @@ -191,7 +191,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; ); await test( 'public-only owned lifecycle remains independently compilable', - { timeout: 120000 }, + { timeout: 600000 }, async (t) => { const f = await fixture(t); for (const [name, value] of Object.entries(historicalPublicOnly)) { @@ -223,7 +223,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; ); await test( 'predecessor SDK graph remains qualified with the historical search form fixture', - { timeout: 120000 }, + { timeout: 600000 }, async (t) => { const f = await fixture(t); // This historical graph predates identity activation. Restore its @@ -264,7 +264,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; ); await test( 'HCP043 search graph remains qualified with the exact historical product shell', - { timeout: 120000 }, + { timeout: 600000 }, async (t) => { const f = await fixture(t); for (const [name, value] of Object.entries(historicalPublicOnly)) @@ -281,10 +281,10 @@ import { auditOutput } from '../../tools/check-output.mjs'; ); await test( 'actual output qualification', - { timeout: 180000 }, + { timeout: 1800000 }, async (t) => { /** @param {string} name @param {(context: import('node:test').TestContext) => Promise<void>} run @param {number} [timeout] */ - const check = (name, run, timeout = 10000) => + const check = (name, run, timeout = 120000) => t.test(name, { timeout }, run); await check( 'actual compiled payload passes and build dispatch invokes the guard last', @@ -1559,7 +1559,12 @@ import { auditOutput } from '../../tools/check-output.mjs'; f.web, new URL('../../tools/check-output.mjs', import.meta.url).href ], - { cwd: f.web, encoding: 'utf8', timeout: 8000, maxBuffer: 4096 } + { + cwd: f.web, + encoding: 'utf8', + timeout: 120000, + maxBuffer: 4096 + } ) ); assert.deepEqual(Object.keys(measurement).sort(), [ @@ -1601,7 +1606,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; ); await test( 'actual compiler admits nonempty approved theme/app CSS', - { timeout: 120000 }, + { timeout: 600000 }, async (t) => { ready(t); const layout = path.join(base, 'web/src/routes/+layout.svelte'); diff --git a/web/tests/unit/private-records.test.ts b/web/tests/unit/private-records.test.ts @@ -0,0 +1,289 @@ +import { describe, expect, it } from 'vitest'; +import { + generateSecretKey, + getPublicKey, + finalizeEvent +} from 'applesauce-core/helpers'; +import { nip44 } from 'applesauce-core/helpers/encryption'; +import { + decodePrivateRecord, + privateRecordSnapshot, + privateRecordWire, + inspectPrivateSendReservation +} from '../../src/lib/persistence/private-records.ts'; +const command = '12345678-1234-4234-8234-123456789abc'; +function fixture() { + const key = generateSecretKey(), + peerKey = generateSecretKey(); + let conversation: Uint8Array | undefined, + peerConversation: Uint8Array | undefined; + try { + const owner = getPublicKey(key), + peer = getPublicKey(peerKey); + conversation = nip44.v2.utils.getConversationKey(key, owner); + const outer = finalizeEvent( + { + kind: 1059, + created_at: 1700000000, + tags: [['p', owner]], + content: nip44.v2.encrypt( + 'Ciphertext structure fixture only', + conversation + ) + }, + key + ); + peerConversation = nip44.v2.utils.getConversationKey(key, peer); + const peerOuter = finalizeEvent( + { + kind: 1059, + created_at: 1700000001, + tags: [['p', peer]], + content: nip44.v2.encrypt( + 'Ciphertext structure fixture only', + peerConversation + ) + }, + key + ); + const peerArtifact = { + eventId: peerOuter.id, + wire: JSON.stringify(peerOuter) + }; + const reservation = { + schema: 1, + family: 'private_send_reservation', + owner, + id: command, + revision: 0, + peer, + rumorHash: 'a'.repeat(64), + createdAt: 1700000000 + }; + const operation = { + ...reservation, + family: 'private_send_operation', + revision: 1, + self: { eventId: outer.id, wire: JSON.stringify(outer) }, + peerArtifact: null + }; + const received = { + schema: 1, + family: 'received_envelope', + owner, + id: outer.id, + revision: 0, + outer: JSON.stringify(outer), + observedAtMilliseconds: 1700000000000, + sources: ['wss://inbox.example.org'], + read: null + }; + return { owner, peer, reservation, operation, received, peerArtifact }; + } finally { + conversation?.fill(0); + peerConversation?.fill(0); + key.fill(0); + peerKey.fill(0); + } +} +describe('ciphertext-only private local codecs, not nested authenticity or effect authority', () => { + it('rejects duplicate or escaped-alias outer content fields instead of persisting hidden plaintext', () => { + const f = fixture(); + for (const name of ['"content"', '"\\u0063ontent"']) { + const poison = (raw: string) => + '{' + name + ':"PRIVATE_DUPLICATE_SENTINEL",' + raw.slice(1); + const cases = [ + { ...f.received, outer: poison(f.received.outer) }, + { + ...f.operation, + self: { ...f.operation.self, wire: poison(f.operation.self.wire) } + }, + { + ...f.operation, + peerArtifact: { ...f.peerArtifact, wire: poison(f.peerArtifact.wire) } + } + ]; + for (const record of cases) + expect( + decodePrivateRecord(JSON.stringify(record), f.owner, record.id).ok + ).toBe(false); + } + }); + it('rejects duplicate local/nested metadata while retaining valid reordered/whitespace ciphertext bytes exactly', () => { + const f = fixture(); + const duplicateRoot = + '{"owner":"PRIVATE_DUPLICATE_SENTINEL",' + + JSON.stringify(f.operation).slice(1); + const duplicateArtifact = JSON.stringify(f.operation).replace( + '"self":{', + '"self":{"wire":"PRIVATE_DUPLICATE_SENTINEL",' + ); + const read = { + ...f.received, + read: { rumorHash: 'b'.repeat(64), atMilliseconds: 1 } + }; + const duplicateRead = JSON.stringify(read).replace( + '"read":{', + '"read":{"rumorHash":"PRIVATE_DUPLICATE_SENTINEL",' + ); + for (const [raw, id] of [ + [duplicateRoot, f.operation.id], + [duplicateArtifact, f.operation.id], + [duplicateRead, f.received.id] + ]) + expect(decodePrivateRecord(raw, f.owner, id).ok).toBe(false); + const parsed = JSON.parse(f.received.outer) as Record<string, unknown>; + const reordered = + '\n' + + JSON.stringify( + { + sig: parsed.sig, + content: parsed.content, + tags: parsed.tags, + kind: parsed.kind, + created_at: parsed.created_at, + pubkey: parsed.pubkey, + id: parsed.id + }, + null, + 2 + ) + + '\t'; + const record = { ...f.received, outer: reordered }, + decoded = decodePrivateRecord(JSON.stringify(record), f.owner, record.id); + expect(decoded.ok).toBe(true); + if (!decoded.ok) throw Error('valid whitespace/reordering rejected'); + expect(privateRecordSnapshot(decoded.value, f.owner, record.id)).toEqual( + record + ); + }); + it('admits strict metadata reservations, self recovery and received ciphertext separately from public records', () => { + const f = fixture(); + for (const record of [f.reservation, f.operation, f.received]) { + const result = decodePrivateRecord( + JSON.stringify(record), + f.owner, + record.id + ); + expect(result.ok).toBe(true); + if (!result.ok) throw Error('expected bounded record'); + expect(privateRecordSnapshot(result.value, f.owner, record.id)).toEqual( + record + ); + expect( + privateRecordSnapshot(result.value, f.peer, record.id) + ).toBeUndefined(); + expect(privateRecordWire(result.value, f.owner, record.id)).toBe( + JSON.stringify(record) + ); + expect( + privateRecordSnapshot({} as typeof result.value, f.owner, record.id) + ).toBeUndefined(); + } + expect( + inspectPrivateSendReservation( + JSON.stringify(f.reservation), + f.owner, + command + ) + ).toEqual(f.reservation); + }); + it('rejects plaintext fields, unknown schema/families, bad scope and unsafe metadata without coercion', () => { + const f = fixture(); + for (const record of [f.reservation, f.operation, f.received]) { + for (const key of ['body', 'subject', 'contact', 'product', 'content']) { + expect( + decodePrivateRecord( + JSON.stringify({ ...record, [key]: 'private sentinel' }), + f.owner, + record.id + ).ok + ).toBe(false); + } + for (const patch of [ + { schema: 2 }, + { family: 'public_draft' }, + { revision: -1 }, + { revision: 1.5 }, + { owner: f.peer }, + { id: 'bad' } + ]) { + expect( + decodePrivateRecord( + JSON.stringify({ ...record, ...patch }), + f.owner, + record.id + ).ok + ).toBe(false); + } + expect( + decodePrivateRecord(JSON.stringify(record), f.peer, record.id).ok + ).toBe(false); + } + expect( + decodePrivateRecord( + { toString: () => JSON.stringify(f.operation) }, + f.owner, + command + ).ok + ).toBe(false); + expect(decodePrivateRecord('{bad', f.owner, command).ok).toBe(false); + }); + it('requires exact signed bounded1059 ciphertext and role destination, never rumor or seal plaintext', () => { + const f = fixture(), + outer = JSON.parse(f.operation.self.wire) as Record<string, unknown>; + for (const patch of [ + { kind: 14 }, + { kind: 13 }, + { content: 'cleartext' }, + { tags: [['p', f.peer]] }, + { sig: '0'.repeat(128) }, + { privateBody: 'sentinel' } + ]) { + const next = { + ...f.operation, + self: { + eventId: f.operation.self.eventId, + wire: JSON.stringify({ ...outer, ...patch }) + } + }; + expect( + decodePrivateRecord(JSON.stringify(next), f.owner, command).ok + ).toBe(false); + } + const oversized = { + ...f.operation, + self: { + ...f.operation.self, + wire: f.operation.self.wire + ' '.repeat(32768) + } + }; + expect( + decodePrivateRecord(JSON.stringify(oversized), f.owner, command).ok + ).toBe(false); + expect( + decodePrivateRecord( + JSON.stringify({ ...f.operation, self: null }), + f.owner, + command + ).ok + ).toBe(false); + expect( + decodePrivateRecord( + JSON.stringify({ ...f.received, sources: ['https://inbox.example'] }), + f.owner, + f.received.id + ).ok + ).toBe(false); + expect( + decodePrivateRecord( + JSON.stringify({ + ...f.received, + read: { rumorHash: 'bad', atMilliseconds: 1 } + }), + f.owner, + f.received.id + ).ok + ).toBe(false); + }); +}); diff --git a/web/vitest.config.ts b/web/vitest.config.ts @@ -7,6 +7,7 @@ export default defineConfig({ test: { environment: 'node', fileParallelism: false, + testTimeout: 120_000, include: ['tests/unit/**/*.test.ts'], setupFiles: ['tests/unit/ssr-effects.ts'], passWithNoTests: false