Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 604 lines 22 KB
Raw
1 import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2 import { isBlockedIp, assertSafeFetchTarget, ssrfSafeFetch } from '../../src/utils/ssrfGuard.node';
3 import { ComposioBlockedInternalUrlError } from '../../src/errors/SsrfErrors';
4
5 vi.mock('node:dns/promises', () => ({
6 lookup: vi.fn(),
7 }));
8
9 vi.mock('../../src/utils/pinnedDispatcher.node', () => ({
10 createPinnedDispatcher: vi.fn(() => Promise.resolve({ close: () => Promise.resolve() })),
11 hasCustomGlobalDispatcher: vi.fn(() => false),
12 pinnedHttpFetch: vi.fn(),
13 }));
14
15 import {
16 createPinnedDispatcher,
17 hasCustomGlobalDispatcher,
18 pinnedHttpFetch,
19 } from '../../src/utils/pinnedDispatcher.node';
20
21 const mockCreatePinnedDispatcher = vi.mocked(createPinnedDispatcher);
22 const mockHasCustomGlobalDispatcher = vi.mocked(hasCustomGlobalDispatcher);
23 const mockPinnedHttpFetch = vi.mocked(pinnedHttpFetch);
24
25 // eslint-disable-next-line no-restricted-imports
26 import { lookup } from 'node:dns/promises';
27
28 const mockLookup = vi.mocked(lookup);
29 const resolvesTo = (...addresses: string[]) =>
30 mockLookup.mockResolvedValue(
31 addresses.map(address => ({ address, family: address.includes(':') ? 6 : 4 })) as never
32 );
33
34 describe('isBlockedIp', () => {
35 it('blocks IPv4 loopback, private, and link-local ranges', () => {
36 for (const ip of [
37 '127.0.0.1',
38 '127.1.2.3',
39 '10.0.0.5',
40 '172.16.0.1',
41 '172.31.255.255',
42 '192.168.1.1',
43 '169.254.169.254', // cloud metadata
44 '100.64.0.1', // CGNAT
45 '0.0.0.0',
46 '224.0.0.1', // multicast
47 '233.252.0.1', // MCAST-TEST-NET
48 '192.88.99.1', // 6to4 relay anycast (deprecated)
49 ]) {
50 expect(isBlockedIp(ip), ip).toBe(true);
51 }
52 });
53
54 it('allows public IPv4 addresses', () => {
55 for (const ip of [
56 '8.8.8.8',
57 '1.1.1.1',
58 '93.184.216.34',
59 '172.15.0.1',
60 '172.32.0.1',
61 '223.255.255.255',
62 '192.88.100.1',
63 ]) {
64 expect(isBlockedIp(ip), ip).toBe(false);
65 }
66 });
67
68 it('blocks IPv6 loopback, ULA, link-local, and mapped/compat internal addresses', () => {
69 for (const ip of [
70 '::1',
71 '::',
72 'fc00::1',
73 'fd12:3456::1',
74 'fe80::1',
75 '::ffff:127.0.0.1',
76 '::ffff:169.254.169.254',
77 // IPv4-compatible ::/96 (deprecated) — the bypass Bugbot flagged
78 '::127.0.0.1',
79 '::169.254.169.254',
80 '::10.0.0.1',
81 // ...and their normalized hex forms (what dns.lookup / URL parsing yield)
82 '::7f00:1', // ::127.0.0.1
83 '::a9fe:a9fe', // ::169.254.169.254
84 ]) {
85 expect(isBlockedIp(ip), ip).toBe(true);
86 }
87 });
88
89 it('blocks the transition ranges that carry an arbitrary IPv4 address', () => {
90 // A public-looking literal that still names internal space. The Python
91 // guard rejects all of these through `ipaddress.is_global`.
92 for (const ip of [
93 '2002:7f00:1::', // 6to4 for 127.0.0.1
94 '2002:c0a8:1::', // 6to4 for 192.168.0.1
95 '2002:8080:8080::', // 6to4 for a public address — the range goes as a whole
96 '2001::7f00:1', // Teredo 2001::/32
97 '2001:2::1', // benchmarking
98 '2001:10::1', // ORCHID
99 '2001:db8::1', // documentation
100 '64:ff9b:1::7f00:1', // local-use NAT64 for 127.0.0.1
101 '100::1', // discard-only
102 'fec0::1', // site-local (deprecated)
103 ]) {
104 expect(isBlockedIp(ip), ip).toBe(true);
105 }
106 });
107
108 it('allows public IPv6 and public IPv4-mapped/compat addresses', () => {
109 for (const ip of [
110 '2606:4700:4700::1111',
111 '::ffff:8.8.8.8',
112 '::8.8.8.8',
113 '::808:808',
114 // Neighbours of the ranges above, so the list does not overreach.
115 '2001:4860:4860::8888',
116 '2a00:1450:4001:80f::200e',
117 ]) {
118 expect(isBlockedIp(ip), ip).toBe(false);
119 }
120 });
121
122 it('fails closed for non-IP strings', () => {
123 expect(isBlockedIp('not-an-ip')).toBe(true);
124 expect(isBlockedIp('')).toBe(true);
125 });
126 });
127
128 describe('assertSafeFetchTarget', () => {
129 beforeEach(() => mockLookup.mockReset());
130
131 it('rejects non-http(s) schemes', async () => {
132 await expect(assertSafeFetchTarget('file:///etc/passwd')).rejects.toBeInstanceOf(
133 ComposioBlockedInternalUrlError
134 );
135 await expect(assertSafeFetchTarget('ftp://example.com/x')).rejects.toBeInstanceOf(
136 ComposioBlockedInternalUrlError
137 );
138 });
139
140 it('rejects a host that resolves to an internal address', async () => {
141 resolvesTo('169.254.169.254');
142 await expect(assertSafeFetchTarget('http://metadata.internal/latest')).rejects.toBeInstanceOf(
143 ComposioBlockedInternalUrlError
144 );
145 });
146
147 it('rejects when ANY resolved address is internal (DNS pinning bypass)', async () => {
148 resolvesTo('93.184.216.34', '127.0.0.1');
149 await expect(assertSafeFetchTarget('http://evil.example/x')).rejects.toBeInstanceOf(
150 ComposioBlockedInternalUrlError
151 );
152 });
153
154 it('allows a host that resolves only to public addresses, returning the address to connect to', async () => {
155 resolvesTo('93.184.216.34');
156 await expect(assertSafeFetchTarget('https://example.com/file.pdf')).resolves.toEqual([
157 '93.184.216.34',
158 ]);
159 });
160
161 it('blocks an IPv6 literal loopback host', async () => {
162 resolvesTo('::1');
163 await expect(assertSafeFetchTarget('http://[::1]:8080/x')).rejects.toBeInstanceOf(
164 ComposioBlockedInternalUrlError
165 );
166 });
167 });
168
169 describe('ssrfSafeFetch', () => {
170 const mockFetch = vi.fn();
171
172 beforeEach(() => {
173 mockLookup.mockReset();
174 mockFetch.mockReset();
175 mockCreatePinnedDispatcher.mockClear();
176 mockHasCustomGlobalDispatcher.mockReturnValue(false);
177 mockPinnedHttpFetch.mockReset();
178 // Deterministic even on machines that carry the runtime env-proxy opt-in.
179 vi.stubEnv('NODE_USE_ENV_PROXY', '');
180 vi.stubGlobal('fetch', mockFetch);
181 });
182 afterEach(() => vi.unstubAllGlobals());
183
184 it('connects to the address it validated, rather than resolving the host again', async () => {
185 resolvesTo('93.184.216.34');
186 mockFetch.mockResolvedValue(new Response('data', { status: 200 }));
187
188 await ssrfSafeFetch('https://example.com/file.pdf');
189
190 // Without this the host is resolved twice — once to validate, once to
191 // connect — and a short-TTL record can answer those two lookups
192 // differently (DNS rebinding, issue #4151).
193 expect(mockCreatePinnedDispatcher).toHaveBeenCalledWith(['93.184.216.34']);
194 expect(mockFetch.mock.calls[0][1].dispatcher).toBeDefined();
195 });
196
197 it("re-pins each redirect hop to that hop's own validated address", async () => {
198 mockLookup
199 .mockResolvedValueOnce([{ address: '93.184.216.34', family: 4 }] as never)
200 .mockResolvedValueOnce([{ address: '151.101.1.140', family: 4 }] as never);
201 mockFetch
202 .mockResolvedValueOnce(
203 new Response(null, { status: 302, headers: { location: 'https://cdn.example.com/f' } })
204 )
205 .mockResolvedValueOnce(new Response('data', { status: 200 }));
206
207 await ssrfSafeFetch('https://example.com/file.pdf');
208
209 expect(mockCreatePinnedDispatcher.mock.calls).toEqual([
210 [['93.184.216.34']],
211 [['151.101.1.140']],
212 ]);
213 });
214
215 it('respects a caller-supplied dispatcher instead of pinning', async () => {
216 resolvesTo('93.184.216.34');
217 mockFetch.mockResolvedValue(new Response('data', { status: 200 }));
218 const callerDispatcher = { close: () => Promise.resolve() };
219
220 await ssrfSafeFetch('https://example.com/file.pdf', {
221 dispatcher: callerDispatcher,
222 } as RequestInit);
223
224 // An explicit dispatcher is a routing choice by the caller (e.g. a proxy);
225 // overriding it would dial the validated origin instead of that route.
226 expect(mockCreatePinnedDispatcher).not.toHaveBeenCalled();
227 expect(mockFetch.mock.calls[0][1].dispatcher).toBe(callerDispatcher);
228 });
229
230 it('does not pin when a non-stock global dispatcher is configured', async () => {
231 resolvesTo('93.184.216.34');
232 mockFetch.mockResolvedValue(new Response('data', { status: 200 }));
233 mockHasCustomGlobalDispatcher.mockReturnValue(true);
234
235 await ssrfSafeFetch('https://example.com/file.pdf');
236
237 // The configured route (a ProxyAgent and friends) resolves the hostname
238 // itself; fetch falls back to it when no dispatcher is passed.
239 expect(mockCreatePinnedDispatcher).not.toHaveBeenCalled();
240 expect(mockFetch.mock.calls[0][1].dispatcher).toBeUndefined();
241 });
242
243 it('does not pin while the runtime env-proxy mode is active', async () => {
244 resolvesTo('93.184.216.34');
245 mockFetch.mockResolvedValue(new Response('data', { status: 200 }));
246 vi.stubEnv('NODE_USE_ENV_PROXY', '1');
247 vi.stubEnv('HTTPS_PROXY', 'http://proxy.example:3128');
248
249 await ssrfSafeFetch('https://example.com/file.pdf');
250
251 expect(mockCreatePinnedDispatcher).not.toHaveBeenCalled();
252 expect(mockFetch.mock.calls[0][1].dispatcher).toBeUndefined();
253 });
254
255 it('pins again when NO_PROXY=* bypasses every host', async () => {
256 resolvesTo('93.184.216.34');
257 mockFetch.mockResolvedValue(new Response('data', { status: 200 }));
258 vi.stubEnv('NODE_USE_ENV_PROXY', '1');
259 vi.stubEnv('HTTPS_PROXY', 'http://proxy.example:3128');
260 vi.stubEnv('NO_PROXY', '*');
261
262 await ssrfSafeFetch('https://example.com/file.pdf');
263
264 expect(mockCreatePinnedDispatcher).toHaveBeenCalledWith(['93.184.216.34']);
265 });
266
267 it('keeps Bun fetch on an explicitly configured environment proxy', async () => {
268 resolvesTo('93.184.216.34');
269 mockFetch.mockResolvedValue(new Response('data', { status: 200 }));
270 vi.stubEnv('HTTPS_PROXY', 'http://proxy.example:3128');
271 vi.stubEnv('NO_PROXY', '');
272 const bunDescriptor = Object.getOwnPropertyDescriptor(process.versions, 'bun');
273 Object.defineProperty(process.versions, 'bun', {
274 configurable: true,
275 value: '1.4.0',
276 });
277
278 try {
279 await ssrfSafeFetch('https://example.com/file.pdf');
280 } finally {
281 if (bunDescriptor) {
282 Object.defineProperty(process.versions, 'bun', bunDescriptor);
283 } else {
284 delete (process.versions as NodeJS.ProcessVersions & { bun?: string }).bun;
285 }
286 }
287
288 expect(mockPinnedHttpFetch).not.toHaveBeenCalled();
289 expect(mockFetch).toHaveBeenCalledWith(
290 'https://example.com/file.pdf',
291 expect.objectContaining({ redirect: 'manual' })
292 );
293 });
294
295 it('fails closed in strict mode when a caller dispatcher prevents pinning', async () => {
296 resolvesTo('93.184.216.34');
297
298 await expect(
299 ssrfSafeFetch('https://example.com/file.pdf', { dispatcher: {} } as RequestInit, {
300 requirePinnedConnection: true,
301 })
302 ).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
303
304 expect(mockCreatePinnedDispatcher).not.toHaveBeenCalled();
305 expect(mockFetch).not.toHaveBeenCalled();
306 });
307
308 it('fails closed in strict mode when a custom global dispatcher prevents pinning', async () => {
309 resolvesTo('93.184.216.34');
310 mockHasCustomGlobalDispatcher.mockReturnValue(true);
311
312 await expect(
313 ssrfSafeFetch('https://example.com/file.pdf', {}, { requirePinnedConnection: true })
314 ).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
315
316 expect(mockCreatePinnedDispatcher).not.toHaveBeenCalled();
317 expect(mockFetch).not.toHaveBeenCalled();
318 });
319
320 it('fails closed in strict mode when Bun would use an environment proxy', async () => {
321 resolvesTo('93.184.216.34');
322 vi.stubEnv('HTTPS_PROXY', 'http://proxy.example:3128');
323 vi.stubEnv('NO_PROXY', '');
324 const bunDescriptor = Object.getOwnPropertyDescriptor(process.versions, 'bun');
325 Object.defineProperty(process.versions, 'bun', {
326 configurable: true,
327 value: '1.4.0',
328 });
329
330 try {
331 await expect(
332 ssrfSafeFetch('https://example.com/file.pdf', {}, { requirePinnedConnection: true })
333 ).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
334 } finally {
335 if (bunDescriptor) {
336 Object.defineProperty(process.versions, 'bun', bunDescriptor);
337 } else {
338 delete (process.versions as NodeJS.ProcessVersions & { bun?: string }).bun;
339 }
340 }
341
342 expect(mockPinnedHttpFetch).not.toHaveBeenCalled();
343 expect(mockFetch).not.toHaveBeenCalled();
344 });
345
346 it('applies strict pinning requirements to every redirect hop', async () => {
347 mockLookup
348 .mockResolvedValueOnce([{ address: '93.184.216.34', family: 4 }] as never)
349 .mockResolvedValueOnce([{ address: '151.101.1.140', family: 4 }] as never);
350 mockFetch.mockResolvedValueOnce(
351 new Response(null, {
352 status: 302,
353 headers: { location: 'https://cdn.example.com/file.pdf' },
354 })
355 );
356 vi.stubEnv('NODE_USE_ENV_PROXY', '1');
357 vi.stubEnv('HTTP_PROXY', '');
358 vi.stubEnv('HTTPS_PROXY', 'http://proxy.example:3128');
359 vi.stubEnv('NO_PROXY', '');
360
361 await expect(
362 ssrfSafeFetch('http://example.com/file.pdf', {}, { requirePinnedConnection: true })
363 ).rejects.toBeInstanceOf(ComposioBlockedInternalUrlError);
364
365 expect(mockFetch).toHaveBeenCalledTimes(1);
366 expect(mockCreatePinnedDispatcher).toHaveBeenCalledTimes(1);
367 });
368
369 it('validates and fetches a public URL', async () => {
370 resolvesTo('93.184.216.34');
371 const ok = new Response('data', { status: 200 });
372 mockFetch.mockResolvedValue(ok);
373
374 const res = await ssrfSafeFetch('https://example.com/file.pdf');
375 expect(res.status).toBe(200);
376 expect(mockFetch).toHaveBeenCalledWith(
377 'https://example.com/file.pdf',
378 expect.objectContaining({ redirect: 'manual' })
379 );
380 });
381
382 it('re-validates redirect hops and blocks a redirect into internal space', async () => {
383 // First host is public; it 302-redirects to an internal metadata endpoint.
384 mockLookup
385 .mockResolvedValueOnce([{ address: '93.184.216.34', family: 4 }] as never)
386 .mockResolvedValueOnce([{ address: '169.254.169.254', family: 4 }] as never);
387 mockFetch.mockResolvedValueOnce(
388 new Response(null, {
389 status: 302,
390 headers: { location: 'http://169.254.169.254/latest/meta-data/' },
391 })
392 );
393
394 await expect(ssrfSafeFetch('https://public.example/redirect')).rejects.toBeInstanceOf(
395 ComposioBlockedInternalUrlError
396 );
397 // Only the first (public) fetch runs; the internal hop is blocked before fetching.
398 expect(mockFetch).toHaveBeenCalledTimes(1);
399 });
400
401 it('throws after exceeding the redirect budget', async () => {
402 resolvesTo('93.184.216.34');
403 mockFetch.mockResolvedValue(
404 new Response(null, { status: 302, headers: { location: 'https://example.com/again' } })
405 );
406
407 await expect(ssrfSafeFetch('https://example.com/start', {}, 2)).rejects.toBeInstanceOf(
408 ComposioBlockedInternalUrlError
409 );
410 });
411
412 it('releases the redirect body before following the hop', async () => {
413 resolvesTo('93.184.216.34');
414 const fetchCallsWhenReleased: number[] = [];
415 const cancel = vi.fn(() => {
416 fetchCallsWhenReleased.push(mockFetch.mock.calls.length);
417 });
418 const redirect = new Response(new ReadableStream({ cancel }), {
419 status: 302,
420 headers: { location: 'https://example.com/final' },
421 });
422 mockFetch
423 .mockResolvedValueOnce(redirect)
424 .mockResolvedValueOnce(new Response('data', { status: 200 }));
425
426 const res = await ssrfSafeFetch('https://example.com/start');
427
428 expect(res.status).toBe(200);
429 expect(cancel).toHaveBeenCalledOnce();
430 expect(redirect.bodyUsed).toBe(true);
431 // Released while following the first hop, not left dangling for the rest of the call.
432 expect(fetchCallsWhenReleased).toEqual([1]);
433 });
434
435 it('releases every hop body when the redirect budget is exhausted', async () => {
436 resolvesTo('93.184.216.34');
437 const cancel = vi.fn();
438 // A fresh body per hop: cancelling the same stream twice is a no-op the second time.
439 mockFetch.mockImplementation(
440 async () =>
441 new Response(new ReadableStream({ cancel }), {
442 status: 302,
443 headers: { location: 'https://example.com/again' },
444 })
445 );
446
447 await expect(ssrfSafeFetch('https://example.com/start', {}, 2)).rejects.toBeInstanceOf(
448 ComposioBlockedInternalUrlError
449 );
450 // maxRedirects = 2 => hops 0, 1, 2 are fetched before the budget throws.
451 expect(cancel).toHaveBeenCalledTimes(3);
452 });
453
454 // `redirect: 'manual'` means `fetch` never applies its own redirect rules, so
455 // the guard applies them: a 303 sends every method to a bodiless result
456 // request, 301/302 do that to a POST only, and 307/308 replay both. The
457 // Python guard follows the same table in `safe_request`.
458 it.each([
459 { status: 301, method: 'POST', expected: 'GET', replays: false },
460 { status: 301, method: 'PUT', expected: 'PUT', replays: true },
461 { status: 302, method: 'POST', expected: 'GET', replays: false },
462 { status: 302, method: 'PUT', expected: 'PUT', replays: true },
463 { status: 303, method: 'POST', expected: 'GET', replays: false },
464 { status: 303, method: 'PUT', expected: 'GET', replays: false },
465 { status: 303, method: 'HEAD', expected: 'HEAD', replays: false },
466 { status: 307, method: 'POST', expected: 'POST', replays: true },
467 { status: 308, method: 'PUT', expected: 'PUT', replays: true },
468 ])(
469 'sends $method as $expected after a $status',
470 async ({ status, method, expected, replays }) => {
471 resolvesTo('93.184.216.34');
472 mockFetch
473 .mockResolvedValueOnce(
474 new Response(null, { status, headers: { location: 'https://example.com/result' } })
475 )
476 .mockResolvedValueOnce(new Response('data', { status: 200 }));
477
478 await ssrfSafeFetch('https://example.com/create', {
479 method,
480 body: 'payload',
481 headers: { 'Content-Type': 'application/octet-stream', 'X-Test': 'kept' },
482 });
483
484 const [url, init] = mockFetch.mock.calls[1];
485 expect(url).toBe('https://example.com/result');
486 expect(init.method).toBe(expected);
487 expect(init.body).toBe(replays ? 'payload' : undefined);
488 expect(new Headers(init.headers).get('content-type')).toBe(
489 replays ? 'application/octet-stream' : null
490 );
491 // Only the headers that describe the body go with it.
492 expect(new Headers(init.headers).get('x-test')).toBe('kept');
493 }
494 );
495
496 it('keeps the downgrade across later hops', async () => {
497 resolvesTo('93.184.216.34');
498 mockFetch
499 .mockResolvedValueOnce(
500 new Response(null, { status: 303, headers: { location: 'https://example.com/result' } })
501 )
502 .mockResolvedValueOnce(
503 new Response(null, { status: 307, headers: { location: 'https://example.com/final' } })
504 )
505 .mockResolvedValueOnce(new Response('data', { status: 200 }));
506
507 await ssrfSafeFetch('https://example.com/create', { method: 'POST', body: 'payload' });
508
509 // A 307 replays whatever the request is *now*, not what it started as.
510 expect(mockFetch.mock.calls[2][1].method).toBe('GET');
511 expect(mockFetch.mock.calls[2][1].body).toBeUndefined();
512 });
513
514 // A credential header is addressed to the origin the caller named, so a hop
515 // that leaves that origin must not carry it: `redirect: 'manual'` means
516 // `fetch` never strips it for us. The Python guard applies the same rule.
517 const credentialed = {
518 Authorization: 'Bearer token',
519 'Proxy-Authorization': 'Basic cHJveHk=',
520 Cookie: 'session=abc',
521 'X-Test': 'kept',
522 };
523
524 it.each([
525 { location: 'https://other.example.com/elsewhere', differs: 'host' },
526 { location: 'http://example.com/elsewhere', differs: 'scheme' },
527 { location: 'https://example.com:8443/elsewhere', differs: 'port' },
528 ])('drops credential headers on a redirect to a different $differs', async ({ location }) => {
529 resolvesTo('93.184.216.34');
530 mockFetch
531 .mockResolvedValueOnce(new Response(null, { status: 307, headers: { location } }))
532 .mockResolvedValueOnce(new Response('data', { status: 200 }));
533
534 await ssrfSafeFetch('https://example.com/download', { headers: credentialed });
535
536 const [url, init] = mockFetch.mock.calls[1];
537 expect(url).toBe(location);
538 const headers = new Headers(init.headers);
539 expect(headers.get('authorization')).toBeNull();
540 expect(headers.get('proxy-authorization')).toBeNull();
541 expect(headers.get('cookie')).toBeNull();
542 expect(headers.get('x-test')).toBe('kept');
543 });
544
545 it('keeps credential headers on a same-origin redirect', async () => {
546 resolvesTo('93.184.216.34');
547 mockFetch
548 .mockResolvedValueOnce(
549 new Response(null, { status: 307, headers: { location: 'https://example.com:443/moved' } })
550 )
551 .mockResolvedValueOnce(new Response('data', { status: 200 }));
552
553 await ssrfSafeFetch('https://example.com/download', { headers: credentialed });
554
555 const headers = new Headers(mockFetch.mock.calls[1][1].headers);
556 expect(headers.get('authorization')).toBe('Bearer token');
557 expect(headers.get('proxy-authorization')).toBe('Basic cHJveHk=');
558 expect(headers.get('cookie')).toBe('session=abc');
559 expect(headers.get('x-test')).toBe('kept');
560 });
561
562 it('drops credential headers together with the body on a cross-origin 303', async () => {
563 resolvesTo('93.184.216.34');
564 mockFetch
565 .mockResolvedValueOnce(
566 new Response(null, {
567 status: 303,
568 headers: { location: 'https://other.example.com/result' },
569 })
570 )
571 .mockResolvedValueOnce(new Response('data', { status: 200 }));
572
573 await ssrfSafeFetch('https://example.com/create', {
574 method: 'POST',
575 body: 'payload',
576 headers: { ...credentialed, 'Content-Type': 'application/octet-stream' },
577 });
578
579 const init = mockFetch.mock.calls[1][1];
580 expect(init.method).toBe('GET');
581 expect(init.body).toBeUndefined();
582 const headers = new Headers(init.headers);
583 expect(headers.get('content-type')).toBeNull();
584 expect(headers.get('authorization')).toBeNull();
585 expect(headers.get('x-test')).toBe('kept');
586 });
587
588 it.each([300, 304, 305, 306])(
589 'returns a %i without following its location',
590 async (status: number) => {
591 resolvesTo('93.184.216.34');
592 // Only 301/302/303/307/308 are redirects to follow; a `location` on any
593 // other 3xx does not make it one.
594 mockFetch.mockResolvedValue(
595 new Response(null, { status, headers: { location: 'https://example.com/elsewhere' } })
596 );
597
598 const res = await ssrfSafeFetch('https://example.com/file.pdf');
599
600 expect(res.status).toBe(status);
601 expect(mockFetch).toHaveBeenCalledTimes(1);
602 }
603 );
604 });