Back
+30 XP
Concept

Zero-Trust HTTP Header Validator

HTTP headers passed by reverse proxies or web clients can contain CRLF injection characters ( ) or conflicting Transfer-Encoding and Content-Length headers. Validating headers against RFC 7230 rules thwarts HTTP Request Smuggling, header splitting, and cache poisoning attacks.

python
# Detect CRLF injection:
if '\r' in key or '\n' in key or '\r' in val or '\n' in val:
    raise SecurityError("CRLF header injection detected")

# Zero-Trust HTTP Header Validator When an HTTP proxy routes requests to your Python backend over persistent keep-alive connections, subtle disagreements on how headers are parsed can lead to catastrophic **HTTP Request Smuggling** (RFC 7230). 🌐🛡️ Two primary header attack vectors: 1. **CRLF Injection / Header Splitting**: Injecting `\r\n` into a header value tricks downstream servers into interpreting the remainder of the value as an entirely new HTTP header or a separate fake HTTP response! 2. **Transfer-Encoding vs Content-Length (TE.CL / CL.TE)**: If a request specifies both `Content-Length: 4` and `Transfer-Encoding: chunked`, the proxy might read 4 bytes while the backend reads the chunked body. The unread bytes remain in the connection buffer, poisoning the next user's request. ```python # RFC 7230: Reject conflicting framing headers if "content-length" in headers and "transfer-encoding" in headers: raise SecurityError("Request Smuggling hazard detected") ``` ## Pop Quiz Why does sending both `Content-Length` and `Transfer-Encoding: chunked` enable HTTP Request Smuggling? - [ ] Front-end reverse proxies and backend servers prioritize conflicting framing headers differently, desynchronizing request boundaries on shared connections. - [ ] Dual headers cause the Linux network stack to drop TCP connection reset flags. - [ ] It triggers automatic HTML decompression in intermediate firewall appliances. - [ ] Because HTTP/1.1 proxies convert chunked streams into binary WebSocket frames. ## Real-World Challenge Implement `validate_http_headers(headers: dict) -> dict`. - Lowercase header keys and strip surrounding whitespace. - Reject any header containing CRLF characters (`\r` or `\n`) with `ValueError`. - Verify that `content-length` is a non-negative integer. - Reject any request containing both `content-length` and `transfer-encoding` with `ValueError`. <details> <summary>Solution</summary> ```python def validate_http_headers(headers: dict) -> dict: normalized = {} has_cl = False has_te = False for raw_k, raw_v in headers.items(): if not isinstance(raw_k, str) or not isinstance(raw_v, str): raise TypeError("Header keys and values must be strings") k = raw_k.strip().lower() v = raw_v.strip() # CRLF Injection Guard if "\r" in k or "\n" in k or "\r" in v or "\n" in v: raise ValueError(f"CRLF injection character detected in header '{k}'") if k == "content-length": if not v.isdigit(): raise ValueError(f"Invalid Content-Length value: '{v}'") has_cl = True if k == "transfer-encoding": has_te = True normalized[k] = v # HTTP Request Smuggling Guard (RFC 7230 Section 3.3.3) if has_cl and has_te: raise ValueError( "Conflicting Content-Length and Transfer-Encoding headers detected: Request Smuggling hazard." ) return normalized # Verification # 1. Valid headers clean = validate_http_headers({ "Host": " api.xicae.com ", "Content-Length": " 48 ", "Accept": "application/json" }) assert clean["host"] == "api.xicae.com" assert clean["content-length"] == "48" # 2. Smuggling rejected smuggle_blocked = False try: validate_http_headers({ "Content-Length": "10", "Transfer-Encoding": "chunked" }) except ValueError: smuggle_blocked = True assert smuggle_blocked is True # 3. CRLF injection rejected crlf_blocked = False try: validate_http_headers({ "User-Agent": "Mozilla/5.0\r\nX-Injected-Header: admin" }) except ValueError: crlf_blocked = True assert crlf_blocked is True ``` </details>