LibreChat/api/server/services/Files/Audio/speechSSRF.spec.js
Dustin Healy 96404bfc73
🛡️ fix: SSRF-Guard Speech (STT/TTS) and OCR Outbound Requests at Connect Time (#14560)
* fix: SSRF-guard speech (STT/TTS) and OCR outbound requests at connect time

Speech (STT/TTS) and OCR issued outbound HTTP to operator-provided target URLs
with only proxy config attached, so a target that resolves to a private, loopback,
link-local, or cloud-metadata address was reachable by the server. This is the same
class already guarded for the custom models fetch, Actions, avatar, MCP, and the
OpenAI/Anthropic endpoint clients.

Add one helper applySSRFSafeAgentIfDirect(config, url, allowedAddresses) that rejects
non-http(s) or unparseable target URLs, sets maxRedirects to 0 so a redirect cannot
bypass the connect-time check, and attaches createSSRFSafeAgents when no proxy or agent
is already set (proxy precedence preserved). Wire it into the six speech/OCR call sites
and thread each section's allowedAddresses exemption from pr-01.

Scope is private/internal SSRF only. It does not restrict forwarding a credential to a
public host, which is a separate egress-allowlist concern. Absent an allowedAddresses
entry, private targets now fail closed, matching endpoints, actions, and MCP. Document
the new fields in librechat.example.yaml with the operator warning that allowedAddresses
hostnames are trusted before the private-IP check.

* fix: block literal private-IP hosts and thread STT exemptions through generic uploads

applySSRFSafeAgentIfDirect only attached the DNS-lookup agents, but Node
skips the custom lookup for IP-literal hosts, so a literal private IP such
as http://127.0.0.1 connected unchecked. Reject literal private IPs
synchronously in the helper, reusing the same allowedAddresses exemption
logic as the lookup path.

The generic audio-upload path did not forward the section-level
allowedAddresses to sttRequest, so a private STT endpoint permitted via
speech.stt.allowedAddresses failed with ESSRF outside the speech route.
Thread the exemption through files/audio.ts and widen the STTService type.

* fix: derive effective SSRF port for literal IPs and validate OCR target before opening its stream

The literal-IP precheck normalized an empty URL.port to '', so
allowedAddresses exemptions on a default port (127.0.0.1:80, [::1]:443)
never matched. Derive 80/443 from the scheme when the port is omitted.

uploadDocumentToMistral opened the upload file stream before the SSRF
check, so a blocked or malformed target threw with the descriptor still
open. Run the proxy/SSRF setup before fs.createReadStream.

Reword the librechat.example.yaml allowedAddresses guidance to prefer a
private IP literal over a hostname, and reconcile the speech/OCR SSRF
specs to assert the synchronous literal-IP block instead of driving the
connect-time lookup that Node skips for IP literals.

* fix: block literal private IPs before the proxy return, destroy OCR stream on failure, canonicalize IPv6 exemptions

Move the literal-IP check above the proxy/agent early return so a literal
private IP is rejected even when a proxy is configured; document that a
forward proxy must be SSRF-enforcing.

Wrap the Mistral upload post in try/finally and destroy the file stream,
so an async connect-time block does not leak the descriptor.

Canonicalize IP literals in normalizeAddressCandidate through the same URL
serialization targets use, so IPv4-mapped and expanded IPv6 exemptions match.
2026-08-06 09:06:04 -04:00

91 lines
3 KiB
JavaScript

jest.mock('axios');
jest.mock('@librechat/data-schemas', () => ({
logger: { warn: jest.fn(), error: jest.fn(), debug: jest.fn() },
}));
jest.mock('~/server/services/Config', () => ({ getAppConfig: jest.fn() }));
jest.mock('./streamAudio', () => ({
getRandomVoiceId: jest.fn(),
createChunkProcessor: jest.fn(),
splitTextIntoChunks: jest.fn(),
}));
const axios = require('axios');
const { STTService } = require('./STTService');
const { textToSpeech } = require('./TTSService');
afterEach(() => {
jest.restoreAllMocks();
axios.post.mockReset();
});
describe('STT sttRequest SSRF guard (real agent)', () => {
const audioFile = { originalname: 'a.wav', mimetype: 'audio/wav', size: 1 };
const audioBuffer = Buffer.from('audio');
const provider = { url: 'http://10.0.0.5:8080', apiKey: 'sk', model: 'whisper-1' };
it('blocks a private-IP provider url with ESSRF before any request goes out', async () => {
const service = new STTService();
await expect(
service.sttRequest('openai', provider, { audioBuffer, audioFile, language: '' }),
).rejects.toMatchObject({ code: 'ESSRF' });
expect(axios.post).not.toHaveBeenCalled();
});
it('exempts a host:port in the section allowedAddresses and sets maxRedirects 0 with agents', async () => {
axios.post.mockResolvedValue({ status: 200, data: { text: 'ok' } });
const service = new STTService();
await service.sttRequest('openai', provider, { audioBuffer, audioFile, language: '' }, [
'10.0.0.5:8080',
]);
const options = axios.post.mock.calls[0][2];
expect(options.maxRedirects).toBe(0);
expect(options.httpAgent).toBeDefined();
expect(options.httpsAgent).toBeDefined();
});
});
describe('TTS textToSpeech SSRF guard (real agent)', () => {
function buildReqRes(allowedAddresses) {
const req = {
body: { input: 'hi', voice: 'v1' },
user: { id: 'u1' },
config: {
speech: {
tts: {
...(allowedAddresses ? { allowedAddresses } : {}),
localai: { url: 'http://10.0.0.5:8080', apiKey: 'sk', voices: ['v1'] },
},
},
},
};
const res = {
setHeader: jest.fn(),
headersSent: false,
status: jest.fn(() => ({ end: jest.fn(), send: jest.fn() })),
end: jest.fn(),
};
return { req, res };
}
it('blocks a private-IP provider url and never issues the outbound request', async () => {
const { req, res } = buildReqRes();
await textToSpeech(req, res);
expect(axios.post).not.toHaveBeenCalled();
});
it('exempts a host:port in the section allowedAddresses and sets maxRedirects 0 with agents', async () => {
axios.post.mockResolvedValue({ status: 200, data: { pipe: jest.fn(), on: jest.fn() } });
const { req, res } = buildReqRes(['10.0.0.5:8080']);
await textToSpeech(req, res);
const options = axios.post.mock.calls[0][2];
expect(options.maxRedirects).toBe(0);
expect(options.httpAgent).toBeDefined();
expect(options.httpsAgent).toBeDefined();
});
});