Skip to content

[Feature]: Expose new functions getDnsLookup() and isNetworkConnectionAllowed() for non-Agent connections (Node.js) #71

Description

Feature Description

Expose two functions on AntiSSRFPolicy, both based on code that already exists:

  1. getDnsLookup(): LookupFunction: returns the policy-enforcing lookup that getHttpAgent()/getHttpsAgent() already install (antiSSRFDnsLookup).
  2. isNetworkConnectionAllowed(ipaddresses: string[]): boolean: a public version of _isNetworkConnectionAllowed. Needed for IP-literal hosts, which never go through a lookup.

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 accepts connect: { 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions