Checking every hop of a server-side fetch

An allow-list that only looks at the first URL loses when that host redirects the server somewhere else.

Lesson 57 of 69 · 14 min read · practice only with authorization

Diagram

From an allowed first hop to a second address

Follow the path. One example at a time.

Step 1 of 4

First URL

The caller sends a URL whose host is on your list, or looks like a partner.

Illustration: the first URL is not the last URL
# A preview starts at one URL.
# A redirect can point the next hop somewhere else.
What changes the outcome
Hop 1

The host is on the list.

Hop 2

The client follows a redirect automatically.

Policy

The second address was never classified.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. The first host is not the destination

    What fails
    HTTP clients follow redirects unless you tell them not to. The policy has to move with the client.
    Who is affected
    People who depend on the internal service the second hop can reach.
    What you would notice
    A library call that leaves redirect following on, after a single allow-list check.
    What you change
    Use a manual redirect mode and run the same check on the next URL.
  2. How many hops

    What fails
    An unbounded redirect chain is also a way to spend your sockets. Two hops is already generous for a preview.
    Who is affected
    Everyone waiting on the fetcher if a chain holds connections open.
    What you would notice
    No cap in the client configuration.
    What you change
    Cap redirects. Fail closed when the cap is hit.
  3. Relative redirects

    What fails
    A Location value can be a path on the same host or an absolute URL elsewhere. Both need a parser, not string concatenation.
    Who is affected
    The same internal population if the absolute URL leaves the list.
    What you would notice
    Code that joins the header onto a string.
    What you change
    Parse the Location against the current URL, then classify the result.
  4. What you log

    What fails
    Log the host you refused and the hop number. Do not log a secret the redirect was trying to carry, and do not fetch it “to be sure.”
    Who is affected
    Operators who need to see the failure without reproducing it.
    What you would notice
    Logs that store the full target including query secrets, or logs that store nothing.
    What you change
    Record scheme, host, and hop. Keep the body out of the log.

Allow-lists die on the second hop

An allow-list is the right idea: the server fetches partners you named, not the open internet. It stops being that idea when the HTTP client follows a redirect by default. The first request goes to the partner. The second goes wherever the partner, or whoever can speak for that host, sends you.

That second place might be a private address, a link-local address, or a different public host that your list did not name. The caller did not have to type that address. They only had to name a first hop that would forward the server. The policy has to be evaluated on the URL you are about to open, every time.

Manual redirects are the readable version

Configure the client not to follow redirects. Read the status. If it is a redirect, parse the Location header relative to the current URL, run the same allow-list and address classification, and only then request the next hop. Stop after one or two hops. A preview does not need a long chain.

Relative Locations stay on the current host only if the parser says so. Do not concatenate the header onto a string. URL parsers exist so a scheme and a host cannot hide in a path.

A partner is not a proxy you operate

Teams argue that a partner on the list is trusted, so their redirects are trusted. The partner’s redirect can be an open feature of their own, or a compromise, or a mistake. Your allow-list is a list of hosts, not a blank check for any host they mention. If you need their other host, put that host on the list explicitly.

You would notice a client constructed with default options next to a check that runs once. You would also notice logs that record only the caller’s original string, so an investigation cannot see the hop that left the list. Log the host and the decision. Leave bodies and query secrets out.

What you change

Ship the manual loop, the shared policy function, and the cap. Use that one function for previews, webhooks, and importers so a new feature cannot forget the second check. Add a test where the first host is allowed and the redirect target is not, and assert that no connection is attempted to the second address.

The people affected are whoever relies on the internal or unlisted service the server could reach. The finding says that. The fix is the hop check, not a promise that partners will behave.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

The first URL is on your allow-list. The HTTP client is left on its default, and that host redirects elsewhere. What failed?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapServer-side request forgery: fetchers, internal targets, and allow-lists that holdHow features that fetch a URL — webhooks, previews, importers, PDF renderers — can be pointed at internal services, how testers confirm SSRF safely with a canary host, and how to build an allow-list that survives redirects and DNS tricks.
  1. 01Metadata and link-local fetchesA server that fetches a caller-influenced address can reach link-local services, including a cloud metadata endpoint the caller cannot open from their own machine.

Practice

  1. LabSSRF allowlist evaluatorAn image import feature fetches URLs on the server. Combine host matching, address checks, redirect checks, and pinning until nine test URLs behave.

Glossary

  1. CardServer-side request forgeryThe server is induced to fetch a URL the attacker chooses, which can reach internal services the attacker cannot see directly.