SSRF + safefetch

The internal-network attack and how Grit's safefetch blocks it.

9 minhard

Server-Side Request Forgery is the bug where your server fetches a URL somebody else chose. It looks like a feature, because it is one: "import from URL", "fetch the image", "send this webhook", "render that page as a PDF". The server makes the request, so the request comes from inside your network.

Why it is worse than it sounds

Your laptop cannot reach your database. Your server can. An attacker who makes your server fetch a URL has borrowed its position on the network, and that position is the whole point of a firewall.

What the attacker is really asking for

attacker your API things only you can reach | | | | POST /import | | | { url: "http:// | | | 169.254.169.254/ | | | latest/meta-data/ | | | iam/..." } | | |--------------------->| | | | GET (from inside) | | |------------------------->| cloud metadata | |<-------------------------| IAM credentials |<---------------------| | | 200, body echoed | | | back in the response| |
The request is yours, so it passes every control that trusts your server.

169.254.169.254 is the cloud metadata service on AWS, GCP and Azure. It answers without authentication, because it assumes that anything able to reach it is already the machine. It hands out IAM credentials. That one address is why SSRF is rated the way it is.

The other targets are less famous and just as useful: your Redis on 127.0.0.1:6379, an internal admin panel on 10.0.x.x, a Kubernetes API, a health endpoint that leaks configuration.

The fix is one package

Grit ships internal/safefetch. Use it for every request to a URL the caller chose, and never http.Get:

apps/api/internal/handlers/import.go
import "myapp/internal/safefetch"
resp, err := safefetch.Get(c.Request.Context(), in.URL)
if errors.Is(err, safefetch.ErrBlocked) {
respond.Fail(c, respond.CodeValidation, "That URL is not allowed")
return
}
if err != nil {
respond.WriteError(c, err, "Could not fetch that URL")
return
}
defer resp.Body.Close()

What it actually blocks, and when

The interesting part is when. A naive guard parses the URL, resolves the hostname, checks the address against a block list, and then calls http.Get. That is a race: the attacker controls the DNS record, so the name can resolve to a harmless address for your check and to 169.254.169.254 a millisecond later for the real request. It is called DNS rebinding and it defeats every check that happens before the dial.

safefetch puts the check in the dialer's Control function, which runs after the address is resolved and before the socket connects. There is no gap to race:

apps/api/internal/safefetch/safefetch.go
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 5 * time.Second,
// Runs with the resolved address, before connect.
Control: controlSafeDial,
}).DialContext,
},

A redirect is a second request to an address you never checked. Anything that follows redirects has to re-check every hop, which is the other half of why this belongs in a shared client rather than in each handler.

The timeouts are short deliberately. A fetch that can be made to hang is an amplification vector: a few hundred requests to a URL that never answers will exhaust your connection pool without anything looking like an attack.

The part a library cannot do for you

safefetch stops your server reaching somewhere it should not. It cannot decide whether the response is safe to show the caller. Echoing a fetched body straight back turns a blocked request into an information leak the moment some future address is allowed, so return what the feature needs and not the raw body.

Try it

Add an endpoint that takes a URL and reports the response status and content type. Nothing else.

One. Write it with http.Get first. Point it at http://169.254.169.254/ and at http://127.0.0.1:6379/. Note what happens.

Two. Switch to safefetch.Get and repeat both.

Three. Explain in one sentence why resolving the hostname yourself and checking it before calling http.Get would not be a fix.

Quick check

Your handler validates that the user's URL is https, rejects anything resolving to a private range, then calls http.Get. What is still wrong?

Spot a typo? Have an idea?

Help us improve this lesson. One click opens a GitHub issue with the lesson URL pre-filled, suggest clearer wording, report a bug, or request more depth. The course keeps improving thanks to learners like you.

Suggest an improvement on GitHub