On this page
Legacy IPv4 URL formats can make different-looking hostnames point to the same address. Before updating a URL allowlist, compare how your application parses those forms and how it checks the address used for the connection. A string that does not look like a conventional IP address may still resolve to loopback or another destination your service should not fetch.
Key Takeaways
- Some URL parsers normalize shortened, integer, and hexadecimal IPv4 forms into dotted-decimal addresses.
- Test the parser and HTTP client used in your deployment; do not assume every language or library interprets these inputs identically.
- Apply your destination policy to the parsed hostname and resolved IP addresses, and enforce it when connecting and following redirects.
- A successful parse or hostname match is not proof that a destination is safe.
Why legacy IPv4 URL formats matter
An IPv4 address is commonly written as four decimal components, such as 127.0.0.1. Some parsers also accept forms that represent the same address with fewer components, one integer, or hexadecimal notation. For example, a WHATWG-style URL parser may normalize 127.1, 2130706433, and 0x7f000001 to 127.0.0.1.
This matters when validation and fetching interpret the input differently. A filter that rejects the literal text 127.0.0.1 but passes 127.1 may be bypassed if the HTTP client subsequently treats both as loopback. Likewise, a rule that decides a host is a domain merely because it does not look like dotted-decimal IPv4 can misclassify an address.
The WHATWG URL Standard and the documentation for your language's URL parser are important references for the exact parsing rules. They were not retrieved for this article, and the examples below were not executed here. Verify the current documentation and behavior of your deployed parser and HTTP client before publishing or relying on an allowlist change.
Advertisement
Compare forms locally
The following Node.js script parses fictional local URLs only. It makes no network requests. It uses Node's URL implementation; other parsers and clients may produce different results.
Save it as compare-urls.js and run node compare-urls.js in the Node.js environment you use for your service:
const { URL } = require('node:url');
const inputs = [
'http://127.0.0.1/',
'http://127.1/',
'http://2130706433/',
'http://0x7f000001/',
'http://127.0.0.1:99999/'
];
for (const input of inputs) {
try {
const parsed = new URL(input);
console.log({
input,
serialized: parsed.href,
hostname: parsed.hostname,
host: parsed.host
});
} catch (error) {
console.log({ input, error: error.message });
}
}
For a WHATWG-style parser, expect the first four inputs to be interpreted as the same loopback address, and the invalid port to cause a parse error. Confirm that expectation in your installed runtime rather than treating it as an observed result from this article. hostname excludes the port; host includes a non-default port when present.
Repeat the comparison with the URL parser and HTTP client actually used by your application. Record both what the validator sees and what the client will connect to. If they disagree, resolve the discrepancy before accepting the input. Do not test by sending requests to public or third-party systems.
Apply the policy to the destination
Parsing is only the first step. For a URL-fetching feature, use a layered policy:
- Parse and restrict the URL. Permit only the schemes and ports your feature needs. Reject malformed URLs and, unless explicitly needed, URLs containing user information. Treat the parser's structured hostname as the hostname—not a substring extracted from the original input.
- Check the hostname policy. Compare canonical hostnames against an explicit allowlist. If subdomains are allowed, enforce a label boundary rather than using a plain suffix match:
notexample.commust not matchexample.com. Define how you handle case, trailing dots, and internationalized names. - Check IP addresses. If the hostname is an IP literal, apply your IP-range policy to the parsed address. If it is a domain, resolve it and apply the policy to the resulting addresses. For a public-only fetcher, prohibit loopback, private, link-local, and other non-public or otherwise prohibited destinations for both IPv4 and IPv6. Account for IPv4-mapped IPv6 addresses as well.
- Enforce the decision at connection time. Connect only to an address that was checked, and ensure the connection actually uses that address. A separate validation lookup followed by an unrestricted client lookup leaves a DNS-rebinding and time-of-check/time-of-use gap. A conservative policy can reject a DNS answer set if any address is prohibited.
- Revalidate redirects. Disable automatic redirects or intercept each one before following it. Parse and apply the full scheme, hostname, address, and connection policy to every target; limit the number of redirects and avoid forwarding credentials across origins.
Implementation depends on the client and deployment. If you pin a vetted IP for a domain, preserve the intended hostname for HTTP and TLS certificate verification. Make sure connection pooling, retries, and proxies cannot silently route a request to an unchecked destination. Enforce network egress restrictions as a separate layer in case application checks fail.
Advertisement
A hostname allowlist can still be useful, but it answers a different question from an IP-range policy. An allowed hostname might resolve to a prohibited address, and DNS can change between validation and connection. Conversely, blocking private IP ranges does not establish that every public destination is authorized for your application.
Common Mistakes
- Filtering raw strings alone. Alternate representations, normalization, user information, and ports can make string matching disagree with the client's interpretation.
- Validating with one parser and fetching with another. Differences in interpretation can invalidate an otherwise careful check. Exercise the complete validation-to-connection path in your own environment.
- Checking only the first URL. Redirects can move a request to a destination that would have failed the original policy.
- Checking DNS once but letting the client resolve again. The address used for the connection may differ from the one inspected.
- Ignoring operational limits. Even an authorized destination can consume resources. Set connection and response timeouts, response-size limits, and appropriate authentication and authorization for access to the fetching feature.
Best Practices
Maintain regression cases for conventional and legacy IP literals, prohibited IPv4 and IPv6 ranges, domain names with mixed allowed and prohibited DNS answers, redirects, and parser errors. Use controlled DNS and HTTP fixtures for connection and redirect tests; the parsing script above does not test either.
If your policy requires rejecting legacy notation outright, validate the original authority against a deliberately defined input grammar as well as checking the parsed destination. Normalization can erase evidence of the original spelling. Prefer a well-reviewed parser and validator over ad hoc substring rules, and confirm that the grammar and fetcher agree on what they accept.
Conclusion
Before changing an allowlist, compare legacy IPv4 forms with the parser and client you deploy. Then enforce hostname and IP-range rules on the destination actually used for every connection and redirect.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.