代码之家  ›  专栏  ›  技术社区  ›  Tagar

Django-如何禁用Referer检查

  •  0
  • Tagar  · 技术社区  · 6 年前

    https://github.com/cloudera/hue )nginx背后用于负载平衡和SSL卸载。

    日志文件包含

    下午5:32:32警告进入
    https://hue-dev.discover.abc.com/hue/accounts/login/?next=/ 不 比赛 https://hue-dev.discover.abc.com:443/

    有没有办法在Django项目中禁用Referer检查?

    https://security.stackexchange.com/questions/66165/does-referrer-header-checking-offer-any-real-world-security-improvement

    我在nginx.conf中已经有以下内容

      proxy_set_header        Host $host;
    
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;
    
      proxy_set_header        X-Forwarded-Host $host:$server_port;
      proxy_set_header        X-Forwarded-Server $host;
    

    并尝试了以下有关Referer http属性的更改:

    1. proxy_pass_header Referer
    2. proxy_hide_header Referer
    3. proxy_set_header $http_referer

    同样,对我来说,在Django中禁用Referer check会更容易。

    https://github.com/django/django/blob/22e8ab02863819093832de9f771bf40a62a6bd4a/django/middleware/csrf.py#L280

    referer 变量有一个urlparse对象(请参阅 https://docs.python.org/3/library/urllib.parse.html )其中包含带有 port .

    请再次注意错误- netloc 不匹配,因为一个有端口(443),另一个没有端口(443端口是https的默认端口):

    Referer检查失败-


    不匹配
    https://hue-dev.discover.abc.com:443/ .

    所以我想应该是某种 Referer 在nginx config中进行字段转换以切断 443

    也在这里发布了Django bug- https://code.djangoproject.com/ticket/30017 是否通过nginx配置至少编辑参考文件以剪切/添加https端口443?

    2 回复  |  直到 4 年前
        1
  •  1
  •   nebuler    6 年前

    你看到了吗 https://hue-dev.discover.abc.com:443/ 由于nginx配置中的以下行:

    proxy_set_header        X-Forwarded-Host $host:$server_port;
    

    你已经有了 X-Forwarded-Proto $scheme 在您的配置中指定协议,以便安全使用 X-Forwarded-Host $host 相反。这会解决你的问题。

    如果忽略上述内容,另一个选项是添加 hue-dev.discover.abc.com:443 CSRF_TRUSTED_ORIGINS 在django设置中。

    至于您最初的问题,在您的情况下,没有办法禁用django的referer检查。看 here .

        2
  •  0
  •   Tagar    6 年前

    我通过在URL中嵌入端口443设置静态Referer找到了解决此问题的方法

    proxy_set_header Referer https://hue-dev.discover.abc.com:443/;

    虽然我更喜欢@nebuler的答案。

    仍然希望这个bug可以在Django中修复。 他们的推荐人认为 https://www.com https://www.com:443 太不一样了。 https 具有默认端口443,因此它们是相同的。