/** * SSRF guard — Server-Side Request Forgery defense for `fetch_and_index`. * * Policy (massa-th0th is STRICT BY DEFAULT, unlike context-mode's lenient mode): * * HARD BLOCK — no legitimate fetch use case reaches these: * - non-http(s) schemes (file://, gopher://, javascript:, data:, …) * - 169.254.0.0/16 link-local — INCLUDES 169.254.169.254 (AWS/GCP/Azure * IMDS cloud-credential endpoint, the highest-value SSRF target) * - IPv6 link-local fe80::/10 * - multicast (224.0.0.0/4 IPv4, ff00::/8 IPv6) * - unspecified / current-network (0.0.0.0/8, ::) * - loopback (127.0.0.0/8, ::1) — local dev servers are out of scope here * - RFC1918 private (10/8, 172.16/12, 192.168/16) and IPv6 ULA (fc00::/7) * - malformed / non-IP strings * * The massa-th0th trust model treats fetch targets as OPEN WORLD (arbitrary * URLs the agent fetched at the user's request), so a private/loopback hit is * treated as a rebinding attack probe, not a local-dev convenience. This is the * conservative default; there is no lenient escape hatch (unlike context-mode's * `CTX_FETCH_STRICT` toggle) — if a future task needs to fetch localhost it can * add an explicit allowlist. * * DNS-rebinding defense (TOCTOU-safe): * 1. Resolve the hostname ONCE and classify EACH resolved address. * 2. PIN the connection to the already-classified resolved IP by rewriting * the fetch URL to the literal IP and setting the `Host` header to the * original hostname. This closes the TOCTOU: the fetch's own connect-time * DNS re-resolution is bypassed entirely because the URL host is now a * literal IP (no lookup occurs). An attacker cannot serve a public IP at * check-time and a private IP at connect-time for the SAME request — the * connect-time resolution never happens. * (The alternative — a custom undici `connect.lookup` hook — does NOT work * under bun: bun's native fetch ignores the undici Dispatcher entirely, * and even the bundled undici shim does not invoke `connect.lookup`. The * literal-IP + Host-header approach is the only one that works under bun * and is also portable to node/undici.) * 3. On fetch, walk redirects MANUALLY (do not let fetch auto-follow) and * re-resolve + re-classify + re-PIN the hostname of every hop. An attacker * can return a public IP for the first request, then 302 to an internal * host whose DNS now resolves to 169.254.169.254. Manual redirect walking * with per-hop resolution+pinning defeats this. * * Exported seams: * - classifyIp(ip) — pure IP classifier (tested directly with literals). * - assertUrlSafe(url) — resolve + classify, returns pinned IP(s); throws on * any blocked IP. * - fetchWithSsrfGuard(url, opts) — fetch with DNS-pinned connect + manual * redirect walk that re-runs assertUrlSafe (and re-pins) at every hop. */ export type IpClass = "block" | "public"; /** Hard cap on redirect hops we will walk before giving up (loop defense). */ export declare const MAX_REDIRECTS = 8; /** * Injectable DNS resolver seam. Defaults to node:dns/promises `lookup`. Tests * swap this (via `setDnsResolver`) to simulate DNS rebinding / IMDS aliases * WITHOUT patching the read-only module namespace. Production code never calls * the setter. */ export type DnsResolver = (hostname: string) => Promise<{ address: string; }[]>; /** Test seam: override the DNS resolver. Returns a restore function. */ export declare function setDnsResolver(resolver: DnsResolver): () => void; /** * Classify a single IP literal. * * Returns "block" for link-local / loopback / private / multicast / unspecified * / malformed. Returns "public" only for genuinely routable addresses. * * IPv4-mapped IPv6 (`::ffff:127.0.0.1`) is routed through the IPv4 classifier so * loopback mapped into the v6 space is still caught. * * RFC 6874 zone identifiers (`fe80::1%eth0`, URL-encoded `%25eth0`) are stripped * BEFORE classification — without the strip, `::1%eth0` no longer matches * `=== "::1"` and would fall through to "public", silently bypassing the guard. */ export declare function classifyIp(rawIp: string): IpClass; /** Error thrown when a URL or one of its redirect hops is blocked by SSRF. */ export declare class SsrfBlockedError extends Error { readonly url: string; readonly resolvedIp?: string | undefined; constructor(message: string, url: string, resolvedIp?: string | undefined); } /** * Result of `assertUrlSafe`: the connection-pin parameters the caller must use * when issuing the fetch so the TOCTOU gap between check-time and connect-time * DNS resolution is closed. * * - `pinnedIp`: the already-classified IP literal to connect to (replaces the * hostname in the fetch URL). For a URL that already names a literal IP, * this is that same IP. For a hostname, this is the first public resolved * address. * - `originalHost`: the hostname to send in the `Host` header so virtual * hosting + TLS SNI still work. Equals the URL's hostname (brackets stripped * for IPv6). When the URL already names a literal IP, this equals pinnedIp * and no Host header rewrite is needed. * - `isLiteralIpUrl`: true when the original URL host was already an IP * literal (no DNS resolution occurred). Lets the caller skip the rewrite. */ export interface UrlSafetyResult { pinnedIp: string; originalHost: string; isLiteralIpUrl: boolean; } /** * Resolve `hostname` and assert every resolved IP is public. Throws * `SsrfBlockedError` on the first blocked address. A hostname that resolves to * a MIX of public + private is still rejected (TOCTOU-safe: we re-resolve on * fetch, so even if the resolver rotates answers, every hop is checked). * * Returns the `UrlSafetyResult` (pinned IP + original hostname) so the caller * can pin the fetch connection to the already-classified IP and close the * TOCTOU between check-time and connect-time DNS resolution. */ export declare function assertUrlSafe(rawUrl: string): Promise; export interface FetchGuardOptions { /** Per-request timeout (ms). Default 30_000. */ timeoutMs?: number; /** AbortSignal from the caller (e.g. for test cancellation). */ signal?: AbortSignal; /** Composed AbortSignal: combines the timeout timer and the caller's signal. */ _compose?: (signals: AbortSignal[]) => AbortSignal; } /** * Fetch a URL with the SSRF DNS-pin + redirect-walk defense. * * TWO-LAYER DEFENSE (TOCTOU-safe): * 1. DNS PIN: `assertUrlSafe` resolves the hostname once and classifies the * IP. We then rewrite the fetch URL to connect to that LITERAL IP and set * the `Host` header to the original hostname. Because the fetch URL's host * is now a literal IP, NO DNS lookup occurs at connect time — the attacker * cannot serve a different (private) IP at connect. bun's fetch honors the * `Host` header for both HTTP virtual hosting and HTTPS TLS SNI. * 2. REDIRECT WALK: does NOT auto-follow. Each 3xx Location header is * re-resolved + re-classified + re-pinned before the next hop. Any blocked * hop throws `SsrfBlockedError`. Capped at MAX_REDIRECTS to defeat loops. * * Returns the final (non-redirect) Response. The caller reads the body. */ export declare function fetchWithSsrfGuard(rawUrl: string, opts?: FetchGuardOptions): Promise; //# sourceMappingURL=ssrf.d.ts.map