Skip to content

Commit 012f8fc

Browse files
fix: block HTTPS-to-HTTP redirects in outbound SSRF fetch (#1176)
Cleartext downgrades after a TLS hop drop confidentiality of the request; reject them instead of following. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent f5d3b75 commit 012f8fc

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@truefoundry/trueforge-core': patch
3+
'@truefoundry/trueforge': patch
4+
---
5+
6+
Block HTTPS-to-HTTP redirects in the outbound SSRF-guarded fetch so cleartext downgrades cannot follow a secure hop.

‎packages/trueforge-core/src/core/util/ssrfGuard.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ function nextHop(
267267
if (!URL_VERIFY.allowedProtocols.includes(nextUrl.protocol)) {
268268
throw new OutboundUrlBlockedError(`${OUTBOUND_URL_BLOCKED}: only http and https are allowed`);
269269
}
270+
// Cleartext after TLS would drop confidentiality of the hop; block downgrades.
271+
if (current.protocol === 'https:' && nextUrl.protocol === 'http:') {
272+
throw new OutboundUrlBlockedError(`${OUTBOUND_URL_BLOCKED}: https to http redirect is not allowed`);
273+
}
270274

271275
const headers = new Headers(init.headers);
272276
let method = (init.method ?? 'GET').toUpperCase();

‎packages/trueforge-core/tests/core/util/ssrfGuard.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import http from 'node:http';
2+
import { fetch as undiciFetch, Response as UndiciResponse } from 'undici';
23
import {
34
configureOutboundFetches,
45
defaultMcpOutboundFetchOptions,
@@ -10,6 +11,16 @@ import {
1011
} from '../../../src/core/util/outboundFetch';
1112
import { assertSafeOutboundUrl, configureOutboundUrlGuard } from '../../../src/core/util/ssrfGuard';
1213

14+
jest.mock('undici', () => {
15+
const actual = jest.requireActual<typeof import('undici')>('undici');
16+
return {
17+
...actual,
18+
fetch: jest.fn((...args: Parameters<typeof actual.fetch>) => actual.fetch(...args)),
19+
};
20+
});
21+
22+
const mockedUndiciFetch = jest.mocked(undiciFetch);
23+
1324
function resetOutbound(): void {
1425
configureOutboundUrlGuard({ allowedHosts: [], blockedHosts: [] });
1526
configureOutboundFetches({
@@ -20,6 +31,10 @@ function resetOutbound(): void {
2031
}
2132

2233
afterEach(() => {
34+
mockedUndiciFetch.mockReset();
35+
mockedUndiciFetch.mockImplementation((...args) =>
36+
jest.requireActual<typeof import('undici')>('undici').fetch(...args),
37+
);
2338
resetOutbound();
2439
});
2540

@@ -108,6 +123,14 @@ describe('ssrfFetch', () => {
108123
}
109124
});
110125

126+
it('does not follow an https-to-http redirect', async () => {
127+
mockedUndiciFetch.mockResolvedValueOnce(
128+
new UndiciResponse(null, { status: 302, headers: { location: 'http://93.184.216.34/' } }),
129+
);
130+
await expect(ssrfFetch('https://example.com/')).rejects.toThrow(/https to http redirect/);
131+
expect(mockedUndiciFetch).toHaveBeenCalledTimes(1);
132+
});
133+
111134
it('follows a same-origin redirect when the host is allowed', async () => {
112135
configureOutboundUrlGuard({ allowedHosts: ['127.0.0.1'], blockedHosts: [] });
113136
const { server, origin } = await listen((req, res) => {

0 commit comments

Comments
 (0)