Check a webhook URL against an allow-list, and test the shortcuts that fail
Validate a webhook URL against one trusted host. Python fixtures run thirteen inputs against seven designs and show which shortcuts leak.
A service lets a customer enter a callback URL, and the server later sends a request to it. If the check on that URL is weak, the server can be pointed at a host the customer should not reach. The OWASP Server-Side Request Forgery Prevention Cheat Sheet names this kind of feature, along with avatar-by-URL downloads, as a common enabler.
The sheet separates two cases. When the application only needs to call identified and trusted applications, it recommends an allow-list. When it must call any external address, an allow-list is not available and other defenses apply. This guide tests only a parsed-component boundary within the first case. C is a teaching fixture, not the sheet's complete recommended request-building pattern. The sheet also says to validate with an allow-list, to compare a valid IP or domain against the trusted list, to avoid validating a domain by resolving it, and to disable redirect following in the client. It warns that parsers can be abused. Its current guidance prefers accepting only an IP address or domain, matching an explicit trusted entry, and building the request from that entry with an application-fixed scheme, port and path. Do not carry a user-supplied URL's path or query into the next parser. The fixture accepts whole URLs to expose weak comparisons; it does not implement that safer request builder. This guide was prepared with AI assistance and reviewed on 18 November 2026. Nothing here makes a network request.
The inputs
The one trusted host is hooks.example.com. Three good URLs are tried: the plain one, an upper-case host, and one with :443. Ten bad ones are tried: a URL with the trusted name as credentials before an @, the trusted name as a prefix of another domain, a longer domain ending in the trusted name, a query string that mentions it, a loopback IP, a hex-form IP, a file: URL, plain http, port 8443, and a backslash before the @.
Seven designs
- A, substring. Accepts if the URL contains the trusted name. It accepts seven of the ten bad inputs and rejects the upper-case host.
- B, prefix. Accepts if the string starts with
https://hooks.example.com. It still accepts the credentials form, the longer domain and port 8443, and rejects the upper-case good host. - C, parse, then compare. Uses
urlsplit, then requires schemehttps, an exact host in the allow-list, no credentials, and port 443 or none. It accepts all three good URLs and none of the bad ones. - Four mutations of C. Removing the credentials check lets only the backslash input through. In that input the parser reports the trusted host and treats the text before the
@as credentials. Matching the host by suffix lets in only the longer domain. Dropping the port check lets in only port 8443. Dropping the scheme check lets in onlyhttp.
def c_parsed(url):
u = urlsplit(url)
return (u.scheme == 'https'
and u.hostname in ALLOWED_HOSTS
and u.username is None and u.password is None
and u.port in (None, 443))
The fixture asserts all ten-input accepted sets exactly and each of the three good outcomes for every design, so a mutation that leaks something extra, or nothing, fails the test. The version in the fixture also wraps parsing in a guard for malformed input, which the short listing leaves out.
Parser boundaries, not raw syntax validation
Eighteen extra probes exercise C separately from the ten-input mutation set. It refuses unmatched or non-IP brackets, nonnumeric, too-high or negative ports, missing hosts, relative and protocol-relative inputs, plain or empty credentials, a trailing-dot host and an encoded-dot host. Unexpected exceptions fail these probes; malformed bracket and port errors are not treated as success.
It accepts an empty port, zero-padded port 00443, an upper-case scheme and a fragment. On Python 3.10.12, urlsplit also strips tabs and newlines: a tab inside this host and a leading newline both reach the trusted-host comparison and are accepted. C therefore compares parsed components, not a canonical raw-URL grammar. These are observed parser behaviors, not recommendations for an HTTP client. Type validation and a stricter raw-input policy are outside this fixture.
Literal IP strings
The sheet notes that IP parsers differ in how they treat hex, octal and dword forms, and that libraries should supply the value you compare. Its list names Java, .NET, JavaScript and Ruby, and Python is not on it. The fixture checks Python's standard ipaddress.ip_address directly. It accepts 127.0.0.1 and ::1, and rejects 0x7f.1, 2130706433 and 0177.0.0.1. That is a result for Python 3.10.12 only.
What this does not cover
The backslash input exposes a parser boundary: Python reports the trusted host and puts the preceding text in credentials. This test shows only that parser's answer, not a comparison with a second parser or what an HTTP client does with the same string. It does not test DNS: a trusted name that resolves to an internal address is a separate risk, and the sheet requires validating resolved addresses against permitted networks and binding connections, retries and fallbacks to those validated addresses while preserving the hostname for Host, SNI and certificate verification. A second unchecked lookup can bypass validation; DNS monitoring alone is not enforcement. It does not test redirects, which the sheet says to disable. It also does not cover case 2, requests to any external address. An allow-list check does not replace network-layer controls.
Sources
- OWASP Server-Side Request Forgery Prevention Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html