app

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

extension-adversarial.spec.ts (5680B)


      1 import { expect, test, type Page } from '@playwright/test';
      2 import { build } from 'vite';
      3 import { fileURLToPath } from 'node:url';
      4 import { createHash } from 'node:crypto';
      5 import { createStaticHarness } from '../integration/harness/static.ts';
      6 import type * as PublicFixture from './harness/sign-listing.ts';
      7 import type * as PrivateFixture from './harness/private-preparation.ts';
      8 declare global {
      9   interface Window {
     10     hcp136Public: typeof PublicFixture;
     11     hcp136Private: typeof PrivateFixture;
     12     hcp136LegacyReads: number;
     13     hcp136SignAttempts: number;
     14   }
     15 }
     16 let server: Awaited<ReturnType<typeof createStaticHarness>>;
     17 const bundles = new Map<'public' | 'private', string>();
     18 test.beforeAll(async () => {
     19   server = await createStaticHarness();
     20   for (const [role, entry, name] of [
     21     ['public', './harness/sign-listing.ts', 'hcp136Public'],
     22     ['private', './harness/private-preparation.ts', 'hcp136Private']
     23   ] as const) {
     24     const result = await build({
     25       configFile: false,
     26       logLevel: 'silent',
     27       build: {
     28         write: false,
     29         minify: false,
     30         lib: {
     31           entry: fileURLToPath(new URL(entry, import.meta.url)),
     32           name,
     33           formats: ['iife']
     34         }
     35       }
     36     });
     37     const output = Array.isArray(result) ? result[0] : result;
     38     if (!('output' in output)) throw Error('missing fixture bundle');
     39     const chunks = output.output.filter((item) => item.type === 'chunk');
     40     expect(chunks).toHaveLength(1);
     41     bundles.set(role, chunks[0].code);
     42     console.log(
     43       JSON.stringify({
     44         fixture: 'HCP136_HOSTILE_PROVIDER_ORIGINAL_CONSUMER',
     45         role,
     46         bundleSha256: createHash('sha256').update(chunks[0].code).digest('hex'),
     47         moduleIds: Object.keys(chunks[0].modules),
     48         qualification:
     49           'Actual Chromium/native storage/original SDK controlled providers; no installed extension, external client or operator Q.'
     50       })
     51     );
     52   }
     53 });
     54 test.afterAll(async () => server.close());
     55 async function load(page: Page, role: 'public' | 'private') {
     56   await page.goto(server.url + '/search');
     57   const bundle = bundles.get(role);
     58   if (!bundle) throw Error('missing actual bundle');
     59   await page.addScriptTag({ content: bundle });
     60 }
     61 test('hostile signature denial leaves original captured intent without artifact or automatic prompt retry', async ({
     62   page
     63 }) => {
     64   await load(page, 'public');
     65   const result = await page.evaluate(async () => {
     66     const f = await window.hcp136Public.createFixture();
     67     window.hcp136SignAttempts = 0;
     68     const provider = window.nostr as
     69       { signEvent(): Promise<never> } | undefined;
     70     if (!provider) throw Error('missing controlled provider');
     71     provider.signEvent = () => {
     72       window.hcp136SignAttempts++;
     73       return Promise.reject(Error('HOSTILE_PROVIDER_PRIVATE_ERROR_SENTINEL'));
     74     };
     75     try {
     76       const r = await f.exact();
     77       return { r, attempts: window.hcp136SignAttempts };
     78     } finally {
     79       f.close();
     80     }
     81   });
     82   expect(result.r.status).not.toBe('prepared');
     83   expect(result.r.retry).not.toBe('prepared');
     84   expect(result.r.same).toBe(true);
     85   expect(result.r.revision).toBe(1);
     86   expect(result.r.artifact).toBeNull();
     87   expect(result.r.plan).toBeUndefined();
     88   expect(result.r.originalCapture).toBe(true);
     89   expect(result.attempts).toBe(1);
     90   expect(JSON.stringify(result)).not.toContain(
     91     'HOSTILE_PROVIDER_PRIVATE_ERROR_SENTINEL'
     92   );
     93 });
     94 for (const mode of ['declined', 'missing'] as const)
     95   test(`${mode} original self encryption never inspects legacy fallback or persists plaintext`, async ({
     96     page
     97   }) => {
     98     await load(page, 'private');
     99     const result = await page.evaluate(async (mode) => {
    100       const s = window.hcp136Private,
    101         f = await s.makeFixture();
    102       const provider = window.nostr;
    103       if (!provider) throw Error('missing controlled provider');
    104       window.hcp136LegacyReads = 0;
    105       Object.defineProperty(provider, 'nip04', {
    106         configurable: true,
    107         get() {
    108           window.hcp136LegacyReads++;
    109           throw Error('legacy fallback forbidden');
    110         }
    111       });
    112       const before = f.counts(),
    113         stored = await f.stored();
    114       f.mode(mode);
    115       try {
    116         const first = await s.prepareSelfRecovery(
    117           f.preparation,
    118           'reviewed_self_recovery'
    119         );
    120         const afterFirst = f.counts();
    121         const retry = await s.prepareSelfRecovery(
    122           f.preparation,
    123           'reviewed_self_recovery'
    124         );
    125         return {
    126           first: first.status,
    127           retry: retry.status,
    128           before,
    129           afterFirst,
    130           after: f.counts(),
    131           legacy: window.hcp136LegacyReads,
    132           acknowledged: !!s.preparedSelfRecovery(f.preparation),
    133           composer: s.privateComposerSnapshot(f.composer),
    134           unchanged: stored === (await f.stored()),
    135           plaintext: (await f.stored()).includes(f.marker)
    136         };
    137       } finally {
    138         f.close();
    139       }
    140     }, mode);
    141     expect(result.acknowledged).toBe(false);
    142     expect(result.composer?.state).not.toBe('saved_encrypted');
    143     if (mode === 'missing') expect(result.composer).toBeUndefined();
    144     else expect(result.composer?.dirty).toBe(true);
    145     expect(result.legacy).toBe(0);
    146     expect(result.unchanged).toBe(true);
    147     expect(result.plaintext).toBe(false);
    148     expect(result.after.signs).toBe(result.before.signs);
    149     expect(result.after.decrypts).toBe(result.before.decrypts);
    150     expect(result.afterFirst.encrypts - result.before.encrypts).toBe(
    151       mode === 'missing' ? 0 : 1
    152     );
    153     expect(result.after.encrypts - result.afterFirst.encrypts).toBe(
    154       mode === 'missing' ? 0 : 1
    155     );
    156   });