Dev.to Security 🔐 Cybersecurity 👁 2 📖 3 min read

SSRF: How User-Controlled Fetches Can Reach Private Resources

Server-Side Request Forgery, or SSRF, happens when a server makes a request using input that comes from the user. If that input is not checked well, the server can be tricked into reading private paths, internal URLs, or

SSRF: How User-Controlled Fetches Can Reach Private Resources

Server-Side Request Forgery, or SSRF, happens when a server makes a request using input that comes from the user. If that input is not checked well, the server can be tricked into reading private paths, internal URLs, or other protected resources.

This lab shows a weak user-controlled fetch flow in Acme IT Support. The app loads a path from the form, then returns the file content as base64.

Example of the vulnerable idea:

<?php
$target = $_POST['target'];

// Unsafe, user input is used directly
$content = file_get_contents($target);
echo base64_encode($content);
?>

The problem is not only the fetch, it is also the bad check on the input value. If the app only blocks a word at the start, you can still try a different path that reaches the same private target.

A better design is to send a resource ID from the form, then map that ID to a server-side path. This is what many real apps do, because the browser never needs to know the file path.

Overview

You can see how the app first accepts normal choices, then blocks a direct private value, and later still allows access when the input is changed to x/../private. This shows why prefix checks are weak and why path validation must be done after normalization.

Ethical Considerations

SSRF testing should only be done on systems you own or have explicit permission to test. Unauthorized access to internal resources is illegal. A controlled lab is the right place to practice this kind of test.

Walkthrough

Step 1: Open the Target Page

We start by opening the account page in Acme IT Support. The page shows several selectable resources, each one mapped to a file path.

The browser developer tools show a radio input with a value that maps to a server-side file. That means the server trusts the selected path and uses it to load the file.

Step 2: Update a Resource Normally

We select a valid resource and submit the form to see the normal flow.

The update works and the page shows a base64 data URL in the background-image style. This confirms the server reads the file content and returns it to the page.

Step 3: Try a Private Value

Next, we change the radio value to private to test the filter.

This is the first check against the restricted path. If the app trusts the raw input, the request may reach something it should not.

Step 4: See the Prefix Block

The app blocks the request and shows an error.

The message says URL cannot start with private. This is a prefix check, so it only looks at the start of the string. That kind of rule is easy to bypass.

Step 5: Bypass the Check

We change the value to x/../private to keep the same final target while avoiding the blocked prefix.

The check only looks at the start of the value, so x/../private passes the prefix test. After path handling, it still points to the private resource.

Step 6: Read the Private Content

We submit the modified request and get the private content back.

The response contains base64 data from the internal path. The attack works because the app checks the raw string, not the resolved path.

Remediation

To reduce SSRF risk:

  • Accept a resource ID, not a file path, from the browser
  • Keep the resource catalog on the server, in code, config, or database
  • Resolve the final file path from trusted server-side data only
  • Reject any input that is not in the known resource catalog
  • Add network controls for SSRF defense in depth, such as internal IP blocks and metadata service protection
<?php
// The browser only sends resource_id
$resourceCatalog = [
    '1' => ['file' => 'assets/resources/1.bin'],
    '2' => ['file' => 'assets/resources/2.bin'],
    '3' => ['file' => 'assets/resources/3.bin'],
    '4' => ['file' => 'assets/resources/4.bin'],
    '5' => ['file' => 'assets/resources/5.bin'],
    '6' => ['file' => 'assets/resources/6.bin'],
];

$resourceId = $_POST['resource_id'];

if (!isset($resourceCatalog[$resourceId])) {
    die('Invalid resource selection');
}

$resourcePath = $resourceCatalog[$resourceId]['file'];
$content = file_get_contents($resourcePath);
echo base64_encode($content);
?>

If the application must work with user-supplied paths for some reason, normalize the path, resolve it against a fixed base directory, and reject anything outside that directory before any read happens.

Summary

SSRF attacks happen when a server fetches user input without enough validation. This lab shows how a simple prefix rule can be bypassed with a relative path like x/../private. The safer fix is to use an allowlist and validate the final resolved path before any fetch happens.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.