---
title: "Your server fetches a URL the user sent? Here is how to close SSRF"
description: "SSRF is when an attacker makes your server send a request somewhere it should never reach, such as your internal network or the cloud metadata service. The fix is an allowlist the server builds the request from, no redirect following, and checking the address after DNS."
url: https://ihsanalapsi.dev/articles/ssrf-url-fetch-allowlist
lang: en
date: 2026-11-11
updated: 2026-11-11
tags: ["App security", "Web development"]
author: Ihsan Alapsi (إحسان العبسي)
---
# Your server fetches a URL the user sent? Here is how to close SSRF

A small field on the profile page: "Paste your photo URL". The user pastes a link, and your server goes and fetches the image. The same field, with a different link, makes your server knock on a door inside your network that nobody outside can reach. This article shows how that flaw happens and how to close it in your code today.

## What is SSRF?

SSRF (Server-Side Request Forgery) is a flaw where an attacker makes your server send a request to an address the attacker picks. Your server sends it, so the request leaves from your server's place on the network and with its access.

The [OWASP SSRF Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html) describes it as "an attack vector that abuses an application to interact with the internal/external network or the machine itself". Among the places that open it, the same cheat sheet lists an image URL the user supplies and custom webhooks.

For a business owner: think of a receptionist with a badge that opens every door in the company, and a visitor from outside hands over a note saying "bring me the file from room 4". The receptionist doesn't ask why, and walks off with the badge.

## Why can your server reach places the attacker can't?

Your server can reach them because it sits inside the network, and the mistake is that the code trusts the URL that came from the user and sends a request to it as is. The attacker doesn't need to break into anything: they give you the address, and your code does the rest.

This is what the vulnerable code looks like:

```javascript
app.post('/profile/avatar', async (req, res) => {
  const response = await fetch(req.body.imageUrl);
  const image = await response.arrayBuffer();
  await saveAvatar(req.user.id, image);
  res.sendStatus(204);
});
```

This code sends a request to any address: an internal admin panel on the company network, a service running on the same server with no password because it is "internal", or the cloud metadata service that hands out information about the server itself. The cheat sheet also says the second request doesn't have to be HTTP, so it can use another scheme such as `file://`.

## How do you close SSRF when you know where the request should go?

You close SSRF here with an allowlist: the server builds the request itself from an address on the list, and the user only picks from the list. This is the first case in the OWASP cheat sheet, when the application talks to known destinations.

In order:

1. Don't accept a full URL from the user. The cheat sheet says it word for word: "Do not accept complete URLs from the user". Take a name or an ID from them, and match it against the list.
2. Build the URL on the server from the entry that matched, not from pieces the user typed.
3. Stop your request library from following redirects: "Disable the support for the following of the redirection in your web client". An allowed address can answer with a redirect to an address that isn't allowed, and then your check was for nothing.
4. Check the address after DNS. The cheat sheet says "Domain allowlisting alone does not prevent DNS rebinding": a domain name can return a different IP between the check and the connection, so check the IP you got and connect to that one.
5. Close the network too: a firewall that sets where the server may talk, so a wrong request is stopped even when the code is wrong.

With fetch, the [MDN documentation for the redirect option](https://developer.mozilla.org/en-US/docs/Web/API/RequestInit) says its default is `follow`, meaning the request follows redirects on its own, and that the value `error` rejects the request when the response is a redirect. So the same feature becomes:

```javascript
const IMAGE_SOURCES = new Map([
  ['cdn', 'https://images.example.com/avatars/'],
]);

app.post('/profile/avatar', async (req, res) => {
  const base = IMAGE_SOURCES.get(req.body.source);
  const fileName = String(req.body.fileName ?? '');

  if (!base || !/^[a-z0-9-]+\.(png|jpg)$/.test(fileName)) {
    return res.status(400).json({ error: 'Image source not allowed' });
  }

  const url = new URL(fileName, base);
  const response = await fetch(url, { redirect: 'error' });
  await saveAvatar(req.user.id, await response.arrayBuffer());
  res.sendStatus(204);
});
```

Here the user picks a source from the list and a file name in a fixed shape, and the server puts the URL together. If the source answers with a redirect somewhere else, the request fails instead of following it.

If you want to see how a redirect becomes a flaw on your own site, we wrote about [open redirects in return URLs](/articles/open-redirect-return-url).

## What if your feature has to accept any outside URL?

If the feature has to accept any URL, such as link previews or webhooks, an allowlist won't work, so a deny-list becomes the last resort, with IP checks and a firewall around it. This is the second case in the cheat sheet.

The cheat sheet is clear that a deny-list is weak: "Deny-lists are bypass-prone. Prefer allow-lists." If you have no other choice, the least you should block, per the same cheat sheet:

- The cloud metadata service addresses.
- localhost.
- Private ranges and link-local addresses.

Before comparing, the cheat sheet advises normalizing the address, because the same IP can be written in more than one form.

And if your server runs on AWS, the same cheat sheet says IMDSv2 is "an additional defense-in-depth mechanism for AWS that mitigates some of the instances of SSRF", and recommends moving to it and disabling IMDSv1.

## Where do you start today?

Start by finding every place in your code that sends a request to an address that came from the user. That's the door, and there is usually more than one.

1. Look for every `fetch` or request library call that takes an address from the request: an image by URL, a webhook, a link preview, a file import from a URL.
2. For each one, ask: do I know the destinations in advance? If yes, turn it into an allowlist like the example above.
3. Add `redirect: 'error'` to every request of this kind.
4. If you're on AWS, check whether your servers are on IMDSv2.

This kind of flaw shows up a lot in manual testing, because automated scanners don't know what your server can reach from the inside. If you want someone to test your platform the way an attacker would, that's the work of [Muhassan](/muhassan). And all our articles on application security are in one place: [app security articles](/articles?tag=app-security).

Which feature on your platform takes a URL from the user and has the server open it?

## FAQ

**Can a deny-list replace the allowlist?**

Only as a last resort. OWASP says deny-lists are bypass-prone and recommends allow-lists. If you have no other choice, block at least the cloud metadata endpoints, localhost and the private ranges.

**If I only accept certain domain names, am I done?**

No. OWASP says domain allowlisting alone does not prevent DNS rebinding, so check the IP that DNS returned and connect to the same address you checked.

**Can a webhook open the door to SSRF?**

Yes. Custom webhooks are among the examples OWASP lists, because the user gives you a URL and your server sends requests to it. Treat it like any URL that comes from outside.

**My site runs on AWS. Is there anything extra to do?**

Yes. The OWASP cheat sheet recommends moving to IMDSv2 and disabling IMDSv1, because IMDSv2 is an extra layer of defense that mitigates some SSRF cases against the metadata service.
