Files
SillyTavern/tests/search-visit.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

98 lines
3.5 KiB
JavaScript

import { describe, test, expect, jest, beforeAll, afterAll, beforeEach } from '@jest/globals';
// secrets.js reads this config value at module load. The env var override takes
// precedence over config.yaml, so the search router imports without a config file.
process.env.SILLYTAVERN_ALLOWKEYSEXPOSURE = 'false';
const fetchMock = jest.fn();
jest.unstable_mockModule('node-fetch', () => ({
default: fetchMock,
}));
/** @type {import('express').Router} */
let router;
/** @type {import('../src/private-request-filter.js').getUntrustedRequestAgent} */
let getUntrustedRequestAgent;
/** @type {import('node:http').Server} */
let server;
let baseUrl;
beforeAll(async () => {
({ router } = await import('../src/endpoints/search.js'));
({ getUntrustedRequestAgent } = await import('../src/private-request-filter.js'));
const express = (await import('express')).default;
const app = express();
app.use(express.json());
app.use('/api/search', router);
server = app.listen(0, '127.0.0.1');
await new Promise(resolve => server.once('listening', resolve));
baseUrl = `http://127.0.0.1:${server.address().port}`;
});
afterAll(() => new Promise(resolve => server.close(resolve)));
beforeEach(() => {
fetchMock.mockReset();
fetchMock.mockResolvedValue({
ok: true,
headers: { get: () => 'text/html' },
text: async () => '<html>ok</html>',
});
});
function visit(url) {
return fetch(`${baseUrl}/api/search/visit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url }),
});
}
describe('POST /api/search/visit URL validation', () => {
const rejectedUrls = [
['missing url', undefined],
['empty url', ''],
['relative url', '/etc/passwd'],
['ftp protocol', 'ftp://example.com/'],
['file protocol', 'file:///etc/passwd'],
['non-standard port', 'http://example.com:8080/'],
['literal IPv4 loopback', 'http://127.0.0.1/'],
['literal IPv4 private', 'http://10.0.0.1/'],
['literal IPv4 metadata service', 'http://169.254.169.254/'],
['hex IPv4 form', 'http://0x7f000001/'],
['octal IPv4 form', 'http://0177.0.0.1/'],
['decimal IPv4 form', 'http://2130706433/'],
['bracketed IPv6 loopback', 'http://[::1]/'],
['bracketed IPv6 link-local', 'http://[fe80::1]/'],
['localhost', 'http://localhost/'],
['localhost subdomain', 'http://sub.localhost/'],
['https localhost', 'https://localhost/'],
];
for (const [label, url] of rejectedUrls) {
test(`rejects ${label} with 400 before any request is made`, async () => {
const response = await visit(url);
expect(response.status).toBe(400);
expect(fetchMock).not.toHaveBeenCalled();
});
}
const allowedUrls = [
['public http url', 'http://example.com/'],
['public https url', 'https://example.com/some/page?query=1'],
];
for (const [label, url] of allowedUrls) {
test(`passes ${label} to fetch through the untrusted request agent`, async () => {
const response = await visit(url);
expect(response.status).toBe(200);
expect(await response.text()).toBe('<html>ok</html>');
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(url, expect.objectContaining({
agent: getUntrustedRequestAgent(),
}));
expect(getUntrustedRequestAgent()).toBeDefined();
});
}
});