Proxy Headers Middleware
Relevant source files
The following files were used as context for generating this wiki page:
Proxy Headers Middleware is a component designed to handle incoming requests that have passed through a proxy or load balancer. Its primary purpose is to ensure that the application receives the correct client information (IP address and protocol scheme) by inspecting standard proxy headers like X-Forwarded-For and X-Forwarded-Proto. Without this middleware, the application might incorrectly identify the proxy's IP as the client's IP.
The middleware modifies the ASGI scope by updating the client and scheme fields based on information extracted from trusted proxy headers. It is particularly essential in containerized environments (e.g., Docker Swarm, Kubernetes) where reverse proxies often front the application server.
Sources: uvicorn/middleware/proxy_headers.py:7-21, CHANGELOG.md:154-159
Core Logic and Architecture
The middleware operates by intercepting the ASGI __call__ and evaluating the connection's source against a set of trusted hosts. If the immediate connection comes from a trusted host, the middleware parses specific headers to re-identify the original requester.
Trusted Host Verification
The middleware uses a internal _TrustedHosts helper class to manage and validate trusted entities. It supports:
- Literals: Exact string matches (e.g., "127.0.0.1", "unix:///foo/bar").
- IP Addresses: Individual IPv4 and IPv6 addresses.
- IP Networks: Entire ranges defined in CIDR notation (e.g., "10.0.0.0/8").
- Wildcards: Using
"*"allows trusting all incoming headers.
Sources: uvicorn/middleware/proxy_headers.py:101-140, CHANGELOG.md:157-159
Processing Flow
The following diagram illustrates how a request is processed through the middleware:
The middleware only processes requests if the immediate client_host is found within the configured trusted_hosts. It specifically looks for single occurrences of the headers to prevent spoofing.
Sources: uvicorn/middleware/proxy_headers.py:32-60
Header Handling
The middleware focuses on two primary headers to reconstruct the original request context.
X-Forwarded-Proto
This header indicates whether the original request was http, https, ws, or wss. The middleware updates the scope["scheme"] accordingly. If the request type is websocket, it ensures the scheme is normalized to ws or wss.
Sources: uvicorn/middleware/proxy_headers.py:46-52
X-Forwarded-For
This header contains a comma-separated list of IP addresses. The middleware traverses this list to find the first "untrusted" host, which represents the actual client.
| Functionality | Implementation Detail |
|---|---|
| Parsing | Supports bare IPs, IPv4 host:port, and bracketed IPv6 [host]:port. |
| Traversal | Checks the list in reverse order (right-to-left) to find the first non-proxy IP. |
| Fallback | If all hosts in the list are trusted, it falls back to the first host in the list. |
| Port Preservation | Attempts to preserve the forwarded client port if provided in the header. |
Sources: uvicorn/middleware/proxy_headers.py:74-98, uvicorn/middleware/proxy_headers.py:151-168, CHANGELOG.md:46-46
Configuration and Usage
The middleware is typically configured via the Uvicorn Config object or CLI flags.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
app | ASGI3Application | N/A | The next ASGI application in the stack. |
trusted_hosts | list[str] | str | "127.0.0.1" | A list or comma-separated string of trusted proxy IPs/Networks. |
Sources: uvicorn/middleware/proxy_headers.py:27-30, CHANGELOG.md:575-580
Security Considerations
To avoid spoofing, the middleware only consumes proxy headers when exactly one copy of the header is present in the request. If duplicate X-Forwarded-For or X-Forwarded-Proto headers are detected, they are ignored to maintain security integrity.
Sources: uvicorn/middleware/proxy_headers.py:43-45, tests/middleware/test_proxy_headers.py:449-470
Relationship with Protocols
The middleware operates independently of the underlying protocol implementation (e.g., httptools, h11, websockets). It modifies the ASGI scope before it reaches the application layer, ensuring consistent client information across different transport types.
Conclusion
The Proxy Headers Middleware is a critical security and infrastructure component in Uvicorn. By correctly interpreting X-Forwarded-* headers, it allows ASGI applications to maintain accurate logs, perform IP-based rate limiting, and handle protocol-specific logic (like secure cookies) even when deployed behind complex proxy architectures.
Sources: uvicorn/middleware/proxy_headers.py:7-15, CHANGELOG.md:154-159