Functions
apply_proxy_headers
2
dict[str, Any]
▼
Rewrite ASGI scope fields using proxy headers from a trusted peer.
**When the …
apply_proxy_headers
2
dict[str, Any]
▼
def apply_proxy_headers(scope: dict[str, Any], *, trusted_hosts: frozenset[str]) -> dict[str, Any]
Rewrite ASGI scope fields using proxy headers from a trusted peer.
When the direct peer is trusted:
clientis overwritten with the leftmost IP fromX-Forwarded-Forschemeis overwritten fromX-Forwarded-Protoserverhost is overwritten fromX-Forwarded-HostHostis rewritten fromX-Forwarded-Hostfor downstream routing
When the direct peer is not trusted (ortrusted_hostsis empty),
allX-Forwarded-*headers are stripped from the scope to prevent
downstream apps from trusting spoofed values.
Parameters
| Name | Type | Description |
|---|---|---|
scope |
dict[str, Any] |
Mutable ASGI scope dict (modified in place and returned). |
trusted_hosts |
frozenset[str] |
Tuple of trusted peer IPs/hostnames. The wildcard |
Returns
dict[str, Any]
_split_host_port
2
tuple[str, int]
▼
Split a Host-style value into an ASGI server tuple.
_split_host_port
2
tuple[str, int]
▼
def _split_host_port(host: str, default_port: int) -> tuple[str, int]
Parameters
| Name | Type | Description |
|---|---|---|
host |
str |
|
default_port |
int |
Returns
tuple[str, int]
_replace_header
3
list[tuple[bytes, bytes]…
▼
Replace the first header named *name*, or append it if missing.
_replace_header
3
list[tuple[bytes, bytes]…
▼
def _replace_header(headers: list[list[bytes]], name: bytes, value: bytes) -> list[tuple[bytes, bytes] | list[bytes]]
Parameters
| Name | Type | Description |
|---|---|---|
headers |
list[list[bytes]] |
|
name |
bytes |
|
value |
bytes |
Returns
list[tuple[bytes, bytes] | list[bytes]]
_strip_forwarded_headers
1
None
▼
Remove all X-Forwarded-* headers from an ASGI scope.
Prevents untrusted client…
_strip_forwarded_headers
1
None
▼
def _strip_forwarded_headers(scope: dict[str, Any]) -> None
Remove all X-Forwarded-* headers from an ASGI scope.
Prevents untrusted clients from injecting proxy headers that downstream ASGI apps might naively trust.
Parameters
| Name | Type | Description |
|---|---|---|
scope |
dict[str, Any] |