Skip to content

fix(security): mitigate SSRF on the url parameter#787

Closed
deniak wants to merge 2 commits into
mainfrom
fix/ssrf-url-validation
Closed

fix(security): mitigate SSRF on the url parameter#787
deniak wants to merge 2 commits into
mainfrom
fix/ssrf-url-validation

Conversation

@deniak

@deniak deniak commented Jun 26, 2026

Copy link
Copy Markdown
Member

Summary

  • Adds ssrf.ts with a safeFetch() wrapper and checkHostname() helper that guard against Server-Side Request Forgery on the url parameter
  • Uses ipaddr.js (already a transitive dependency) to classify resolved IP addresses and block everything outside the public unicast range (loopback, private RFC-1918, link-local, cloud metadata 169.254.169.254, multicast, unspecified)
  • safeFetch() uses redirect: "manual" to intercept every redirect and re-validates the destination hostname before following it, capped at 5 hops
  • Replaces bare fetch() calls in generators/respec.ts (crawlRaw) and generators/bikeshed.ts (generateIssuesList)
  • Adds a checkHostname() guard in resolveUrlOrFile() before the URL is handed to ReSpec's toHTML(), which runs in a headless browser and cannot be intercepted at the fetch layer
  • 13 unit tests covering all blocked ranges (IPv4, IPv6, IPv4-mapped IPv6) and the public allow-list

What a reviewer should know

The fix follows the guidance from the security report: resolve DNS and block private/loopback/link-local/multicast/cloud-metadata ranges, and re-validate after every redirect. ipaddr.js handles the range classification so there is no hand-rolled CIDR arithmetic.

The toHTML() call in ReSpec cannot have its internal fetches intercepted (it spawns a headless browser), so the mitigation there is the upstream checkHostname() check — the URL is validated before being passed in.

Test plan

  • npm test passes (unit tests run via tsx test/index.test.ts)
  • curl "http://localhost:3000/?type=respec&url=http://169.254.169.254/" → 400
  • curl "http://localhost:3000/?type=respec&url=http://127.0.0.1/" → 400
  • curl "http://localhost:3000/?type=respec&url=http://192.168.1.1/" → 400
  • A valid public spec URL still generates correctly

🤖 Generated with Claude Code

Resolves a reported SSRF vulnerability where the `url` parameter was
passed to fetch() without any IP-level validation, allowing requests
to loopback, private, link-local, and cloud metadata addresses.

Changes:
- Add ssrf.ts with `safeFetch()` and `checkHostname()` helpers.
  Uses ipaddr.js (already a transitive dep) to classify resolved IPs
  and blocks everything except the unicast (public) range.
- `safeFetch()` intercepts every redirect (redirect: "manual") and
  re-validates the destination hostname before following it, capped
  at 5 hops.
- Replace bare fetch() calls in generators/respec.ts (crawlRaw) and
  generators/bikeshed.ts (generateIssuesList) with safeFetch().
- Add checkHostname() call in resolveUrlOrFile() before passing a URL
  to ReSpec's toHTML() (which runs in a headless browser context and
  cannot be intercepted at the fetch layer).
- Add 13 unit tests covering all blocked ranges and public allow-list.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@deniak deniak requested a review from kfranqueiro June 26, 2026 08:04
Previously used only as a transitive dep; now relied on directly
in ssrf.ts so it should be pinned in package.json.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kfranqueiro

kfranqueiro commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Superseded by (human-authored) #788 which covers cases which this missed or broke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants