hyperlane_plugin_ip_filter/struct.rs
1use super::*;
2
3/// Resolved client IP metadata stored in Hyperlane [`Context`] under [`ATTR_CLIENT_IP`].
4#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
5pub struct ClientIp {
6 /// Canonicalized client IP (IPv4-mapped IPv6 addresses are normalized to IPv4).
7 #[get(type(copy))]
8 pub(crate) ip: IpAddr,
9 /// Canonicalized direct TCP peer IP address, when available from the transport stream.
10 #[get(type(copy))]
11 pub(crate) peer_ip: Option<IpAddr>,
12 /// `true` when `ip` was peeled from a trusted proxy forwarding header.
13 #[get(type(copy))]
14 pub(crate) via_proxy: bool,
15}
16
17/// Outcome of evaluating a request through [`CompiledIpFilter`].
18#[derive(Clone, Copy, Data, Debug, Eq, PartialEq)]
19pub struct IpFilterDecision {
20 /// Whether the request was allowed, denied, or skipped via path exemption.
21 #[get(type(copy))]
22 pub(crate) action: IpFilterAction,
23 /// Resolved client IP metadata, when an address could be determined.
24 #[get(type(copy))]
25 pub(crate) client_ip: Option<ClientIp>,
26}
27
28/// User-facing configuration for [`IpFilterPlugin`] and [`CompiledIpFilter`].
29#[derive(Clone, Data, Debug, Eq, PartialEq)]
30pub struct IpFilterConfig {
31 /// Allowed CIDR networks (empty allows all IPs not matching `denylist`).
32 pub(crate) allowlist: Vec<IpNet>,
33 /// Denied CIDR networks (always evaluated before `allowlist`).
34 pub(crate) denylist: Vec<IpNet>,
35 /// Trusted proxy CIDRs permitted to supply forwarding headers.
36 pub(crate) trusted_proxies: Vec<IpNet>,
37 /// Maximum trusted proxy hops peeled right-to-left before stopping.
38 #[get(type(copy))]
39 pub(crate) max_trusted_hops: usize,
40 /// Whether RFC 7239 `Forwarded` (`for=...`) is inspected when `X-Forwarded-For` is absent.
41 #[get(type(copy))]
42 pub(crate) trust_forwarded_header: bool,
43 /// Whether `X-Real-IP` is inspected as a fallback when `X-Forwarded-For` and `Forwarded` are absent.
44 #[get(type(copy))]
45 pub(crate) trust_x_real_ip: bool,
46 /// Exact request paths exempt from allowlist/denylist rejection.
47 pub(crate) exempt_paths: Vec<String>,
48 /// Segment-bounded path prefixes exempt from allowlist/denylist rejection.
49 pub(crate) exempt_prefixes: Vec<String>,
50 /// HTTP status code emitted on rejected requests (default `403 Forbidden`).
51 #[get(type(copy))]
52 pub(crate) reject_status: HttpStatus,
53 /// `Content-Type` header emitted on rejected requests.
54 pub(crate) reject_content_type: String,
55 /// Response body bytes emitted on rejected requests.
56 pub(crate) reject_body: Vec<u8>,
57}
58
59/// Flat CSR `/8` radix table of sorted disjoint IPv4 `(start, end)` intervals.
60#[derive(Clone, Debug)]
61pub(crate) struct V4CidrCsr {
62 /// 256-bit bitmap marking IPv4 `/8` blocks fully covered (`O(1)` single bit test).
63 pub(crate) full_slash8: [u64; 4],
64 /// Bucket boundary offsets (`offsets[b]..offsets[b + 1]`).
65 pub(crate) offsets: Box<[u32; CIDR_OFFSET_LEN]>,
66 /// Contiguous sorted disjoint IPv4 intervals across all `/8` buckets.
67 pub(crate) intervals: Box<[(u32, u32)]>,
68}
69
70/// Flat CSR `/8` radix table of sorted disjoint IPv6 `(start, end)` intervals.
71#[derive(Clone, Debug)]
72pub(crate) struct V6CidrCsr {
73 /// 256-bit bitmap marking IPv6 `/8` blocks fully covered (`O(1)` single bit test).
74 pub(crate) full_slash8: [u64; 4],
75 /// Bucket boundary offsets (`offsets[b]..offsets[b + 1]`).
76 pub(crate) offsets: Box<[u32; CIDR_OFFSET_LEN]>,
77 /// Contiguous sorted disjoint IPv6 intervals across all `/8` buckets.
78 pub(crate) intervals: Box<[(u128, u128)]>,
79}
80
81/// Zero-allocation `/8` radix-indexed IPv4/IPv6 CIDR lookup table using merged disjoint intervals.
82#[derive(Clone, Debug)]
83pub struct CidrTable {
84 /// Compiled IPv4 CIDR lookup index (inline for small tables, `/8` CSR radix for large tables).
85 pub(crate) v4: V4CidrIndex,
86 /// Compiled IPv6 CIDR lookup index (inline for small tables, `/8` CSR radix for large tables).
87 pub(crate) v6: V6CidrIndex,
88 /// Total number of source CIDR entries compiled into this table.
89 pub(crate) entry_count: usize,
90}
91
92/// Precompiled zero-allocation IP filter engine backing [`IpFilterPlugin`].
93#[derive(Clone, Debug)]
94pub struct CompiledIpFilter {
95 /// Original configuration retained for inspection.
96 pub(crate) config: IpFilterConfig,
97 /// Compiled CIDR allowlist (`is_empty()` means allow-all).
98 pub(crate) allowlist: CidrTable,
99 /// Compiled CIDR denylist (checked first).
100 pub(crate) denylist: CidrTable,
101 /// Compiled trusted proxy CIDR table.
102 pub(crate) trusted_proxies: CidrTable,
103 /// Small inline exact exempt path list used when `len <= EXEMPT_SET_THRESHOLD`.
104 pub(crate) exempt_small: Box<[String]>,
105 /// Exact exempt paths in an `O(1)` `XxHash3_64` hash set when `len > EXEMPT_SET_THRESHOLD`.
106 pub(crate) exempt_set: Option<HashSetXxHash3_64<String>>,
107 /// Normalized exempt path prefixes.
108 pub(crate) exempt_prefixes: Box<[String]>,
109 /// Pre-serialized HTTP rejection response wire bytes sent without allocating.
110 pub(crate) prebuilt_reject_wire: Vec<u8>,
111}
112
113/// Process-wide [`IpFilterPlugin`] singleton holding a compiled [`CompiledIpFilter`].
114#[derive(Clone, Debug)]
115pub struct IpFilterPlugin {
116 /// Precompiled IP filter engine.
117 pub(crate) compiled: CompiledIpFilter,
118}
119
120/// Opt-in request middleware extracting real client IP and enforcing CIDR allow/deny rules.
121#[derive(Clone, Copy, Debug, Default)]
122#[hyperlane::request_middleware(-125)]
123pub struct IpFilterHook;