Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
harden endpointGuard against IPv4-mapped IPv6 metadata bypass (review)
Adam Eivyd1300ed15213
diff --git a/server/lib/aiToolkit/internal/endpointGuard.js b/server/lib/aiToolkit/internal/endpointGuard.jsindex 2aa59203af..d8f5e86010 100644--- a/server/lib/aiToolkit/internal/endpointGuard.js+++ b/server/lib/aiToolkit/internal/endpointGuard.js@@ -66,10 +66,26 @@ const ALLOWED_PROVIDER_HOSTS = new Set([const IPV4_RE = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;-/** Strip an IPv4-mapped IPv6 prefix (`::ffff:127.0.0.1`) down to the v4 literal. */+/**+ * Collapse an IPv4-mapped IPv6 address down to its dotted-decimal v4 literal so+ * the metadata / private-range checks below see the real target. Handles both+ * the dotted form (`::ffff:169.254.169.254`) and the hex-compressed form+ * (`::ffff:a9fe:a9fe`) that WHATWG URL emits — otherwise a mapped-v6 metadata+ * address would bypass the "metadata always blocked" invariant. (Bare+ * decimal/hex/octal IPv4 like `0xa9fea9fe` is already canonicalized to+ * dotted-decimal by `new URL()`, so it needs no handling here.)+ */function unwrapMappedV4(host) {- const m = /^::ffff:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/i.exec(host);- return m ? m[1] : host;+ const h = host.toLowerCase();+ const dotted = /^::ffff:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/.exec(h);+ if (dotted) return dotted[1];+ const hex = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(h);+ if (hex) {+ const hi = parseInt(hex[1], 16);+ const lo = parseInt(hex[2], 16);+ return `${(hi >> 8) & 255}.${hi & 255}.${(lo >> 8) & 255}.${lo & 255}`;+ }+ return host;}function isLoopbackOrPrivateV4(host) {diff --git a/server/lib/aiToolkit/internal/endpointGuard.test.js b/server/lib/aiToolkit/internal/endpointGuard.test.jsindex b454d1dc0b..a1ffa8aff5 100644--- a/server/lib/aiToolkit/internal/endpointGuard.test.js+++ b/server/lib/aiToolkit/internal/endpointGuard.test.js@@ -37,6 +37,9 @@ describe('endpointGuard — evaluateSecretEndpoint', () => {expect(evaluateSecretEndpoint('http://[fd12:3456::1]:11434/v1').allowed).toBe(true);expect(evaluateSecretEndpoint('http://[fe80::1]:11434/v1').allowed).toBe(true);});+ it('allows IPv4-mapped IPv6 loopback (::ffff:127.0.0.1)', () => {+ expect(evaluateSecretEndpoint('http://[::ffff:127.0.0.1]:1234/v1').allowed).toBe(true);+ });});describe('cloud-metadata endpoints are ALWAYS blocked', () => {@@ -58,6 +61,14 @@ describe('endpointGuard — evaluateSecretEndpoint', () => {it('blocks AWS IPv6 IMDS fd00:ec2::254', () => {expect(evaluateSecretEndpoint('http://[fd00:ec2::254]/', { allowCustomEndpoint: true }).allowed).toBe(false);});+ it('blocks metadata via decimal/hex IPv4 (URL canonicalizes to 169.254.169.254)', () => {+ expect(evaluateSecretEndpoint('http://2852039166/', { allowCustomEndpoint: true }).allowed).toBe(false);+ expect(evaluateSecretEndpoint('http://0xa9fea9fe/', { allowCustomEndpoint: true }).allowed).toBe(false);+ });+ it('blocks metadata via IPv4-mapped IPv6 (both dotted and hex-compressed forms)', () => {+ expect(evaluateSecretEndpoint('http://[::ffff:169.254.169.254]/', { allowCustomEndpoint: true }).allowed).toBe(false);+ expect(evaluateSecretEndpoint('http://[::ffff:a9fe:a9fe]/', { allowCustomEndpoint: true }).allowed).toBe(false);+ });});describe('known paid-LLM provider hosts are allowed', () => {