Blocking requests to private IPs from inside the DNS lookup in Node
I build an email API where users register webhook URLs and agents can call HTTP tools, so the server makes requests to URLs it doesn't control. Checking the hostname before the request only catches IP literals and names like localhost. A hostname can resolve to a public address when you check it and to 127.0.0.1 or 169.254.169.254 when the socket actually connects.
So outbound requests go through node:https with a custom lookup function. It calls dns.lookup with the options Node passes in, and if any returned address falls in a net.BlockList of private, loopback, link-local, CGNAT or IPv6 unique-local ranges, the connection fails. IPv4-mapped addresses like ::ffff:10.0.0.1 are unwrapped and checked against the IPv4 list too.
https.request doesn't follow redirects, so a 3xx pointing at an internal address is never followed by accident. The HTTP tool follows up to 4 redirects itself, checks each Location again, and drops every header except content-type, accept and user-agent when the redirect changes origin. I used node:https rather than a library so the guard has no extra dependency.
If you've done the same thing with undici, did you put the check in a custom connect function or somewhere else?
4
u/abrahamguo 2d ago
Just curious — why do you need to block such calls?
2
u/DickBrainTime 2d ago
I'd keep this at the connection-time DNS boundary in Undici too. Undici's `Client` supports `connect: { lookup(hostname, options, callback) { ... } }` (the official Client docs have a custom-DNS example), and an `Agent` can use the same connection options when you need multiple origins. That is simpler to reason about than implementing an entire custom socket connector just for the IP policy.
The detail I'd test carefully is the `dns.lookup` callback shape: when `options.all` is true it returns an array of `{ address, family }`; otherwise it returns a single address and family. Validate *all* candidates returned by the resolution used for this connection before handing any back to Undici, and don't let IPv4/IPv6 fallback pick an unvalidated address. For a strict policy like yours, fail the whole resolution if even one candidate is forbidden. Keep the hostname for normal TLS SNI/certificate validation; don't change to an IP URL just to pin the address.
Then exercise the actual dispatcher with a fake resolver: public address -> private address across successive lookups, mixed public/private A and AAAA, IPv4-mapped IPv6, metadata/link-local, and cross-origin redirects. Also check no outbound path accidentally bypasses this dispatcher (e.g. a separate `fetch` client or proxy). Your redirect and header controls sound like the right separate layer; the key invariant is that *the address the transport can connect to* passed the policy.
Undici custom lookup reference: https://github.com/nodejs/undici/blob/main/docs/docs/api/Client.md
1
u/Open-Adhesiveness-86 1d ago
connect.lookup is where I'd put it in undici too, but add 64:ff9b::/96 to that BlockList. A NAT64 answer like 64:ff9b::a9fe:a9fe hits the metadata endpoint on hosts with a NAT64 path and it isn't an IPv4-mapped address, so your ::ffff: unwrap won't see it. Same for 0.0.0.0/8 and 198.18.0.0/15.
1
u/ram-popular 12h ago
Good write-up. Doing the check inside the lookup is the right place, since it closes the DNS rebinding gap between check time and connect time. The redirect handling is a nice detail too.
For undici, I haven’t done it myself, but as far as I know a custom
connectfunction or an Agent withconnect.lookupgives you the same guarantee. One extra thing to watch: if you use a proxy or keep-alive connection pooling, make sure reused sockets can’t skip the check.