app

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

commit 4622502fd189a6f21f6ec5031aa0973ab70cb087
parent abab6f250b3cff13c3b9f5b156a76d1dd802a54f
Author: triesap <tyson@radroots.org>
Date:   Wed,  7 Oct 2026 11:32:31 +0000

nostr: invalidate the private generation after AUTH owner loss (HCP-068)

- Invalidate the original identity and private cache on fresh key or capability loss.
- Fence invalidation against stale callbacks and successor sessions.
- Keep signing refusal paused and retain outstanding permission ownership.
- Cover initial and late key failures, capability loss and cache clearing in Chromium.

Diffstat:
Mweb/src/lib/nostr/inbox-auth.ts | 29+++++++++++++++++++++++++++++
Mweb/src/lib/nostr/private-pool.ts | 19+++++++++++++++++++
Mweb/src/lib/runtime/private-session.ts | 14++++++++++++++
Mweb/tests/e2e/harness/inbox-auth.ts | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mweb/tests/e2e/inbox-auth.spec.ts | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 225 insertions(+), 4 deletions(-)

diff --git a/web/src/lib/nostr/inbox-auth.ts b/web/src/lib/nostr/inbox-auth.ts @@ -8,6 +8,7 @@ import { reservePrivateAuthResponse, sendPrivateAuthResponse, closePrivateAuthConnection, + invalidatePrivateAuthGeneration, type PrivatePool, type PrivateAuthChallenge } from './private-pool.ts'; @@ -83,6 +84,17 @@ export async function beginInboxAuthentication( const field = Object.getOwnPropertyDescriptor(value, name); return field && 'value' in field ? field.value : undefined; } + function messagingCapabilitiesCurrent(): boolean { + try { + const cipher = new ExtensionSigner().nip44; + return ( + typeof cipher?.encrypt === 'function' && + typeof cipher?.decrypt === 'function' + ); + } catch { + return false; + } + } // Fixed seven-field response reconstruction never invokes provider getters, // iterators/toJSON or mutable cached SDK signature markers. function bindResponse( @@ -145,6 +157,14 @@ export async function beginInboxAuthentication( new ExtensionSigner().getPublicKey() ); if ( + proof.current() && + (key.status === 'denied' || + (key.status === 'settled' && + key.current && + (key.value !== proof.owner || !messagingCapabilitiesCurrent()))) + ) + invalidatePrivateAuthGeneration(connection, proof); + if ( key.status !== 'settled' || !key.current || key.value !== proof.owner || @@ -194,6 +214,15 @@ export async function beginInboxAuthentication( new ExtensionSigner().getPublicKey() ); if ( + proof.current() && + (effectKey.status === 'denied' || + (effectKey.status === 'settled' && + effectKey.current && + (effectKey.value !== proof.owner || + !messagingCapabilitiesCurrent()))) + ) + invalidatePrivateAuthGeneration(connection, proof); + if ( effectKey.status !== 'settled' || !effectKey.current || effectKey.value !== proof.owner || diff --git a/web/src/lib/nostr/private-pool.ts b/web/src/lib/nostr/private-pool.ts @@ -21,6 +21,7 @@ import { privateSessionOwnership, subscribePrivateSessionClose, recheckPrivateSession, + invalidatePrivateSessionGeneration, type PrivateSession } from '../runtime/private-session.ts'; import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts'; @@ -67,6 +68,7 @@ export type PrivateAuthChallenge = Readonly<{ current(): boolean; }>; type AuthControl = { + invalidate(proof: PrivateAuthChallenge): boolean; challenge(): PrivateAuthChallenge | undefined; stopped(): boolean; reserve(proof: PrivateAuthChallenge): boolean; @@ -204,6 +206,17 @@ export function getPrivatePool( ); } const control: AuthControl = { + invalidate(proof) { + if ( + !proofs.has(proof) || + !proof.current() || + !current() || + proof.connection !== generation || + proof.challenge !== challenge + ) + return false; + return invalidatePrivateSessionGeneration(session); + }, stopped: () => !current(), challenge() { if (!current() || challenge === undefined || responded) @@ -593,3 +606,9 @@ export function closePrivateAuthConnection( ): void { authConnections.get(connection)?.close(); } +export function invalidatePrivateAuthGeneration( + connection: PrivateAuthConnection, + proof: PrivateAuthChallenge +): boolean { + return authConnections.get(connection)?.invalidate(proof) ?? false; +} diff --git a/web/src/lib/runtime/private-session.ts b/web/src/lib/runtime/private-session.ts @@ -2,6 +2,7 @@ import { identityMessagingOwnership, recheckIdentityOwner, subscribeIdentityInvalidation, + disconnectIdentity, type IdentitySession } from './identity-session.ts'; import type { PublicEffectCapture } from './effect-ownership.ts'; @@ -138,6 +139,19 @@ export function subscribePrivateSessionClose( export function closePrivateSession(token: PrivateSession): boolean { return sessions.get(token)?.close() ?? false; } +// A trusted private owner has actually observed key/capability loss. Invalidate +// synchronously inside its occupied permission job without a second provider +// request. The original captured generation must still be current: a late old +// callback cannot disconnect a newly connected generation of this identity. +export function invalidatePrivateSessionGeneration( + token: PrivateSession +): boolean { + const state = sessions.get(token); + if (!state?.current()) return false; + disconnectIdentity(state.identity); + state.close(); + return true; +} // Every new finite network action gets a fresh SDK owner observation; neither // a remembered key nor a still-current presentation snapshot substitutes. export async function recheckPrivateSession( diff --git a/web/tests/e2e/harness/inbox-auth.ts b/web/tests/e2e/harness/inbox-auth.ts @@ -4,12 +4,21 @@ import { createIdentitySession, connectIdentity, probeIdentityMessaging, - disconnectIdentity + disconnectIdentity, + identityMessagingOwnership } from '../../../src/lib/runtime/identity-session.ts'; import { createPrivateSession, - closePrivateSession + closePrivateSession, + privateSessionSnapshot } from '../../../src/lib/runtime/private-session.ts'; +import * as ActualPrivateSession from '../../../src/lib/runtime/private-session.ts'; +import { + getPrivateStore, + insertPrivateEnvelope, + privateStoreSnapshot +} from '../../../src/lib/messaging/private-store.ts'; +import { verifyEnvelope } from '../../../src/lib/nostr/verified-envelope.ts'; import { getPrivatePool, beginPrivateAuthConnection, @@ -41,13 +50,20 @@ export async function makeAuthFixture() { let mode: 'exact' | 'author' | 'relay' | 'challenge' | 'cached' | 'denied' = 'exact', signs = 0, - changed = false; + changed = false, + invalidKey = false; let before = () => Promise.resolve(); Object.defineProperty(window, 'nostr', { configurable: true, value: { getPublicKey: () => - Promise.resolve(changed ? 'f'.repeat(64) : fixture.owner), + Promise.resolve( + invalidKey + ? 'not-a-public-key' + : changed + ? 'f'.repeat(64) + : fixture.owner + ), signEvent: async (input: Parameters<typeof fixture.sign>[0]) => { signs++; await before(); @@ -76,6 +92,20 @@ export async function makeAuthFixture() { const policy = fixturePolicy(), pool = getPrivatePool(session, policy, [inbox]); if (!pool) throw new Error('missing private pool'); + const store = getPrivateStore(session); + if (!store) throw new Error('missing genuine raw store'); + const cached = verifyEnvelope( + JSON.stringify( + fixture.sign({ + kind: 1059, + created_at: 101, + tags: [['p', fixture.owner]], + content: 'opaque raw ciphertext fixture' + }) + ) + ); + if (!cached.ok || insertPrivateEnvelope(store, cached.value) !== 'accepted') + throw new Error('missing actual verified raw cache fixture'); return { pool, policy, @@ -90,9 +120,52 @@ export async function makeAuthFixture() { changeOwner: () => { changed = true; }, + loseCapability: () => { + const provider: unknown = Reflect.get(window, 'nostr'); + if (!provider || typeof provider !== 'object') + throw new Error('missing controlled provider'); + Object.defineProperty(provider, 'nip44', { + configurable: true, + value: undefined + }); + }, + failKey: (mode: 'invalid' | 'missing') => { + if (mode === 'invalid') invalidKey = true; + else Reflect.deleteProperty(window, 'nostr'); + }, logout: () => disconnectIdentity(identity), reopen: () => getPrivatePool(session, policy, [inbox]), startPage: () => subscribePrivatePage(pool, 1, () => {}), + ownership: () => ({ + private: privateSessionSnapshot(session)?.current, + identity: identityMessagingOwnership(identity)?.current() ?? false, + cache: privateStoreSnapshot(store) + }), + async staleGenerationInvalidation() { + disconnectIdentity(identity); + await connectIdentity(identity); + await probeIdentityMessaging(identity, 'reviewed_self_copy'); + const successor = await createPrivateSession( + identity, + 'reviewed_private_session' + ); + if (!successor) throw new Error('missing genuine successor'); + const invalidate: unknown = Reflect.get( + ActualPrivateSession, + 'invalidatePrivateSessionGeneration' + ); + const result: unknown = + typeof invalidate === 'function' + ? Reflect.apply(invalidate, undefined, [session]) + : 'missing'; + const observed = { + result, + successor: privateSessionSnapshot(successor)?.current, + identity: identityMessagingOwnership(identity)?.current() ?? false + }; + closePrivateSession(successor); + return observed; + }, async rawPortBypass() { const connection = (await Reflect.apply( beginPrivateAuthConnection, diff --git a/web/tests/e2e/inbox-auth.spec.ts b/web/tests/e2e/inbox-auth.spec.ts @@ -468,3 +468,89 @@ test('reviewed AUTH uses the same genuine already-open private page connection', expect(frames.filter((x) => x[0] === 'REQ')).toHaveLength(1); expect(frames.filter((x) => x[0] === 'AUTH')).toHaveLength(1); }); +for (const phase of ['initial', 'post-sign'] as const) + test(`actual ${phase} AUTH key loss invalidates original session identity and retained private cache`, async ({ + page + }) => { + const result = await page.evaluate(async (phase) => { + const f = window.hcp068Fixture, + before = f.ownership(); + if (phase === 'initial') f.changeOwner(); + else + f.beforeSign(() => { + f.changeOwner(); + return Promise.resolve(); + }); + const result = await window.hcp068.respondInboxAuthentication( + window.hcp068Action + ); + return { before, result, after: f.ownership() }; + }, phase); + expect(result.before.private).toBe(true); + expect(result.before.identity).toBe(true); + expect(result.before.cache.count).toBe(1); + expect(result.result.status).not.toBe('accepted'); + expect(result.after.private).toBe(false); + expect(result.after.identity).toBe(false); + expect(result.after.cache).toEqual({ closed: true, count: 0, bytes: 0 }); + expect(frames.filter((x) => x[0] === 'AUTH')).toHaveLength(0); + }); +test('stale original generation cannot invalidate a genuinely connected successor', async ({ + page +}) => { + expect( + await page.evaluate(() => + window.hcp068Fixture.staleGenerationInvalidation() + ) + ).toEqual({ result: false, successor: true, identity: true }); +}); +for (const phase of ['initial', 'post-sign'] as const) + test(`actual ${phase} capability loss invalidates AUTH session and cache`, async ({ + page + }) => { + const result = await page.evaluate(async (phase) => { + const f = window.hcp068Fixture; + if (phase === 'initial') f.loseCapability(); + else + f.beforeSign(() => { + f.loseCapability(); + return Promise.resolve(); + }); + const result = await window.hcp068.respondInboxAuthentication( + window.hcp068Action + ); + return { result, after: f.ownership() }; + }, phase); + expect(result.result.status).not.toBe('accepted'); + expect(result.after.private).toBe(false); + expect(result.after.identity).toBe(false); + expect(result.after.cache).toEqual({ closed: true, count: 0, bytes: 0 }); + expect(frames.filter((x) => x[0] === 'AUTH')).toHaveLength(0); + }); +for (const mode of ['invalid', 'missing'] as const) + for (const phase of ['initial', 'post-sign'] as const) + test(`actual ${phase} ${mode} key observation invalidates original ownership`, async ({ + page + }) => { + const result = await page.evaluate( + async ({ mode, phase }) => { + const f = window.hcp068Fixture; + if (phase === 'initial') f.failKey(mode); + else + f.beforeSign(() => { + f.failKey(mode); + return Promise.resolve(); + }); + const result = await window.hcp068.respondInboxAuthentication( + window.hcp068Action + ); + return { result, after: f.ownership() }; + }, + { mode, phase } + ); + expect(result.result.status).not.toBe('accepted'); + expect(result.after.private).toBe(false); + expect(result.after.identity).toBe(false); + expect(result.after.cache).toEqual({ closed: true, count: 0, bytes: 0 }); + expect(frames.filter((x) => x[0] === 'AUTH')).toHaveLength(0); + });