python
auth_error
ai_generated
true
可疑操作:无效的 HTTP_HOST 头
django.core.exceptions.SuspiciousOperation: Invalid HTTP_HOST header: 'evil.com'.
ID: python/django-query-parameter-injection
80%修复率
86%置信度
0证据数
2024-07-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.2 | active | — | — | — |
| 4.0 | active | — | — | — |
| 5.0 | active | — | — | — |
根因分析
Django 验证 Host 头时发现不在允许列表中
English
请求中的 Host 头不匹配 ALLOWED_HOSTS 设置
解决方案
-
100% 成功率 在 ALLOWED_HOSTS 中列出有效域名
ALLOWED_HOSTS = ['example.com', 'www.example.com']
-
95% 成功率 使用环境变量配置
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')
无效尝试
常见但无效的做法:
-
在 ALLOWED_HOSTS 中添加通配符 '*'
80% 失败
降低安全性,允许任意 Host 头,可能导致 HTTP Host 头攻击
-
忽略错误继续处理
100% 失败
Django 会抛出 SuspiciousOperation 异常,无法忽略