fix: add SSRF protections to fetchHeaders#99
Open
dmchaledev wants to merge 1 commit into
Open
Conversation
fetchHeaders took any user-supplied URL and fetched it with no scheme, hostname, or IP validation, and followed redirects unchecked — an SSRF vector when this library scans customer-supplied targets server-side (its advertised ASM/CI-gate use case). It could reach cloud metadata endpoints, loopback, and RFC1918 addresses, including via a redirect hop to an otherwise-disallowed target. Reject non-http(s) schemes, resolve and reject loopback/link-local/ private hostnames by default, and validate each redirect hop instead of only the initial URL (bounded to 5 hops). Add an explicit opt-out (allowPrivateNetworks / --allow-private) for legitimate local/staging scans. Adds test/fetch.test.ts, which previously had 0% coverage (#65). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M9yz9hxzVPHy9QdTvA4tA4
This was referenced Jul 13, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fetchHeaders(src/fetch.ts) took any user-supplied URL and fetched it with no scheme, hostname, or IP validation, and followed redirects (redirect: 'follow') with no re-validation of where they landed. That's an SSRF vector the moment this library scans customer-/user-supplied targets server-side — exactly the "ASM platform integrations" use case advertised in the README. Concretely, before this change,analyze(untrustedUrl)would happily fetchhttp://169.254.169.254/latest/meta-data/(cloud instance metadata),http://127.0.0.1:*, any RFC1918 address, or reach one of those via a redirect from an otherwise-fine public URL.http(s)schemes up front.dns/promises.lookup, all addresses) and reject loopback, link-local (169.254.0.0/16incl. the metadata IP), RFC1918 private ranges, and their IPv6 equivalents (::1,fc00::/7,fe80::/10, IPv4-mapped::ffff:...) by default.redirect: 'follow'toredirect: 'manual'and validate every hop the same way before following it, bounded to 5 redirects, instead of only checking the initial URL.{ allowPrivateNetworks: true }(library) /--allow-private(CLI) — for legitimate local/staging scans, so this is secure-by-default rather than secure-by-accident-breakage.node:dns/promisesandnode:net), keeping the package's zero-dependency footprint.Also adds
test/fetch.test.ts, which previously had 0% coverage (#65) — 9 cases covering the public-host path, scheme rejection, loopback/RFC1918/metadata-IP rejection, IPv6 loopback/ULA rejection, redirect-to-private rejection, bounded redirect following, the redirect-limit error, and theallowPrivateNetworksopt-out.Closes #91.
Test plan
npm run typecheckpassesnpm test— 95 tests passing acrossanalyzer.test.tsand the newfetch.test.tsnpm run buildsucceedsnpm audit --audit-level=high— 0 vulnerabilitiesnode dist/cli.js http://169.254.169.254/latest/meta-data/→ rejected, exit 1node dist/cli.js http://127.0.0.1:1→ rejected, exit 1node dist/cli.js file:///etc/passwd→ rejected (unsupported scheme), exit 1node dist/cli.js https://example.com→ normal scan, unaffected (F grade, as expected for a bare site)127.0.0.1:4321: blocked by default, and reachable with--allow-private --json, producing a correct report🤖 Generated with Claude Code
https://claude.ai/code/session_01M9yz9hxzVPHy9QdTvA4tA4
Generated by Claude Code