python runtime_error ai_generated true

h11._util.LocalProtocolError: 非法的头部值 b'\n'

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

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

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2024-10-22首次发现

版本兼容性

版本状态引入弃用备注
0.100+ active — — —
0.110+ active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

    Embedded \n still present.

  2. 75% 失败

    Same ASGI protocol violation under the hood.