private-records.test.ts (11538B)
1 import { describe, expect, it } 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 decodePrivateRecord, 10 privateReceivedOuterMetadata, 11 type PrivateRecordHandle, 12 privateRecordSnapshot, 13 privateRecordWire, 14 inspectPrivateSendReservation 15 } from '../../src/lib/persistence/private-records.ts'; 16 const command = '12345678-1234-4234-8234-123456789abc'; 17 function fixture(extended = false) { 18 const key = generateSecretKey(), 19 peerKey = generateSecretKey(); 20 let conversation: Uint8Array | undefined, 21 peerConversation: Uint8Array | undefined; 22 try { 23 const owner = getPublicKey(key), 24 peer = getPublicKey(peerKey); 25 conversation = nip44.v2.utils.getConversationKey(key, owner); 26 const outer = finalizeEvent( 27 { 28 kind: 1059, 29 created_at: 1700000000, 30 tags: extended 31 ? [ 32 ['nonce', '3', '8'], 33 ['p', owner, 'wss://untrusted.example.org'] 34 ] 35 : [['p', owner]], 36 content: nip44.v2.encrypt( 37 'Ciphertext structure fixture only', 38 conversation 39 ) 40 }, 41 key 42 ); 43 peerConversation = nip44.v2.utils.getConversationKey(key, peer); 44 const peerOuter = finalizeEvent( 45 { 46 kind: 1059, 47 created_at: 1700000001, 48 tags: [['p', peer]], 49 content: nip44.v2.encrypt( 50 'Ciphertext structure fixture only', 51 peerConversation 52 ) 53 }, 54 key 55 ); 56 const peerArtifact = { 57 eventId: peerOuter.id, 58 wire: JSON.stringify(peerOuter) 59 }; 60 const reservation = { 61 schema: 1, 62 family: 'private_send_reservation', 63 owner, 64 id: command, 65 revision: 0, 66 peer, 67 rumorHash: 'a'.repeat(64), 68 createdAt: 1700000000 69 }; 70 const operation = { 71 ...reservation, 72 family: 'private_send_operation', 73 revision: 1, 74 self: { eventId: outer.id, wire: JSON.stringify(outer) }, 75 peerArtifact: null 76 }; 77 const received = { 78 schema: 1, 79 family: 'received_envelope', 80 owner, 81 id: outer.id, 82 revision: 0, 83 outer: JSON.stringify(outer), 84 observedAtMilliseconds: 1700000000000, 85 sources: ['wss://inbox.example.org'], 86 read: null 87 }; 88 return { owner, peer, reservation, operation, received, peerArtifact }; 89 } finally { 90 conversation?.fill(0); 91 peerConversation?.fill(0); 92 key.fill(0); 93 peerKey.fill(0); 94 } 95 } 96 describe('ciphertext-only private local codecs, not nested authenticity or effect authority', () => { 97 it('CR005 transport metadata belongs only to the genuine received handle and exact owner/id', () => { 98 const f = fixture(true), 99 decoded = decodePrivateRecord( 100 JSON.stringify(f.received), 101 f.owner, 102 f.received.id 103 ); 104 expect(decoded.ok).toBe(true); 105 if (!decoded.ok) throw Error('strict fixture rejected'); 106 expect( 107 privateReceivedOuterMetadata(decoded.value, f.owner, f.received.id) 108 ).toEqual({ id: f.received.id, createdAt: 1700000000 }); 109 expect( 110 privateReceivedOuterMetadata(decoded.value, f.peer, f.received.id) 111 ).toBeUndefined(); 112 expect( 113 privateReceivedOuterMetadata(decoded.value, f.owner, command) 114 ).toBeUndefined(); 115 expect( 116 privateReceivedOuterMetadata( 117 Object.freeze({ ...decoded.value }) as PrivateRecordHandle, 118 f.owner, 119 f.received.id 120 ) 121 ).toBeUndefined(); 122 expect( 123 privateReceivedOuterMetadata( 124 Object.freeze({}) as PrivateRecordHandle, 125 undefined, 126 undefined 127 ) 128 ).toBeUndefined(); 129 const copy = privateReceivedOuterMetadata( 130 decoded.value, 131 f.owner, 132 f.received.id 133 )!; 134 Object.assign(copy, { createdAt: 0 }); 135 const detached = privateRecordSnapshot( 136 decoded.value, 137 f.owner, 138 f.received.id 139 )!; 140 Object.assign(detached, { outer: 'untrusted changed snapshot' }); 141 expect( 142 privateReceivedOuterMetadata(decoded.value, f.owner, f.received.id) 143 ).toEqual({ id: f.received.id, createdAt: 1700000000 }); 144 const sender = fixture(); 145 for (const record of [sender.operation, sender.reservation]) { 146 const other = decodePrivateRecord( 147 JSON.stringify(record), 148 sender.owner, 149 record.id 150 ); 151 expect(other.ok).toBe(true); 152 if (!other.ok) throw Error('strict sender fixture rejected'); 153 expect( 154 privateReceivedOuterMetadata(other.value, sender.owner, record.id) 155 ).toBeUndefined(); 156 } 157 }); 158 159 it('retains original receive hints/inert tags but keeps stored outgoing artifacts strict', () => { 160 const f = fixture(true), 161 decoded = decodePrivateRecord( 162 JSON.stringify(f.received), 163 f.owner, 164 f.received.id 165 ); 166 expect(decoded.ok).toBe(true); 167 if (!decoded.ok) throw Error('bounded extended received wire rejected'); 168 expect( 169 privateRecordSnapshot(decoded.value, f.owner, f.received.id) 170 ).toEqual(f.received); 171 expect( 172 decodePrivateRecord(JSON.stringify(f.operation), f.owner, f.operation.id) 173 .ok 174 ).toBe(false); 175 }); 176 it('rejects duplicate or escaped-alias outer content fields instead of persisting hidden plaintext', () => { 177 const f = fixture(); 178 for (const name of ['"content"', '"\\u0063ontent"']) { 179 const poison = (raw: string) => 180 '{' + name + ':"PRIVATE_DUPLICATE_SENTINEL",' + raw.slice(1); 181 const cases = [ 182 { ...f.received, outer: poison(f.received.outer) }, 183 { 184 ...f.operation, 185 self: { ...f.operation.self, wire: poison(f.operation.self.wire) } 186 }, 187 { 188 ...f.operation, 189 peerArtifact: { ...f.peerArtifact, wire: poison(f.peerArtifact.wire) } 190 } 191 ]; 192 for (const record of cases) 193 expect( 194 decodePrivateRecord(JSON.stringify(record), f.owner, record.id).ok 195 ).toBe(false); 196 } 197 }); 198 it('rejects duplicate local/nested metadata while retaining valid reordered/whitespace ciphertext bytes exactly', () => { 199 const f = fixture(); 200 const duplicateRoot = 201 '{"owner":"PRIVATE_DUPLICATE_SENTINEL",' + 202 JSON.stringify(f.operation).slice(1); 203 const duplicateArtifact = JSON.stringify(f.operation).replace( 204 '"self":{', 205 '"self":{"wire":"PRIVATE_DUPLICATE_SENTINEL",' 206 ); 207 const read = { 208 ...f.received, 209 read: { rumorHash: 'b'.repeat(64), atMilliseconds: 1 } 210 }; 211 const duplicateRead = JSON.stringify(read).replace( 212 '"read":{', 213 '"read":{"rumorHash":"PRIVATE_DUPLICATE_SENTINEL",' 214 ); 215 for (const [raw, id] of [ 216 [duplicateRoot, f.operation.id], 217 [duplicateArtifact, f.operation.id], 218 [duplicateRead, f.received.id] 219 ]) 220 expect(decodePrivateRecord(raw, f.owner, id).ok).toBe(false); 221 const parsed = JSON.parse(f.received.outer) as Record<string, unknown>; 222 const reordered = 223 '\n' + 224 JSON.stringify( 225 { 226 sig: parsed.sig, 227 content: parsed.content, 228 tags: parsed.tags, 229 kind: parsed.kind, 230 created_at: parsed.created_at, 231 pubkey: parsed.pubkey, 232 id: parsed.id 233 }, 234 null, 235 2 236 ) + 237 '\t'; 238 const record = { ...f.received, outer: reordered }, 239 decoded = decodePrivateRecord(JSON.stringify(record), f.owner, record.id); 240 expect(decoded.ok).toBe(true); 241 if (!decoded.ok) throw Error('valid whitespace/reordering rejected'); 242 expect(privateRecordSnapshot(decoded.value, f.owner, record.id)).toEqual( 243 record 244 ); 245 }); 246 it('admits strict metadata reservations, self recovery and received ciphertext separately from public records', () => { 247 const f = fixture(); 248 for (const record of [f.reservation, f.operation, f.received]) { 249 const result = decodePrivateRecord( 250 JSON.stringify(record), 251 f.owner, 252 record.id 253 ); 254 expect(result.ok).toBe(true); 255 if (!result.ok) throw Error('expected bounded record'); 256 expect(privateRecordSnapshot(result.value, f.owner, record.id)).toEqual( 257 record 258 ); 259 expect( 260 privateRecordSnapshot(result.value, f.peer, record.id) 261 ).toBeUndefined(); 262 expect(privateRecordWire(result.value, f.owner, record.id)).toBe( 263 JSON.stringify(record) 264 ); 265 expect( 266 privateRecordSnapshot({} as typeof result.value, f.owner, record.id) 267 ).toBeUndefined(); 268 } 269 expect( 270 inspectPrivateSendReservation( 271 JSON.stringify(f.reservation), 272 f.owner, 273 command 274 ) 275 ).toEqual(f.reservation); 276 }); 277 it('rejects plaintext fields, unknown schema/families, bad scope and unsafe metadata without coercion', () => { 278 const f = fixture(); 279 for (const record of [f.reservation, f.operation, f.received]) { 280 for (const key of ['body', 'subject', 'contact', 'product', 'content']) { 281 expect( 282 decodePrivateRecord( 283 JSON.stringify({ ...record, [key]: 'private sentinel' }), 284 f.owner, 285 record.id 286 ).ok 287 ).toBe(false); 288 } 289 for (const patch of [ 290 { schema: 2 }, 291 { family: 'public_draft' }, 292 { revision: -1 }, 293 { revision: 1.5 }, 294 { owner: f.peer }, 295 { id: 'bad' } 296 ]) { 297 expect( 298 decodePrivateRecord( 299 JSON.stringify({ ...record, ...patch }), 300 f.owner, 301 record.id 302 ).ok 303 ).toBe(false); 304 } 305 expect( 306 decodePrivateRecord(JSON.stringify(record), f.peer, record.id).ok 307 ).toBe(false); 308 } 309 expect( 310 decodePrivateRecord( 311 { toString: () => JSON.stringify(f.operation) }, 312 f.owner, 313 command 314 ).ok 315 ).toBe(false); 316 expect(decodePrivateRecord('{bad', f.owner, command).ok).toBe(false); 317 }); 318 it('requires exact signed bounded1059 ciphertext and role destination, never rumor or seal plaintext', () => { 319 const f = fixture(), 320 outer = JSON.parse(f.operation.self.wire) as Record<string, unknown>; 321 for (const patch of [ 322 { kind: 14 }, 323 { kind: 13 }, 324 { content: 'cleartext' }, 325 { tags: [['p', f.peer]] }, 326 { sig: '0'.repeat(128) }, 327 { privateBody: 'sentinel' } 328 ]) { 329 const next = { 330 ...f.operation, 331 self: { 332 eventId: f.operation.self.eventId, 333 wire: JSON.stringify({ ...outer, ...patch }) 334 } 335 }; 336 expect( 337 decodePrivateRecord(JSON.stringify(next), f.owner, command).ok 338 ).toBe(false); 339 } 340 const oversized = { 341 ...f.operation, 342 self: { 343 ...f.operation.self, 344 wire: f.operation.self.wire + ' '.repeat(32768) 345 } 346 }; 347 expect( 348 decodePrivateRecord(JSON.stringify(oversized), f.owner, command).ok 349 ).toBe(false); 350 expect( 351 decodePrivateRecord( 352 JSON.stringify({ ...f.operation, self: null }), 353 f.owner, 354 command 355 ).ok 356 ).toBe(false); 357 expect( 358 decodePrivateRecord( 359 JSON.stringify({ ...f.received, sources: ['https://inbox.example'] }), 360 f.owner, 361 f.received.id 362 ).ok 363 ).toBe(false); 364 expect( 365 decodePrivateRecord( 366 JSON.stringify({ 367 ...f.received, 368 read: { rumorHash: 'bad', atMilliseconds: 1 } 369 }), 370 f.owner, 371 f.received.id 372 ).ok 373 ).toBe(false); 374 }); 375 });