room-admission.test.ts (4939B)
1 import { describe, it, expect } from 'vitest'; 2 import { 3 getPublicKey, 4 getEventHash, 5 type UnsignedEvent 6 } from 'applesauce-core/helpers'; 7 import { 8 inspectConversationData, 9 admitReceivedConversation, 10 conversationSnapshot 11 } from '../../src/lib/messaging/admit-conversation.ts'; 12 import type { ReceivedNestedEnvelope } from '../../src/lib/nostr/unwrap-admission.ts'; 13 const owner = getPublicKey(new Uint8Array(32).fill(83)), 14 peer = getPublicKey(new Uint8Array(32).fill(84)), 15 other = getPublicKey(new Uint8Array(32).fill(85)); 16 function rumor( 17 sender = peer, 18 recipient = owner, 19 content = 'Untrusted plain text', 20 extra: string[][] = [] 21 ) { 22 const template: UnsignedEvent = { 23 pubkey: sender, 24 kind: 14, 25 created_at: 1700000000, 26 tags: [['p', recipient], ...extra], 27 content 28 }; 29 return JSON.stringify({ id: getEventHash(template), ...template }); 30 } 31 describe('detached exact room inspection grants no authenticated custody', () => { 32 it('inbound owner membership selects authenticated sender as peer', () => 33 expect(inspectConversationData(rumor(), owner, 'inbound')).toMatchObject({ 34 owner, 35 peer, 36 sender: peer, 37 recipient: owner, 38 role: 'inbound' 39 })); 40 it('self archive retains original recipient instead of changing rumor room', () => 41 expect( 42 inspectConversationData(rumor(owner, peer), owner, 'self_archive') 43 ).toMatchObject({ 44 owner, 45 peer, 46 sender: owner, 47 recipient: peer, 48 role: 'self_archive' 49 })); 50 it('third party rumor excludes connected account', () => 51 expect( 52 inspectConversationData(rumor(peer, other), owner, 'inbound') 53 ).toBeUndefined()); 54 it('group and duplicate-recipient fanout are rejected', () => { 55 expect( 56 inspectConversationData( 57 rumor(peer, owner, 'text', [['p', other]]), 58 owner, 59 'inbound' 60 ) 61 ).toBeUndefined(); 62 expect( 63 inspectConversationData( 64 rumor(peer, owner, 'text', [['p', owner]]), 65 owner, 66 'inbound' 67 ) 68 ).toBeUndefined(); 69 }); 70 it('self request has no other participant', () => 71 expect( 72 inspectConversationData(rumor(owner, owner), owner, 'self_archive') 73 ).toBeUndefined()); 74 it('expected inbound/archive role must match actual authenticated sender', () => { 75 expect( 76 inspectConversationData(rumor(), owner, 'self_archive') 77 ).toBeUndefined(); 78 expect( 79 inspectConversationData(rumor(owner, peer), owner, 'inbound') 80 ).toBeUndefined(); 81 expect( 82 inspectConversationData(rumor(), owner, 'automatic') 83 ).toBeUndefined(); 84 }); 85 it('subject URLs and body contact strings cannot select another peer', () => 86 expect( 87 inspectConversationData( 88 rumor(peer, owner, `Contact ${other} https://untrusted.example.org`, [ 89 ['subject', other] 90 ]), 91 owner, 92 'inbound' 93 ) 94 ).toMatchObject({ peer, sender: peer })); 95 it('unknown parent remains a detached private hash without public lookup', () => 96 expect( 97 inspectConversationData( 98 rumor(peer, owner, 'text', [ 99 ['e', 'a'.repeat(64), 'wss://untrusted.example.org'] 100 ]), 101 owner, 102 'inbound' 103 ) 104 ).toMatchObject({ peer, replyTo: 'a'.repeat(64) })); 105 it('fresh rumor hash is required before room inspection', () => { 106 const row = JSON.parse(rumor()) as Record<string, unknown>; 107 expect( 108 inspectConversationData( 109 JSON.stringify({ ...row, id: '0'.repeat(64) }), 110 owner, 111 'inbound' 112 ) 113 ).toBeUndefined(); 114 }); 115 it('malformed and caller object values are isolated', () => { 116 expect(inspectConversationData('{}', owner, 'inbound')).toBeUndefined(); 117 expect( 118 inspectConversationData(JSON.parse(rumor()) as unknown, owner, 'inbound') 119 ).toBeUndefined(); 120 expect(inspectConversationData(rumor(), other, 'inbound')).toBeUndefined(); 121 expect(inspectConversationData(rumor(), owner, 'inbound')).toBeDefined(); 122 }); 123 it('unsupported tags and signed rumor cannot become room data', () => { 124 expect( 125 inspectConversationData( 126 rumor(peer, owner, 'text', [['file', 'https://untrusted.example.org']]), 127 owner, 128 'inbound' 129 ) 130 ).toBeUndefined(); 131 const row = JSON.parse(rumor()) as Record<string, unknown>; 132 expect( 133 inspectConversationData( 134 JSON.stringify({ ...row, sig: '0'.repeat(128) }), 135 owner, 136 'inbound' 137 ) 138 ).toBeUndefined(); 139 }); 140 it('structural nested objects and copied detached rooms grant no proof', () => { 141 const fake = {} as ReceivedNestedEnvelope; 142 expect(admitReceivedConversation(fake, 'inbound')).toBeUndefined(); 143 expect( 144 conversationSnapshot( 145 inspectConversationData( 146 rumor(), 147 owner, 148 'inbound' 149 ) as unknown as Parameters<typeof conversationSnapshot>[0] 150 ) 151 ).toBeUndefined(); 152 }); 153 });