Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 80 ยท Web Application Security

SSRF, Unsafe Deserialization and XXE

Learn how server-side request forgery, pickle deserialization and XML external entities work, and how to defend with allow-lists, safe formats and strict parsers.

The previous lessons dealt with input that ends up in a query or a page. This one covers three cases where the "input" is something the server then acts on in a powerful way: a URL that the server fetches (SSRF), a serialized object that the server rebuilds (unsafe deserialization), and an XML document that can pull in other resources (XXE). In each case the server does work on the attacker's behalf, from inside the trusted network, with the server's own permissions.

All three share a defensive principle: do not give untrusted data the ability to choose what the server does. Restrict destinations to an allow-list, use data-only formats such as JSON, and configure parsers so they cannot load anything external. The demos here are defensive checks on fake data; nothing makes a real network request. They build on Input Validation and Sanitization and map to the Top 10 entries in The OWASP Top 10.

Server-side request forgery (SSRF)

Many features make the server fetch a URL that a user supplies: "import from URL", webhooks, link previews, PDF generators, image proxies. In SSRF, the attacker supplies a URL that points somewhere they could not reach themselves:

  • http://127.0.0.1:8080/admin, an admin interface that only listens on the local machine.
  • http://10.0.0.12/, an internal service on the private network.
  • http://169.254.169.254/..., the cloud metadata endpoint, a link-local address that, on many cloud platforms, hands out credentials for the machine's role. This is the classic high-impact SSRF target.

The server sits inside the firewall and is trusted by internal systems, so a successful SSRF turns it into a proxy into the private network. Even "blind" SSRF, where the response is not shown, can scan internal ports by measuring errors and timing, or trigger actions via internal GET requests.

flowchart LR
    A["Attacker"] -->|"url=http://169.254.169.254/"| W["Web app<br/>fetches the URL"]
    W -->|"request from inside the network"| M["Metadata service<br/>or internal admin"]
    M -->|"credentials or data"| W
    W -->|"returned or leaked"| A
    F["Firewall"] -.->|"blocks direct access"| A

The firewall stops the attacker, but not the server acting for them.

Defending with allow-lists

The best defense is to avoid the feature or to restrict it to a small allow-list of known hosts (for example your image CDN). When users really can name arbitrary public sites, apply several layers:

  1. Parse the URL with a real parser and allow only https (and http if you must).
  2. Resolve the host name to IP addresses yourself and reject loopback, private, link-local, multicast, reserved and unspecified ranges, including IPv6 and IPv4-mapped IPv6 forms.
  3. Connect to the IP you validated, not to the name again. Otherwise an attacker's DNS server can answer with a public address for your check and an internal address for the real request ("DNS rebinding").
  4. Do not follow redirects blindly: revalidate every hop, or disable redirects.
  5. Limit response size and time, and do not return raw response bodies or detailed errors to the user.
  6. Put the fetcher in a network segment with no access to internal services, and require the newer metadata protocol versions that need a special header. Network controls survive bugs in your code (Firewalls, Segmentation and Zero Trust).

The checker below uses a fake in-memory "DNS" so it is deterministic:

Python A URL fetch guard

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what the guard does not trust: the host name (it checks what it resolves to), the number format (2130706433 is 127.0.0.1 written as one integer) and the scheme (file: and gopher: can read files or talk to odd services). In production the real DNS answer must be pinned for the connection, which the next example illustrates.

Python DNS rebinding and why you pin the IP

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first function validates one answer and then connects using another. That gap between check and use is the whole attack. Pinning removes the gap.

Unsafe deserialization

Serialization turns objects into bytes to store or send; deserialization rebuilds them. Some formats can describe not just data but instructions for constructing arbitrary objects. Python's pickle is the best-known example: loading a pickle can call any importable function with chosen arguments. The Python documentation warns against unpickling untrusted data, and the lesson is the same in other languages (Java native serialization, PHP unserialize, .NET BinaryFormatter, YAML's full loaders): never deserialize untrusted input with a format that can instantiate arbitrary types.

This demo is harmless on purpose. The "attack" object makes pickle.loads call print, which proves that code selected by the data ran during loading. A real attacker would choose a far worse function.

