mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-16 13:00:55 +00:00
🥸 fix: Block Private IPv4-Translated IPv6 Targets (#18002)
* 🥸 fix: Block Private IPv4-Translated IPv6 Targets
* fix: Accept Translated Private Address Exemptions
---------
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
ea8c133c0b
commit
222b51cf96
5 changed files with 210 additions and 8 deletions
|
|
@ -77,6 +77,18 @@ describe('createSSRFSafeAgents', () => {
|
|||
expect(internal.createConnection).toBeInstanceOf(Function);
|
||||
});
|
||||
|
||||
it('blocks a translated private literal before dialing a redirect target', () => {
|
||||
const dial = jest.spyOn(httpAgentPrototype, 'createConnection').mockReturnValue({});
|
||||
const agents = createSSRFSafeAgents(null, null, { blockLiteralHosts: true });
|
||||
const internal = agents.httpAgent as unknown as {
|
||||
createConnection: (options: Record<string, unknown>) => unknown;
|
||||
};
|
||||
expect(() => internal.createConnection({ host: '::ffff:0:a00:1', port: 80 })).toThrow(
|
||||
expect.objectContaining({ code: 'ESSRF' }),
|
||||
);
|
||||
expect(dial).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should scope allowedAddresses by the request port in the HTTP agent lookup', async () => {
|
||||
mockDnsResult('10.0.0.5', 4);
|
||||
let lookupError: NodeJS.ErrnoException | null = null;
|
||||
|
|
@ -126,6 +138,47 @@ describe('createSSRFSafeUndiciConnect', () => {
|
|||
expect(result.err!.code).toBe('ESSRF');
|
||||
});
|
||||
|
||||
it.each(['::ffff:0:a9fe:a9fe', '0:0:0:0:ffff:0:169.254.169.254'])(
|
||||
'blocks translated private DNS answer %s in single and all-address lookups',
|
||||
async (address) => {
|
||||
const connect = createSSRFSafeUndiciConnect();
|
||||
for (const all of [false, true]) {
|
||||
if (all) {
|
||||
mockDnsAllResult([
|
||||
{ address: '2606:4700:4700::1111', family: 6 },
|
||||
{ address, family: 6 },
|
||||
]);
|
||||
} else {
|
||||
mockDnsResult(address, 6);
|
||||
}
|
||||
const error = await new Promise<NodeJS.ErrnoException | null>((resolve) => {
|
||||
connect.lookup('translated.example.com', { all }, (err) => resolve(err));
|
||||
});
|
||||
expect(error?.code).toBe('ESSRF');
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it('allows a translated public DNS answer', async () => {
|
||||
mockDnsResult('::ffff:0:808:808', 6);
|
||||
const connect = createSSRFSafeUndiciConnect();
|
||||
const error = await new Promise<NodeJS.ErrnoException | null>((resolve) => {
|
||||
connect.lookup('public.example.com', {}, (err) => resolve(err));
|
||||
});
|
||||
expect(error).toBeNull();
|
||||
});
|
||||
|
||||
it('keeps translated DNS exemptions scoped to the connection port', async () => {
|
||||
mockDnsResult('0:0:0:0:ffff:0:10.0.0.1', 6);
|
||||
for (const port of ['8080', '8081']) {
|
||||
const connect = createSSRFSafeUndiciConnect(['[::ffff:0:a00:1]:8080'], port);
|
||||
const error = await new Promise<NodeJS.ErrnoException | null>((resolve) => {
|
||||
connect.lookup('translated.example.com', {}, (err) => resolve(err));
|
||||
});
|
||||
expect(error?.code ?? null).toBe(port === '8080' ? null : 'ESSRF');
|
||||
}
|
||||
});
|
||||
|
||||
it('lookup should allow public IPs', async () => {
|
||||
mockDnsResult('93.184.216.34', 4);
|
||||
const connect = createSSRFSafeUndiciConnect();
|
||||
|
|
@ -441,6 +494,18 @@ describe('applySSRFSafeAgentIfDirect', () => {
|
|||
expect(code).toBe('ESSRF');
|
||||
});
|
||||
|
||||
it.each(['::ffff:0:a00:1', '0:0:0:0:ffff:0:169.254.169.254'])(
|
||||
'blocks translated private literal %s even with a configured proxy',
|
||||
(address) => {
|
||||
expect(() =>
|
||||
applySSRFSafeAgentIfDirect(
|
||||
{ proxy: { host: '127.0.0.1', port: 8080 } },
|
||||
`http://[${address}]/`,
|
||||
),
|
||||
).toThrow(expect.objectContaining({ code: 'ESSRF' }));
|
||||
},
|
||||
);
|
||||
|
||||
it('exempts a literal private IP present in allowedAddresses', () => {
|
||||
const config: AxiosRequestConfig = {};
|
||||
applySSRFSafeAgentIfDirect(config, 'http://127.0.0.1:9000', ['127.0.0.1:9000']);
|
||||
|
|
|
|||
|
|
@ -416,6 +416,90 @@ describe('isPrivateIP - IPv4-mapped IPv6 hex-normalized form (CVE-style SSRF byp
|
|||
});
|
||||
});
|
||||
|
||||
describe('IPv4-translated IPv6 SSRF protection', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
const privateAddresses = [
|
||||
'::ffff:0:a00:1',
|
||||
'::ffff:0:ac10:1',
|
||||
'::ffff:0:c0a8:101',
|
||||
'::ffff:0:a9fe:a9fe',
|
||||
'::ffff:0:7f00:1',
|
||||
'::ffff:0:0:1',
|
||||
'::ffff:0:6440:1',
|
||||
'::ffff:0:c000:1',
|
||||
'::ffff:0:c612:1',
|
||||
'::ffff:0:e000:1',
|
||||
'::ffff:0:ffff:ffff',
|
||||
'::ffff:0:10.0.0.1',
|
||||
'0:0:0:0:ffff:0:a00:1',
|
||||
'0000:0000:0000:0000:FFFF:0000:0A00:0001',
|
||||
'0:0:0:0:ffff::a00:1',
|
||||
'0:0:0:0:ffff::',
|
||||
'[::FFFF:0:A00:1]',
|
||||
];
|
||||
|
||||
it.each(privateAddresses)(
|
||||
'blocks private translated address %s in every preflight guard',
|
||||
async (address) => {
|
||||
expect(isPrivateIP(address)).toBe(true);
|
||||
expect(isSSRFTarget(address)).toBe(true);
|
||||
expect(await resolveHostnameSSRF(address)).toBe(true);
|
||||
const host = address.replace(/^\[|\]$/g, '');
|
||||
const url = `http://[${host}]/`;
|
||||
expect(await isActionDomainAllowed(url)).toBe(false);
|
||||
expect(await isMCPDomainAllowed({ url })).toBe(false);
|
||||
expect(mockedLookup).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it.each(['fe80::1%lo', 'fe90::1%eth0', 'fd00::1%3', '::ffff:0:a00:1%lo'])(
|
||||
'blocks private scoped IPv6 address %s',
|
||||
(address) => {
|
||||
expect(isPrivateIP(address)).toBe(true);
|
||||
expect(isSSRFTarget(address)).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(['::ffff:0:a00:1', '0:0:0:0:ffff:0:10.0.0.1'])(
|
||||
'blocks raw translated DNS answer %s',
|
||||
async (address) => {
|
||||
mockedLookup.mockResolvedValue([{ address, family: 6 }] as never);
|
||||
expect(await resolveHostnameSSRF('translated.example.com')).toBe(true);
|
||||
expect(await isActionDomainAllowed('http://translated.example.com')).toBe(false);
|
||||
expect(await isMCPDomainAllowed({ url: 'http://translated.example.com' })).toBe(false);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(['::ffff:0:808:808', '0:0:0:0:ffff:0:8.8.8.8', '::ffff:1:a00:1'])(
|
||||
'allows public or non-translated address %s',
|
||||
async (address) => {
|
||||
expect(isPrivateIP(address)).toBe(false);
|
||||
const url = `http://[${address}]/`;
|
||||
expect(await isActionDomainAllowed(url)).toBe(true);
|
||||
expect(await isMCPDomainAllowed({ url })).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
it('keeps translated-address exemptions scoped to the configured port', async () => {
|
||||
const allowedAddresses = ['[::ffff:0:10.0.0.1]:8080'];
|
||||
expect(
|
||||
await isActionDomainAllowed('http://[::ffff:0:a00:1]:8080', null, allowedAddresses),
|
||||
).toBe(true);
|
||||
expect(
|
||||
await isActionDomainAllowed('http://[::ffff:0:a00:1]:8081', null, allowedAddresses),
|
||||
).toBe(false);
|
||||
expect(
|
||||
await isMCPDomainAllowed({ url: 'http://[::ffff:0:a00:1]:8080' }, null, allowedAddresses),
|
||||
).toBe(true);
|
||||
expect(
|
||||
await isMCPDomainAllowed({ url: 'http://[::ffff:0:a00:1]:8081' }, null, allowedAddresses),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isActionDomainAllowed - IPv4-mapped IPv6 hex SSRF bypass (end-to-end)', () => {
|
||||
beforeEach(() => {
|
||||
mockedLookup.mockResolvedValue([{ address: '93.184.216.34', family: 4 }] as never);
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@
|
|||
* 172.16/12, 192.0.0/24 (RFC 5736), 192.168/16, 198.18/15 (benchmarking),
|
||||
* 224/3 (multicast/reserved).
|
||||
* - IPv6: ::1, ::, fc00::/7 (unique-local), fe80::/10 (link-local).
|
||||
* - 4-in-6 mappings: ::ffff:A.B.C.D and the hex form ::ffff:HHHH:HHHH.
|
||||
* - 4-in-6 mappings: IPv4-mapped (::ffff:0:0/96), compatible (::/96), and
|
||||
* translated (::ffff:0:0:0/96), in dotted or hex form.
|
||||
* - Embedded private IPv4 in 6to4 (2002::/16), NAT64 (64:ff9b::/96), and
|
||||
* Teredo (2001::/32) addresses.
|
||||
*/
|
||||
|
|
@ -108,21 +109,36 @@ function hasPrivateEmbeddedIPv4(ipv6: string): boolean {
|
|||
|
||||
/**
|
||||
* Checks if an IP address belongs to a private, reserved, or link-local range.
|
||||
* Handles IPv4, IPv6, and IPv4-mapped IPv6 addresses (::ffff:A.B.C.D).
|
||||
* Handles IPv4, IPv6, and IPv4-embedded IPv6 addresses.
|
||||
*/
|
||||
export function isPrivateIP(ip: string): boolean {
|
||||
const normalized = ip
|
||||
let normalized = ip
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/^\[|\]$/g, '');
|
||||
|
||||
const mappedMatch = normalized.match(/^::ffff:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
|
||||
const mappedMatch = normalized.match(
|
||||
/^::ffff:(?:0:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/,
|
||||
);
|
||||
if (mappedMatch) {
|
||||
const [, a, b, c] = mappedMatch.map(Number);
|
||||
return isPrivateIPv4(a, b, c);
|
||||
}
|
||||
|
||||
const hexMappedMatch = normalized.match(/^(?:::ffff:|::)([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
|
||||
// DNS can return expanded or dotted-tail IPv6 instead of URL-normalized hosts.
|
||||
if (normalized.includes(':')) {
|
||||
try {
|
||||
// A zone ID selects an interface; classify the address independently of it.
|
||||
const zoneIndex = normalized.indexOf('%');
|
||||
const address = zoneIndex === -1 ? normalized : normalized.slice(0, zoneIndex);
|
||||
normalized = new URL(`http://[${address}]/`).hostname.slice(1, -1);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// IPv4-compatible, mapped, and RFC 2765 translated forms share the low 32 bits.
|
||||
const hexMappedMatch = normalized.match(/^(?:::ffff:(?:0:)?|::)([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
|
||||
if (hexMappedMatch) {
|
||||
const hi = parseInt(hexMappedMatch[1], 16);
|
||||
const lo = parseInt(hexMappedMatch[2], 16);
|
||||
|
|
|
|||
|
|
@ -2065,6 +2065,11 @@ describe('allowedAddressesSchema', () => {
|
|||
['[fd00::1]:8080', 'IPv6 unique-local with port'],
|
||||
['[fe80::1]:8080', 'IPv6 link-local with port'],
|
||||
['[::ffff:10.0.0.5]:8080', 'IPv4-mapped IPv6 of a private address'],
|
||||
['[::ffff:0:10.0.0.1]:8080', 'IPv4-translated dotted private address'],
|
||||
['[::ffff:0:a00:1]:8080', 'IPv4-translated hex private address'],
|
||||
['[::ffff:0:a9fe:a9fe]:80', 'IPv4-translated metadata address'],
|
||||
['[0:0:0:0:ffff:0:10.0.0.1]:8080', 'expanded IPv4-translated address'],
|
||||
['[0000:0000:0000:0000:FFFF:0000:0A00:0001]:8080', 'padded translated address'],
|
||||
['[64:ff9b::a00:1]:8080', 'NAT64 embedding private 10.0.0.1'],
|
||||
['[2002:a00:1::]:8080', '6to4 embedding private 10.0.0.1'],
|
||||
['[2001::ffff:f5ff:fffe]:8080', 'Teredo embedding a private address'],
|
||||
|
|
@ -2090,6 +2095,9 @@ describe('allowedAddressesSchema', () => {
|
|||
['ws://10.0.0.5', 'ws URL'],
|
||||
['10.0.0.0/24', 'CIDR range'],
|
||||
['[64:ff9b::808:808]:8080', 'NAT64 embedding public 8.8.8.8'],
|
||||
['[::ffff:0:808:808]:8080', 'translated public IPv4 in hex form'],
|
||||
['[::ffff:0:8.8.8.8]:8080', 'translated public IPv4 in dotted form'],
|
||||
['[::ffff:1:a00:1]:8080', 'address outside the translated prefix'],
|
||||
['[2002:808:808::]:8080', '6to4 embedding public 8.8.8.8'],
|
||||
['/path', 'leading slash / path'],
|
||||
['10.0.0.5/api', 'embedded path'],
|
||||
|
|
@ -2200,6 +2208,21 @@ describe('allowedAddressesSchema', () => {
|
|||
});
|
||||
|
||||
describe('integration with configSchema', () => {
|
||||
it.each(['actions', 'mcpSettings'] as const)(
|
||||
'accepts translated private-address exemptions on %s',
|
||||
(section) => {
|
||||
const allowedAddresses = ['[::ffff:0:10.0.0.1]:8080'];
|
||||
const result = configSchema.safeParse({
|
||||
version: '1.0',
|
||||
[section]: { allowedAddresses },
|
||||
});
|
||||
expect(result.success).toBe(true);
|
||||
if (result.success) {
|
||||
expect(result.data[section]?.allowedAddresses).toEqual(allowedAddresses);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it('accepts the field on endpoints', () => {
|
||||
const result = configSchema.safeParse({
|
||||
version: '1.0',
|
||||
|
|
|
|||
|
|
@ -340,6 +340,14 @@ function hasPrivateEmbeddedIPv4Literal(value: string): boolean {
|
|||
|
||||
function isPrivateIPv6Literal(value: string): boolean {
|
||||
if (!value.includes(':')) return false;
|
||||
// Keep canonicalization aligned with the runtime IP classifier.
|
||||
try {
|
||||
const zoneIndex = value.indexOf('%');
|
||||
const address = zoneIndex === -1 ? value : value.slice(0, zoneIndex);
|
||||
value = new URL(`http://[${address}]/`).hostname.slice(1, -1);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
if (value === '::1' || value === '::') return true;
|
||||
if (value.startsWith('fc') || value.startsWith('fd')) return true; // fc00::/7
|
||||
// fe80::/10 — first hextet 0xfe80–0xfebf
|
||||
|
|
@ -348,9 +356,15 @@ function isPrivateIPv6Literal(value: string): boolean {
|
|||
const hextet = parseInt(firstHextet, 16);
|
||||
if ((hextet & 0xffc0) === 0xfe80) return true;
|
||||
}
|
||||
// 4-in-6: ::ffff:A.B.C.D
|
||||
const mappedMatch = value.match(/^::ffff:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/);
|
||||
if (mappedMatch) return isPrivateIPv4Literal(mappedMatch[1]);
|
||||
// IPv4-compatible, mapped, and RFC 2765 translated forms.
|
||||
const mappedMatch = value.match(/^(?:::ffff:(?:0:)?|::)([0-9a-f]{1,4}):([0-9a-f]{1,4})$/);
|
||||
if (mappedMatch) {
|
||||
const hi = parseInt(mappedMatch[1], 16);
|
||||
const lo = parseInt(mappedMatch[2], 16);
|
||||
return isPrivateIPv4Literal(
|
||||
[(hi >> 8) & 0xff, hi & 0xff, (lo >> 8) & 0xff, lo & 0xff].join('.'),
|
||||
);
|
||||
}
|
||||
return hasPrivateEmbeddedIPv4Literal(value);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue