Original text(via Hacker News)
PR #481 is open on fix/ssrf-metadata-protection: 3 files changed, 104 insertions, 12 deletions.
Before
// services/preview/src/fetcher.ts (before)
export async function fetchPreview(rawUrl: string): Promise<Preview> {
const res = await fetch(rawUrl, { redirect: 'follow' }); // follows anywhere
if (!res.ok) throw new UpstreamError(res.status);
return renderPreview(res);
}
Any URL goes in, the fetch follows every redirect, and nothing ever checks which IP the hostname resolved to.
After
// services/preview/src/net.ts (new)
import ipaddr from 'ipaddr.js';
import dns from 'node:dns/promises';
const BLOCKED_RANGES = [
'0.0.0.0/8', '10.0.0.0/8', '127.0.0.0/8', '169.254.0.0/16',
'172.16.0.0/12', '192.168.0.0/16',
'::/128', '::1/128', 'fc00::/7', 'fe80::/10',
].map((cidr) => ipaddr.parseCIDR(cidr));
export async function assertSafeTarget(hostname: string): Promise<string> {
const { address } = await dns.lookup(hostname);
const parsed = ipaddr.parse(address);
if (BLOCKED_RANGES.some((range) => parsed.match(range))) {
throw new UnsafeTargetError(hostname, address);
}
return address;
}
// services/preview/src/fetcher.ts (after)
import { assertSafeTarget, parseHttpUrl, fetchPinned } from './net';
const MAX_REDIRECTS = 3;
export async function fetchPreview(rawUrl: string): Promise<Preview> {
let target = parseHttpUrl(rawUrl); // http(s) only, no embedded credentials
for (let hop = 0; hop <= MAX_REDIRECTS; hop++) {
const ip = await assertSafeTarget(target.hostname); // revalidated every hop
// connects to the IP we just validated, so a rebinding DNS can't swap it
const res = await fetchPinned(target, ip, { redirect: 'manual' });
if (res.status < 300 || res.status >= 400) return renderPreview(res);
target = parseHttpUrl(res.headers.get('location'), target.origin);
}
throw new TooManyRedirectsError();
}
Regression tests
// services/preview/test/fetcher.test.ts (excerpt)
it('refuses the EC2 metadata endpoint', async () => {
await expect(fetchPreview('http://169.254.169.254/latest/meta-data/'))
.rejects.toThrow(UnsafeTargetError);
});
it('refuses localhost and private ranges', async () => {
await expect(fetchPreview('http://localhost:8080/debug')).rejects.toThrow(UnsafeTargetError);
await expect(fetchPreview('http://10.0.4.12/')).rejects.toThrow(UnsafeTargetError);
});
it('follows no redirect into an internal network', async () => {
server.enqueueRedirect('http://10.0.4.12/');
await expect(fetchPreview('https://cdn.example/banner.png'))
.rejects.toThrow(UnsafeTargetError);
});
it('pins the validated IP so rebinding DNS cannot redirect the connection', async () => {
dnsMock.lookupReturnsOnce('203.0.113.7').thenReturns('10.0.9.9');
server.enqueuePng();
await fetchPreview('https://rebind.example/banner.png');
expect(server.lastConnectedIp()).toBe('203.0.113.7');
});
it('still renders allowed public URLs', async () => {
server.enqueuePng();
const preview = await fetchPreview('https://cdn.example/banner.png');
expect(preview.mimeType).toBe('image/png');
});
Pull request
fix(preview): block SSRF to cloud metadata and internal networks
## Summary
The preview fetcher followed redirects without checking where they pointed,
so a user-supplied URL could reach the EC2 metadata endpoint and return the
instance role's temporary credentials. Confirmed by the red team as SEC-1042.
- Resolve each hostname and reject loopback, RFC 1918, link-local and the
IPv6 equivalents before connecting.
- Follow redirects manually and revalidate every hop.
- Pin the connection to the validated IP so DNS rebinding cannot swap it
between check and connect.
Fixes SEC-1042.
## Risk
Validation only touches the fetch path. Allowed public previews behave the
same as before. If a legitimate target gets blocked, it fails loudly with
UnsafeTargetError instead of silently changing behavior.
## Testing
- 8 new regression tests in services/preview/test/fetcher.test.ts
- npm test: 61 passed, typecheck clean
- Verified by hand: metadata endpoint, localhost, 10/8 direct, redirect into
169.254.169.254, double-resolution rebinding attempt
## Rollback
Revert and redeploy. No schema or config change.
## Out of scope
- IMDSv2 enforcement fleet-wide, tracked as SEC-1043
- Scoping down the preview IAM role, tracked as SEC-1044