python auth_error ai_generated true

django.core.exceptions.SuspiciousOperation: Invalid HTTP_HOST header: 'evil.com'.

ID: python/django-query-parameter-injection

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-07-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.2 active
4.0 active
5.0 active

Root Cause

请求中的 Host 头不匹配 ALLOWED_HOSTS 设置

generic

中文

Django 验证 Host 头时发现不在允许列表中

Workarounds

  1. 100% success 在 ALLOWED_HOSTS 中列出有效域名
    ALLOWED_HOSTS = ['example.com', 'www.example.com']
  2. 95% success 使用环境变量配置
    ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',')

Dead Ends

Common approaches that don't work:

  1. 在 ALLOWED_HOSTS 中添加通配符 '*' 80% fail

    降低安全性,允许任意 Host 头,可能导致 HTTP Host 头攻击

  2. 忽略错误继续处理 100% fail

    Django 会抛出 SuspiciousOperation 异常,无法忽略