Vulnerability class

Server-side request forgery (SSRF) in Python

Server-side request forgery is a flaw where an attacker makes the server itself send a request to a destination of their choosing, reaching internal services and cloud metadata that the attacker could never reach directly.

CWE
CWE-918
OWASP
A01:2025 Broken Access Control
Tag
ssrf, high impact
On this page

How it shows up in Python

Any feature that fetches a URL the user supplies is a candidate: a webhook, an "import from URL", a link preview, an avatar fetched from a profile address. The code looks harmless:

import requests
from flask import request

@app.post("/preview")
def preview():
    url = request.form["url"]
    resp = requests.get(url, timeout=5)
    return {"title": extract_title(resp.text)}

The server will fetch whatever url says, including http://169.254.169.254/... (the cloud metadata service, which can hand out credentials), http://localhost:6379/ (an internal Redis), or a link-local or private-range address that only the server can reach. From the outside an attacker cannot touch those; through this endpoint they can.

SSRF is hard to fix by inspecting the string, because a hostname can resolve to an internal address, a redirect can send a safe-looking URL to an internal one, and there are many spellings of the same address. The reliable defences are structural:

import ipaddress
import socket
from urllib.parse import urlparse
import requests
from flask import request, abort

def safe_target(url: str) -> str:
    parts = urlparse(url)
    if parts.scheme not in ("http", "https") or not parts.hostname:
        abort(400)
    for info in socket.getaddrinfo(parts.hostname, parts.port or 80):
        ip = ipaddress.ip_address(info[4][0])
        if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved:
            abort(400)
    return url

@app.post("/preview")
def preview():
    url = safe_target(request.form["url"])
    resp = requests.get(url, timeout=5, allow_redirects=False)
    return {"title": extract_title(resp.text)}

Resolve the host and reject private, loopback, link-local and reserved addresses; allow only http and https; and disable redirects (or re-validate each hop), because a permitted host can redirect to a forbidden one. Stronger still is to route outbound fetches through an egress proxy that enforces an allowlist, so the policy lives in one place instead of every call site.

Why it is hard to see

  • The request never leaves through the edge. SSRF is the server making an internal, east-west call. A web application firewall and a CDN watch inbound traffic to the origin, not the origin's own outbound requests.
  • A hostname that resolves inward. The URL contains an ordinary public-looking hostname whose DNS record points at an internal address, so there is nothing in the string to match.
  • Redirect to an internal target. The submitted URL is on an allowed host and returns a redirect to 169.254.169.254 or localhost. A check on the original string passes.
  • Assembled or decoded in the app. The destination is built from a base and a user-supplied path, or decoded from an encoded parameter, so no single field is a URL.

What the es-python event looks like

es-python reports at the point the connection is actually made, which is the one place every one of those bypasses converges. The base event is socket connect, socket getaddrinfo or socket gethostbyname (the call requests, urllib and friends all funnel through); when the destination the untrusted value resolves to is a loopback, link-local, or private-range address, an ssrf event fires alongside it, tagged ssrf. Because this is decided on the address the program is dialling, an internal hostname and an in-application redirect both still fire: the final destination is what is checked, not the submitted string.

The ssrf label is a strong signal when it fires, and the base socket connect event on untrusted input shows every outbound destination the request steered. The severity follows the source. Under a Raise rule, the connection raises a RuntimeError before it is made.

How to fix and prevent it

  1. Resolve the host and reject private, loopback, link-local and reserved IP addresses, for every address a hostname resolves to.
  2. Allow only http and https, and an allowlist of hosts where you can.
  3. Disable redirects or re-validate every hop; the first URL is only the start.
  4. Route outbound fetches through an egress proxy that enforces the allowlist in one place.
  5. Lock down the metadata endpoint (require IMDSv2 on AWS) so a fetch that does slip through gets nothing useful.
  6. In Report mode, watch the ssrf tag and the socket connect events on request data to see every destination user input can steer.

Classification

SSRF is CWE-918, Server-Side Request Forgery. In the Top 10 for 2025 OWASP folded SSRF into A01:2025 Broken Access Control, which lists CWE-918 among its mapped weaknesses.

Questions people ask

The questions that come up on forums and in reviews about this class, each answered in a few sentences.

Why is SSRF dangerous in a cloud environment?

Because the server can reach what the attacker cannot: internal services, admin interfaces and the cloud metadata endpoint, which hands out instance credentials to any process on the machine. One forged request from a service that fetches user-supplied URLs can turn into cloud credentials. The fix is to decide the destination from an allowlist and to check the resolved address, not the submitted string.

Does validating the URL hostname stop SSRF?

Only partly: a hostname check on the submitted string is bypassed by a DNS name that resolves to an internal address, by an HTTP redirect to one, and by alternate spellings of the same IP address. Check the address the program is about to connect to, after resolution and after each redirect, and disable redirects where you can. es-python makes the same decision at the connection, on the final destination, which is why an internal hostname and a redirect both still produce an ssrf event.

How does es-python detect SSRF?

It reports at the point the connection is made, socket connect, socket getaddrinfo or socket gethostbyname, when the destination was chosen by untrusted data, and adds an ssrf event when that destination is a loopback, link-local or private-range address. The check is on the address the program is dialling, so an internal hostname and an in-application redirect both fire. It recognises the standard spellings of an address, not alternate encodings, and a Raise rule stops the connection before it is made.

Watch for Server-side request forgery (SSRF) in your own code

es-python runs your Python application unchanged and reports when untrusted data reaches the operation, with where the data came from and the line that used it. Ask to see it on your own application.

EyalSec Pricing Docs Security Research Vulnerabilities Play Security glossary About Contact עברית Login Get started Protect my Python app