app

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

approved-signing.test.ts (7764B)


      1 import { describe, expect, it } from 'vitest';
      2 import { getEventHash } from 'applesauce-core/helpers';
      3 import { verifyEnvelope } from '../../src/lib/nostr/verified-envelope.ts';
      4 import { decodePublicRecord } from '../../src/lib/persistence/records.ts';
      5 import {
      6   makeFixture,
      7   approveCapturedPublicSigning,
      8   disposableApprovedTemplate,
      9   bindApprovedResponse,
     10   capturedArtifactSnapshot,
     11   publicRecordSnapshot
     12 } from '../e2e/harness/approved-signing.ts';
     13 import type { ApprovedPublicSigning } from '../../src/lib/nostr/approved-signing.ts';
     14 import type { PublicRecordHandle } from '../../src/lib/persistence/records.ts';
     15 function approved(f: ReturnType<typeof makeFixture>) {
     16   const value = approveCapturedPublicSigning(
     17     f.record,
     18     f.owner,
     19     f.id,
     20     'reviewed_captured_operation'
     21   );
     22   if (!value) throw new Error('missing approved fixture');
     23   return value;
     24 }
     25 describe('immutable approved signer responses', () => {
     26   it('rejects a revoked response Proxy without throwing beyond the finite boundary', () => {
     27     const f = makeFixture();
     28     try {
     29       const a = approved(f),
     30         revoked = Proxy.revocable({}, {});
     31       revoked.revoke();
     32       expect(() => bindApprovedResponse(a, revoked.proxy)).not.toThrow();
     33       expect(bindApprovedResponse(a, revoked.proxy)).toBeUndefined();
     34     } finally {
     35       f.close();
     36     }
     37   });
     38   for (const kind of [30402, 5, 10050] as const)
     39     it(`admits genuine exact signed ${kind} from a captured reviewed operation`, () => {
     40       const f = makeFixture(kind);
     41       try {
     42         const a = approved(f),
     43           copy = disposableApprovedTemplate(a);
     44         if (!copy) throw new Error('missing copy');
     45         const artifact = bindApprovedResponse(a, f.sign(copy));
     46         expect(artifact).toBeDefined();
     47         expect(artifact && capturedArtifactSnapshot(artifact)?.hash).toBe(
     48           getEventHash(f.template)
     49         );
     50         expect(artifact && capturedArtifactSnapshot(artifact)?.author).toBe(
     51           f.owner
     52         );
     53       } finally {
     54         f.close();
     55       }
     56     });
     57   for (const mode of [
     58     'author',
     59     'kind',
     60     'time',
     61     'tags',
     62     'content',
     63     'cached',
     64     'mutate'
     65   ] as const)
     66     it(`rejects hostile ${mode} even with a mutable verified cache flag`, () => {
     67       const f = makeFixture();
     68       try {
     69         const a = approved(f),
     70           copy = disposableApprovedTemplate(a);
     71         if (!copy) throw new Error('missing copy');
     72         const response = f.sign(copy, mode);
     73         expect(verifyEnvelope(JSON.stringify(response)).ok).toBe(
     74           mode !== 'cached'
     75         );
     76         expect(bindApprovedResponse(a, response)).toBeUndefined();
     77         expect(disposableApprovedTemplate(a)).toEqual(f.template);
     78       } finally {
     79         f.close();
     80       }
     81     });
     82   it('retains independent original despite mutable caller snapshots and disposable references', () => {
     83     const f = makeFixture();
     84     try {
     85       const a = approved(f),
     86         copy = disposableApprovedTemplate(a);
     87       if (!copy) throw new Error('missing copy');
     88       copy.tags.push(['changed']);
     89       copy.content = 'changed';
     90       const snapshot = publicRecordSnapshot(f.record, f.owner, f.id);
     91       if (snapshot && snapshot.family !== 'public_draft')
     92         Object.assign(snapshot.capture, { wire: '{}' });
     93       expect(disposableApprovedTemplate(a)).toEqual(f.template);
     94       expect(bindApprovedResponse(a, f.sign(f.template))).toBeDefined();
     95     } finally {
     96       f.close();
     97     }
     98   });
     99   it('requires genuine owner/id scoped record and explicit review', () => {
    100     const f = makeFixture();
    101     try {
    102       expect(
    103         approveCapturedPublicSigning(f.record, f.owner, f.id, undefined)
    104       ).toBeUndefined();
    105       expect(
    106         approveCapturedPublicSigning(
    107           f.record,
    108           f.owner,
    109           crypto.randomUUID(),
    110           'reviewed_captured_operation'
    111         )
    112       ).toBeUndefined();
    113       expect(
    114         approveCapturedPublicSigning(
    115           {} as PublicRecordHandle,
    116           f.owner,
    117           f.id,
    118           'reviewed_captured_operation'
    119         )
    120       ).toBeUndefined();
    121       expect(
    122         disposableApprovedTemplate({} as ApprovedPublicSigning)
    123       ).toBeUndefined();
    124       expect(
    125         bindApprovedResponse({} as ApprovedPublicSigning, f.sign(f.template))
    126       ).toBeUndefined();
    127     } finally {
    128       f.close();
    129     }
    130   });
    131   it('reconstructs own primitive fields without calling getters, toJSON or tag iterators', () => {
    132     const f = makeFixture();
    133     try {
    134       const a = approved(f),
    135         event = f.sign(f.template);
    136       let touched = 0;
    137       Object.defineProperty(event, 'toJSON', {
    138         value: () => {
    139           touched++;
    140           throw new Error('test-only');
    141         }
    142       });
    143       Object.defineProperty(event.tags, Symbol.iterator, {
    144         value: () => {
    145           touched++;
    146           throw new Error('test-only');
    147         }
    148       });
    149       expect(bindApprovedResponse(a, event)).toBeDefined();
    150       Object.defineProperty(event, 'content', {
    151         get: () => {
    152           touched++;
    153           return f.template.content;
    154         }
    155       });
    156       expect(bindApprovedResponse(a, event)).toBeUndefined();
    157       expect(touched).toBe(0);
    158     } finally {
    159       f.close();
    160     }
    161   });
    162   it('rejects draft or already signed records and another owner without creating sign authority', () => {
    163     const f = makeFixture(),
    164       other = makeFixture();
    165     try {
    166       expect(
    167         approveCapturedPublicSigning(
    168           f.record,
    169           other.owner,
    170           f.id,
    171           'reviewed_captured_operation'
    172         )
    173       ).toBeUndefined();
    174       const row = publicRecordSnapshot(f.record, f.owner, f.id);
    175       if (!row || row.family === 'public_draft')
    176         throw new Error('missing operation');
    177       const event = f.sign(f.template);
    178       const signed = decodePublicRecord(
    179         JSON.stringify({
    180           ...row,
    181           artifact: { eventId: event.id, wire: JSON.stringify(event) }
    182         }),
    183         f.owner,
    184         f.id
    185       );
    186       expect(signed.ok).toBe(true);
    187       if (signed.ok)
    188         expect(
    189           approveCapturedPublicSigning(
    190             signed.value,
    191             f.owner,
    192             f.id,
    193             'reviewed_captured_operation'
    194           )
    195         ).toBeUndefined();
    196       const draft = decodePublicRecord(
    197         JSON.stringify({
    198           schema: 1,
    199           family: 'public_draft',
    200           owner: f.owner,
    201           id: f.id,
    202           revision: 0,
    203           savedAtMilliseconds: 0,
    204           form: {
    205             title: '',
    206             description: '',
    207             location: '',
    208             amount: '',
    209             currency: '',
    210             unit: '',
    211             quantity: '',
    212             contactType: '',
    213             contactValue: ''
    214           }
    215         }),
    216         f.owner,
    217         f.id
    218       );
    219       expect(draft.ok).toBe(true);
    220       if (draft.ok)
    221         expect(
    222           approveCapturedPublicSigning(
    223             draft.value,
    224             f.owner,
    225             f.id,
    226             'reviewed_captured_operation'
    227           )
    228         ).toBeUndefined();
    229     } finally {
    230       f.close();
    231       other.close();
    232     }
    233   });
    234   it('bounds huge/sparse/malformed tags and rejects unsupported numbers before encoding', () => {
    235     const f = makeFixture();
    236     try {
    237       const a = approved(f),
    238         event = f.sign(f.template);
    239       for (const value of [
    240         null,
    241         [],
    242         { ...event, tags: Array(1025) },
    243         { ...event, tags: [Array(4097)] },
    244         { ...event, created_at: -0 },
    245         { ...event, kind: NaN },
    246         { ...event, content: 'x'.repeat(131073) },
    247         { ...event, tags: [['x', '\ud800']] }
    248       ])
    249         expect(bindApprovedResponse(a, value)).toBeUndefined();
    250     } finally {
    251       f.close();
    252     }
    253   });
    254 });