Expand description
Forwarded (RFC 7239) and the de facto X-Forwarded-For,
X-Forwarded-Proto, and X-Forwarded-Host, with client-address
resolution through trusted proxies.
The leftmost X-Forwarded-For address is whatever the client typed; only
addresses appended by proxies you trust mean anything. client_address
ignores the header unless the connection’s peer is trusted, walks the
chain from the right, and stops at the first address it cannot trust. A
malformed chain fails closed: use the peer address.
§Examples
use core::net::IpAddr;
use parse_net::{self as pn, ClientAddress, ForwardedSource};
let proxy: IpAddr = "10.0.0.2".parse()?;
let inner: IpAddr = "10.0.0.1".parse()?;
let trusted = |a: IpAddr| a == proxy || a == inner;
let xff: [&[u8]; 1] = [b"203.0.113.9, 198.51.100.7, 10.0.0.1"];
let ip = pn::client_address(proxy, ForwardedSource::XForwardedFor(&xff), &trusted, 16)?;
assert_eq!(ip, ClientAddress::Forwarded { addr: "198.51.100.7".parse()?, hops: 2 });
// The same header from an untrusted peer is ignored entirely.
let stranger: IpAddr = "192.0.2.1".parse()?;
assert_eq!(pn::client_address(stranger, ForwardedSource::XForwardedFor(&xff), &trusted, 16)?,
ClientAddress::Peer(stranger));Structs§
- Element
- One
Forwardedelement. - Forwarded
Elements - Validated
Forwardedfield lines. - Resolved
client_addressand whether the other header kind was present too.- XffChain
- A validated
X-Forwarded-Forchain.
Enums§
- Client
Address - The client address a request is attributed to.
- Forwarded
Source - Which header the deployment reads the chain from: every field line of it, in order. The two are never merged (merging is a parser differential: two proxies could disagree on the order).
- Node
- A
fororbynode (RFC 7239 section 6). - Node
Port - A node port: a number or an obfuscated identifier.
- Proto
- The protocol a request arrived over at the proxy.
Traits§
- Trust
- Decides whether an address belongs to a proxy you trust.
Functions§
- client_
address - Resolves the client address of a request that arrived from
peer. - client_
address_ detailed - As
client_address, and reports whetherother_lines(the field lines of the header kind not configured) were present. - parse
- Parses every
Forwardedfield line (RFC 7239 section 4). - parse_
x_ forwarded_ for - Parses every
X-Forwarded-Forfield line. - parse_
x_ forwarded_ host - Parses
X-Forwarded-Host: oneHostvalue; a list is refused. - parse_
x_ forwarded_ proto - Parses
X-Forwarded-Proto: a URI scheme, case-insensitive (Protonameshttp,https,ws, andwss); a comma list only when every member is the same. - write_
element - Writes one
Forwardedelement:for,proto,by, andhost, in that order, each when present, with IPv6 addresses and nodes with ports quoted. For proxies that append their hop.