app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

commit f405253b5775c21d1d7aea766e3b6ef028783690
parent 2803f5e293b86e862b982fae573351124097a6a9
Author: triesap <tyson@radroots.org>
Date:   Wed, 14 Oct 2026 02:47:15 +0000

harvestcircle: classify complete IPv6 relay prefixes

- Parse bounded IPv6 words into exact 128-bit prefix comparisons.
- Exclude frozen special-purpose ranges while preserving canonical URL rules.
- Cover prefix boundaries and support consumers with 161 focused unit passes.
- Preserve current source guards and seven actual artifact controls.

Diffstat:
Mweb/src/lib/config/relays.ts | 62++++++++++++++++++++++++++++++++++++++++++++++++++------------
Mweb/tests/provenance/output.test.mjs | 1+
Mweb/tests/unit/relay-config.test.ts | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mweb/tests/unit/support-config.test.ts | 24++++++++++++++++++++++++
Mweb/tools/check-output.mjs | 8++++----
5 files changed, 168 insertions(+), 16 deletions(-)

diff --git a/web/src/lib/config/relays.ts b/web/src/lib/config/relays.ts @@ -38,19 +38,57 @@ function fields( return count === names.length; } +// URL admission supplies canonical bracketed hex, never a zone or dotted +// IPv4 tail. Independently bound/validate all eight words before BigInt work. +function ipv6Bits(value: string): bigint | undefined { + if (value.length > 39 || !/^[0-9a-f:]{2,39}$/.test(value)) return undefined; + const halves = value.split('::'); + if (halves.length > 2) return undefined; + const left = halves[0] ? halves[0].split(':') : []; + const right = halves.length === 2 && halves[1] ? halves[1].split(':') : []; + const present = left.length + right.length; + if ( + (halves.length === 1 ? present !== 8 : present >= 8) || + ![...left, ...right].every((word) => /^[0-9a-f]{1,4}$/.test(word)) + ) + return undefined; + const words = [...left, ...Array<string>(8 - present).fill('0'), ...right]; + return words.reduce((bits, word) => (bits << 16n) | BigInt('0x' + word), 0n); +} +function ipv6Prefix(bits: bigint, network: bigint, length: number): boolean { + const shift = BigInt(128 - length); + return bits >> shift === network >> shift; +} +function publicIpv6(host: string): boolean { + if (!host.endsWith(']')) return false; + const bits = ipv6Bits(host.slice(1, -1)); + if ( + bits === undefined || + !ipv6Prefix(bits, 0x20000000000000000000000000000000n, 3) + ) + return false; + // Frozen IANA IPv6 Special-Purpose registry revision 2025-10-09: + // https://www.iana.org/assignments/iana-ipv6-special-registry/. + // RFC4291 prefix bits, RFC9637 documentation /20. Conservative relay policy + // excludes special protocol/service allocations including globally reachable + // exceptions within 2001::/23 and AS112; this is not a routability assertion. + // Other registry rows (mapped, translation, local, multicast, SRv6, discard) + // are already outside 2000::/3. IPv4/DNS admission is independent below. + const special: readonly (readonly [bigint, number])[] = [ + [0x20010000000000000000000000000000n, 23], + [0x20010db8000000000000000000000000n, 32], + [0x20020000000000000000000000000000n, 16], + [0x2620004f800000000000000000000000n, 48], + [0x3fff0000000000000000000000000000n, 20] + ]; + // Literal configuration only; browser resolution/connection and actual + // operator access still require their separate qualification. + return !special.some(([network, length]) => + ipv6Prefix(bits, network, length) + ); +} function publicAddress(host: string): boolean { - if (host.startsWith('[')) { - // Canonical global-unicast IPv6 only; mapped IPv4, local, multicast and - // unspecified forms fail closed. This is literal admission, never DNS or - // IP-at-connect enforcement (the browser owns connection resolution). - const head = host.slice(1, -1).split(':'); - const first = Number.parseInt(head[0], 16); - return ( - first >= 0x2000 && - first <= 0x3fff && - !(first === 0x2001 && ['db8', '0', '2', '10', '20'].includes(head[1])) - ); - } + if (host.startsWith('[')) return publicIpv6(host); if (/^[0-9.]+$/.test(host)) { const [a, b, c] = host.split('.').map(Number); return ( diff --git a/web/tests/provenance/output.test.mjs b/web/tests/provenance/output.test.mjs @@ -1938,6 +1938,7 @@ import { auditOutput } from '../../tools/check-output.mjs'; } for (const producer of [ 'src/lib/config/operation-flags.ts', + 'src/lib/config/relays.ts', 'src/lib/runtime/capabilities.ts', 'src/lib/components/AppShell.svelte', 'src/lib/components/ListingEditor.svelte', diff --git a/web/tests/unit/relay-config.test.ts b/web/tests/unit/relay-config.test.ts @@ -33,6 +33,95 @@ const input = () => ({ operatorDenylist: [] }); +// Frozen IANA IPv6 Special-Purpose registry revision 2025-10-09, reviewed +// 2026-10-14: https://www.iana.org/assignments/iana-ipv6-special-registry/. +// RFC4291 defines 128-bit prefixes; RFC9637 reserves the full 3fff::/20. +// Conservative relay literals exclude special services even where IANA marks +// a specific service globally reachable. No DNS/connect/operator claim follows. +describe('CR006 complete binary IPv6 literal policy', () => { + it.each([ + '2001::', + '2001:1ff:ffff:ffff:ffff:ffff:ffff:ffff', + '2001:1::1', + '2001:1::2', + '2001:1::3', + '2001:2::1', + '2001:3::1', + '2001:4:112::1', + '2001:10::1', + '2001:1f:ffff:ffff:ffff:ffff:ffff:ffff', + '2001:20::1', + '2001:2f:ffff:ffff:ffff:ffff:ffff:ffff', + '2001:30::1', + '2001:3f:ffff:ffff:ffff:ffff:ffff:ffff', + '2001:db8::', + '2001:db8:ffff:ffff:ffff:ffff:ffff:ffff', + '2002::', + '2002:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + '2620:4f:8000::', + '2620:4f:8000:ffff:ffff:ffff:ffff:ffff', + '3fff::', + '3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff', + '::', + '::1', + '::ffff:808:808', + '64:ff9b::808:808', + '64:ff9b:1::1', + '100::1', + '100:0:0:1::1', + '5f00::1', + 'fc00::1', + 'fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + 'fe80::1', + 'febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + 'ff02::1', + '4000::1' + ])('excludes reviewed special-purpose literal %s', (address) => { + expect(canonicalRelayOrigin(`wss://[${address}]`)).toBeUndefined(); + }); + it.each([ + '2000::', + '2000:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + '2001:200::', + '2001:db7:ffff:ffff:ffff:ffff:ffff:ffff', + '2001:db9::', + '2001:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + '2003::', + '2620:4f:7fff:ffff:ffff:ffff:ffff:ffff', + '2620:4f:8001::', + '3ffe:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + '3fff:1000::', + '3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', + '2606:4700:4700::1111', + '2606:4700:4700:1:2:3:4:5', + '2606:0:1:2:3:4:5:6', + '2606::1:2:3:4:5' + ])( + 'keeps ordinary public literal outside excluded prefixes %s', + (address) => { + const origin = `wss://[${address}]:8443`; + expect(canonicalRelayOrigin(origin)).toBe(origin); + } + ); + it.each([ + 'wss://[2606:4700:4700:0:0:0:0:1111]', + 'wss://[2606::1:2:3:4:5:6]', + 'wss://[2606:4700:4700::01111]', + 'wss://[2606::1::2]', + 'wss://[2606:4700:4700::1111%25eth0]', + 'wss://[2606:4700:4700::1111]:443', + 'wss://[2606:4700:4700::1111]:0', + 'wss://[2606:4700:4700::1111]:65536', + 'wss://[2606:4700:4700::1111]/', + 'wss://[2606:4700:4700::1111]/path', + 'wss://user@[2606:4700:4700::1111]', + 'wss://[2606:4700:4700::1111]?q=x', + 'wss://[2606:4700:4700::1111]#fragment' + ])('retains canonical URL admission before binary policy %s', (origin) => { + expect(canonicalRelayOrigin(origin)).toBeUndefined(); + }); +}); + describe('fixed relay policy', () => { it('admits canonical WSS origins and explicit optional NIP50 capability', () => { expect(canonicalRelayOrigin('wss://relay.example.org:8443')).toBe( diff --git a/web/tests/unit/support-config.test.ts b/web/tests/unit/support-config.test.ts @@ -17,6 +17,30 @@ const raw = (fields: Record<string, unknown> = {}) => JSON.stringify({ ...fixture, ...fields }); describe('HCP124 original bounded deployment support configuration', () => { + it.each([ + '3fff::1', + '3fff:fff:ffff:ffff:ffff:ffff:ffff:ffff', + '2001:db8::1', + '2001:1f::1', + '2001:3f::1', + '2002::1', + '2620:4f:8000::1', + '::ffff:808:808', + 'fc00::1' + ])('CR006 rejects special IPv6 support destination %s', (address) => { + expect( + validateSupportPolicy(raw({ destination: `https://[${address}]/report` })) + ).toBeUndefined(); + }); + it.each(['2606:4700:4700::1111', '3fff:1000::1', '2001:200::1'])( + 'CR006 preserves canonical public IPv6 HTTPS support destination %s', + (address) => { + const href = `https://[${address}]:8443/report`; + const policy = validateSupportPolicy(raw({ destination: href })); + expect(policy).toBeDefined(); + expect(policy && readSupportPolicy(policy).destination).toBe(href); + } + ); it('admits the exact inert fixture and returns a detached snapshot', () => { const policy = validateSupportPolicy(raw()); expect(policy).toBeDefined(); diff --git a/web/tools/check-output.mjs b/web/tools/check-output.mjs @@ -293,7 +293,7 @@ async function operationalFlagsAdmission(web) { 'src/lib/config/operation-flags.ts': 'd5e0ce74a17de7a06100dcc75886616655fbd398c657027e7d6990d97aa8a0c1', 'src/lib/config/relays.ts': - 'f54a4e91c6561cf02e5aebfb7b62a7913faa4416a5664f2d14d2e3f3f9886f55', + 'ca8c63df89509728a7b87e65232f1da751621ed4777993a079730472a0923957', 'src/lib/config/support.ts': 'a5b4c179db6f13ebf85081960e2f7c330c3e894666fbf8f0472edb1c5c818872', 'src/lib/contracts/local-records.ts': @@ -551,7 +551,7 @@ async function privacyDataAdmission(web) { 'src/lib/config/deployment-support.ts': 'c698313143943749bb457674825b648f6af29477f07cae960f3435c035cd6bd8', 'src/lib/config/relays.ts': - 'f54a4e91c6561cf02e5aebfb7b62a7913faa4416a5664f2d14d2e3f3f9886f55', + 'ca8c63df89509728a7b87e65232f1da751621ed4777993a079730472a0923957', 'src/lib/config/support.ts': 'a5b4c179db6f13ebf85081960e2f7c330c3e894666fbf8f0472edb1c5c818872', 'src/lib/contracts/local-records.ts': @@ -770,7 +770,7 @@ async function supportReportingAdmission(web) { 'src/lib/config/deployment-support.ts': 'c698313143943749bb457674825b648f6af29477f07cae960f3435c035cd6bd8', 'src/lib/config/relays.ts': - 'f54a4e91c6561cf02e5aebfb7b62a7913faa4416a5664f2d14d2e3f3f9886f55', + 'ca8c63df89509728a7b87e65232f1da751621ed4777993a079730472a0923957', 'src/lib/config/support.ts': 'a5b4c179db6f13ebf85081960e2f7c330c3e894666fbf8f0472edb1c5c818872', 'src/lib/contracts/local-records.ts': @@ -961,7 +961,7 @@ async function privateBlockingAdmission(web) { 'src/lib/config/deployment-relays.ts': '14443151ff819a11a4f9c71e4ff756d1fbdd7cf6c36242ac712c378e68583aa2', 'src/lib/config/relays.ts': - 'f54a4e91c6561cf02e5aebfb7b62a7913faa4416a5664f2d14d2e3f3f9886f55', + 'ca8c63df89509728a7b87e65232f1da751621ed4777993a079730472a0923957', 'src/lib/contracts/local-records.ts': '04ddd51339a439665a94d4a4de7b23c2257d4a673873a672d4427e9764d21f3f', 'src/lib/contracts/public-key.ts':