Block Private Hosts (SSRF)
Medium About 15 min Free, no account needed
Reject internal hosts so a user-supplied URL cannot make the server call itself or its neighbors.
The brief
The bug in plain terms: This is a Server-Side Request Forgery (SSRF), where your server fetches a URL the user hands it, so the user can aim your server at targets they could never reach themselves. By supplying an internal address (the server's own loopback, a machine on the private network, or the cloud metadata endpoint at
169.254.169.254that often hands out credentials), an attacker turns your trusted server into their proxy into the internal network. The trap: blocking the obvious namelocalhostis not enough, because the same internal machines are reachable through many other spellings (127.0.0.1,0.0.0.0,10.x,192.168.x,169.254.x), so a safe check must reject every internal IP range rather than a few magic words.
A webhook feature fetches a URL the user provides. Before it does, it calls
IsPublicHost on the URL's host to decide whether the fetch is allowed. The
current check only blocks the literal localhost, so a user can still point the
server at its own loopback address, the private network around it, or the cloud
metadata endpoint, a server-side request forgery (SSRF) that reaches resources
the user never should.
Return true only when host is safe to fetch (a public destination), and
false for any internal target.
Treat as internal: localhost, 0.0.0.0, and any host in the loopback
(127.x), private (10.x, 192.168.x), or link-local (169.254.x) ranges.
- Functional tests: ordinary public hosts are allowed;
localhostis blocked. - Security tests: loopback, private, and link-local targets must be blocked. The naive starter allows them.
Starter code
using System;
public class Solution
{
// Decides whether a user-supplied host is safe for the server to fetch.
public bool IsPublicHost(string host)
{
// BUG: only the literal "localhost" is blocked. The loopback IP, the private network, and
// the link-local metadata address all sail through, enabling SSRF.
return host != "localhost";
}
}How it is graded
Functional tests and adversarial payloads both have to pass before the solution is accepted.
Related guides
What it practices
- Server-side request forgery
- Block requests to private, loopback and alternately spelled internal addresses.
Related katas
- Alternate IPv4 Spellings (SSRF) Medium Pro Read octal, hex, and shortened IPv4 hosts the way the URL parser does, so another spelling of an internal address cannot slip past the SSRF filter.
- Comment Renderer (HTML Injection) Easy Free Fix a stored-XSS hole in a comment renderer without breaking its output.
- Filename Sanitizer Easy Free Reduce an uploaded filename to a safe, single path segment.
- Log Forging (CRLF) Easy Free Stop user input from forging extra log lines with embedded newlines.