CCyberwiki

Server-side request forgery

Server-side request forgery
ClassAccess control
Also known asSSRF; Cross Site Port Attack (XSPA)
CWECWE-918: Server-Side Request Forgery (SSRF)
OWASP Top 10A01:2025 Broken Access Control
Typical severityHigh
AffectsImage downloads from a link, webhooks, open proxies; cloud servers that accept IMDSv1

A web server that fetches a URL a user supplied, without checking where the request goes, has a server-side request forgery (SSRF) vulnerability.1 The request leaves from the server itself, so it reaches services on the internal network and the cloud instance metadata service, which are closed to the attacker from outside.

Description

Features that fetch an address a user typed include an avatar downloaded from an image URL and a webhook or callback URL the user registers.2 The same feature can scan ports on hosts in the internal network. The URL need not be HTTP: file:// reads documents on the server, and gopher:// and tftp:// reach other services.1

In cloud hosting, SSRF is often used to steal credentials and access tokens from the metadata service.2 On Amazon EC2 it answers at 169.254.169.254.3 A misconfigured proxy or web application firewall that forwards requests to any host opens the same path.4

Mechanism

A Next.js route downloads a profile picture from a pasted URL:

// app/api/avatar/route.ts
export async function POST(req: Request) {
  const { imageUrl } = await req.json();
  const res = await fetch(imageUrl); // any address the user sends
  const image = await res.arrayBuffer();
  // resize, save and show the image
}

Anna sends https://images.example.com/anna.png and gets her picture. An attacker sends http://169.254.169.254/latest/meta-data/, and the server calls the metadata service of its own EC2 instance. By default the metadata service accepts both IMDSv1 and IMDSv2, and IMDSv1 answers a request that carries no token.3 If the route shows what it downloaded, the attacker reads the reply.

Attacker, on the internetPOST /api/avatarApplication serverfetch(imageUrl)Metadata service169.254.169.254Firewall: no direct routefrom the internet inwardsThe server calls the address the attacker choseand passes the reply back.
Figure 1. The server behind the firewall fetches the URL the attacker sent.

Checks on the URL text miss several forms. For http://example.com\@evil.com, a parser that follows the WHATWG URL standard reads the host as example.com, while Python's urllib.parse returns evil.com. The address ::ffff:169.254.169.254 reaches the same host as 169.254.169.254. A domain can point to a public address when it is checked and to an internal one when it is fetched, through a second DNS lookup (DNS rebinding), and a redirect can lead from an allowed address to an internal one.2

Impact

A forged request can read application data, run unauthorised code or commands, or get past protection mechanisms.1 The credentials the metadata service hands out work for every cloud resource the server's role is allowed to use.5 The OWASP Top 10:2025 files SSRF under Broken Access Control.6

Notable incidents

Capital One (2019)

On 19 July 2019 Capital One found that an outsider had gained access to its data.7 Federal prosecutors in Seattle, announcing an arrest on 29 July, said the criminal complaint traced the intrusion to a misconfigured web application firewall.8

Brian Krebs, citing a source with direct knowledge of the investigation, reported that the firewall was ModSecurity running with Apache on AWS. The intruder made it relay requests to the metadata service, and the firewall's permissions let it list and read every file in any bucket. Krebs called the method an SSRF attack.5

Capital One counted about 100 million people affected in the United States and about 6 million in Canada.7

Detection

Search the code for fetch, axios, got, requests, httpx or urllib calls whose address comes from a request or a field users can set.

Prevention

Fixed destinations

When the server calls only known services, accept no URL from the user. Map the user's choice to a destination listed in the code, with a fixed scheme, port and path.2

const body = await req.json();
const PARTNERS = { invoices: "https://invoices.example.com/api/v1/" };
const base = PARTNERS[body.partner];
if (!base) return new Response("Unknown partner", { status: 400 });
const res = await fetch(base + encodeURIComponent(body.invoiceId));

Addresses chosen by users

A webhook must reach any address the user registers, so the checks move to the moment of the request:2

  1. Accept only http and https.
  2. Look up all the host name's IPv4 (A) and IPv6 (AAAA) records and check that every address is public.
  3. Connect to the address you checked, and keep the host name for the Host header (the line in the request that names the site) and the certificate check.
  4. Turn off redirects in the HTTP client.

In Python, the ipaddress module does the public check:2

