|
3 | 3 | * 4G/5G never has to relay the bytes. Security-critical: this is an SSRF sink, so we |
4 | 4 | * - allow only http/https, |
5 | 5 | * - manually follow redirects, re-validating every hop, |
6 | | - * - DNS-resolve each host and reject any private/reserved address, |
| 6 | + * - DNS-resolve each host, reject any private/reserved address, and then PIN the |
| 7 | + * connection to the exact validated IP (closing the DNS-rebinding window between |
| 8 | + * our lookup and the connect), |
7 | 9 | * - cap the response size. |
8 | | - * |
9 | | - * (DNS rebinding between our lookup and fetch remains a theoretical risk; documented |
10 | | - * in THREAT_MODEL.md. Deployments needing stronger guarantees can run behind an egress |
11 | | - * proxy that enforces the same allowlist.) |
12 | 10 | */ |
13 | 11 | import { lookup } from 'node:dns/promises'; |
| 12 | +import { isIP } from 'node:net'; |
14 | 13 | import { Readable } from 'node:stream'; |
| 14 | +import { Agent } from 'undici'; |
15 | 15 | import { assertAllowedUrl, isPrivateAddress, SsrfError } from '@opencoperlock/shared'; |
16 | 16 |
|
17 | 17 | const MAX_REDIRECTS = 5; |
18 | 18 |
|
19 | | -async function assertHostResolvesPublic(hostname: string): Promise<void> { |
20 | | - // Literal IPs are already checked by assertAllowedUrl; this covers DNS names. |
| 19 | +/** |
| 20 | + * Resolve a host, ensure every address is public, and return one validated address to |
| 21 | + * connect to. Literal IPs are returned as-is (already checked by assertAllowedUrl). |
| 22 | + */ |
| 23 | +async function resolvePublicAddress(hostname: string): Promise<string> { |
| 24 | + if (isIP(hostname)) return hostname; |
21 | 25 | const records = await lookup(hostname, { all: true }); |
| 26 | + if (records.length === 0) throw new SsrfError('Host does not resolve'); |
22 | 27 | for (const record of records) { |
23 | 28 | if (isPrivateAddress(record.address)) { |
24 | 29 | throw new SsrfError('Host resolves to a private or reserved address'); |
25 | 30 | } |
26 | 31 | } |
| 32 | + return records[0]!.address; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * An undici dispatcher whose DNS lookup is pinned to a single, pre-validated IP. The TLS |
| 37 | + * SNI / Host header still use the original hostname, so certificate validation is intact; |
| 38 | + * only the TCP target is fixed — a rebinding response after our check cannot redirect us |
| 39 | + * to an internal address. |
| 40 | + */ |
| 41 | +function pinnedDispatcher(ip: string): Agent { |
| 42 | + const family = isIP(ip); |
| 43 | + return new Agent({ |
| 44 | + connect: { |
| 45 | + // undici calls this dns.lookup-style; honour both the `all` and single-result forms. |
| 46 | + lookup(_hostname, options, callback) { |
| 47 | + if (options && (options as { all?: boolean }).all) { |
| 48 | + callback(null, [{ address: ip, family }] as never); |
| 49 | + } else { |
| 50 | + (callback as (err: Error | null, address: string, family: number) => void)(null, ip, family); |
| 51 | + } |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
27 | 55 | } |
28 | 56 |
|
29 | 57 | export interface RemoteSource { |
@@ -65,12 +93,14 @@ export async function openRemoteSource( |
65 | 93 | let current = assertAllowedUrl(rawUrl); |
66 | 94 |
|
67 | 95 | for (let hop = 0; hop <= MAX_REDIRECTS; hop += 1) { |
68 | | - await assertHostResolvesPublic(current.hostname); |
| 96 | + const pinnedIp = await resolvePublicAddress(current.hostname); |
| 97 | + // `dispatcher` is an undici extension to global fetch; not in the DOM RequestInit type. |
69 | 98 | const res = await fetch(current, { |
70 | 99 | redirect: 'manual', |
71 | 100 | headers: { 'user-agent': 'OpenCoperLock-RemoteUpload/0.1' }, |
72 | 101 | signal, |
73 | | - }); |
| 102 | + dispatcher: pinnedDispatcher(pinnedIp), |
| 103 | + } as RequestInit & { dispatcher: Agent }); |
74 | 104 |
|
75 | 105 | // Manual redirect handling so we re-validate each Location. |
76 | 106 | if (res.status >= 300 && res.status < 400) { |
|
0 commit comments