Skip to content
Visual explainer 2h 30m to learn 4 steps

Broken access control & SSRF

Spot missing server-side authorization checks and explain SSRF risks in the cloud.

By Invicly Tech, , in Cyber Security Fundamentals. About 10 minutes to read.

Topic 26 of 49

In short

Broken access control lets a user reach data or actions they should not, such as changing an ID to view another's record (IDOR). Server-side request forgery tricks a server into making requests, often to internal or cloud metadata endpoints. Both demand checks on the server.

Explained simply

Access control decides who may see or change what. Broken access control means the server forgets to check. The most common example is IDOR (insecure direct object reference): you see your invoice at /invoices/1001, change the number to 1002, and the server happily shows a stranger's invoice.

The app's screens might hide buttons and links you should not use, but that is not security. Anyone can send requests directly with tools like curl. The server must check every request, every time, as if the user interface did not exist.

Server-side request forgery (SSRF) is a different trick. Some features make the server fetch a URL for you, like importing a profile picture from a link or sending a webhook. If the server fetches whatever address it is given, an attacker can make it reach places only the server can reach.

In the cloud this is especially dangerous. Each virtual machine can ask a special internal address for information about itself, sometimes including temporary access keys. An SSRF bug that reaches that address can leak those keys.

Press Play on a diagram to walk through it one step at a time.

SequenceSSRF to metadata
Attacker
Vulnerable server
Metadata endpoint
  1. 1Give me this URL (internal)
    Attacker to Vulnerable server: Give me this URL (internal)
  2. 2Server fetches it
    Vulnerable server to Metadata endpoint: Server fetches it
  3. 3Returns secrets
    Metadata endpoint to Vulnerable server: Returns secrets
  4. 4Leaks them in response
    Vulnerable server to Attacker: Leaks them in response
CompareTwo server-side failures
Broken access control (IDOR)
  • User asks for a record they do not own
  • Server skips the ownership check
  • Leaks or changes others' data
  • Fix: per-object check on every request
SSRF
  • User supplies a URL
  • Server fetches it from inside the network
  • Reaches internal services and metadata
  • Fix: allow-lists, IP checks, egress proxy, IMDSv2

In depth

Horizontal, vertical and hidden access control failures

Horizontal escalation is reaching another user's data at the same level, such as IDOR on invoices. Vertical escalation is reaching higher privileges, such as a normal user calling /admin/users/delete because the endpoint only checks that someone is logged in.

Some failures hide in the request body. Mass assignment happens when an app copies every JSON field into a database object, so a user adds "role": "admin" to their profile update and it is saved. Others hide in methods: GET /api/orders/7 is checked, but DELETE /api/orders/7 is not.

Random IDs like UUIDs make guessing harder, but they are not access control: IDs leak through URLs, logs and shared links. The real fix is an ownership or permission check on the server for every object, for every method. A neat way to do it is to include the owner in the query itself: SELECT ... WHERE id = ? AND owner_id = ?. Then another user's record simply is not found.

  • Check on every endpoint and every HTTP method
  • Allow-list fields a user may update; never bind the whole body
  • Return 404 for records the user may not see

Enforcing authorization on the server

Good authorization is central, consistent and tested. Put the decision in one place: a middleware, a decorator, or a policy function like can(user, "invoice:read", invoice). Then every handler calls it the same way, and reviewers can see the rules in one file. Deny by default: if no rule allows an action, it is refused.

Write tests for it. For each sensitive endpoint, test three users: the owner (allowed), another user at the same level (refused) and an admin (whatever the policy says). These tests catch the most common regression, where a new endpoint is added without a check.

Log every refusal with the user, the action and the object ID. A user collecting many refusals in a short time, especially on sequential IDs, is probably probing. Rate limits on object lookups also slow down mass enumeration if a bug slips through.

FlowAuthorization check on every request
  1. 1
    Authenticate

    Who is this user?

  2. 2
    Load object

    Invoice 1002

  3. 3
    Policy check

    Owner, role, tenant match?

  4. 4
    Deny by default

    404 or 403 and log it

  5. 5
    Allow

    Return only permitted fields

SSRF and cloud metadata endpoints

SSRF appears wherever the server fetches a URL that the user influences: link previews, image import by URL, PDF generators that load HTML, webhooks and file importers. The attacker supplies an address that the server can reach but they cannot: localhost admin panels, internal services on private IP ranges, or the cloud metadata service.

On AWS, Azure and Google Cloud, the metadata service is at 169.254.169.254, a link-local address reachable only from the machine itself. It can return instance details and, if a role is attached, temporary credentials. In 2019, an SSRF-style attack against a misconfigured firewall let an attacker take such credentials from Capital One and read data from storage buckets.

The providers added protections. Google Cloud requires a Metadata-Flavor: Google header and Azure a Metadata: true header, which simple SSRF often cannot add. AWS IMDSv2 requires a session token obtained first with a PUT request, and its default hop limit of 1 stops the token from reaching most containers and proxies. Enforce IMDSv2 on every instance.

SequenceWhy IMDSv2 blocks simple SSRF
Attacker
Vulnerable app
Metadata service
  1. 1Fetch http://169.254.169.254/...
    Attacker to Vulnerable app: Fetch http://169.254.169.254/...
  2. 2Plain GET, no token
    Vulnerable app to Metadata service: Plain GET, no token
  3. 3401: token required (IMDSv2)
    Metadata service to Vulnerable app: 401: token required (IMDSv2)
  4. 4Error, no credentials leaked
    Vulnerable app to Attacker: Error, no credentials leaked

