incoming-wire.test.ts (7398B)
1 import { expect, it } from 'vitest'; 2 import { getEventHash, type UnsignedEvent } from 'applesauce-core/helpers'; 3 import { 4 incomingGiftWrapTags, 5 parseIncomingLayer, 6 readIncomingPairRumor 7 } from '../../src/lib/nostr/incoming-wire.ts'; 8 import { inspectConversationData } from '../../src/lib/messaging/admit-conversation.ts'; 9 import { messageToWireParts } from '../../src/lib/contracts/message-v1/index.ts'; 10 const sender = 11 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 12 owner = 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5'; 13 function rumor(tags: string[][], content = ' Original readable question\n') { 14 const template: UnsignedEvent = { 15 kind: 14, 16 pubkey: sender, 17 created_at: 1700000000, 18 tags, 19 content 20 }; 21 return { id: getEventHash(template), ...template }; 22 } 23 it('accepts bounded intended-recipient hints and inert outer metadata without routing authority', () => { 24 for (const tags of [ 25 [['p', owner]], 26 [['p', owner, 'wss://untrusted.example.org']], 27 [ 28 ['nonce', '7', '8'], 29 ['p', owner, ''], 30 ['vendor', 'untrusted inert text'] 31 ] 32 ]) 33 expect(incomingGiftWrapTags(tags, owner)).toBe(true); 34 for (const tags of [ 35 [], 36 [['p', sender]], 37 [ 38 ['p', owner], 39 ['p', owner] 40 ], 41 [ 42 ['p', owner], 43 ['p', sender] 44 ], 45 [['p', owner, 'hint', 'extra']], 46 [ 47 ['p', owner], 48 ['expiration', '1900000000'] 49 ], 50 [['p', owner], []], 51 [ 52 ['p', owner], 53 ['vendor', 'x'.repeat(4097)] 54 ], 55 [['p', owner], ...Array.from({ length: 1024 }, () => ['vendor'])], 56 [ 57 ['p', owner], 58 ['vendor', '\ud800'] 59 ] 60 ]) 61 expect(incomingGiftWrapTags(tags, owner)).toBe(false); 62 }); 63 it('hashes original tag order and preserves bounded q/unknown metadata while projecting semantics', () => { 64 const tags = [ 65 ['subject', 'Question'], 66 ['q', 'b'.repeat(64), 'wss://untrusted.example.org', sender], 67 ['vendor', 'untrusted inert text'], 68 ['e', 'a'.repeat(64), 'wss://parent.example.org'], 69 ['p', owner, 'wss://recipient.example.org'] 70 ]; 71 const value = rumor(tags), 72 raw = '\n' + JSON.stringify(value, null, 2) + '\n'; 73 const incoming = readIncomingPairRumor(raw, sender); 74 expect(incoming?.original).toEqual(value); 75 expect(incoming?.message).toEqual({ 76 recipients: [ 77 { public_key: owner, relay_url: 'wss://recipient.example.org' } 78 ], 79 content: value.content, 80 reply_to: { id: 'a'.repeat(64), relays: 'wss://parent.example.org' }, 81 subject: 'Question' 82 }); 83 expect(inspectConversationData(raw, owner, 'inbound')).toMatchObject({ 84 owner, 85 peer: sender, 86 sender, 87 recipient: owner, 88 rumorId: value.id, 89 content: value.content, 90 subject: 'Question', 91 replyTo: 'a'.repeat(64) 92 }); 93 expect(inspectConversationData(raw, sender, 'self_archive')).toMatchObject({ 94 peer: owner, 95 rumorId: value.id, 96 content: value.content 97 }); 98 expect(inspectConversationData(raw, owner, 'self_archive')).toBeUndefined(); 99 expect( 100 inspectConversationData(raw, '0'.repeat(64), 'inbound') 101 ).toBeUndefined(); 102 // The receive profile does not rewrite the strict Lib189 outgoing writer. 103 expect(messageToWireParts(JSON.stringify(incoming?.message))?.tags).toEqual([ 104 ['p', owner, 'wss://recipient.example.org'], 105 ['e', 'a'.repeat(64), 'wss://parent.example.org'], 106 ['subject', 'Question'] 107 ]); 108 }); 109 it('rejects ambiguous recipients/parents/subjects and unsupported retention/file semantics', () => { 110 const badTags = [ 111 [], 112 [['p', sender]], 113 [ 114 ['p', owner], 115 ['p', owner] 116 ], 117 [ 118 ['p', owner], 119 ['p', sender] 120 ], 121 [['p', owner, 'hint', 'extra']], 122 [ 123 ['p', owner], 124 ['subject', 'A'], 125 ['subject', 'B'] 126 ], 127 [ 128 ['p', owner], 129 ['subject', 'A', 'extra'] 130 ], 131 [ 132 ['p', owner], 133 ['e', 'a'.repeat(64)], 134 ['e', 'b'.repeat(64)] 135 ], 136 [ 137 ['p', owner], 138 ['e', 'a'.repeat(64), 'hint', 'marker'] 139 ], 140 [ 141 ['p', owner], 142 ['e', 'malformed'] 143 ], 144 [ 145 ['p', owner], 146 ['expiration', '1900000000'] 147 ], 148 [ 149 ['p', owner], 150 ['file', 'untrusted'] 151 ], 152 [ 153 ['p', owner], 154 ['imeta', 'untrusted'] 155 ], 156 [ 157 ['p', owner], 158 ['vendor', '\ud800'] 159 ] 160 ]; 161 for (const tags of badTags) 162 expect(readIncomingPairRumor(JSON.stringify(rumor(tags)))).toBeUndefined(); 163 }); 164 it('altered original tags/hash/author/kind and noncanonical numeric spellings never grant admission', () => { 165 const value = rumor([ 166 ['subject', 'Question'], 167 ['p', owner] 168 ]); 169 const raw = JSON.stringify(value); 170 for (const modified of [ 171 { ...value, id: '0'.repeat(64) }, 172 { ...value, tags: [...value.tags].reverse() }, 173 { ...value, pubkey: owner }, 174 { ...value, kind: 15 }, 175 { ...value, sig: '0'.repeat(128) }, 176 { ...value, verified: true }, 177 { ...value, content: 'x'.repeat(4097) } 178 ]) 179 expect(readIncomingPairRumor(JSON.stringify(modified))).toBeUndefined(); 180 expect(readIncomingPairRumor(raw, owner)).toBeUndefined(); 181 for (const modified of [ 182 raw.replace('"kind":14', '"kind":14.0'), 183 raw.replace('1700000000', '1700000000.0'), 184 raw.replace('1700000000', '1.7e9'), 185 '{"kind":14,' + raw.slice(1), 186 '{"\\u006bkind":14,' + raw.slice(1), 187 '{"\\u006b\\u0069\\u006e\\u0064":14,' + raw.slice(1) 188 ]) { 189 expect(readIncomingPairRumor(modified)).toBeUndefined(); 190 expect(inspectConversationData(modified, owner, 'inbound')).toBeUndefined(); 191 } 192 }); 193 it('accepts exact body/tag/element limits and rejects their immediate excess', () => { 194 expect( 195 readIncomingPairRumor( 196 JSON.stringify(rumor([['p', owner]], 'x'.repeat(4096))) 197 ) 198 ).toBeDefined(); 199 expect( 200 readIncomingPairRumor( 201 JSON.stringify(rumor([['p', owner]], 'x'.repeat(4097))) 202 ) 203 ).toBeUndefined(); 204 const count = [['p', owner], ...Array.from({ length: 1023 }, () => ['x'])]; 205 expect(readIncomingPairRumor(JSON.stringify(rumor(count)))).toBeDefined(); 206 expect(incomingGiftWrapTags(count, owner)).toBe(true); 207 expect(incomingGiftWrapTags([...count, ['x']], owner)).toBe(false); 208 const elements = [ 209 ['p', owner, ''], 210 ['vendor', '', '', '', ''], 211 ...Array.from({ length: 1022 }, () => ['vendor', '', '', '']) 212 ]; 213 expect(incomingGiftWrapTags(elements, owner)).toBe(true); 214 expect( 215 incomingGiftWrapTags( 216 elements.map((row, i) => (i === 1 ? [...row, ''] : row)), 217 owner 218 ) 219 ).toBe(false); 220 expect( 221 incomingGiftWrapTags( 222 [ 223 ['p', owner], 224 ['vendor', 'x'.repeat(4096)] 225 ], 226 owner 227 ) 228 ).toBe(true); 229 expect( 230 incomingGiftWrapTags( 231 [ 232 ['p', owner], 233 ['vendor', 'x'.repeat(4097)] 234 ], 235 owner 236 ) 237 ).toBe(false); 238 }); 239 it('raw objects cannot invoke conversion and layer/key/UTF8 limits remain defensive', () => { 240 let calls = 0; 241 const input = { 242 toJSON() { 243 calls++; 244 return rumor([['p', owner]]); 245 } 246 }; 247 expect(readIncomingPairRumor(input)).toBeUndefined(); 248 expect(parseIncomingLayer(input, 8192)).toBeUndefined(); 249 expect(calls).toBe(0); 250 expect(parseIncomingLayer('x'.repeat(8193), 8192)).toBeUndefined(); 251 expect(parseIncomingLayer('{"a":1,"a":2}', 8192)).toBeUndefined(); 252 expect(parseIncomingLayer('{"a":"\\ud800"}', 8192)).toBeUndefined(); 253 });