Django - 禁止(CSRF cookie未设置) [英] Django - Forbidden (CSRF cookie not set.)

查看:329
本文介绍了Django - 禁止(CSRF cookie未设置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中等流量的Django网站(每天约4000/5000次访问)。今天我在settings.py上配置了LOGGING选项,发送一个包含Info级别的电子邮件,只需检查一切是否正常...



有我的惊喜,我收到以下错误:
[Django]警告(EXTERNAL IP):禁止(CSRF cookie未设置。)

 没有堆栈跟踪

< WSGIRequest
路径:/ cadastro / usuario /,
GET:< QueryDict:{}>
POST :< QueryDict:{** xxxxxxx(some varibles ....)and **:u'csrfmiddlewaretoken':[u'4wqRKQXZsTmXJaOkCsGobWyG1rzihc8x'],}>
COOKIES:{},
META:{'CONTENT_LENGTH':'381',
'CONTENT_TYPE':'application / x-www-form-urlencoded',
'CSRF_COOKIE':'qzc4i7JdHoQLJ8N5aI9MTlamOZMOKmP0',
'DOCUMENT_ROOT' :'/ opt / nginx / html',
'HTTP_ACCEPT':'text / html,application / xhtml + xml,* / *',
'HTTP_ACCEPT_ENCODING':'gzip,deflate',
'HTTP_ACCEPT_LANGUAGE':'pt-BR',
'HTTP_CACHE_CONTROL': 'no-cache',
'HTTP_CONNECTION':'Keep-Alive',
'HTTP_CONTENT_LENGTH':'381',
'HTTP_CONTENT_TYPE':'application / x-www-form-urlencoded ',
'HTTP_HOST':'xxxxxx',
'HTTP_REFERER':'http:// xxxx / y / z',
'HTTP_USER_AGENT':'Mozilla / 5.0 MSIE 9.0; Windows NT 6.1; Trident / 5.0)',
'PATH_INFO':u'/ y / z',
'QUERY_STRING':'',
'REMOTE_ADDR':'187.27.35.123',
'REMOTE_PORT':'54221',
'REQUEST_METHOD':'POST',
'REQUEST_URI':'y / z',
'SCRIPT_NAME':u'',
'SERVER_NAME':'xxxxxxx',
'SERVER_PORT':'80',
'SERVER_PROTOCOL':'HTTP / 1.1',
'uwsgi.version':'0.9.6.5' ,
'wsgi.errors':< open file'wsgi_input',mode'w'at 0xa126338>,
'wsgi.file_wrapper':<内置函数uwsgi_sendfile>
'wsgi.input':< open file'wsgi_input',mode'r'at 0xa126a70>


'wsgi.multiprocess':True,
'wsgi.multithread ':False,
'wsgi.run_once':False,
'wsgi.url_scheme':'http',
'wsgi.version':(1,0)}>

我试图重现这个错误,但我不能。我在Firefox和Chrome上测试,清理所有的cookies ...一切都很好。但是我有十几个时间,总是有不同的IP,所以我想这不是一个攻击...所有我的形式有 {%csrf_token%} django .middleware.csrf.CsrfViewMiddleware 在MIDDLEWARE_CLASSES上配置。



上面的日志消息非常清楚, CSRF_COOKIE 不为空。我正在使用Django 1.4。



[UPDATED]
我认为这些用户没有启用Cookie ...
所以...问题是:如何使用CSRF与没有启用Cookie的用户?

解决方案

正如我在主要评论中提到的,当403因为CSRF失败而被提出时,您将看到错误。



您不必担心尝试在没有启用Cookie的情况下处理针对用户的CSRF保护。 CSRF攻击的本质就是使用浏览器cookie。如果不使用它们,则受CSRF保护的请求将失败(如您所见)。因此,您仍然受到保护。



Django允许您在发生CSRF故障时设置用于客户端的特定视图: https://docs.djangoproject.com/en/dev/ref/settings/#std%3asetting-CSRF_FAILURE_VIEW



真的,你不应该做任何事情,除了注意到有一些请求以无效的方式尝试POST到您的服务器。


I have a Django web site with medium traffic (about 4000/5000 visits per day). Today I configured the "LOGGING" option on settings.py to send an email with "Info" level, just check if everything was ok...

There was my surprise, I am getting the following error: [Django] WARNING (EXTERNAL IP): Forbidden (CSRF cookie not set.)

No stack trace available

<WSGIRequest
path:/cadastro/usuario/,
GET:<QueryDict: {}>,
POST:<QueryDict: {**xxxxxxx (some varibles....) and**: u'csrfmiddlewaretoken': [u'4wqRKQXZsTmXJaOkCsGobWyG1rzihc8x'], }>,
COOKIES:{},
META:{'CONTENT_LENGTH': '381',
 'CONTENT_TYPE': 'application/x-www-form-urlencoded',
 'CSRF_COOKIE': 'qzc4i7JdHoQLJ8N5aI9MTlamOZMOKmP0',
 'DOCUMENT_ROOT': '/opt/nginx/html',
 'HTTP_ACCEPT': 'text/html, application/xhtml+xml, */*',
 'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
 'HTTP_ACCEPT_LANGUAGE': 'pt-BR',
 'HTTP_CACHE_CONTROL': 'no-cache',
 'HTTP_CONNECTION': 'Keep-Alive',
 'HTTP_CONTENT_LENGTH': '381',
 'HTTP_CONTENT_TYPE': 'application/x-www-form-urlencoded',
 'HTTP_HOST': 'xxxxxx',
 'HTTP_REFERER': 'http://xxxx/y/z',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
 'PATH_INFO': u'/y/z',
 'QUERY_STRING': '',
 'REMOTE_ADDR': '187.27.35.123',
 'REMOTE_PORT': '54221',
 'REQUEST_METHOD': 'POST',
 'REQUEST_URI': 'y/z',
 'SCRIPT_NAME': u'',
 'SERVER_NAME': 'xxxxxxx',
 'SERVER_PORT': '80',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'uwsgi.version': '0.9.6.5',
 'wsgi.errors': <open file 'wsgi_input', mode 'w' at 0xa126338>,
 'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,
 'wsgi.input': <open file 'wsgi_input', mode 'r' at 0xa126a70>,


 'wsgi.multiprocess': True,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0)}>

I tried to reproduce this error, but I couldn´t. I test on Firefox and Chrome, cleaned all cookies... Everything is fine. But I am getting this error a dozen of time, always with different IP, so I suppose that it isn't an attack... All my form has {% csrf_token %} and django.middleware.csrf.CsrfViewMiddleware is configured on MIDDLEWARE_CLASSES.

The log message above is very clear that CSRF_COOKIE isnt empty. I am using Django 1.4.

[UPDATED] I Think that theses users dont have cookies enabled... So... The problem is: How to use CSRF with users that don´t have cookies enabled?

解决方案

As I had mentioned in my main comment, you will see that error when a 403 is raised because of a CSRF failure.

You should not need to worry about trying to handle CSRF protection against users without cookies enabled. The very nature of a CSRF attack requires the use of browser cookies. If they are not used, the CSRF-protected request will fail (as you have seen). Thus, you are still protected.

Django lets you set a specific view to use for the client in the event of a CSRF failure: https://docs.djangoproject.com/en/dev/ref/settings/#std%3asetting-CSRF_FAILURE_VIEW

Really, you should not need to do anything other than note that there are requests that are trying to POST to your server in an invalid way.

这篇关于Django - 禁止(CSRF cookie未设置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