python runtime_error ai_generated true

h11._util.LocalProtocolError: Illegal header value b'\n'

ID: python/uvicorn-h11-local-protocol-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-10-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100+ active — — —
0.110+ active — — —

Root Cause

Response header contains a newline or invalid character, often from user input echoed into a header via Response(headers=...).

generic

中文

响应头包含换行符或非法字符,通常来自用户输入被原样放入 Response(headers=...)。

Workarounds

  1. 92% success
    value = value.replace('\r', '').replace('\n', '')  # sanitize before setting header
  2. 88% success
    Use JSONResponse(body=...) for untrusted data instead of headers.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Embedded \n still present.

  2. 75% fail

    Same ASGI protocol violation under the hood.