Restricting outbound requests from servers

Defend SSRF in several layers, because each one alone can be bypassed. In the application, allow-list the destinations a feature really needs, such as one partner's API domain. If arbitrary URLs are required, allow only http and https, resolve the host name, and reject private, loopback, link-local and other reserved addresses. Then connect to the IP you checked, so a second DNS lookup cannot return a different address (DNS rebinding), and either disable redirects or re-check every redirect target.

In the network, give servers no direct internet access by default. Send required outbound traffic through an egress proxy that enforces an allow-list and logs every destination. Block 169.254.169.254 for workloads that do not need metadata, for example with a network policy in Kubernetes.

For detection, alert on internal or metadata addresses appearing in request parameters, on outbound connections from web servers to unusual destinations, and on cloud credentials being used from IP addresses outside your environment.

  • Private ranges: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
  • Also block 127.0.0.0/8, 169.254.0.0/16, ::1, fc00::/7, fe80::/10
  • Disable file://, gopher:// and other unneeded schemes

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Explain IDOR with an example~40m
  2. 2
    Enforce authorization on the server~40m
  3. 3
    Explain SSRF and metadata endpoints~40m
  4. 4
    Restrict outbound requests from servers~40m

Key points

  • Authorization must be enforced on the server, never the client.
  • IDOR exposes records by changing an identifier.
  • SSRF makes a server fetch attacker-chosen URLs.
  • Restrict outbound requests and block metadata endpoints.

Key terms

Tap a card to flip it. 0/4 known

Examples

Fixing an IDOR with an owner-scoped query

Step 1: the vulnerable handler loads an invoice by ID only, so any logged-in user can read any invoice. Step 2: change the query to include the current user's ID, so another user's invoice is simply not found. Step 3: return 404, so the attacker cannot even confirm the ID exists. Step 4: log the miss with the user and the requested ID. Step 5: add a test where user B requests user A's invoice and expects 404.

python
# Before (vulnerable):
#   row = db.execute("SELECT * FROM invoices WHERE id = ?", (invoice_id,)).fetchone()

def get_invoice(db, current_user_id: int, invoice_id: int):
    row = db.execute(
        "SELECT id, total, status FROM invoices WHERE id = ? AND owner_id = ?",
        (invoice_id, current_user_id),
    ).fetchone()
    if row is None:
        log.warning("invoice access denied user=%s invoice=%s", current_user_id, invoice_id)
        return 404, {"error": "not found"}
    return 200, dict(row)

Checking a URL before the server fetches it

A link preview feature fetches user-supplied URLs. Step 1: allow only https. Step 2: resolve the host name and check every address it returns. Step 3: reject private, loopback, link-local, reserved and multicast addresses, which covers 169.254.169.254 and localhost. Step 4: when fetching, connect to the IP you just checked and turn off automatic redirects, or repeat the check for each redirect. Step 5: as a second layer, route these fetches through an egress proxy and enforce IMDSv2 on the host.

python
import ipaddress, socket
from urllib.parse import urlparse

def safe_target(url: str):
    u = urlparse(url)
    if u.scheme != "https" or not u.hostname:
        return None
    ips = {info[4][0] for info in socket.getaddrinfo(u.hostname, u.port or 443)}
    for ip in map(ipaddress.ip_address, ips):
        if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved or ip.is_multicast:
            return None
    return ips   # connect only to these IPs, with redirects disabled

print(safe_target("https://169.254.169.254/latest/meta-data/"))   # None

Quick reference

  • IDOR: change an ID, see someone else's data; fix with per-object server-side checks
  • Hiding a button is not access control; the server must check every request
  • Scope queries by owner: WHERE id = ? AND owner_id = ?
  • Never bind the whole request body to a model; allow-list editable fields
  • Cloud metadata lives at 169.254.169.254; SSRF targets it for credentials
  • Enforce IMDSv2: aws ec2 modify-instance-metadata-options --http-tokens required
  • SSRF defence: allow-list, block private IPs, pin the resolved IP, no redirects
  • Alert on 169.254.169.254 or localhost inside request parameters

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A normal user sends POST /admin/users/5/delete and it works. What kind of failure is this?

Question 2

2Switching invoice IDs from numbers to random UUIDs is best described as:

Question 3

3What does AWS IMDSv2 require that blocks many simple SSRF attacks?

Question 4

4A URL check resolves a host name to a public IP, but the HTTP library resolves it again and gets 127.0.0.1. What is this bypass called?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    List three features in a typical web app that could be vulnerable to SSRF and three endpoints that need an ownership check.

  2. Exercise 2medium

    Write automated tests for an endpoint GET /api/notes/{id}: the owner gets 200, another user gets 404, and an anonymous request gets 401.

  3. Exercise 3hard

    Find every EC2 instance in a lab AWS account that still allows IMDSv1 and switch it to IMDSv2 only.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between IDOR and broken access control?
IDOR is one specific type of broken access control: reaching an object by changing its identifier. Broken access control also includes missing role checks, mass assignment and more.
Is SSRF only a cloud problem?
No. It can reach any internal service, such as admin panels, databases with HTTP interfaces or internal APIs. The cloud metadata service just makes the impact very visible.
Should I return 403 or 404 when a user asks for someone else's record?
Many teams return 404, so attackers cannot tell whether the record exists. Either is acceptable if it is consistent and the refusal is logged.

Go deeper

ShareXLinkedInWhatsApp