app

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

private-cache-scope.test.ts (3404B)


      1 import { expect, it } from 'vitest';
      2 import {
      3   getPrivateCacheScope,
      4   retainPrivateCacheWire,
      5   privateCacheWire,
      6   privateCacheSnapshot,
      7   clearPrivateCacheScope,
      8   closePrivateCacheScope,
      9   type PrivateCacheScope
     10 } from '../../src/lib/nostr/private-cache-scope.ts';
     11 import {
     12   acceptUnlockedConversation,
     13   readUnlockedMessages,
     14   unlockedSessionSnapshot,
     15   unlockedSessionOwnership,
     16   unlockedConversationCurrent,
     17   type UnlockedSession
     18 } from '../../src/lib/messaging/unlocked-session.ts';
     19 import {
     20   receivedNestedEnvelopeSnapshot,
     21   receivedNestedEnvelopeOwnership,
     22   type ReceivedNestedEnvelope
     23 } from '../../src/lib/nostr/unwrap-admission.ts';
     24 import {
     25   conversationSnapshot,
     26   conversationOwnership,
     27   type AdmittedConversation
     28 } from '../../src/lib/messaging/admit-conversation.ts';
     29 import type { PrivateSession } from '../../src/lib/runtime/private-session.ts';
     30 it('detached data, imported verification marks, getters and proxy copies cannot mint private cache or nested room custody', () => {
     31   let inspected = 0;
     32   const getter = Object.defineProperty({}, 'owner', {
     33     get() {
     34       inspected++;
     35       return '0'.repeat(64);
     36     }
     37   });
     38   const proxy = new Proxy(
     39     {},
     40     {
     41       get() {
     42         inspected++;
     43         throw new Error('untrusted getter');
     44       }
     45     }
     46   );
     47   const revoked = Proxy.revocable({}, {});
     48   revoked.revoke();
     49   const data = {
     50     owner: '0'.repeat(64),
     51     peer: '1'.repeat(64),
     52     rumorId: '2'.repeat(64),
     53     content: 'Detached private-data canary',
     54     verified: true
     55   };
     56   const candidates = [
     57     {},
     58     Object.freeze({}),
     59     { ...data },
     60     Object.freeze({ ...data }),
     61     JSON.parse(JSON.stringify(data)) as object,
     62     Object.create(data) as object,
     63     { [Symbol.for('verified')]: true },
     64     getter,
     65     proxy,
     66     revoked.proxy
     67   ];
     68   expect(candidates).toHaveLength(10);
     69   expect(typeof window).toBe('undefined');
     70   for (const candidate of candidates) {
     71     const cache = candidate as PrivateCacheScope,
     72       unlocked = candidate as UnlockedSession;
     73     const nested = candidate as ReceivedNestedEnvelope,
     74       room = candidate as AdmittedConversation;
     75     expect(
     76       getPrivateCacheScope(candidate as PrivateSession, 'projection_ownership')
     77     ).toBeUndefined();
     78     expect(
     79       getPrivateCacheScope(candidate as PrivateSession, 'raw_ciphertext')
     80     ).toBeUndefined();
     81     for (const action of [
     82       () => retainPrivateCacheWire(cache, data.rumorId, JSON.stringify(data)),
     83       () => privateCacheWire(cache, data.rumorId),
     84       () => privateCacheSnapshot(cache),
     85       () => clearPrivateCacheScope(cache),
     86       () => closePrivateCacheScope(cache)
     87     ])
     88       expect(action).toThrow('private_cache_invalid');
     89     expect(acceptUnlockedConversation(unlocked, room)).toBe('rejected');
     90     expect(readUnlockedMessages(unlocked)).toEqual({
     91       status: 'rejected',
     92       messages: []
     93     });
     94     expect(unlockedSessionSnapshot(unlocked)).toEqual({
     95       closed: true,
     96       count: 0,
     97       bytes: 0
     98     });
     99     expect(unlockedSessionOwnership(unlocked)).toBeUndefined();
    100     expect(unlockedConversationCurrent(unlocked, room)).toBe(false);
    101     expect(receivedNestedEnvelopeSnapshot(nested)).toBeUndefined();
    102     expect(receivedNestedEnvelopeOwnership(nested)).toBeUndefined();
    103     expect(conversationSnapshot(room)).toBeUndefined();
    104     expect(conversationOwnership(room)).toBeUndefined();
    105   }
    106   expect(inspected).toBe(0);
    107 });