inbox-policy.test.ts (3940B)
1 import test from 'node:test'; 2 import assert from 'node:assert/strict'; 3 import { PrivateKeySigner } from 'applesauce-signers'; 4 import { createInboxPolicy, fixtureRelay } from './harness/inbox-policy.ts'; 5 6 const now = 1800000000; 7 async function inputs() { 8 const owner = new PrivateKeySigner(), 9 outer = new PrivateKeySigner(), 10 peer = new PrivateKeySigner(); 11 const author = await owner.getPublicKey(), 12 recipient = await peer.getPublicKey(); 13 const auth = await owner.signEvent({ 14 kind: 22242, 15 created_at: now, 16 tags: [ 17 ['relay', fixtureRelay], 18 ['challenge', 'challenge'] 19 ], 20 content: '' 21 }); 22 const wrap = await outer.signEvent({ 23 kind: 1059, 24 created_at: now - 48 * 3600 - 300, 25 tags: [['p', recipient]], 26 content: 'opaque policy fixture ciphertext' 27 }); 28 return { 29 owner, 30 outer, 31 peer, 32 author, 33 recipient, 34 auth, 35 wrap, 36 close() { 37 owner.key.fill(0); 38 outer.key.fill(0); 39 peer.key.fill(0); 40 } 41 }; 42 } 43 void test('expected policy independently verifies AUTH and disposable outer author', async () => { 44 const x = await inputs(); 45 try { 46 const policy = createInboxPolicy({ now, retentionSeconds: 30 * 86400 }), 47 connection = policy.connect('challenge'); 48 assert.deepEqual(connection.frame(JSON.stringify(['AUTH', x.auth])), [ 49 ['OK', x.auth.id, true, ''] 50 ]); 51 assert.notEqual(x.wrap.pubkey, x.author); 52 assert.deepEqual(connection.frame(JSON.stringify(['EVENT', x.wrap])), [ 53 ['OK', x.wrap.id, true, ''] 54 ]); 55 assert.equal(policy.size(), 1); 56 assert.deepEqual( 57 connection.frame( 58 JSON.stringify([ 59 'REQ', 60 'wrong', 61 { kinds: [1059], '#p': [x.recipient], limit: 200 } 62 ]) 63 ), 64 [['CLOSED', 'wrong', 'restricted: recipient-only']] 65 ); 66 connection.close(); 67 assert.deepEqual(connection.frame(JSON.stringify(['EVENT', x.wrap])), [ 68 ['NOTICE', 'closed: fixture connection'] 69 ]); 70 } finally { 71 x.close(); 72 } 73 }); 74 void test('invalid signature, wrong AUTH binding and plaintext kinds fail closed without admission', async () => { 75 const x = await inputs(); 76 try { 77 const policy = createInboxPolicy({ now, retentionSeconds: 30 * 86400 }), 78 c = policy.connect('challenge'); 79 assert.equal( 80 c.frame( 81 JSON.stringify(['AUTH', { ...x.auth, sig: '0'.repeat(128) }]) 82 )[0][2], 83 false 84 ); 85 const wrong = await x.owner.signEvent({ 86 ...x.auth, 87 tags: [ 88 ['relay', 'wss://wrong.example.org/'], 89 ['challenge', 'challenge'] 90 ] 91 }); 92 assert.equal(c.frame(JSON.stringify(['AUTH', wrong]))[0][2], false); 93 assert.equal(c.frame(JSON.stringify(['EVENT', x.wrap]))[0][2], false); 94 const fresh = policy.connect('challenge'); 95 fresh.frame(JSON.stringify(['AUTH', x.auth])); 96 for (const kind of [13, 14]) { 97 const plaintext = await x.owner.signEvent({ 98 kind, 99 created_at: now, 100 tags: [], 101 content: 'unsupported plaintext fixture' 102 }); 103 assert.equal( 104 fresh.frame(JSON.stringify(['EVENT', plaintext]))[0][2], 105 false 106 ); 107 } 108 assert.equal( 109 fresh.frame( 110 JSON.stringify(['EVENT', { ...x.wrap, sig: '0'.repeat(128) }]) 111 )[0][2], 112 false 113 ); 114 assert.deepEqual(fresh.frame('{'), [['NOTICE', 'invalid: fixture frame']]); 115 assert.equal(policy.size(), 0); 116 } finally { 117 x.close(); 118 } 119 }); 120 void test('modeled restrictive author policy exposes disposable-wrap refusal', async () => { 121 const x = await inputs(); 122 try { 123 const p = createInboxPolicy({ 124 now, 125 retentionSeconds: 30 * 86400, 126 requireOuterAuthor: true 127 }), 128 c = p.connect('challenge'); 129 c.frame(JSON.stringify(['AUTH', x.auth])); 130 assert.equal(c.frame(JSON.stringify(['EVENT', x.wrap]))[0][2], false); 131 assert.equal(p.size(), 0); 132 } finally { 133 x.close(); 134 } 135 });