inbox-ingress.test.ts (3942B)
1 import { describe, it, expect } from 'vitest'; 2 import { 3 generateSecretKey, 4 getPublicKey, 5 finalizeEvent 6 } from 'applesauce-core/helpers'; 7 import { nip44 } from 'applesauce-core/helpers/encryption'; 8 import { 9 admitInboxEnvelope, 10 inboxEnvelopeSnapshot 11 } from '../../src/lib/persistence/inbox-envelope-repository.ts'; 12 import { 13 inboxIngressSnapshot, 14 type InboxIngress 15 } from '../../src/lib/nostr/inbox-ingress.ts'; 16 function fixture(kind = 1059, target?: string, content?: string) { 17 const key = generateSecretKey(), 18 other = generateSecretKey(); 19 try { 20 const owner = getPublicKey(other), 21 destination = target ?? owner, 22 conversation = nip44.v2.utils.getConversationKey(key, destination); 23 try { 24 const event = finalizeEvent( 25 { 26 kind, 27 created_at: 1700000000, 28 tags: [['p', destination]], 29 content: 30 content ?? 31 nip44.v2.encrypt( 32 'Encrypted structural outer fixture only', 33 conversation 34 ) 35 }, 36 key 37 ); 38 return { owner, event, wire: JSON.stringify(event) }; 39 } finally { 40 conversation.fill(0); 41 } 42 } finally { 43 key.fill(0); 44 other.fill(0); 45 } 46 } 47 describe('bounded untrusted owner-addressed inbox outer', () => { 48 it('admits actual signed1059 as ciphertext only, not a trusted nested message', () => { 49 const f = fixture(), 50 token = admitInboxEnvelope( 51 f.wire, 52 f.owner, 53 'wss://archive.example.org', 54 100 55 ); 56 expect(token).toBeDefined(); 57 const view = inboxEnvelopeSnapshot(token!); 58 expect(view).toMatchObject({ 59 owner: f.owner, 60 id: f.event.id, 61 outer: f.wire, 62 read: null 63 }); 64 expect(view).not.toHaveProperty('body'); 65 expect(view).not.toHaveProperty('sender'); 66 }); 67 it('wrong destination cannot enter this owner namespace', () => { 68 const f = fixture(); 69 expect( 70 admitInboxEnvelope( 71 f.wire, 72 'b'.repeat(64), 73 'wss://archive.example.org', 74 100 75 ) 76 ).toBeUndefined(); 77 }); 78 it('bad signature is rejected before retention', () => { 79 const f = fixture(); 80 expect( 81 admitInboxEnvelope( 82 JSON.stringify({ ...f.event, sig: '0'.repeat(128) }), 83 f.owner, 84 'wss://archive.example.org', 85 100 86 ) 87 ).toBeUndefined(); 88 }); 89 it('non1059 never becomes a private inbox outer', () => { 90 const f = fixture(14); 91 expect( 92 admitInboxEnvelope(f.wire, f.owner, 'wss://archive.example.org', 100) 93 ).toBeUndefined(); 94 }); 95 it('unsupported outer ciphertext version is isolated despite valid outer signature', () => { 96 const f = fixture( 97 1059, 98 undefined, 99 btoa(String.fromCharCode(1) + 'a'.repeat(100)) 100 ); 101 expect( 102 admitInboxEnvelope(f.wire, f.owner, 'wss://archive.example.org', 100) 103 ).toBeUndefined(); 104 }); 105 it('32KiB outer bound is enforced before retained credit', () => { 106 const f = fixture( 107 1059, 108 undefined, 109 btoa(String.fromCharCode(2) + 'a'.repeat(30000)) 110 ); 111 expect( 112 admitInboxEnvelope(f.wire, f.owner, 'wss://archive.example.org', 100) 113 ).toBeUndefined(); 114 }); 115 it('malformed scope and observer metadata do not coerce caller values', () => { 116 const f = fixture(); 117 let calls = 0; 118 const object = { 119 toString() { 120 calls++; 121 return f.wire; 122 } 123 }; 124 expect( 125 admitInboxEnvelope(object, f.owner, 'wss://archive.example.org', 100) 126 ).toBeUndefined(); 127 expect( 128 admitInboxEnvelope(f.wire, f.owner, 'ws://127.0.0.1', 100) 129 ).toBeUndefined(); 130 expect( 131 admitInboxEnvelope(f.wire, f.owner, 'wss://archive.example.org', -1) 132 ).toBeUndefined(); 133 expect(calls).toBe(0); 134 }); 135 it('forged ingress handle cannot acquire a private receive workflow', () => { 136 expect(inboxIngressSnapshot({} as InboxIngress)).toBeUndefined(); 137 }); 138 });