Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(ssrf): prove a guarded fetch dials the vetted address
Yudhi Armyndharisa1bb1c5b7c6f
diff --git a/src/common/security/ssrf-guard.spec.ts b/src/common/security/ssrf-guard.spec.tsindex a0ebf2a21..14b1183c2 100644--- a/src/common/security/ssrf-guard.spec.ts+++ b/src/common/security/ssrf-guard.spec.ts@@ -264,8 +264,8 @@ describe('withSafeFetch (guarded + pinned fetch)', () => {it('pins the connection by passing a dispatcher to fetch for a hostname target', async () => {// The security property: for a DNS hostname the connection MUST go through a pinned dispatcher,- // else fetch re-resolves DNS independently and the rebind window reopens. Removing the pin- // (dispatcher = undefined) makes this fail.+ // else fetch re-resolves DNS independently and the rebind window reopens. Dropping the dispatcher+ // fails here; the real-socket test below fails if it stops dialling the vetted address.(dnsPromises.lookup as jest.Mock).mockResolvedValueOnce([{ address: '93.184.216.34', family: 4 }]);(undiciFetch as jest.Mock).mockResolvedValue({ status: 200, type: 'basic' });const use = jest.fn(() => 'used');@@ -836,6 +836,28 @@ describe('withSafeFetch followRedirects validates every hop over real sockets',}});+ it('connects a vetted hostname to the address it was vetted at, not a fresh resolution', async () => {+ const server = createServer((_req, res) => {+ res.writeHead(200, { 'content-type': 'text/plain' });+ res.end('PINNED');+ });+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));+ const port = (server.address() as AddressInfo).port;+ // `.invalid` never resolves through real DNS, so the request can only reach the server through+ // the address the guard vetted and pinned into the dispatcher.+ process.env.SSRF_ALLOWED_HOSTS = 'pinned.invalid';+ (dnsPromises.lookup as jest.Mock).mockResolvedValueOnce([{ address: '127.0.0.1', family: 4 }]);++ try {+ const body = await withSafeFetch(`http://pinned.invalid:${port}/`, {}, async response => await response.text(), {+ guard: true,+ });+ expect(body).toBe('PINNED');+ } finally {+ await new Promise<void>(resolve => server.close(() => resolve()));+ }+ });+it('refuses a redirect chain longer than the hop cap', async () => {const server = createServer((_req, res) => {const port = (server.address() as AddressInfo).port;