inbox-operators.test.ts (10396B)
1 import { describe, expect, it } from 'vitest'; 2 import { 3 inboxOperatorQualificationSnapshot, 4 inboxOperatorQualificationWire, 5 validateInboxOperatorQualification, 6 type InboxOperatorQualification 7 } from '../../src/lib/config/inbox-operators.ts'; 8 import { deploymentInboxOperatorQualification } from '../../src/lib/config/deployment-inbox-operators.ts'; 9 import { deploymentRelayPolicy } from '../../src/lib/config/deployment-relays.ts'; 10 import { 11 readRelayPolicy, 12 validateRelayPolicy, 13 type RelayPolicy 14 } from '../../src/lib/config/relays.ts'; 15 16 function policyInput() { 17 return { 18 schemaVersion: 1, 19 public: [ 20 { 21 origin: 'wss://public.example.org', 22 read: true, 23 write: false, 24 nip50: false 25 } 26 ], 27 inbox: [{ origin: 'wss://inbox.example.org', read: true, write: true }], 28 postingEnabled: false, 29 messagingEnabled: true, 30 operatorDenylist: [] 31 }; 32 } 33 function input() { 34 return { 35 schemaVersion: 1, 36 revision: 1, 37 authority: 'source_fixture', 38 policyWire: JSON.stringify(policyInput()), 39 evidenceSha256: 'a'.repeat(64), 40 relays: [ 41 { 42 origin: 'wss://inbox.example.org', 43 auth: 'not_required', 44 recipientRead: 'supported', 45 namedArchiveAck: 'supported', 46 exactReadback: 'supported', 47 retention: 'operator_defined' 48 } 49 ] 50 }; 51 } 52 const admit = (value: unknown) => 53 validateInboxOperatorQualification(JSON.stringify(value)); 54 const genuinePolicy = () => validateRelayPolicy(JSON.stringify(policyInput()))!; 55 56 describe('CR008 S1 source-owned operator declaration boundaries', () => { 57 it('keeps deployment unconfigured and bound to the original disabled policy', () => { 58 expect( 59 inboxOperatorQualificationSnapshot(deploymentInboxOperatorQualification) 60 ).toEqual({ 61 schemaVersion: 1, 62 revision: 1, 63 authority: 'unconfigured', 64 policyWire: JSON.stringify(readRelayPolicy(deploymentRelayPolicy)), 65 evidenceSha256: null, 66 relays: [] 67 }); 68 expect( 69 inboxOperatorQualificationWire( 70 deploymentInboxOperatorQualification, 71 deploymentRelayPolicy 72 ) 73 ).toBeDefined(); 74 expect( 75 inboxOperatorQualificationWire( 76 deploymentInboxOperatorQualification, 77 genuinePolicy() 78 ) 79 ).toBeUndefined(); 80 }); 81 it('retains the exact full declaration and separate fixture/operator authority, without readiness', () => { 82 for (const authority of ['source_fixture', 'operator']) { 83 const value = { ...input(), authority }, 84 token = admit(value)!; 85 expect(inboxOperatorQualificationSnapshot(token)).toEqual(value); 86 expect(inboxOperatorQualificationWire(token, genuinePolicy())).toBe( 87 JSON.stringify(value) 88 ); 89 expect(Object.isFrozen(token)).toBe(true); 90 expect(Object.keys(token)).toEqual([]); 91 expect(inboxOperatorQualificationSnapshot(token)).not.toHaveProperty( 92 'readiness' 93 ); 94 } 95 expect(admit({ ...input(), readiness: true })).toBeUndefined(); 96 }); 97 it('retains unsupported declarations and explicit AUTH requirements as metadata', () => { 98 for (const auth of ['not_required', 'explicit_required', 'unsupported']) { 99 const value = input(); 100 value.relays[0] = { 101 ...value.relays[0], 102 auth, 103 recipientRead: 'unsupported', 104 namedArchiveAck: 'unsupported', 105 exactReadback: 'unsupported', 106 retention: 'unsupported' 107 }; 108 expect(inboxOperatorQualificationSnapshot(admit(value)!)).toEqual(value); 109 } 110 }); 111 it('cannot mutate retained origins, capabilities, evidence or authority using detached views', () => { 112 const value = input(), 113 token = admit(value)!, 114 original = JSON.stringify(value); 115 value.relays[0].origin = 'wss://outside.example.org'; 116 value.evidenceSha256 = 'b'.repeat(64); 117 const view = inboxOperatorQualificationSnapshot( 118 token 119 )! as unknown as ReturnType<typeof input>; 120 view.authority = 'operator'; 121 view.relays[0].auth = 'unsupported'; 122 view.relays.push({ 123 ...view.relays[0], 124 origin: 'wss://outside.example.org' 125 }); 126 view.policyWire = '{}'; 127 expect(inboxOperatorQualificationWire(token, genuinePolicy())).toBe( 128 original 129 ); 130 expect(inboxOperatorQualificationSnapshot(token)?.authority).toBe( 131 'source_fixture' 132 ); 133 expect(inboxOperatorQualificationSnapshot(token)?.relays).toHaveLength(1); 134 const forged = { ...token } as InboxOperatorQualification; 135 expect(inboxOperatorQualificationSnapshot(forged)).toBeUndefined(); 136 expect( 137 inboxOperatorQualificationWire(forged, genuinePolicy()) 138 ).toBeUndefined(); 139 expect( 140 inboxOperatorQualificationWire(token, {} as RelayPolicy) 141 ).toBeUndefined(); 142 }); 143 it('invalidates binding for every changed policy dimension, even when the selected origin survives', () => { 144 const token = admit(input())!; 145 for (const patch of [ 146 { messagingEnabled: false }, 147 { postingEnabled: true }, 148 { inbox: [{ ...policyInput().inbox[0], write: false }] }, 149 { public: [{ ...policyInput().public[0], write: true }] }, 150 { 151 operatorDenylist: [ 152 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' 153 ] 154 } 155 ]) { 156 const changed = validateRelayPolicy( 157 JSON.stringify({ ...policyInput(), ...patch }) 158 )!; 159 expect(changed).toBeDefined(); 160 expect(inboxOperatorQualificationWire(token, changed)).toBeUndefined(); 161 } 162 }); 163 it('retains at most three unique approved canonical inbox origins and never public-only hints', () => { 164 const value = input(), 165 origins = [1, 2, 3].map((n) => `wss://inbox${n}.example.org`); 166 value.policyWire = JSON.stringify({ 167 ...policyInput(), 168 inbox: origins.map((origin) => ({ origin, read: true, write: true })) 169 }); 170 value.relays = origins.map((origin) => ({ ...value.relays[0], origin })); 171 expect(admit(value)).toBeDefined(); 172 expect( 173 admit({ ...value, relays: [...value.relays, value.relays[0]] }) 174 ).toBeUndefined(); 175 expect( 176 admit({ ...value, relays: [value.relays[0], value.relays[0]] }) 177 ).toBeUndefined(); 178 for (const origin of [ 179 'wss://public.example.org', 180 'wss://outside.example.org', 181 'wss://inbox.example.org/', 182 'wss://inbox.example.org:443', 183 'ws://inbox.example.org', 184 'wss://127.0.0.1', 185 'wss://user@inbox.example.org' 186 ]) 187 expect( 188 admit({ ...input(), relays: [{ ...input().relays[0], origin }] }) 189 ).toBeUndefined(); 190 }); 191 it('rejects missing/unknown root and relay fields, false booleans and malformed evidence without coercion', () => { 192 const value = input(); 193 for (const patch of [ 194 { schemaVersion: 2 }, 195 { revision: 0 }, 196 { revision: -1 }, 197 { revision: 1.1 }, 198 { revision: Number.MAX_SAFE_INTEGER + 1 }, 199 { authority: 'approved' }, 200 { authority: true }, 201 { evidenceSha256: null }, 202 { evidenceSha256: 'A'.repeat(64) }, 203 { evidenceSha256: 'a'.repeat(63) }, 204 { evidenceSha256: true }, 205 { relays: [] }, 206 { relays: {} }, 207 { policyWire: {} }, 208 { policyWire: '{}' }, 209 { privateKey: 'SECRET_SENTINEL' } 210 ]) 211 expect(admit({ ...value, ...patch })).toBeUndefined(); 212 for (const patch of [ 213 { auth: true }, 214 { recipientRead: true }, 215 { namedArchiveAck: false }, 216 { exactReadback: 'ready' }, 217 { retention: 'unlimited' }, 218 { credential: 'SECRET_SENTINEL' } 219 ]) 220 expect( 221 admit({ ...value, relays: [{ ...value.relays[0], ...patch }] }) 222 ).toBeUndefined(); 223 for (const key of Object.keys(value)) { 224 const broken: Record<string, unknown> = { ...value }; 225 delete broken[key]; 226 expect(admit(broken)).toBeUndefined(); 227 } 228 for (const key of Object.keys(value.relays[0])) { 229 const broken: Record<string, unknown> = { ...value.relays[0] }; 230 delete broken[key]; 231 expect(admit({ ...value, relays: [broken] })).toBeUndefined(); 232 } 233 expect( 234 admit({ 235 ...value, 236 authority: 'unconfigured', 237 relays: [], 238 evidenceSha256: null 239 }) 240 ).toBeDefined(); 241 expect( 242 admit({ ...value, authority: 'unconfigured', evidenceSha256: null }) 243 ).toBeUndefined(); 244 expect( 245 admit({ ...value, authority: 'unconfigured', relays: [] }) 246 ).toBeUndefined(); 247 }); 248 it('rejects duplicate/escaped keys, whitespace and altered canonical numeric spelling in either JSON layer', () => { 249 const raw = JSON.stringify(input()); 250 for (const malformed of [ 251 raw.replace('"revision":1', '"revision":2,"revision":1'), 252 raw.replace('"authority":', '"\\u0061uthority":'), 253 raw.replace( 254 '"auth":"not_required"', 255 '"auth":"unsupported","auth":"not_required"' 256 ), 257 raw.replace('"revision":1', '"revision":1e0'), 258 raw + '\n', 259 JSON.stringify(input(), null, 2) 260 ]) 261 expect(validateInboxOperatorQualification(malformed)).toBeUndefined(); 262 for (const policyWire of [ 263 JSON.stringify(policyInput()).replace( 264 '"messagingEnabled":true', 265 '"messagingEnabled":false,"messagingEnabled":true' 266 ), 267 JSON.stringify(policyInput()).replace( 268 '"schemaVersion"', 269 '"\\u0073chemaVersion"' 270 ), 271 JSON.stringify(policyInput(), null, 2) 272 ]) 273 expect(admit({ ...input(), policyWire })).toBeUndefined(); 274 }); 275 it('bounds raw UTF8 before parsing and does not execute caller getters or coercions', () => { 276 for (const raw of [ 277 '{', 278 'null', 279 '[]', 280 'false', 281 ' '.repeat(65537), 282 JSON.stringify({ extra: 'é'.repeat(33000) }), 283 '"\ud800"' 284 ]) 285 expect(validateInboxOperatorQualification(raw)).toBeUndefined(); 286 let reads = 0; 287 const hostile = { 288 get relays() { 289 reads++; 290 throw Error('getter executed'); 291 }, 292 toString() { 293 reads++; 294 return JSON.stringify(input()); 295 } 296 }; 297 expect(validateInboxOperatorQualification(hostile)).toBeUndefined(); 298 expect( 299 validateInboxOperatorQualification(Object.create(input())) 300 ).toBeUndefined(); 301 expect( 302 validateInboxOperatorQualification({ 303 ...input(), 304 [Symbol('secret')]: true 305 }) 306 ).toBeUndefined(); 307 expect(reads).toBe(0); 308 }); 309 });