Test URL fields for server-side request forgery (SSRF)

Your API lets users set a webhook URL. Or import a file from a link. Or set an avatar from a URL. Each time, your server makes a request to an address a user chose. If that address is http://169.254.169.254/latest/meta-data/ or http://localhost:6379/, your server just made a request into your own network on an attacker's behalf. That's SSRF, number seven on the OWASP API Top 10.

Find the fields

  • Webhook and callback URLs.
  • "Import from URL", "fetch metadata", link previews, PDF or screenshot generators.
  • Avatar, logo or image URLs that the server downloads or resizes.
  • OAuth or OpenID settings where a customer gives an issuer URL.

Anything that calls fetch(userInput) on the server side.

The addresses to try

ssrf.csv
name,url
loopback,http://127.0.0.1/
localhost name,http://localhost:8080/admin
ipv6 loopback,http://[::1]/
private 10,http://10.0.0.1/
private 192,http://192.168.1.1/
aws metadata,http://169.254.169.254/latest/meta-data/
gcp metadata,http://metadata.google.internal/computeMetadata/v1/
decimal ip,http://2130706433/
zero ip,http://0.0.0.0/
file scheme,file:///etc/passwd
gopher scheme,gopher://127.0.0.1:6379/_INFO

Add your own internal hostnames (postgres, redis, api.internal) and a URL on a domain you control that redirects to http://127.0.0.1/. Redirects are the classic bypass: the first URL looks fine, the second one isn't.

The request and the check

Use {{url}} in the field:

JSON
{ "name": "SSRF test {{$guid}}", "url": "{{url}}", "events": ["order.paid"] }

The safest APIs refuse the URL right away:

JavaScript
const name = pm.iterationData.get('name');

pm.test(`${name}: internal URL is refused`, () => {
  pm.expect(pm.response.code).to.be.oneOf([400, 422]);
});

pm.test(`${name}: nothing from inside leaks into the response`, () => {
  pm.expect(pm.response.text()).to.not.match(/ami-id|instance-id|computeMetadata|root:x:0:0|redis_version/i);
});
Terminal
requestarium run-collection "Webhooks API" --folder "Security/SSRF" --env Staging --data ssrf.csv

When the fetch happens later

Webhooks are fetched when an event fires, not when the URL is saved. If your API accepts the URL and checks it at delivery time, test that too:

  1. Save the webhook with an internal URL (expect 2xx if that's your design).
  2. Trigger the event.
  3. Read the delivery log or webhook status from your API, and check the delivery was refused, not "200 OK from 127.0.0.1".

Checking at delivery time is actually more robust: a hostname can resolve to a public address when saved and a private one later.

What a good implementation does

So you know what you're testing for:

  • resolves the hostname and refuses private, loopback, link-local and metadata ranges, after every redirect;
  • allows only http and https;
  • uses a short timeout and a size limit on what it downloads;
  • never returns the fetched body, or an error that includes it, to the user.