relay-config.test.ts (13046B)
1 import { describe, expect, it } from 'vitest'; 2 import { spawnSync } from 'node:child_process'; 3 import { fileURLToPath } from 'node:url'; 4 import { 5 canonicalRelayOrigin, 6 validateRelayPolicy, 7 readRelayPolicy, 8 type RelayPolicy, 9 requireLaunchRelayPolicy, 10 publicRelayTargets, 11 inboxRelayTargets 12 } from '../../src/lib/config/relays.ts'; 13 import { deploymentRelayPolicy } from '../../src/lib/config/deployment-relays.ts'; 14 15 const admit = (value: unknown) => validateRelayPolicy(JSON.stringify(value)); 16 const publicRelay = (origin = 'wss://public.example.org') => ({ 17 origin, 18 read: true, 19 write: true, 20 nip50: false 21 }); 22 const inboxRelay = (origin = 'wss://inbox.example.org') => ({ 23 origin, 24 read: true, 25 write: true 26 }); 27 const input = () => ({ 28 schemaVersion: 1, 29 public: [publicRelay()], 30 inbox: [inboxRelay()], 31 postingEnabled: true, 32 messagingEnabled: true, 33 operatorDenylist: [] 34 }); 35 36 // Frozen IANA IPv6 Special-Purpose registry revision 2025-10-09, reviewed 37 // 2026-10-14: https://www.iana.org/assignments/iana-ipv6-special-registry/. 38 // RFC4291 defines 128-bit prefixes; RFC9637 reserves the full 3fff::/20. 39 // Conservative relay literals exclude special services even where IANA marks 40 // a specific service globally reachable. No DNS/connect/operator claim follows. 41 describe('CR006 complete binary IPv6 literal policy', () => { 42 it.each([ 43 '2001::', 44 '2001:1ff:ffff:ffff:ffff:ffff:ffff:ffff', 45 '2001:1::1', 46 '2001:1::2', 47 '2001:1::3', 48 '2001:2::1', 49 '2001:3::1', 50 '2001:4:112::1', 51 '2001:10::1', 52 '2001:1f:ffff:ffff:ffff:ffff:ffff:ffff', 53 '2001:20::1', 54 '2001:2f:ffff:ffff:ffff:ffff:ffff:ffff', 55 '2001:30::1', 56 '2001:3f:ffff:ffff:ffff:ffff:ffff:ffff', 57 '2001:db8::', 58 '2001:db8:ffff:ffff:ffff:ffff:ffff:ffff', 59 '2002::', 60 '2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 61 '2620:4f:8000::', 62 '2620:4f:8000:ffff:ffff:ffff:ffff:ffff', 63 '3fff::', 64 '3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff', 65 '::', 66 '::1', 67 '::ffff:808:808', 68 '64:ff9b::808:808', 69 '64:ff9b:1::1', 70 '100::1', 71 '100:0:0:1::1', 72 '5f00::1', 73 'fc00::1', 74 'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 75 'fe80::1', 76 'febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 77 'ff02::1', 78 '4000::1' 79 ])('excludes reviewed special-purpose literal %s', (address) => { 80 expect(canonicalRelayOrigin(`wss://[${address}]`)).toBeUndefined(); 81 }); 82 it.each([ 83 '2000::', 84 '2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 85 '2001:200::', 86 '2001:db7:ffff:ffff:ffff:ffff:ffff:ffff', 87 '2001:db9::', 88 '2001:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 89 '2003::', 90 '2620:4f:7fff:ffff:ffff:ffff:ffff:ffff', 91 '2620:4f:8001::', 92 '3ffe:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 93 '3fff:1000::', 94 '3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 95 '2606:4700:4700::1111', 96 '2606:4700:4700:1:2:3:4:5', 97 '2606:0:1:2:3:4:5:6', 98 '2606::1:2:3:4:5' 99 ])( 100 'keeps ordinary public literal outside excluded prefixes %s', 101 (address) => { 102 const origin = `wss://[${address}]:8443`; 103 expect(canonicalRelayOrigin(origin)).toBe(origin); 104 } 105 ); 106 it.each([ 107 'wss://[2606:4700:4700:0:0:0:0:1111]', 108 'wss://[2606::1:2:3:4:5:6]', 109 'wss://[2606:4700:4700::01111]', 110 'wss://[2606::1::2]', 111 'wss://[2606:4700:4700::1111%25eth0]', 112 'wss://[2606:4700:4700::1111]:443', 113 'wss://[2606:4700:4700::1111]:0', 114 'wss://[2606:4700:4700::1111]:65536', 115 'wss://[2606:4700:4700::1111]/', 116 'wss://[2606:4700:4700::1111]/path', 117 'wss://user@[2606:4700:4700::1111]', 118 'wss://[2606:4700:4700::1111]?q=x', 119 'wss://[2606:4700:4700::1111]#fragment' 120 ])('retains canonical URL admission before binary policy %s', (origin) => { 121 expect(canonicalRelayOrigin(origin)).toBeUndefined(); 122 }); 123 }); 124 125 describe('fixed relay policy', () => { 126 it('admits canonical WSS origins and explicit optional NIP50 capability', () => { 127 expect(canonicalRelayOrigin('wss://relay.example.org:8443')).toBe( 128 'wss://relay.example.org:8443' 129 ); 130 const value = input(); 131 value.public[0].nip50 = true; 132 expect(readRelayPolicy(admit(value)!).public[0].nip50).toBe(true); 133 }); 134 it.each([ 135 '', 136 'ws://relay.example.org', 137 'https://relay.example.org', 138 '//relay.example.org', 139 ' wss://relay.example.org', 140 'wss://relay.example.org\n', 141 'wss://relay.example.org\\x', 142 'wss://user:password@relay.example.org', 143 'wss://@relay.example.org', 144 'wss://relay.example.org/path', 145 'wss://relay.example.org?token=private', 146 'wss://relay.example.org#fragment', 147 'WSS://RELAY.EXAMPLE.ORG', 148 'wss://relay.example.org/', 149 'wss://relay.example.org:443', 150 'wss://relay.example.org:0', 151 'wss://relay.example.org:65536', 152 'wss://relay%2eexample.org', 153 'wss://relay.example.org\u0000', 154 'wss://localhost', 155 'wss://sub.localhost', 156 'wss://relay.local', 157 'wss://relay.internal', 158 'wss://home.arpa', 159 'wss://relay.home.arpa', 160 'wss://relay.test', 161 'wss://127.0.0.1', 162 'wss://127.1', 163 'wss://2130706433', 164 'wss://0x7f000001', 165 'wss://10.1.2.3', 166 'wss://172.16.0.1', 167 'wss://192.168.0.1', 168 'wss://169.254.169.254', 169 'wss://100.64.0.1', 170 'wss://0.0.0.0', 171 'wss://224.0.0.1', 172 'wss://[::1]', 173 'wss://[::]', 174 'wss://[fc00::1]', 175 'wss://[fe80::1]', 176 'wss://[::ffff:127.0.0.1]', 177 'wss://[ff02::1]', 178 'wss://singlelabel', 179 'wss://relay.example.org.', 180 'wss://-bad.example.org', 181 'wss://relay.example.org\ud800', 182 'wss://' + 'a'.repeat(8193) 183 ])('rejects malformed, credential or private-network origin %s', (origin) => { 184 expect(canonicalRelayOrigin(origin)).toBeUndefined(); 185 expect( 186 admit({ ...input(), public: [publicRelay(origin)] }) 187 ).toBeUndefined(); 188 }); 189 it('admits public address literals without claiming DNS or connect-time enforcement', () => { 190 expect(canonicalRelayOrigin('wss://8.8.8.8')).toBe('wss://8.8.8.8'); 191 expect(canonicalRelayOrigin('wss://[2606:4700:4700::1111]')).toBe( 192 'wss://[2606:4700:4700::1111]' 193 ); 194 }); 195 it('caps each role independently at three, permits the same origin in separate roles', () => { 196 const value = input(); 197 value.public = [1, 2, 3].map((n) => 198 publicRelay(`wss://public${n}.example.org`) 199 ); 200 value.inbox = value.public.map(({ origin }) => inboxRelay(origin)); 201 expect(readRelayPolicy(admit(value)!).inbox).toHaveLength(3); 202 expect( 203 admit({ 204 ...value, 205 public: [...value.public, publicRelay()] 206 }) 207 ).toBeUndefined(); 208 expect( 209 admit({ ...value, inbox: [...value.inbox, inboxRelay()] }) 210 ).toBeUndefined(); 211 expect( 212 admit({ 213 ...input(), 214 public: [publicRelay(), publicRelay()] 215 }) 216 ).toBeUndefined(); 217 expect( 218 admit({ ...input(), inbox: [inboxRelay(), inboxRelay()] }) 219 ).toBeUndefined(); 220 }); 221 it('rejects unknown fields and wrong schema, array and flag types without echoing secrets', () => { 222 for (const value of [ 223 null, 224 [], 225 { ...input(), schemaVersion: 2 }, 226 { ...input(), token: 'private' }, 227 { ...input(), postingEnabled: 1 }, 228 { ...input(), messagingEnabled: 'true' }, 229 { ...input(), public: {} }, 230 { ...input(), operatorDenylist: ['private'] }, 231 { ...input(), public: [{ ...publicRelay(), auth: true }] }, 232 { ...input(), inbox: [{ ...inboxRelay(), nip50: false }] }, 233 { ...input(), public: [{ ...publicRelay(), read: false, nip50: true }] }, 234 { ...input(), public: [{ ...publicRelay(), read: false, write: false }] } 235 ]) 236 expect(admit(value)).toBeUndefined(); 237 }); 238 it('validates denylist keys and preserves an immutable token with detached views', () => { 239 const key = 240 '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; 241 const value = { ...input(), operatorDenylist: [key] }; 242 const admitted = admit(value)!; 243 value.public[0].origin = 'wss://outside.example.org'; 244 value.operatorDenylist.push('bad'); 245 expect(readRelayPolicy(admitted).public[0].origin).toBe( 246 'wss://public.example.org' 247 ); 248 expect(readRelayPolicy(admitted).operatorDenylist).toEqual([key]); 249 expect(Object.isFrozen(admitted)).toBe(true); 250 251 expect(admit({ ...input(), operatorDenylist: [key, key] })).toBeUndefined(); 252 expect( 253 admit({ ...input(), operatorDenylist: ['f'.repeat(64)] }) 254 ).toBeUndefined(); 255 }); 256 it('rejects inherited fields, symbols and accessors without invoking getters', () => { 257 let reads = 0; 258 const getter = { ...input() }; 259 Object.defineProperty(getter, 'public', { 260 get: () => { 261 reads++; 262 return []; 263 }, 264 enumerable: true 265 }); 266 expect(validateRelayPolicy(getter)).toBeUndefined(); 267 expect(reads).toBe(0); 268 expect(validateRelayPolicy(Object.create(input()))).toBeUndefined(); 269 expect( 270 validateRelayPolicy({ ...input(), [Symbol('credential')]: 'private' }) 271 ).toBeUndefined(); 272 }); 273 it('ignores public hints and intersects inbox targets with approved recipient preferences', () => { 274 const policy = admit(input())!; 275 expect(publicRelayTargets(policy, 'read')).toEqual([ 276 'wss://public.example.org' 277 ]); 278 expect( 279 inboxRelayTargets( 280 policy, 281 [ 282 'wss://outside.example.org', 283 'wss://inbox.example.org', 284 'wss://inbox.example.org' 285 ], 286 'write' 287 ) 288 ).toEqual(['wss://inbox.example.org']); 289 expect( 290 inboxRelayTargets(policy, ['wss://outside.example.org'], 'write') 291 ).toEqual([]); 292 expect(inboxRelayTargets(policy, [], 'read')).toEqual([]); 293 const readOnly = admit({ 294 ...input(), 295 inbox: [{ ...inboxRelay(), write: false }] 296 })!; 297 expect( 298 inboxRelayTargets(readOnly, ['wss://inbox.example.org'], 'write') 299 ).toEqual([]); 300 }); 301 it('keeps separate effect flags disabled and production inputs empty until approved', () => { 302 expect(readRelayPolicy(deploymentRelayPolicy).public).toEqual([]); 303 expect(readRelayPolicy(deploymentRelayPolicy).inbox).toEqual([]); 304 expect(readRelayPolicy(deploymentRelayPolicy).postingEnabled).toBe(false); 305 expect(readRelayPolicy(deploymentRelayPolicy).messagingEnabled).toBe(false); 306 expect(() => requireLaunchRelayPolicy(deploymentRelayPolicy)).toThrow( 307 'relay_launch_inputs_missing' 308 ); 309 expect(requireLaunchRelayPolicy(admit(input())!)).toBeDefined(); 310 expect(() => requireLaunchRelayPolicy({} as RelayPolicy)).toThrow( 311 'relay_policy_invalid' 312 ); 313 expect( 314 admit({ ...input(), public: [publicRelay('wss://127.0.0.1')] }) 315 ).toBeUndefined(); 316 }); 317 it('bounds JSON admission and rejects malformed or non-JSON inputs', () => { 318 for (const raw of [ 319 '{', 320 'null', 321 '[]', 322 '"policy"', 323 ' '.repeat(65537), 324 JSON.stringify({ ...input(), operatorDenylist: ['é'.repeat(33000)] }) 325 ]) 326 expect(validateRelayPolicy(raw)).toBeUndefined(); 327 expect(validateRelayPolicy(input())).toBeUndefined(); 328 }); 329 it('keeps retained egress unchanged after detached manifest and target mutations', () => { 330 const token = admit(input())!; 331 const view = readRelayPolicy(token); 332 (view.public as unknown as { origin: string }[])[0].origin = 333 'wss://outside.example.org'; 334 (view.operatorDenylist as string[]).push('changed'); 335 const targets = publicRelayTargets(token, 'read') as string[]; 336 targets.push('wss://outside.example.org'); 337 expect(publicRelayTargets(token, 'read')).toEqual([ 338 'wss://public.example.org' 339 ]); 340 expect(readRelayPolicy(token).operatorDenylist).toEqual([]); 341 expect(() => publicRelayTargets({} as RelayPolicy, 'read')).toThrow( 342 'relay_policy_invalid' 343 ); 344 }); 345 it('preserves separate posting and messaging deployment flags', () => { 346 const token = admit({ 347 ...input(), 348 postingEnabled: false, 349 messagingEnabled: true 350 })!; 351 expect(readRelayPolicy(token).postingEnabled).toBe(false); 352 expect(readRelayPolicy(token).messagingEnabled).toBe(true); 353 }); 354 it('rejects inactive or secret-bearing inbox role data', () => { 355 expect( 356 admit({ 357 ...input(), 358 inbox: [{ ...inboxRelay(), read: false, write: false }] 359 }) 360 ).toBeUndefined(); 361 expect( 362 admit({ ...input(), inbox: [{ ...inboxRelay(), credential: 'private' }] }) 363 ).toBeUndefined(); 364 expect( 365 admit({ ...input(), public: [{ ...publicRelay(), write: 'true' }] }) 366 ).toBeUndefined(); 367 }); 368 it('actually fails the explicit launch preparation command on missing deployed inputs', () => { 369 const result = spawnSync( 370 process.execPath, 371 ['tools/check-launch-relays.mjs'], 372 { 373 cwd: fileURLToPath(new URL('../../', import.meta.url)), 374 encoding: 'utf8', 375 timeout: 86400000 376 } 377 ); 378 expect(result.error).toBeUndefined(); 379 expect(result.status).toBe(1); 380 expect(result.stderr).toContain('relay_launch_inputs_missing'); 381 expect(result.stdout).not.toContain('PASS'); 382 }); 383 });