Trusted Forwarded Headers for AI Gateways
Forwarded and X-Forwarded headers can carry the client address, original host, and original scheme across an AI gateway proxy chain. Every client can also send those fields. A safe deployment removes untrusted values at the first controlled hop, rebuilds forwarding metadata from the connection it received, and gives each downstream service an explicit trust boundary. This guide defines that boundary, the parsing rules, and the acceptance tests for authenticated HTTP LLM routes.

A reverse proxy changes the network facts visible to the next server. The origin sees the proxy address, an internal host, and sometimes plain HTTP after TLS termination. Forwarding headers carry selected facts across that boundary. The security problem starts when an AI service treats a client-supplied Forwarded or X-Forwarded-For value as evidence about the caller. I would reject any AI gateway design that calls these fields identity. They are routing metadata with a provenance requirement.
TL;DR
- Strip client-supplied forwarding fields at the first trusted proxy, then rebuild them from the connection that proxy received.
- Trust only the known proxy suffix of a forwarding chain. Earlier entries remain unverified claims.
- Keep authenticated user or agent identity separate from
for,host, andprotometadata. - Reject malformed, duplicate, oversized, or policy-conflicting values before the LLM request is forwarded.
Forwarding metadata loses its meaning without provenance
RFC 7239 defines the Forwarded request field for information altered or lost while a proxy handles a request. Its standard parameters describe the preceding node with for, the receiving proxy interface with by, the original host with host, and the original scheme with proto. Multiple proxy entries form a comma-separated list in arrival order.
The same RFC says every node on the path, including the client, can modify the field. That sentence sets the operating rule. A value becomes usable only when a trusted component can account for how it entered the message. TLS protects one connection between two endpoints. It gives no historical guarantee for text that arrived inside a field.
This matters on an authenticated HTTP LLM route because forwarding metadata often feeds rate limits, tenant routing, callback URL construction, and security logs. The verified user or agent must still come from the application's authentication context. The identity-aware AI gateway architecture describes that separate handoff.
The trust boundary starts at the first controlled hop
Consider a public load balancer in front of an AI policy gateway, followed by a model adapter. The load balancer accepts the external connection. It is the first component able to observe the peer address and TLS state directly. Its processing sequence should be fixed:
- Remove every inbound
Forwarded,X-Forwarded-For,X-Forwarded-Host, andX-Forwarded-Protofield from an untrusted client. - Create a fresh forwarding element from the accepted connection and validated request target.
- Send that request over an authenticated, encrypted connection to the policy gateway.
- Let later controlled proxies append their own element without rewriting earlier trusted elements.
The concrete artifact is a packet capture with one freshly generated line such as Forwarded: for=192.0.2.60;proto=https;host=ai.example.com. A second inbound line supplied by the client should be absent. Preserving both lines and asking the application to choose is an avoidable parser lottery.
The AI gateway TLS termination guide covers the neighboring connection boundary. Header trust still needs its own configuration after TLS terminates.
Trust the known suffix instead of the leftmost address
A forwarding list can contain data contributed before the request reached infrastructure you control. RFC 7239 warns that the chain listed before a trusted proxy remains untrusted. Therefore, "take the first address" is unsafe whenever public clients can provide a field or an external intermediary can preserve one.
Define the trusted proxy set by workload identity or tightly managed network identity. Then parse from the receiving side of the chain toward the client side. Accept entries contributed by known hops. Stop when the next hop lacks a trusted provenance path. That boundary produces the nearest defensible client address, which may be an external CDN address rather than the original device.
A fixed hop count is brittle. Adding one ingress proxy changes the meaning of every index. CIDR allowlists also need care in autoscaled and multi-cloud deployments. Workload-authenticated links give a stronger basis than an address alone. Keep the chosen trust rule in versioned gateway configuration and record which hop supplied the accepted value.
Parsing has to follow the field grammar
Forwarded is a structured list. Commas separate proxy elements, semicolons separate parameters inside one element, parameter names are case-insensitive, and quoted strings can contain characters unavailable in a token. IPv6 addresses with brackets and ports appear in quoted strings. A split on commas followed by a split on equals signs will eventually misparse a legal value.
Use an RFC-aware parser and enforce deployment limits before allocation. Set maximum field bytes, element count, parameter count, and quoted-string length. Reject duplicate parameters within one element because RFC 7239 prohibits them. Reject invalid quoting, control octets, impossible ports, and duplicate field representations that your framework combines ambiguously.
Standardized Forwarded also differs from the X-Forwarded family. RFC 7239 explains that separate X fields lose the correlation between for, host, and proto. A transition can support both formats, but one canonical internal representation should win. Conflicting forms deserve a 400 response and a connection close, followed by a retained reason code.
Host and scheme values need independent validation
A trusted proxy can accurately report a hostile host value that the client sent. Provenance proves who copied the value. Route approval requires a separate check. Compare host against the gateway's configured public authorities before using it for model routing, redirects, tenant selection, or signed callback URLs. RFC 9110 Section 7.2 treats host and port information as an application-level routing mechanism and identifies it as a frequent target for redirection and cache-poisoning attacks.
Apply the same distinction to proto. The first controlled TLS terminator can assert that the external connection used HTTPS. A downstream service may use that fact when generating an absolute URL. Authorization should never depend on a free-form scheme claim. The gateway's model route comes from configured policy and the validated request target.
for also deserves a narrow role. It can support abuse controls and connection diagnostics. It should never replace authenticated subject, agent, role, or tenant claims. The LLM gateway security guide places those verified inputs in the policy decision before prompt forwarding.
Privacy and logging need a deliberate choice
RFC 7239 treats client addresses and proxy topology as sensitive. It recommends turning the field off by default, configuring each parameter separately, and using obfuscated identifiers where direct addresses serve no operational need. It also warns against copying Forwarded into responses, which can expose the complete proxy chain.
For an AI gateway, decide which record needs the address. A short-lived security event may retain the accepted peer and trusted-hop result. A per-decision AI audit record may need only the authenticated subject, route, policy version, and outcome. Copying a complete internal chain into every prompt audit record increases data exposure without improving authorization.
Obfuscated identifiers can preserve request-level correlation. Generate them per request unless a bounded diagnostic purpose requires longer persistence. Keep the mapping under the same access policy as network security logs. Never place bearer tokens, API keys, email addresses, or tenant secrets into forwarding extensions.
Acceptance tests should cross the deployed proxy chain
A localhost unit test bypasses the component that establishes trust. Send tests through the public load balancer, policy gateway, and model adapter. Include these cases:
- A client supplies a forged leftmost address and
proto=https; the first trusted hop removes both claims and creates fresh values. - Two trusted proxies append elements; the backend selects the configured trusted suffix and records the contributing hop.
- The request contains conflicting
Forwardedand X-Forwarded values; the gateway returns 400 and forwards no LLM request. - An IPv6 node with a port uses the required quotes and parses into the expected normalized form.
- An unapproved
hostarrives through a trusted proxy; route validation rejects it. - An oversized list reaches the field limit; processing stops before policy evaluation.
Retain the normalized forwarding result, trusted-hop decision, authenticated subject, and final model route in the test evidence. The AI gateway observability guide explains why request traces and audit records remain separate artifacts.
DeepInspect
DeepInspect applies controls only to authenticated user or agent traffic deliberately routed over HTTP through the proxy to an LLM endpoint. At that boundary, the deployment should remove untrusted forwarding fields before DeepInspect receives them or configure the first DeepInspect-facing ingress to perform that sanitization. Direct calls that bypass the routed path remain outside control.
For routed traffic, DeepInspect can evaluate the verified application identity alongside normalized connection metadata, prompt classification, destination model, and policy version. Forwarding fields remain contextual evidence. They never replace the authenticated subject supplied by the application, and they never grant model access by themselves.
Book a demo today.