mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 15:17:30 +00:00
fix(security): block IPv6 transition addresses (NAT64/6to4/Teredo) in SSRF guard
An attacker-controlled DNS record pointing at a NAT64 (64:ff9b::/96), 6to4 (2002::/16), or Teredo (2001:0000::/32) address that embeds a private IPv4 (e.g. 64:ff9b::a9fe:a9fe = 169.254.169.254) bypassed the SSRF guard: none of the guard functions recognised these ranges, so on a host that routes the transition prefix the connection reached the embedded private target (cloud metadata / internal SSRF). Add a shared embeddedTransitionIpv4() detector and re-apply each guard's own blocklist to the extracted IPv4 in isAlwaysBlocked, isPrivateNetwork, isLinkLocal (ssrfGuard.ts) and isBlockedIp (egress-policy.ts). A transition address to a public IPv4 stays allowed so legitimate IPv6-only egress is unaffected. egress-policy.ts keeps the detector inline to preserve its dependency-free contract for the isolated plugin subprocess.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { expandIpv6, embeddedTransitionIpv4 } from '../../../src/utils/ipv6';
|
||||
|
||||
describe('expandIpv6', () => {
|
||||
it.each([
|
||||
['::1', [0, 0, 0, 0, 0, 0, 0, 1]],
|
||||
['2606:4700::1111', [0x2606, 0x4700, 0, 0, 0, 0, 0, 0x1111]],
|
||||
['fe80::1', [0xfe80, 0, 0, 0, 0, 0, 0, 1]],
|
||||
// Dotted-quad tail (IPv4-mapped) is normalised into hextets.
|
||||
['::ffff:169.254.169.254', [0, 0, 0, 0, 0, 0xffff, 0xa9fe, 0xa9fe]],
|
||||
['64:ff9b::a9fe:a9fe', [0x0064, 0xff9b, 0, 0, 0, 0, 0xa9fe, 0xa9fe]],
|
||||
])('expands %s', (ip, expected) => {
|
||||
expect(expandIpv6(ip)).toEqual(expected);
|
||||
});
|
||||
|
||||
it.each(['8.8.8.8', 'not-an-ip', '', '::ffff:999.1.1.1', '1::2::3'])(
|
||||
'returns null for non-IPv6 / invalid %s',
|
||||
(ip) => {
|
||||
expect(expandIpv6(ip)).toBeNull();
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe('embeddedTransitionIpv4', () => {
|
||||
it.each([
|
||||
// NAT64 well-known 64:ff9b::/96
|
||||
['64:ff9b::a9fe:a9fe', '169.254.169.254'],
|
||||
['64:ff9b::7f00:1', '127.0.0.1'],
|
||||
['64:ff9b::a00:1', '10.0.0.1'],
|
||||
['64:ff9b::808:808', '8.8.8.8'],
|
||||
['[64:ff9b::a9fe:a9fe]', '169.254.169.254'], // bracketed literal
|
||||
// 6to4 2002::/16
|
||||
['2002:7f00:1::', '127.0.0.1'],
|
||||
['2002:a9fe:a9fe::', '169.254.169.254'],
|
||||
['2002:808:808::', '8.8.8.8'],
|
||||
// Teredo 2001:0000::/32 — client IPv4 is XOR 0xffffffff of the last 32 bits.
|
||||
['2001::5601:5601', '169.254.169.254'],
|
||||
])('extracts %s -> %s', (ip, embedded) => {
|
||||
expect(embeddedTransitionIpv4(ip)).toBe(embedded);
|
||||
});
|
||||
|
||||
it.each([
|
||||
'8.8.8.8', // plain IPv4 — no colon
|
||||
'169.254.169.254',
|
||||
'::ffff:169.254.169.254', // IPv4-mapped, handled by other checks not this one
|
||||
'::a9fe:a9fe', // IPv4-compatible
|
||||
'2606:4700::1111', // ordinary public IPv6
|
||||
'fe80::1', // link-local, not a transition prefix
|
||||
'64:ff9b:1::a9fe:a9fe', // NAT64 local-use /48 prefix — deliberately NOT matched
|
||||
'garbage',
|
||||
])('returns null for non-transition %s', (ip) => {
|
||||
expect(embeddedTransitionIpv4(ip)).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -145,6 +145,40 @@ describe('checkSsrf', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// SEC-005 — IPv6 transition addresses (NAT64/6to4/Teredo) must not tunnel past
|
||||
// the guard by embedding a blocked IPv4 target.
|
||||
describe('IPv6 transition addresses (NAT64/6to4/Teredo)', () => {
|
||||
it.each([
|
||||
['NAT64 → metadata', '64:ff9b::a9fe:a9fe'], // 169.254.169.254
|
||||
['NAT64 → loopback', '64:ff9b::7f00:1'], // 127.0.0.1
|
||||
['6to4 → metadata', '2002:a9fe:a9fe::'],
|
||||
['6to4 → loopback', '2002:7f00:1::'],
|
||||
['Teredo → metadata', '2001::5601:5601'],
|
||||
])('blocks %s (%s)', async (_label, ip) => {
|
||||
mockIp(ip);
|
||||
const result = await checkSsrf('http://attacker.example');
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.isPrivate).toBe(true);
|
||||
});
|
||||
|
||||
it('blocks a NAT64 address embedding an RFC-1918 target (10.0.0.1)', async () => {
|
||||
mockIp('64:ff9b::a00:1');
|
||||
const result = await checkSsrf('http://attacker.example');
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.isPrivate).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['NAT64 → public 8.8.8.8', '64:ff9b::808:808'],
|
||||
['6to4 → public 8.8.8.8', '2002:808:808::'],
|
||||
])('still allows %s — legitimate IPv6→IPv4 egress', async (_label, ip) => {
|
||||
mockIp(ip);
|
||||
const result = await checkSsrf('http://cdn.example');
|
||||
expect(result.allowed).toBe(true);
|
||||
expect(result.isPrivate).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('internal hostname suffixes', () => {
|
||||
it('blocks .local domains', async () => {
|
||||
const result = await checkSsrf('http://myserver.local');
|
||||
@@ -490,6 +524,28 @@ describe('safeFetchLlm', () => {
|
||||
await expect(safeFetchLlm('http://model.example/chat')).rejects.toThrow(SsrfBlockedError);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['NAT64 → metadata', '64:ff9b::a9fe:a9fe'],
|
||||
['6to4 → metadata', '2002:a9fe:a9fe::'],
|
||||
['Teredo → metadata', '2001::5601:5601'],
|
||||
])('blocks an IPv6 transition address to the metadata IP: %s', async (_label, ip) => {
|
||||
mockIp(ip);
|
||||
const mockFetch = vi.fn();
|
||||
vi.stubGlobal('fetch', mockFetch);
|
||||
await expect(safeFetchLlm('http://ollama.evil.example/chat')).rejects.toThrow(SsrfBlockedError);
|
||||
expect(mockFetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('still allows a NAT64 address to a LAN model server (192.168.1.50)', async () => {
|
||||
// The LLM guard permits private/LAN targets — only link-local/metadata is blocked,
|
||||
// so a NAT64 spelling of a LAN model host must keep working.
|
||||
mockIp('64:ff9b::c0a8:132'); // 192.168.1.50
|
||||
const mockFetch = vi.fn().mockResolvedValue({ ok: true } as Response);
|
||||
vi.stubGlobal('fetch', mockFetch);
|
||||
await safeFetchLlm('http://lan-model.example/chat');
|
||||
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('stops following after the redirect cap', async () => {
|
||||
mockIp('203.0.113.10');
|
||||
const mockFetch = vi
|
||||
|
||||
Reference in New Issue
Block a user