Treat URL fetching as an untrusted network operation
Link previews, webhook testers, document importers, image downloaders, website summarizers, and AI knowledge-base pipelines all make HTTP requests on a user's behalf. The risk is not limited to downloading malicious content. If an attacker controls the destination, the application may be induced to contact localhost services, private network hosts, container management interfaces, administrative endpoints, or cloud metadata services. Because the request originates from a trusted backend network, it can reach systems that were never intended to be exposed publicly.
Start by defining the smallest capability the product actually needs. A service that only retrieves page titles and thumbnails should not accept arbitrary protocols, ports, HTTP methods, headers, or request bodies. If every legitimate source belongs to known partners, use an explicit domain allowlist. Adopt a broader public-internet policy only when arbitrary public sites are a real requirement. A narrower capability produces simpler validation rules, clearer monitoring, and a smaller incident-response surface.
Validate the resolved destination, not just the URL string
Blocking strings such as localhost, 127.0.0.1, and a few familiar private ranges is inadequate. URLs may contain IPv6 addresses, unusual numeric IP representations, user-information fields, misleading hostnames, or DNS records that resolve to private infrastructure. Use a well-maintained URL parser, allow only http and https, reject missing hosts and embedded credentials, and restrict ports to those the feature requires. Normalize the hostname before applying domain policy, while preserving the original hostname for TLS and HTTP semantics.
Resolve every A and AAAA record and evaluate every returned address. Deny RFC 1918 networks as well as loopback, link-local, multicast, unspecified, reserved, documentation, and other non-publicly routable IPv4 and IPv6 ranges. Explicitly block cloud metadata addresses and platform-specific control endpoints rather than assuming that every sensitive target falls into one familiar private range.
- Resolve and pin: after approving a DNS result, make the connection to that approved IP so the client cannot perform a second lookup and fall victim to DNS rebinding.
- Preserve the hostname securely: when connecting to a pinned IP over HTTPS, send the original hostname as SNI and validate the certificate against it. Never disable certificate verification to simplify pinning.
- Revalidate every redirect: parse, resolve, and authorize each Location target with the same policy, and enforce a low redirect limit. A safe first URL can redirect to an unsafe destination.
- Reject ambiguity: if the validator, HTTP library, and proxy might interpret a URL differently, fail closed instead of guessing which interpretation is correct.
Make network isolation the second line of defense
Application validation can fail because of library changes, proxy behavior, or an overlooked address format. Fetching therefore should not run directly inside the primary application process. Place it in a dedicated worker, container, or sandbox that holds no database password, cloud administration role, or internal API credential. Route its traffic through a controlled egress proxy that permits only the public destinations required by the product.
At the network layer, use firewall rules, network policies, or proxy controls to deny private CIDRs, host-local services, metadata endpoints, and platform control planes. Where the architecture permits it, the fetching zone should have no route to production VPCs, VPN-connected offices, management networks, or internal service discovery. This adds another deployable component and observation point, but it limits the impact of a validation defect to a low-privilege environment. For sensitive systems, that containment is more dependable than an ever-growing application blacklist.
If the product legitimately needs internal content, such as importing documents from a corporate repository, build a separate integration path. Give it explicit connectors, fixed destinations, a dedicated service identity, and narrowly scoped authorization. Do not give a public, arbitrary-URL fetcher internal routing and attempt to distinguish public mode from internal mode with a request parameter. Configuration mistakes and confused-deputy behavior can erase that boundary surprisingly easily.
Limit responses, record decisions, and test failure paths
Even after the destination is approved, the fetcher must handle slow responses, compression bombs, huge files, and endless streams. Configure connection, read, and total deadlines. Limit redirects, response bytes, and decompressed size, and accept only content types the workflow needs. Inspect headers first, stream the body, and stop immediately when a limit is exceeded rather than buffering the entire response in memory. Bound concurrency, per-origin request rates, and queue depth so the feature cannot become a resource-exhaustion tool.
Logs should capture the original and normalized URL, resolved addresses, policy decision, redirect chain, response size, timeout, and blocking reason. They should not retain passwords embedded in URLs, sensitive query values, or complete response bodies. Alert on repeated requests to private ranges, metadata services, unusual ports, or many different internal-looking addresses. Return consistent public errors so status details and timing differences do not become an oracle for discovering which internal hosts exist.
Before release, automate cases covering IPv4, IPv6, rebinding, cross-domain redirects, unusual IP notation, timeouts, oversized responses, and conflicting DNS answers. Run integration tests against the real egress policy, not only unit tests around a validation function. The practical completion criterion is defense in depth: if one parser check fails, the worker still cannot reach sensitive networks or obtain privileged credentials. When a platform also integrates cloud services, ERP, CRM, and private data sources, security and integration engineers should define this boundary together so operational exceptions do not quietly dismantle it.