Skip to content
Katabench
Try free

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.254 that often hands out credentials), an attacker turns your trusted server into their proxy into the internal network. The trap: blocking the obvious name localhost is 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.

How this is graded

  • Functional tests: ordinary public hosts are allowed; localhost is blocked.
  • Security tests: loopback, private, and link-local targets must be blocked. The naive starter allows them.

Starter code

C#
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.

C# secure coding exercises →

Related guides

What it practices

Server-side request forgery
Block requests to private, loopback and alternately spelled internal addresses.

Related katas