Request Filtering Agent

3.2.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize and use `request-filtering-agent` with Node.js's built-in `http.request` to prevent requests to private IP addresses, showing expected error handling.

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();

view raw JSON →