Repository navigation
Description
Feature Description
Expose two functions on AntiSSRFPolicy, both based on code that already exists:
getDnsLookup(): LookupFunction: returns the policy-enforcinglookupthat getHttpAgent()/getHttpsAgent() already install (antiSSRFDnsLookup).isNetworkConnectionAllowed(ipaddresses: string[]): boolean: a public version of_isNetworkConnectionAllowed. Needed for IP-literal hosts, which never go through alookup.
Problem Statement
AntiSSRFPolicy can currently be enforced only through getHttpAgent()/getHttpsAgent(). Two common Node.js paths don't use an http.Agent:
- undici /
fetch: Node's built-in fetch and libraries that require a fetch implementation use undici, which ignores http.Agent but acceptsconnect: { lookup }. net.connect/tls.connect: for example, a forward proxy that handles CONNECT tunnels so a headless browser's requests are checked.
The lookup function the agents use internally (antiSSRFDnsLookup) isn't exported, so there's no way to apply the policy to these connections. To do it today, callers have to write their own lookup that resolves the host and then calls the underscore-prefixed _isNetworkConnectionAllowed(). That method is internal and may change without notice, or the alternative is reimplementing the whole check.
Proposed Solution
Additive and non-breaking: the existing agents and internal method stay as they are. Expose antiSSRFDnsLookup helper as getDnsLookup, and _isNetworkConnectionAllowed as isNetworkConnectionAllowed.
Implementation:
// src/AntiSSRFPolicy.ts
import { LookupFunction } from "net";
import { antiSSRFDnsLookup } from "./Helpers/AntiSSRFDnsLookup";
public getDnsLookup(): LookupFunction {
return antiSSRFDnsLookup(this);
}
public isNetworkConnectionAllowed(ipaddresses: string[]): boolean {
return this._isNetworkConnectionAllowed(ipaddresses);
}Example usage (with undici and net.connect):
const policy = new AntiSSRFPolicy(PolicyConfigOptions.ExternalOnlyLatest);
const lookup = policy.getDnsLookup();
// undici / fetch
const dispatcher = new undici.Agent({ connect: { lookup } });
await undici.fetch(url, { dispatcher });
// net.connect, e.g. a CONNECT tunnel
if (net.isIP(host) && !policy.isNetworkConnectionAllowed([host])) {
throw new AntiSSRFError("IP address disallowed by policy");
}
const socket = net.connect({ host, port, lookup });Tests would cover:
- both lookup callback forms ({ all: true } and single address), with allowed and denied addresses
- allowed and denied IP literals
- an undici request to a host that resolves to 127.0.0.1 being rejected
Happy to submit a PR if this approach works for you.
Alternative Solutions
- A built-in undici dispatcher, which adds a dependency and doesn't cover net.connect.
Additional Context
No response
Applicable Libraries
- .NET Library
- Node.js Library
Activity