fix: block SSRF to loopback, private and cloud-metadata addresses - #29
Open
rajarshidattapy wants to merge 1 commit into
Open
fix: block SSRF to loopback, private and cloud-metadata addresses#29rajarshidattapy wants to merge 1 commit into
rajarshidattapy wants to merge 1 commit into
Conversation
- Added ALLOW_PRIVATE_TARGETS configuration to .env.example and config struct. - Updated HTTPHandler and ScraperHandler to utilize the new configuration for controlling access to loopback and private addresses. - Introduced netguard package to validate host addresses and prevent SSRF vulnerabilities. - Added tests for netguard functionality and HTTP handler behavior regarding private targets.
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.
What does this PR do?
Fixes #28.
validateURLonly checked that a target parses, uses http/https, and has a non-emptyhost. Nothing stopped
/v1/scrapefrom fetchinghttp://127.0.0.1:8080/health,http://169.254.169.254/latest/meta-data/, or any RFC1918 address and returning the fullresponse body to the caller — cloud credentials and internal admin panels included. The
API has no authentication, so this is reachable by anyone who can reach port 8080.
Adds
internal/netguardand wires it into the two places that reach the network:ScraperHandler.validateURLresolves the host and rejects internaladdresses with
400 invalid_url. Covers/v1/scrape,/v1/url-scraper, and every URLin a batch. It is also the only protection available for the browser handler, whose
dialer lives in the Camoufox process.
net.DialerControlhook on the HTTP handler's transport re-checksthe post-resolution IP of every hop, so redirect chains and DNS rebinding are covered,
which a one-shot hostname check cannot see. Installed only when no proxy is configured:
behind a proxy the dial target is the proxy, which is legitimately private.
Blocked ranges: loopback, private (RFC1918 and
fc00::/7), link-local (which covers the169.254.169.254metadata endpoint), unspecified, and multicast — including theirIPv4-mapped IPv6 forms. Operators who genuinely scrape internal sites can set
ALLOW_PRIVATE_TARGETS=true(default false; logs a warning at startup).Deliberately out of scope:
domain_configs.proxy_urlis not restricted — private proxy addresses are a normalsetup (
PROXY_URL=http://127.0.0.1:8888) and blocking them would break valid configs.Attacker-written domain configs are the separate auth/CORS issue.
unreachable names reach nothing, so the scrape fails with its real error rather than a
spurious 400.
in the package doc. Closing it needs an egress proxy in front of the browser service.
How to test
make upcurl -s -X POST http://localhost:8080/v1/url-scraper/batch -H "Content-Type: application/json"
-d '{"urls":["https://example.com","http://169.254.169.254/"]}'
curl -s -X POST http://localhost:8080/v1/scrape -H "Content-Type: application/json"
-d '{"url":"https://example.com"}' | jq .markdown
and DialControl; handler/http_test.go asserts the dialer guard is wired up by
fetching an httptest loopback server with the flag off (blocked) and on (allowed).
Checklist
One caveat worth keeping honest:
go test -racecouldn't run locally (needs cgo) — CI covers it. Behaviour change to flag in review: a previously-accepted internal URL now returns400at submit time instead of a job that fails later.