跳转到内容
简体中文

IP allowlist

此内容尚不支持你的语言。

IpAllowlist restricts a route subtree to clients whose IP falls inside one of the configured CIDRs — defence-in-depth on top of BearerTokenAuth: even a leaked token is useless off an allowlisted network.

import { IpAllowlist, withMiddleware } from 'actor-ts/http';
const allowlist = IpAllowlist({ allow: ['10.0.0.0/8', '127.0.0.1/32'] });
const managementRoutes = withMiddleware(allowlist, clusterRoutes);

IPv4 and IPv6 CIDRs are both supported ('::1/128', 'fd00::/8'), and IPv4-mapped IPv6 peers (::ffff:a.b.c.d) on a dual-stack socket are normalised so a plain IPv4 CIDR still matches. A non-matching — or missing — client IP fails closed with 403.

IpAllowlist takes a plain options object:

FieldPurpose
allowNon-empty list of CIDR strings; at least one must contain the client IP, else 403. Invalid CIDR syntax throws at construction.
getClientIpOverride IP extraction. Default reads request.remoteAddress (the socket peer). Returning null/undefined denies the request (fail-closed).

The default reads the socket peer and deliberately does not trust x-forwarded-for — that header is client-spoofable when no proxy sits in front. Behind a trusted proxy (Cloudflare, AWS ALB, NGINX) that strips and sets it, opt in explicitly:

IpAllowlist({
allow: ['10.0.0.0/8'],
getClientIp: (request) => request.headers['x-forwarded-for']?.split(',')[0]?.trim(),
});