pub fn enrich_context_from_headers(
ctx: CratestackContext,
headers: &HeaderMap,
trusted_proxy: Option<&TrustedProxyConfig>,
peer: Option<SocketAddr>,
) -> CratestackContextExpand description
Enrich a CratestackContext with the request id (from traceparent) and the
client IP recorded on audit events. Malformed traceparent headers are
silently ignored here — the auth/header-validation layer is the right
place to reject them, not the enrichment seam.
client_ip resolution (#415 — see docs/design/trusted-proxy-client-ip.md
for the decided design):
trusted_proxyisSomeand the request’s socketpeeris in its allowlist: honor whichever single headerTrustedProxyConfig::forwarded_headerselects, walkingmax_hopsentries in from the right end of the chain (right-to-left — seeTrustedProxyConfig::max_hops). The selected hop is then parsed as anstd::net::IpAddr(Finding 2 remediation) — a value that isn’t a real IP address (a spoofed string, a placeholder likeunknown, a malformed entry) is never recorded; if the header is absent, doesn’t parse at that hop depth, or doesn’t parse as an IP, this falls back to the socket peer address rather than recording nothing.- Otherwise (no
trusted_proxyconfigured, or the peer isn’t in its allowlist): headers are never consulted.client_ipis the socket peer address if one is available, or omitted entirely if it isn’t.
The unconfigured default — no Extension<TrustedProxyConfig> applied
and/or no ConnectInfo<SocketAddr> available — is the safe one: headers
are never trusted, and nothing is guessed. client_ip is simply absent
from the audit record.