Files
SillyTavern/tests/private-request-filter.test.js
T
ed563cb7d8 fix: reject localhost and .localhost domains in /api/search/visit to prevent SSRF bypass (#5749)
* fix: reject localhost and .localhost domains in /api/search/visit to prevent SSRF bypass

The hostname check in /api/search/visit only validated IP addresses using
ipRegex, but did not block 'localhost' or subdomains ending in '.localhost'.
This allowed SSRF bypasses to internal services via localhost URLs.

Changes:
- Add explicit rejection of 'localhost' hostname
- Add rejection of any hostname ending in '.localhost' (RFC 6761 reserved)

Closes GHSA-wm7j-m6jm-8797

* fix: enforce SSRF boundary for /api/search/visit at connection time

The URL string checks only guard the literal input. Hostnames resolving
to private addresses, redirects into internal services, and DNS
rebinding all bypassed them. The visit endpoint now routes its outbound
request through a strict PrivateRequestAgent even when the private
request filter is disabled: the resolved IP is validated at connection
time on every hop and the connection is pinned to the validated IP.
When the private request filter is enabled its configured whitelist
keeps applying, and an enabled request proxy keeps owning routing.
Also rejects bracketed IPv6 literals, which kept their brackets and
slipped past the hostname check.

Fixes GHSA-wm7j-m6jm-8797

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Wolfsblvt <wolfsblvt@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-16 22:56:44 +02:00

200 lines
8.0 KiB
JavaScript

import { describe, test, expect, jest, beforeAll, beforeEach, afterAll } from '@jest/globals';
const mockNetConnect = jest.fn(() => ({ type: 'net-socket' }));
const mockTlsConnect = jest.fn(() => ({ type: 'tls-socket' }));
const mockLookup = jest.fn();
jest.unstable_mockModule('node:net', () => ({
default: { connect: mockNetConnect },
}));
jest.unstable_mockModule('node:tls', () => ({
default: { connect: mockTlsConnect },
}));
jest.unstable_mockModule('node:dns', () => ({
default: { promises: { lookup: mockLookup } },
}));
jest.unstable_mockModule('../src/util.js', () => ({
color: {
red: text => text,
green: text => text,
blue: text => text,
yellow: text => text,
},
}));
jest.unstable_mockModule('../src/express-common.js', () => ({
filterValidIpPatterns: patterns => patterns,
}));
/** @type {import('../src/private-request-filter.js').default} */
let initPrivateRequestFilter;
/** @type {import('../src/private-request-filter.js').getUntrustedRequestAgent} */
let getUntrustedRequestAgent;
/** @type {import('node:http').default} */
let http;
/** @type {import('node:https').default} */
let https;
let originalHttpGlobalAgent;
let originalHttpsGlobalAgent;
beforeAll(async () => {
({ default: initPrivateRequestFilter, getUntrustedRequestAgent } = await import('../src/private-request-filter.js'));
({ default: http } = await import('node:http'));
({ default: https } = await import('node:https'));
originalHttpGlobalAgent = http.globalAgent;
originalHttpsGlobalAgent = https.globalAgent;
});
beforeEach(() => {
mockNetConnect.mockClear();
mockTlsConnect.mockClear();
mockLookup.mockReset();
http.globalAgent = originalHttpGlobalAgent;
https.globalAgent = originalHttpsGlobalAgent;
});
afterAll(() => {
http.globalAgent = originalHttpGlobalAgent;
https.globalAgent = originalHttpsGlobalAgent;
});
function initAgent({ privateAddressWhitelist = [], allowUnresolvedHosts = false, enabled = true, requestProxyEnabled = false } = {}) {
initPrivateRequestFilter({
listen: false,
enabled,
privateAddressWhitelist,
logBlocked: false,
logAllowed: false,
allowUnresolvedHosts,
requestProxyEnabled,
});
return http.globalAgent;
}
describe('private request filter', () => {
test('allows direct private IP requests only when whitelisted', async () => {
const agent = initAgent({ privateAddressWhitelist: ['127.0.0.0/8'] });
await agent.connect({}, { host: '127.0.0.1', secureEndpoint: false });
expect(mockNetConnect).toHaveBeenCalledWith(expect.objectContaining({ host: '127.0.0.1' }));
const blockedAgent = initAgent({ privateAddressWhitelist: [] });
await expect(blockedAgent.connect({}, { host: '127.0.0.1', secureEndpoint: false }))
.rejects
.toThrow('Blocked request to private IP address: 127.0.0.1');
});
test('resolves hostnames and blocks when DNS returns private IP', async () => {
mockLookup.mockResolvedValue({ address: '192.168.1.8' });
const agent = initAgent();
await expect(agent.connect({}, { host: 'example.com', secureEndpoint: false }))
.rejects
.toThrow('Blocked request to private IP address: 192.168.1.8');
expect(mockNetConnect).not.toHaveBeenCalled();
});
test('connects to resolved public IP to avoid hostname re-resolution', async () => {
mockLookup.mockResolvedValue({ address: '93.184.216.34' });
const agent = initAgent();
await agent.connect({}, { host: 'example.com', secureEndpoint: false });
expect(mockLookup).toHaveBeenCalledWith('example.com');
expect(mockNetConnect).toHaveBeenCalledWith(expect.objectContaining({ host: '93.184.216.34' }));
});
test('handles unresolved hosts according to allowUnresolvedHosts setting', async () => {
mockLookup.mockRejectedValue(new Error('lookup failed'));
const blockedAgent = initAgent({ allowUnresolvedHosts: false });
await expect(blockedAgent.connect({}, { host: 'missing-host.local', secureEndpoint: false }))
.rejects
.toThrow('Unable to resolve host: missing-host.local. Set privateAddressWhitelist.allowUnresolvedHosts to true to bypass this check.');
expect(mockNetConnect).not.toHaveBeenCalled();
const allowedAgent = initAgent({ allowUnresolvedHosts: true });
await allowedAgent.connect({}, { host: 'missing-host.local', secureEndpoint: false });
expect(mockNetConnect).toHaveBeenCalledWith(expect.objectContaining({ host: 'missing-host.local' }));
});
test('uses tls.connect for secure endpoints', async () => {
mockLookup.mockResolvedValue({ address: '93.184.216.34' });
const agent = initAgent();
await agent.connect({}, { host: 'example.com', secureEndpoint: true });
expect(mockLookup).toHaveBeenCalledWith('example.com');
expect(mockTlsConnect).toHaveBeenCalledWith(expect.objectContaining({ host: '93.184.216.34' }));
expect(mockNetConnect).not.toHaveBeenCalled();
});
});
describe('untrusted request agent', () => {
test('returns undefined when the private request filter is enabled', () => {
initAgent({ enabled: true });
expect(getUntrustedRequestAgent()).toBeUndefined();
});
test('returns undefined when a request proxy owns outbound routing', () => {
initAgent({ enabled: false, requestProxyEnabled: true });
expect(getUntrustedRequestAgent()).toBeUndefined();
});
test('returns the same strict agent instance when the filter is disabled', () => {
initAgent({ enabled: false });
const agent = getUntrustedRequestAgent();
expect(agent).toBeDefined();
expect(getUntrustedRequestAgent()).toBe(agent);
});
test('blocks private addresses even when the configured whitelist allows them', async () => {
initAgent({ enabled: false, privateAddressWhitelist: ['127.0.0.0/8', '::1/128'] });
const agent = getUntrustedRequestAgent();
await expect(agent.connect({}, { host: '127.0.0.1', secureEndpoint: false }))
.rejects
.toThrow('Blocked request to private IP address: 127.0.0.1');
await expect(agent.connect({}, { host: '::1', secureEndpoint: false }))
.rejects
.toThrow('Blocked request to private IP address: ::1');
expect(mockNetConnect).not.toHaveBeenCalled();
});
test('blocks hostnames that resolve to private addresses', async () => {
initAgent({ enabled: false });
mockLookup.mockResolvedValue({ address: '192.168.1.8' });
const agent = getUntrustedRequestAgent();
await expect(agent.connect({}, { host: 'internal.example.com', secureEndpoint: false }))
.rejects
.toThrow('Blocked request to private IP address: 192.168.1.8');
expect(mockNetConnect).not.toHaveBeenCalled();
});
test('blocks unresolvable hostnames', async () => {
initAgent({ enabled: false });
mockLookup.mockRejectedValue(new Error('lookup failed'));
const agent = getUntrustedRequestAgent();
await expect(agent.connect({}, { host: 'missing-host.local', secureEndpoint: false }))
.rejects
.toThrow('Unable to resolve host: missing-host.local');
expect(mockNetConnect).not.toHaveBeenCalled();
});
test('connects public hostnames to their resolved IP for both http and https', async () => {
initAgent({ enabled: false });
mockLookup.mockResolvedValue({ address: '93.184.216.34' });
const agent = getUntrustedRequestAgent();
await agent.connect({}, { host: 'example.com', secureEndpoint: false });
expect(mockNetConnect).toHaveBeenCalledWith(expect.objectContaining({ host: '93.184.216.34' }));
await agent.connect({}, { host: 'example.com', secureEndpoint: true });
expect(mockTlsConnect).toHaveBeenCalledWith(expect.objectContaining({ host: '93.184.216.34' }));
});
});