Request Filtering Agent
request-filtering-agent is an http(s).Agent implementation for Node.js designed to mitigate Server-Side Request Forgery (SSRF) attacks by blocking requests to private and reserved IP addresses by default. Currently stable at v3.2.0, the library has an active release cadence, introducing features like CIDR notation support for allow/deny lists in recent minor versions. Its key differentiator lies in providing a security-focused http.Agent that integrates seamlessly with popular HTTP clients such as node-fetch, axios, and got, while explicitly not supporting Node.js's built-in fetch due to its lack of http.Agent compatibility. The agent dynamically detects DNS-resolved IP addresses, including those from loopback domains like nip.io, ensuring comprehensive protection against internal network access.
Common errors
-
DNS lookup [IP_ADDRESS](family:[NUMBER], host:[HOSTNAME]) is not allowed. Because, It is private IP address.
cause Attempting to connect to a private or reserved IP address which is blocked by `request-filtering-agent` by default.fixVerify the target IP address. If it's legitimately intended to be accessed and is a private IP, configure the `allowIPAddressList` option in `FilteringAgentOptions` to explicitly permit that IP or range. -
ERR_REQUIRE_ESM: require() of ES Module .../request-filtering-agent/index.js from ... not supported.
cause Attempting to use `require()` with `request-filtering-agent` v3.x, which is an ESM-only package.fixRefactor your codebase to use ES module `import` syntax. Ensure your environment supports ESM (e.g., Node.js 20+ and `"type": "module"` in `package.json` for top-level files). If you need CommonJS, downgrade to `request-filtering-agent@^2.0.0`.
Warnings
- breaking Package switched from CommonJS to ESM and requires Node.js 20+.
- breaking Dropped support for older Node.js versions (12, 14, 16), requiring Node.js 18+.
- gotcha Node.js's built-in `fetch` API does not support custom `http.Agent` implementations, making it incompatible with `request-filtering-agent`.
- gotcha CIDR notation support for `allowIPAddressList` and `denyIPAddressList` was introduced in minor updates.
Install
-
npm install request-filtering-agent -
yarn add request-filtering-agent -
pnpm add request-filtering-agent
Imports
- useAgent
const { useAgent } = require('request-filtering-agent');import { useAgent } from 'request-filtering-agent'; - HttpFilteringAgent
const { HttpFilteringAgent } = require('request-filtering-agent');import { HttpFilteringAgent } from 'request-filtering-agent'; - HttpsFilteringAgent
const { HttpsFilteringAgent } = require('request-filtering-agent');import { HttpsFilteringAgent } from 'request-filtering-agent'; - FilteringAgentOptions
import type { FilteringAgentOptions } from 'request-filtering-agent';
Quickstart
import { request } from 'node:http';
import { useAgent, FilteringAgentOptions } from 'request-filtering-agent';
// This URL resolves to a private loopback IP (127.0.0.1) and will be blocked by default.
const url = new URL('http://127.0.0.1:8080/');
const agentOptions: FilteringAgentOptions = {
// Optionally, specify allowed or denied IP lists using CIDR notation.
// allowIPAddressList: ['192.168.1.0/24'],
// denyIPAddressList: ['10.0.0.0/8']
};
// Create a filtering agent instance for the target URL
const agent = useAgent(url, agentOptions);
// Use the agent with Node.js's built-in http.request
const req = request(url, { agent }, (res) => {
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
// Expected error for 127.0.0.1: "DNS lookup 127.0.0.1(...) is not allowed. Because, It is private IP address."
console.error(`Problem with request: ${e.message}`);
});
req.end();