inbox-policy.ts (6080B)
1 import { 2 verifyEnvelope, 3 verifiedEnvelopeSnapshot 4 } from '../../../src/lib/nostr/verified-envelope.ts'; 5 import { 6 PRIVATE_AUTH_BUDGETS, 7 PRIVATE_TRANSPORT_BUDGETS 8 } from '../../../src/lib/config/budgets.ts'; 9 import type { NostrEvent } from 'applesauce-core/helpers'; 10 11 export const fixtureRelay = 'wss://inbox.example.org/'; 12 // Expected-policy loopback model, never a deployment/operator/retention claim. 13 // Clock and declared retention are explicit test inputs. Genuine SDK signatures 14 // are verified afresh across JSON; the relay cannot inspect encrypted senders. 15 export function createInboxPolicy(options: { 16 now: number; 17 retentionSeconds: number; 18 requireOuterAuthor?: boolean; 19 }) { 20 if ( 21 !Number.isSafeInteger(options.now) || 22 options.now < 0 || 23 !Number.isSafeInteger(options.retentionSeconds) || 24 options.retentionSeconds < 48 * 3600 + 300 25 ) 26 throw new Error('invalid fixture policy'); 27 const events = new Map<string, NostrEvent>(); 28 let bytes = 0; 29 function event(value: unknown): NostrEvent | undefined { 30 const wire = JSON.stringify(value); 31 if ( 32 typeof wire !== 'string' || 33 new TextEncoder().encode(wire).length > 34 PRIVATE_TRANSPORT_BUDGETS.envelopeBytes 35 ) 36 return undefined; 37 const verified = verifyEnvelope(wire); 38 return verified.ok ? verifiedEnvelopeSnapshot(verified.value) : undefined; 39 } 40 return { 41 size: () => events.size, 42 connect(challenge: string) { 43 let owner: string | undefined, 44 closed = false, 45 responses = 0; 46 return { 47 close() { 48 closed = true; 49 owner = undefined; 50 }, 51 frame(raw: string): unknown[][] { 52 if (closed) return [['NOTICE', 'closed: fixture connection']]; 53 let frame: unknown; 54 try { 55 if (new TextEncoder().encode(raw).length > 65536) 56 throw new Error('fixture frame too large'); 57 frame = JSON.parse(raw); 58 } catch { 59 return [['NOTICE', 'invalid: fixture frame']]; 60 } 61 if (!Array.isArray(frame)) 62 return [['NOTICE', 'invalid: fixture frame']]; 63 if (frame[0] === 'AUTH' || frame[0] === 'EVENT') { 64 const e = event(frame[1]), 65 id = e?.id ?? ''; 66 if (frame.length !== 2) 67 return [['OK', id, false, 'invalid: fixture envelope']]; 68 if (frame[0] === 'AUTH') { 69 owner = undefined; 70 responses++; 71 if ( 72 !e || 73 responses > PRIVATE_AUTH_BUDGETS.responsesPerConnectionAction || 74 e.kind !== 22242 || 75 e.content !== '' || 76 e.tags.length !== 2 || 77 !e.tags.some( 78 (t) => 79 t.length === 2 && t[0] === 'relay' && t[1] === fixtureRelay 80 ) || 81 !e.tags.some( 82 (t) => 83 t.length === 2 && t[0] === 'challenge' && t[1] === challenge 84 ) 85 ) 86 return [['OK', id, false, 'restricted: invalid AUTH']]; 87 owner = e.pubkey; 88 return [['OK', id, true, '']]; 89 } 90 if (!owner) 91 return [['OK', id, false, 'auth-required: fixture inbox']]; 92 if ( 93 !e || 94 e.kind !== 1059 || 95 e.content.length === 0 || 96 e.tags.length !== 1 || 97 e.tags[0].length !== 2 || 98 e.tags[0][0] !== 'p' || 99 !/^[0-9a-f]{64}$/u.test(e.tags[0][1]) || 100 e.created_at < options.now - options.retentionSeconds || 101 e.created_at > options.now + 300 || 102 (options.requireOuterAuthor && e.pubkey !== owner) 103 ) 104 return [ 105 ['OK', id, false, 'restricted: unsupported outer envelope'] 106 ]; 107 if (events.has(e.id)) return [['OK', id, true, '']]; 108 const size = new TextEncoder().encode(JSON.stringify(e)).length; 109 if ( 110 events.size >= PRIVATE_TRANSPORT_BUDGETS.deliveries || 111 bytes + size > PRIVATE_TRANSPORT_BUDGETS.processedBytes 112 ) 113 return [['OK', id, false, 'restricted: fixture capacity']]; 114 events.set(e.id, e); 115 bytes += size; 116 return [['OK', id, true, '']]; 117 } 118 if (frame[0] === 'CLOSE') return []; 119 if ( 120 frame[0] !== 'REQ' || 121 typeof frame[1] !== 'string' || 122 frame[1].length > 64 123 ) 124 return [['NOTICE', 'invalid: fixture request']]; 125 const id = frame[1], 126 query: unknown = frame[2]; 127 if (!owner) return [['CLOSED', id, 'auth-required: fixture inbox']]; 128 if ( 129 frame.length !== 3 || 130 !query || 131 typeof query !== 'object' || 132 Array.isArray(query) 133 ) 134 return [['CLOSED', id, 'restricted: recipient-only']]; 135 const q = query as Record<string, unknown>; 136 if ( 137 Object.keys(q).length !== 3 || 138 !Array.isArray(q.kinds) || 139 q.kinds.length !== 1 || 140 q.kinds[0] !== 1059 || 141 !Array.isArray(q['#p']) || 142 q['#p'].length !== 1 || 143 q['#p'][0] !== owner || 144 typeof q.limit !== 'number' || 145 !Number.isSafeInteger(q.limit) || 146 q.limit < 1 || 147 q.limit > PRIVATE_TRANSPORT_BUDGETS.requestedPerRelay 148 ) 149 return [['CLOSED', id, 'restricted: recipient-only']]; 150 const rows = [...events.values()] 151 .filter((e) => e.tags[0][1] === owner) 152 .sort( 153 (a, b) => b.created_at - a.created_at || a.id.localeCompare(b.id) 154 ) 155 .slice(0, q.limit); 156 return [ 157 ...rows.map((e) => [ 158 'EVENT', 159 id, 160 JSON.parse(JSON.stringify(e)) as unknown 161 ]), 162 ['EOSE', id] 163 ]; 164 } 165 }; 166 } 167 }; 168 }