Exploiting and Defending Against SSRF in URL Import
Introduction: From Theory to Exploitation
In the previous lesson, we learned what Server-Side Request Forgery (SSRF) is and why it poses serious security risks. We explored how attackers can trick servers into making HTTP requests to unintended destinations, potentially accessing internal networks and cloud infrastructure.
Now it is time to move from theory to practice. In this lesson, you will exploit a real SSRF vulnerability in our pastebin's URL import feature, accessing resources that should be completely off-limits. Then, you will learn how to properly defend against these attacks by implementing three layers of protection: URL validation, hostname blocklists, and DNS resolution checks. By the end of this lesson, you will understand both the attacker's perspective and the defender's toolkit.
Examining the Vulnerable Import Endpoint
Let's start by examining the code that makes our pastebin's import feature vulnerable. This is located in the import.ts route handler, and it is a perfect example of how well-intentioned features can introduce critical security flaws. The feature allows users to create a new snippet by providing a URL to existing code hosted elsewhere, which sounds useful and harmless at first glance.
Here is the vulnerable endpoint:
The endpoint starts by extracting the url and optional title from the request body. Notice the axios interceptor at the top — this simulates a real-world scenario where the AWS metadata endpoint at 169.254.169.254 is accessible from within the server's network. In production AWS environments, this endpoint is actually reachable from EC2 instances, but in our learning environment, we redirect these requests to a mock service running on port 3002. This allows you to practice the attack safely while understanding how it would work in reality.
Now look at what happens next:
This is where the vulnerability lives. The code takes the url parameter directly from user input and passes it straight to axios.get() without any validation whatsoever. There are no checks on what protocol the URL uses, what hostname it points to, or whether that hostname resolves to an internal IP address. The server blindly trusts that the user provided a legitimate external URL.
After fetching the content, the code handles various response types (converting JSON objects to strings, for example) and creates a snippet with a randomly generated ID. Notice that userId is set to 0 to represent anonymous or imported content — this is important because the Snippet model requires a userId field even for content that doesn't belong to any particular user.
Why is this dangerous? Remember that your server has privileges that external users do not have. It can access internal network resources, localhost services, and cloud metadata endpoints. When you pass a user-controlled URL directly to axios.get(), you are essentially giving attackers the ability to make HTTP requests from inside your trusted network. They can target any resource your server can reach, completely bypassing firewalls and network segmentation that would normally protect those resources.
The critical mistake here is treating user input as trustworthy. In security, we always assume user input is malicious until proven otherwise. This endpoint does the opposite — it assumes the URL is safe and legitimate without performing any validation checks.
