go
runtime_error
ai_generated
true
net/http: 头部字段"X-Custom-Header"的值无效
net/http: invalid header field value for "X-Custom-Header"
ID: go/http-header-value-invalid
80%修复率
83%置信度
0证据数
2024-04-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.21 | active | — | — | — |
根因分析
头部值包含HTTP规范不允许的字符(如换行、控制字符),导致WriteHeader失败。
English
Header values contain characters not allowed by HTTP spec (e.g., newlines, control chars), causing WriteHeader to fail.
解决方案
-
90% 成功率
Use httpguts.ValidHeaderFieldValue to validate before setting
-
85% 成功率
Sanitize input by stripping invalid characters: strings.Map(func(r rune) rune { if r < 32 || r > 126 { return -1 }; return r }, val)
无效尝试
常见但无效的做法:
-
60% 失败
Escaping may change semantics; proper sanitization needed
-
40% 失败
May break client expectations if header is required