Skip to main content

Module forwarded

Module forwarded 

Source
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 Forwarded element.
ForwardedElements
Validated Forwarded field lines.
Resolved
client_address and whether the other header kind was present too.
XffChain
A validated X-Forwarded-For chain.

Enums§

ClientAddress
The client address a request is attributed to.
ForwardedSource
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 for or by node (RFC 7239 section 6).
NodePort
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 whether other_lines (the field lines of the header kind not configured) were present.
parse
Parses every Forwarded field line (RFC 7239 section 4).
parse_x_forwarded_for
Parses every X-Forwarded-For field line.
parse_x_forwarded_host
Parses X-Forwarded-Host: one Host value; a list is refused.
parse_x_forwarded_proto
Parses X-Forwarded-Proto: a URI scheme, case-insensitive (Proto names http, https, ws, and wss); a comma list only when every member is the same.
write_element
Writes one Forwarded element: for, proto, by, and host, in that order, each when present, with IPv6 addresses and nodes with ports quoted. For proxies that append their hop.