hyperlane-plugin-ip-filter 22.14.0

A zero-allocation IP allowlist/denylist and anti-spoofing client IP extraction plugin for the Hyperlane framework with /8 radix-indexed CIDR lookup, IPv4-mapped IPv6 canonicalization, RFC 7239 Forwarded/X-Forwarded-For right-to-left proxy peeling, and prebuilt 403 wire responses.
Documentation
use super::*;

/// Resolved client IP metadata stored in Hyperlane [`Context`] under [`ATTR_CLIENT_IP`].
#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
pub struct ClientIp {
    /// Canonicalized client IP (IPv4-mapped IPv6 addresses are normalized to IPv4).
    #[get(type(copy))]
    pub(crate) ip: IpAddr,
    /// Canonicalized direct TCP peer IP address, when available from the transport stream.
    #[get(type(copy))]
    pub(crate) peer_ip: Option<IpAddr>,
    /// `true` when `ip` was peeled from a trusted proxy forwarding header.
    #[get(type(copy))]
    pub(crate) via_proxy: bool,
}

/// Outcome of evaluating a request through [`CompiledIpFilter`].
#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
pub struct IpFilterDecision {
    /// Whether the request was allowed, denied, or skipped via path exemption.
    #[get(type(copy))]
    pub(crate) action: IpFilterAction,
    /// Resolved client IP metadata, when an address could be determined.
    #[get(type(copy))]
    pub(crate) client_ip: Option<ClientIp>,
}

/// User-facing configuration for [`IpFilterPlugin`] and [`CompiledIpFilter`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct IpFilterConfig {
    /// Allowed CIDR networks (empty allows all IPs not matching `denylist`).
    pub(crate) allowlist: Vec<IpNet>,
    /// Denied CIDR networks (always evaluated before `allowlist`).
    pub(crate) denylist: Vec<IpNet>,
    /// Trusted proxy CIDRs permitted to supply forwarding headers.
    pub(crate) trusted_proxies: Vec<IpNet>,
    /// Maximum trusted proxy hops peeled right-to-left before stopping.
    #[get(type(copy))]
    pub(crate) max_trusted_hops: usize,
    /// Whether RFC 7239 `Forwarded` (`for=...`) is inspected when `X-Forwarded-For` is absent.
    #[get(type(copy))]
    pub(crate) trust_forwarded_header: bool,
    /// Whether `X-Real-IP` is inspected as a fallback when `X-Forwarded-For` and `Forwarded` are absent.
    #[get(type(copy))]
    pub(crate) trust_x_real_ip: bool,
    /// Exact request paths exempt from allowlist/denylist rejection.
    pub(crate) exempt_paths: Vec<String>,
    /// Segment-bounded path prefixes exempt from allowlist/denylist rejection.
    pub(crate) exempt_prefixes: Vec<String>,
    /// HTTP status code emitted on rejected requests (default `403 Forbidden`).
    #[get(type(copy))]
    pub(crate) reject_status: HttpStatus,
    /// `Content-Type` header emitted on rejected requests.
    pub(crate) reject_content_type: String,
    /// Response body bytes emitted on rejected requests.
    pub(crate) reject_body: Vec<u8>,
}

/// Zero-allocation `/8` radix-indexed IPv4/IPv6 CIDR lookup table.
#[derive(Clone, Debug)]
pub struct CidrTable {
    /// Fast path set when `0.0.0.0/0` is present.
    pub(crate) v4_any: bool,
    /// Fast path set when `::/0` is present.
    pub(crate) v6_any: bool,
    /// IPv4 prefixes shorter than `/8`.
    pub(crate) v4_short: Vec<(u32, u32)>,
    /// IPv6 prefixes shorter than `/8`.
    pub(crate) v6_short: Vec<(u128, u128)>,
    /// `/8`-indexed sorted exact IPv4 `/32` addresses for `O(log N)` binary search.
    pub(crate) v4_exact: V4ExactBuckets,
    /// `/8`-indexed IPv4 prefixes (`/8`..`/31`).
    pub(crate) v4_prefix: V4PrefixBuckets,
    /// `/8`-indexed sorted exact IPv6 `/128` addresses for `O(log N)` binary search.
    pub(crate) v6_exact: V6ExactBuckets,
    /// `/8`-indexed IPv6 prefixes (`/8`..`/127`).
    pub(crate) v6_prefix: V6PrefixBuckets,
    /// Total number of CIDR entries compiled into this table.
    pub(crate) entry_count: usize,
}

/// Precompiled zero-allocation IP filter engine backing [`IpFilterPlugin`].
#[derive(Clone, Debug)]
pub struct CompiledIpFilter {
    /// Original configuration retained for inspection.
    pub(crate) config: IpFilterConfig,
    /// Compiled CIDR allowlist (`is_empty()` means allow-all).
    pub(crate) allowlist: CidrTable,
    /// Compiled CIDR denylist (checked first).
    pub(crate) denylist: CidrTable,
    /// Compiled trusted proxy CIDR table.
    pub(crate) trusted_proxies: CidrTable,
    /// Exact exempt paths in an `O(1)` `XxHash3_64` hash set.
    pub(crate) exempt_exact: HashSetXxHash3_64<String>,
    /// Normalized exempt path prefixes.
    pub(crate) exempt_prefixes: Vec<String>,
    /// Pre-serialized HTTP rejection response wire bytes sent without allocating.
    pub(crate) prebuilt_reject_wire: ResponseData,
}

/// Process-wide [`IpFilterPlugin`] singleton holding a compiled [`CompiledIpFilter`].
#[derive(Clone, Debug)]
pub struct IpFilterPlugin {
    /// Precompiled IP filter engine.
    pub(crate) compiled: CompiledIpFilter,
}

/// Opt-in request middleware extracting real client IP and enforcing CIDR allow/deny rules.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-125)]
pub struct IpFilterHook;