Django-如何禁用引荐检查

时间:2018-12-06 19:58:57

标签: django nginx nginx-reverse-proxy nginx-config

我已经在nginx后面放置了一个基于Django的Web项目(Cloudera Hue:https://github.com/cloudera/hue),用于负载平衡和SSL卸载。

由于CSRF错误而陷入403错误。 日志文件包含

  

5:32:32 PM警告访问
  10.170.3.21 -anon--“ POST / accounts / login / HTTP / 1.1”-引用检查失败-   https://hue-dev.discover.abc.com/hue/accounts/login/?next=/不   匹配https://hue-dev.discover.abc.com:443/

是否可以在Django项目中禁用引荐检查?

Referer检查不会增加任何安全性,因为很容易欺骗http标头中的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 / Hue后端产生相同的CSRF / Referer检查错误。

再次,对我来说,禁用Django中的Referer检查会更容易。

如果不可能,则问题可能出在以下Django代码中: 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”属性。

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

  

引荐来源检查失败-

     

https://hue-dev.discover.abc.com/hue/accounts/login/?next=/
  不匹配
  https://hue-dev.discover.abc.com:443/

所以我想应该是在nginx config中进行的Referer字段转换,以明确切出(或添加)443端口。

还在此处发布了Django错误-https://code.djangoproject.com/ticket/30017 但是我想仍然可能有一个检查在Django中禁用引荐检查,或者 至少要通过Nginx配置为https剪切/添加端口443?

2 个答案:

答案 0 :(得分:1)

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

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

您已经在配置中使用X-Forwarded-Proto $scheme来指定协议,因此使用X-Forwarded-Host $host应该是安全的。那应该可以解决您的问题。

如果您忽略上述内容,另一种选择是在django设置中将hue-dev.discover.abc.com:443添加到CSRF_TRUSTED_ORIGINS

对于您的原始问题,无法根据您的情况禁用django的引荐检查。参见here

答案 1 :(得分:0)

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

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

尽管我更喜欢@nebuler的答案。

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

相关问题