Bypassing SSRF Filters
Introduction: When Simple Defenses Fail
In the previous lesson, you built solid defenses using manual IP range checking. Those defenses work against basic attacks, but determined attackers have tricks to bypass hand-written validation. Let's see how, and then upgrade to library-based validation that handles these edge cases automatically.
A blocklist that only checks for exact string matches like localhost or 127.0.0.1 can be defeated using alternative IP representations, URL encoding, redirect chains, and DNS rebinding techniques. In this lesson, you will learn how attackers bypass weak defenses and how to build validation that is truly robust using proper IP parsing libraries. By understanding both the attack and defense perspectives, you will be able to protect your applications against sophisticated SSRF exploitation.
Examining a Weak Blocklist Implementation
Let's look at a typical weak defense that many developers implement when they first learn about SSRF. This code appears to address the vulnerability by blocking dangerous hostnames, but its approach is fundamentally flawed because it relies on simple string matching.
The code starts correctly by parsing the user-provided url using the built-in URL class. This ensures we are working with a well-formed URL structure rather than a raw string. However, the protection mechanism that follows is where things go wrong.
This blocklist checks if parsedUrl.hostname exactly matches either localhost or 127.0.0.1. The problem is that IP addresses and hostnames have many alternative representations that resolve to the same destination.
The code assumes that blocking two specific strings is enough, but IP addressing is far more flexible than that. An attacker can represent 127.0.0.1 in hexadecimal, decimal, octal, or even shortened forms, and all of these will resolve to the same loopback address while bypassing this string-based check.
Once the weak validation passes, the code makes the HTTP request using axios.get(url). The comment hints at the problem: there are numerous representations that will bypass the blocklist but still reach localhost. This implementation gives developers a false sense of security while leaving the application completely vulnerable to SSRF attacks.
