URL parsing differences
| Class | Injection |
|---|---|
| Also known as | URL parsing confusion, parser differential |
| Leads to | Open redirect, Server-side request forgery |
| Affects | Code that checks an address with one URL parser and then redirects to it or fetches it with another, or that checks the raw text |
The string http://example.com\@evil.com names two different servers, depending on the program that reads it. A parser that follows the WHATWG URL Standard, as browsers and the URL class in Node.js do, treats the backslash as a slash and finds the host example.com. Python's urllib.parse takes everything before the last @ as user information and returns evil.com.1 When an application checks an address with one reading and acts on the other, the check passes and the request goes somewhere it was meant to block. Such URL parsing differences let an address slip past the checks that guard against open redirects and server-side request forgery.
Two definitions of a URL
A URL is split into a scheme, an authority, a path, a query and a fragment. In RFC 3986, published in 2005, the authority follows // and ends at the next /, ? or #; inside it, an @ separates optional user information from the host.2 Earlier RFCs going back to RFC 1738 in 1994 defined the syntax differently, and libraries kept the rules they were written against so that old code would not break.3
The WHATWG URL Standard describes what browsers do with real input, and states its aim as replacing RFC 3986 and RFC 3987. It removes leading and trailing spaces and control characters, deletes tabs and newlines anywhere in the string, and for schemes such as http and https reads a backslash as a slash. User information and backslashes only raise a validation error, and parsing carries on with a result.4 Python's documentation says its functions take parts from both documents and comply with neither; urlsplit() does no validation, tabs and newlines have been stripped only since Python 3.10, and leading control characters and spaces since 3.12.5 Node.js ships two parsers side by side: the WHATWG URL class, and the older url.parse(), which its documentation calls lenient and non-standard, prone to host name spoofing, and unfit for untrusted input. No CVEs are issued for its flaws.6
Where the readings split
| Input | Naive or second reading | WHATWG reading |
|---|---|---|
http://example.com\@evil.com | Python urlsplit: host evil.com | host example.com, backslash read as a slash |
https://example.com@evil.example/ | Text search: contains example.com | user information example.com, host evil.example |
/\evil.example (as a redirect target) | Text check: starts with /, so a local path | host evil.example, same as //evil.example |
http://0x7f000001/ | Block list on the text 127.0.0.1: no match | hexadecimal IPv4 address, host 127.0.0.1 |
http://[::ffff:169.254.169.254]/ | IPv4 block list: no match | IPv6 address that reaches 169.254.169.254 |
The @ trick is older than the web standard: RFC 3986 gives ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm as an address a person reads as cnn.example.com when the host is 10.0.0.1, and it deprecates the user:password form.2 The same RFC notes that some systems read each dotted part of an IPv4 address as decimal, octal or hexadecimal, a form its own grammar forbids.2 The WHATWG parser accepts the 0x prefix and hands any host that ends in a number to its IPv4 parser.4 An IPv4-mapped IPv6 address reaches the same machine as the plain IPv4 form.1
Check with one parser, fetch with another
In 2022 researchers at Claroty and Snyk compared 16 parsers, among them Python's urllib, curl, Chrome, Java's URL and URI, PHP's parse_url, Node's url and the npm package url-parse, and Go's net/url. They sorted the disagreements into five kinds: a missing or malformed scheme, an unusual number of slashes, backslashes, percent-encoded data, and a URL of one scheme read without a parser for that scheme. Using them they found eight vulnerabilities, open redirects among them, in Flask-Security, Flask-User, Video.js, Nagios XI, the Ruby library Clearance and others; a libcurl bug they reported was patched by its maintainer.3
One case they analysed was a bypass of an early Log4j fix. After the December 2021 remote code execution flaw in Log4j, a Java logging library, the fix allowed JNDI lookups (the Java feature that connects to a named server and loads code from it) only to hosts on a list called allowedLdapHost, which by default held only localhost. The address ldap://127.0.0.1#.evilhost.com:1389/a got past it (CVE-2021-45046). Java's URI class, used for the check, read the host as 127.0.0.1, which was on the list. The code that made the connection read the # differently and, on some systems and configurations, mainly macOS, connected to 127.0.0.1#.evilhost.com.3
Parsing once
The defence is to parse the address once, decide on the parsed parts, and hand the parsed result onward instead of the original text. In JavaScript the WHATWG URL class does the parsing:6
const ALLOWED = new Set(["api.example.com", "cdn.example.com"]);
function checkedUrl(input) {
let url;
try { url = new URL(input); } catch { return null; }
if (url.protocol !== "https:") return null;
if (url.port) return null; // default port only
if (url.username || url.password) return null;
if (!ALLOWED.has(url.hostname)) return null;
return url; // use this object from here on, not input
}The host is compared with an exact allowlist, never searched for as a substring. Where the request is sent by a different library or service, the URL is parsed a second time on the way. The outgoing request is then built from the allowlist entry that matched, with a scheme, port and path fixed in the code, and a URL whose host differs between the parsers involved is rejected.1 Python code that uses urlsplit() checks every part it relies on before trusting it.5 For redirects within the site, the value is resolved against the site's own origin and the parsed origin is compared; the Open redirect article shows the code.
Find every place in this project where an address from a request, a form, a webhook, a database row or a file is checked and then used for a redirect, a fetch, an HTTP client call, an image proxy or a webhook delivery. For each one: parse the address once with the WHATWG URL class (new URL in JavaScript), compare the parsed scheme and hostname with an exact allowlist, reject addresses that carry a username or password, and pass the parsed URL on rather than the original string. Replace substring checks, startsWith checks, regular expressions on the raw text and Node url.parse() calls. List each file and line you changed.
In SecFlash
SecFlash reports the code where an address from a request decides a redirect as SF-SEC-742 and where it decides an outgoing server request as SF-SEC-716, both on the Security page. When a live request shows the site forwarding to an outside address, it reports SF-SEC-606.
See also
- Open redirect
- Server-side request forgery
- Host header attacks
- Input validation
- Cloud instance metadata service
- Category: Injection
References
- a b c "Server-Side Request Forgery Prevention Cheat Sheet". OWASP Cheat Sheet Series.
- a b c Berners-Lee, T., Fielding, R. and Masinter, L. (January 2005). "RFC 3986: Uniform Resource Identifier (URI): Generic Syntax". RFC Editor. IETF, STD 66.
- a b c Claroty Team82 and Snyk (January 2022). "Exploiting URL Parsing Confusion". Claroty.
- a b "URL Standard". WHATWG. Living standard.
- a b "urllib.parse: Parse URLs into components". Python documentation. Python Software Foundation.
- a b "URL". Node.js documentation. OpenJS Foundation.
External links
- URL Standard, WHATWG
- Server-Side Request Forgery Prevention Cheat Sheet, OWASP Cheat Sheet Series