room-admission.ts (5626B)
1 import { 2 getPublicKey, 3 getEventHash, 4 finalizeEvent, 5 type UnsignedEvent 6 } from 'applesauce-core/helpers'; 7 import { nip44 } from 'applesauce-core/helpers/encryption'; 8 import { makeFixture as makeRecoveryFixture } from './resume-preparation.ts'; 9 import { 10 createPrivateSession, 11 closePrivateSession 12 } from '../../../src/lib/runtime/private-session.ts'; 13 import { 14 admitInboxEnvelope, 15 retainInboxEnvelope 16 } from '../../../src/lib/persistence/inbox-envelope-repository.ts'; 17 import { 18 captureReceivedUnwrap, 19 unwrapReceivedEnvelope, 20 stopReceivedUnwrap, 21 type ReceivedUnwrap, 22 type ReceivedNestedEnvelope 23 } from '../../../src/lib/nostr/unwrap-admission.ts'; 24 import { 25 admitReceivedConversation, 26 conversationSnapshot, 27 type AdmittedConversation 28 } from '../../../src/lib/messaging/admit-conversation.ts'; 29 type Scenario = 30 | 'inbound' 31 | 'self_archive' 32 | 'inbound_extensions' 33 | 'self_archive_extensions' 34 | 'duplicate_recipient' 35 | 'duplicate_subject' 36 | 'duplicate_parent' 37 | 'expiration' 38 | 'file' 39 | 'third_party' 40 | 'hostile_text' 41 | 'missing_parent'; 42 export async function makeFixture(scenario: Scenario = 'inbound') { 43 const f = await makeRecoveryFixture(), 44 privateSession = await createPrivateSession( 45 f.identity, 46 'reviewed_private_session' 47 ); 48 if (!privateSession) throw Error('missing genuine private session'); 49 const ownerSecret = new Uint8Array(32).fill(83), 50 peerSecret = new Uint8Array(32).fill(84), 51 outerSecret = new Uint8Array(32).fill(85), 52 other = getPublicKey(outerSecret); 53 const archive = 54 scenario === 'self_archive' || scenario === 'self_archive_extensions', 55 extensions = 56 scenario === 'inbound_extensions' || 57 scenario === 'self_archive_extensions'; 58 const senderSecret = archive ? ownerSecret : peerSecret, 59 sender = getPublicKey(senderSecret), 60 recipient = archive ? f.peer : scenario === 'third_party' ? other : f.owner; 61 const tags: string[][] = [['p', recipient]]; 62 if (scenario === 'duplicate_recipient') tags.push(['p', recipient]); 63 if (scenario === 'duplicate_subject') 64 tags.push(['subject', 'A'], ['subject', 'B']); 65 if (scenario === 'duplicate_parent') 66 tags.push(['e', 'a'.repeat(64)], ['e', 'b'.repeat(64)]); 67 if (scenario === 'expiration') tags.push(['expiration', '1700000001']); 68 if (scenario === 'file') tags.push(['file', 'https://untrusted.example.org']); 69 if (scenario === 'hostile_text') tags.push(['subject', other]); 70 if (scenario === 'missing_parent') 71 tags.push(['e', 'a'.repeat(64), 'wss://untrusted.example.org']); 72 if (extensions) 73 tags.splice( 74 0, 75 tags.length, 76 ['subject', 'Original subject'], 77 ['q', 'b'.repeat(64), 'wss://untrusted.example.org', other], 78 ['vendor', 'untrusted inert metadata'], 79 ['e', 'a'.repeat(64), 'wss://untrusted.example.org'], 80 ['p', recipient, 'wss://untrusted.example.org'] 81 ); 82 const template: UnsignedEvent = { 83 pubkey: sender, 84 kind: 14, 85 created_at: 1700000000, 86 tags, 87 content: 88 scenario === 'hostile_text' 89 ? `Contact ${other} https://untrusted.example.org` 90 : 'Actual encrypted room fixture' 91 }, 92 rumor = { id: getEventHash(template), ...template }; 93 function encrypt(key: Uint8Array, target: string, plain: string) { 94 const conversation = nip44.v2.utils.getConversationKey(key, target); 95 try { 96 return nip44.v2.encrypt(plain, conversation); 97 } finally { 98 conversation.fill(0); 99 } 100 } 101 const seal = finalizeEvent( 102 { 103 kind: 13, 104 created_at: 1699999900, 105 tags: [], 106 content: encrypt(senderSecret, f.owner, JSON.stringify(rumor)) 107 }, 108 senderSecret 109 ); 110 const outer = finalizeEvent( 111 { 112 kind: 1059, 113 created_at: 1699999800, 114 tags: extensions 115 ? [ 116 ['nonce', '3', '8'], 117 ['p', f.owner, 'wss://untrusted.example.org'] 118 ] 119 : [['p', f.owner]], 120 content: encrypt(outerSecret, f.owner, JSON.stringify(seal)) 121 }, 122 outerSecret 123 ); 124 ownerSecret.fill(0); 125 peerSecret.fill(0); 126 outerSecret.fill(0); 127 const admitted = admitInboxEnvelope( 128 JSON.stringify(outer), 129 f.owner, 130 'wss://archive.example.org', 131 100 132 ); 133 if (!admitted) throw Error('missing signed addressed outer'); 134 const saved = await retainInboxEnvelope( 135 f.repository, 136 privateSession, 137 admitted 138 ); 139 if (saved.status !== 'retained') throw Error('no actual retained cipher'); 140 let reader: ReceivedUnwrap | undefined, 141 nested: ReceivedNestedEnvelope | undefined, 142 room: AdmittedConversation | undefined; 143 return { 144 ...f, 145 other, 146 rumorId: rumor.id, 147 async unlock() { 148 reader = captureReceivedUnwrap( 149 f.repository, 150 f.identity, 151 outer.id, 152 'reviewed_inbox_unlock' 153 ); 154 if (!reader) return 'invalid'; 155 const result = await unwrapReceivedEnvelope(reader); 156 if (result.status === 'authenticated') nested = result.envelope; 157 return result.status; 158 }, 159 admit(role: unknown) { 160 room = nested && admitReceivedConversation(nested, role); 161 return room && conversationSnapshot(room); 162 }, 163 snapshot() { 164 return room && conversationSnapshot(room); 165 }, 166 forged() { 167 return admitReceivedConversation({} as ReceivedNestedEnvelope, 'inbound'); 168 }, 169 copied() { 170 return room && conversationSnapshot({ ...room }); 171 }, 172 stop() { 173 if (reader) stopReceivedUnwrap(reader); 174 }, 175 close() { 176 if (reader) stopReceivedUnwrap(reader); 177 closePrivateSession(privateSession); 178 f.close(); 179 } 180 }; 181 }