# What is server-side request forgery (SSRF)?

Canonical: https://hackergpt.org/learn/ssrf/

**Server-side request forgery (SSRF) is a vulnerability where an application fetches a URL or host supplied by a user without restricting where it goes. The user can then point the server at addresses only it can reach, such as an admin service on 127.0.0.1, a database on the private network or the cloud metadata service.**

Part of the roadmap: [Stage 5, Deeper web and APIs](https://hackergpt.org/learn/roadmap/#deeper)

## Why it matters

MITRE files it as CWE-918. It had its own category, A10, in the OWASP Top 10:2021, and the 2025 edition lists it under A01, broken access control. That framing helps: the server has network access the user does not, and the bug lends it to them. Behind a firewall, internal services often trust any request from the web tier, so a request the server makes on a stranger's behalf arrives with that trust.

On cloud hosts the metadata service is the classic target, because it can hand out the instance's credentials. AWS added IMDSv2, which requires a session token from a PUT request, as defence in depth against exactly this. It reduces the damage; the fix is still in the code that fetched the URL.

## Example: a link-preview endpoint, vulnerable and fixed

The usual places are link previews, import from URL, avatar upload by URL, webhooks, and HTML-to-PDF or screenshot services. In our own lab, a link-preview endpoint on localhost that called fetch on the user's URL returned the reply of a stand-in internal admin service on 127.0.0.1:9000, and that service's log showed the request coming from the app itself.

In your own app (https://your-app.example), find every handler where a value named url, link, callback or webhook reaches an HTTP client. The fix allows only the destinations you mean and refuses redirects:

```
// Vulnerable: any destination the user names
const page = await fetch(req.query.url);

// Safer: an allow-list of hosts, checked again on the resolved IP, no redirects
if (!ALLOWED_HOSTS.has(new URL(req.query.url).hostname)) return res.sendStatus(400);
const page = await fetch(req.query.url, { redirect: "error" });
```

If the app must fetch arbitrary public URLs, resolve the hostname yourself, refuse private, loopback and link-local addresses, and connect to the address you checked. Log every outbound fetch with its resolved IP: it is the log that answers where your server connected.

## Common mistakes

- **Blocking the string localhost.** 127.0.0.1, ::1, decimal IPs and DNS names that resolve inward all get through. Check the resolved address.
- **Checking the URL, then letting the client follow redirects.** A public URL can redirect inward. Turn redirects off or check every hop.
- **Checking the hostname once and connecting later.** DNS can answer differently the second time. Connect to the IP you validated.
- **Forgetting background jobs.** A saved webhook or feed URL is fetched later, often from a host with more network access.

## Related terms

- [XML external entity (XXE) injection](https://hackergpt.org/learn/xxe/)
- [Broken access control](https://hackergpt.org/learn/broken-access-control/)
- [API endpoint](https://hackergpt.org/learn/api-endpoint/)
- [Attack surface mapping](https://hackergpt.org/learn/attack-surface-mapping/)

## Go deeper

- [SSRF example: how it works and how to fix it in your app](https://hackergpt.org/blog/ssrf-example/)
- [Open redirect example](https://hackergpt.org/blog/open-redirect-example/)
- [AppMap: list your endpoints, then find the ones that take a URL](https://hackergpt.org/features/appmap/)

## Sources

- [CWE-918: Server-Side Request Forgery (SSRF)](https://cwe.mitre.org/data/definitions/918.html)
- [OWASP Server-Side Request Forgery Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html)
- [OWASP Top 10:2025 A01 Broken Access Control](https://top10.owasp.org/2025/A01_2025-Broken_Access_Control/)
- [AWS: How Instance Metadata Service Version 2 works](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-metadata-v2-how-it-works.html)

Test only sites you own or have written permission to test. To practise without one, use CyberGym, a legal, deliberately vulnerable target. [Practise on CyberGym](https://cybergym.hackergpt.app).