import ipaddress

def is_public(address: str) -> bool:
    return ipaddress.ip_address(address).is_global

Use is_global. For the shared address space 100.64.0.0/10, is_private and is_global are both false, so a check for not is_private lets those addresses through.9

A block list is the last resort. Convert each address to one form first, then refuse at least these ranges:2

Table 1. Minimum block list.
WhatAddresses
Cloud metadata169.254.169.254, fd00:ec2::254, metadata.google.internal
The server itself127.0.0.0/8, 0.0.0.0/8, ::1/128
Private networks10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fc00::/7
Link-local169.254.0.0/16, fe80::/10
Multicast224.0.0.0/4, ff00::/8
IPv6 forms of IPv4::ffff:0:0/96, 64:ff9b::/96, 64:ff9b:1::/48

Next.js

List the allowed image sources in remotePatterns. A request for any other protocol, host name, port or path gets 400 Bad Request; leaving out search allows every query string.10

// next.config.js
module.exports = {
  images: {
    remotePatterns: [
      { protocol: 'https', hostname: 'example.com', port: '', pathname: '/account123/**', search: '' },
    ],
  },
}

Versions of next from 13.4.0 and before 14.1.1 contain CVE-2024-34351, an SSRF in Server Actions that can affect self-hosted apps that use them. The advisory gives no workaround; upgrade to 14.1.1 or later.11

Cloud metadata and the network

On AWS, require IMDSv2. Every request then needs a token from a PUT request, a request without a valid token gets 401 Unauthorized, and a PUT that carries an X-Forwarded-For header is refused.3 To require it on an instance, with your own instance ID:12

aws ec2 modify-instance-metadata-options \
    --instance-id i-1234567890abcdef0 \
    --http-tokens required \
    --http-endpoint enabled

Limit where the application can connect with a firewall and a separate network,2 and give its cloud role only the permissions it needs (see Principle of least privilege).

Prompt for a coding agent

Find every place in this project where the server makes an outgoing request (fetch, axios, got, requests, httpx, urllib, image or file downloads, webhooks) to an address that comes from a request, a form, a URL parameter or a database field that users can set. For each one: if the destination is one of a few known services, replace the user-supplied URL with a fixed list of allowed destinations. If it must accept any address, allow only http and https, resolve the host name and reject any address that is not public (loopback, private, link-local including 169.254.169.254, and IPv6 forms of those), connect to the checked address, and turn off redirects. If this is a Next.js app, check that images.remotePatterns lists exact hosts and that next is 14.1.1 or later. List each file and line you changed, and any request you were unsure about.

In SecFlash

SecFlash reports SF-SEC-716 on the Security page for code that sends an outgoing request to an address taken from input, and SF-SEC-619 on the same page when the attack test gets a success back from a request with the Host header set to 169.254.169.254.

See also

References

  1. a b c "CWE-918: Server-Side Request Forgery". Common Weakness Enumeration. MITRE.
  2. a b c d e f g h "Server-Side Request Forgery Prevention Cheat Sheet". OWASP Cheat Sheet Series.
  3. a b c "Use the Instance Metadata Service to access instance metadata". Amazon EC2 User Guide. AWS.
  4. back "Add defense in depth against open firewalls, reverse proxies, and SSRF vulnerabilities with enhancements to the EC2 Instance Metadata Service". AWS Security Blog. AWS (19 November 2019).
  5. a b Krebs, Brian (2 August 2019). "What We Can Learn from the Capital One Hack". KrebsOnSecurity.
  6. back "A01:2025 Broken Access Control". OWASP Top 10:2025. OWASP Foundation.
  7. a b Capital One. [Notice on the 2019 cybersecurity incident]. capitalone.com.
  8. back U.S. Attorney's Office, Western District of Washington (29 July 2019). "Seattle Tech Worker Arrested for Data Theft Involving Large Financial Services Company". U.S. Department of Justice.
  9. back "ipaddress". Python documentation. Python Software Foundation.
  10. back "next/image". Next.js Docs. Vercel.
  11. back GitHub (9 May 2024). "Next.js Server-Side Request Forgery in Server Actions" (GHSA-fr5h-rqp8-mj6g, CVE-2024-34351). GitHub Advisory Database.
  12. back [Transition to IMDSv2: modify instance metadata options for existing instances]. Amazon EC2 User Guide. AWS.