Python pickle runs code chosen by the data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The restricted unpickler works here because plain data (dicts, lists, strings, numbers) needs no imported classes. It is a mitigation for special cases, not a guarantee, and allow-lists of classes are easy to get wrong. The preferred fixes, in order:

  1. Use a data-only format such as JSON, with validation of the structure afterwards, or a schema-based format like Protocol Buffers.
  2. If you must exchange pickles between your own services, authenticate them with an HMAC over the bytes before loading (HMAC and Message Authentication). A forged pickle then never reaches loads. Anyone who holds the key can still make you execute code, so protect it.
  3. Never unpickle files from users, caches that others can write, or network input.

Deserialization flaws also appear when frameworks accept a "type" field in JSON ("create an object of class X") or when untrusted YAML is loaded with a full loader. Python's safe YAML loader and the habit of mapping JSON into your own plain classes avoid both (Python Security Pitfalls).

XML External Entities (XXE)

XML can define entities, named pieces of replacement text, in a DOCTYPE section at the top of a document. An external entity says "replace this name with the contents of this URL or file". If a server parses XML from users with a parser that resolves external entities, the attacker can make it read local files, make requests to internal URLs (SSRF again) or consume huge amounts of memory with nested entity expansion (the "billion laughs" denial of service).

A document of that kind looks like this (shown as text, never parsed here):

XML
<?xml version="1.0"?>
<!DOCTYPE order [
  <!ENTITY secret SYSTEM "file:///etc/hostname">
]>
<order><note>&secret;</note></order>

Defenses:

  • Turn off DTDs and external entities in the parser. Most modern parsers either disable them by default or have a switch. Older Java, .NET and PHP/libxml configurations were insecure by default, which is why XXE became a famous category.
  • In Python use the defusedxml package, which refuses these constructs, rather than relying on the behaviour of the standard parsers.
  • Prefer JSON when you control the format.
  • Be careful with indirect XML: SVG images, Office documents, SOAP and SAML messages are all XML underneath.
  • Run the parser with minimal file and network permissions.

Without the extra package, you can still write a conservative pre-check that rejects any document containing a DTD or entity declaration, then parse only what passes. A document that legitimately needs a DOCTYPE is rare in web APIs.

Python Rejecting DTDs before parsing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This pre-check is a blunt but safe rule. After it passes, you still validate the content (types, ranges, allowed attributes) like any other input.

The common thread

SSRF, deserialization and XXE all happen when a rich input format lets the sender instruct the server. The defenses repeat across the three: allow-list what is permitted (hosts, classes, element types), prefer simple data formats, check at the moment of use, and limit what the process can reach even if a check is bypassed. This is least privilege and defense in depth applied to parsers and fetchers. Next you will see how headers can add browser-side protections to the same applications.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What makes SSRF dangerous even when the attacker cannot reach the internal network directly?

    The server is trusted by internal systems. By controlling the URL it fetches, the attacker borrows that trust.
  2. 2. Why should an SSRF guard connect to the IP address it validated rather than resolving the name again?

    A hostile DNS server can answer differently between the check and the connection. Pinning the checked IP removes that gap.
  3. 3. Why is pickle.loads on untrusted data dangerous?

    Pickle can reconstruct objects by calling importable callables chosen by the data, so loading it can run attacker-selected code.
  4. 4. Which is the best fix for exchanging data with untrusted clients?

    JSON can only describe plain data, so it cannot instruct the server to call functions. Validate the shape after parsing.
  5. 5. What should you do about XML External Entities?

    External entities let a document read files or request URLs. Parsers should refuse them, and the parser process should also run with minimal permissions.

Summary

  • SSRF turns a server-side URL fetch into a proxy into the internal network and cloud metadata; allow-list destinations, check resolved IPs, pin the IP, restrict redirects and isolate the fetcher on the network.
  • Block loopback, private, link-local and reserved ranges, including odd forms such as decimal IPs and IPv4-mapped IPv6.
  • Deserializing untrusted data with pickle-like formats can execute attacker-chosen code; use JSON or another data-only format instead.
  • If binary object exchange between your own systems is unavoidable, authenticate the bytes with an HMAC before loading.
  • XXE abuses XML entities to read files, reach internal URLs or exhaust memory; disable DTDs and external entities, and use defusedxml in Python.
  • The shared pattern: do not let untrusted data decide what the server does.

Next: HTTP Security Headers, response headers that make browsers enforce several of these protections for you.