403-page in Django - django

I use Django 1.4. I created 403.html file in the same directory as 404.html (404 error page works fine). Yes, I read this. Then I turn off cookies in my browser, try to login and see the default 403-error page, not mine 403.html page:
Forbidden (403)
CSRF verification failed. Request aborted.
More information is available with DEBUG=True.
I restarted Apache, but it doesnt help.
How to fix it? Thanks

This is not the default 403-error page. You are seeing this message because the CSRF middleware does not work when cookies are disabled.
Your custom 403 template has no effect because the CSRF middleware does not use the general 403 view, but the view defined by the setting CSRF_FAILURE_VIEW, which is defined in django.conf.global_settings as django.views.csrf.csrf_failure. As you can see in the source, the message you are seeing is hardcoded in the view.
You could create your own CSRF_FAILURE_VIEW, but that is probably not what you want. I suggest you leave everything as it is and just delete the cookies or use another browser to test as unauthenticated user.

Related

csrf missing error but csrf token is present there i have checked it by inspecting it it is happen in cpanel deployment

Forbidden (403)
CSRF verification failed. Request aborted.
You are seeing this message because this site requires a CSRF cookie when submitting forms. This cookie is required for security reasons, to ensure that your browser is not being hijacked by third parties.
If you have configured your browser to disable cookies, please re-enable them, at least for this site, or for “same-origin” requests.
More information is available with DEBUG=True.
In my django project when i deploy it to c panel its form submission giving me error i have already inclued the csrf token and also it is working perfectly fine during the development i did some research they were talking about the render function i have used redirect and i had to use

Different value of csrf token in response header and browser cookies. csrf verification failing in django 1.9

Most of the SO answers are asking to clear cookies and confirm middleware class. I have already tried that.
Python - 3.4
Django - 1.10
Using VirtualEnv.
I am getting Forbidden (403) CSRF verification failed. Request aborted. error on Django admin login screen. I have hosted my site on pythonanywhere.com with django version 1.9.
I have cleared the browser cookies. All of them.
I reloaded the login screen. Get request.
In browser cookies, which were empty till now, one value has been set for my website, where csrf value is = XPp5hAhylAkt27U4SzGPNU7w8SFBJ3RP
In response header, set cookies was send with cookies value = UT24544MghHLZi0IrGHQlCcpk1v0SbCy . Same value was available in form's source code.
Now I entered the username and password and click on login button.
Received the 403 error CSRF verification failed. Request aborted.
I rechecked all the values of csrf token.
In request header CSRF cookies values = XPp5hAhylAkt27U4SzGPNU7w8SFBJ3RP
In form data csrf values = UT24544MghHLZi0IrGHQlCcpk1v0SbCy
I already have 'django.middleware.csrf.CsrfViewMiddleware', in middleware classes. I cleared browser cache and cookies. Even restarted the system.
I have used exactly same code on different site where it is working perfectly fine.
Why there are different csrf-token values? What is the solution to this problem?
update 1: If I set debug = False in settings, it works fine. But I cant keep it as code is live.
update 2: Upon further investigation I found out that somehow browser cookie csrftoken's value is not being set to correct value which is being passed in response header. If I delete and the cookie from browser and then set it to correct value from console, post requests work.
update 3 : Now same issue is happening with every post request or form submission I am doing on my web app. CSRF token value sent in response header and source code is not same as the one being set in browser cookies.
update 4: Setting CSRF_COOKIE_NAME = "csrf_token" also didn't helped.
The unexpected value for the CSRF token is set when the browser tries to fetch the favicon.
The URL you have defined for your favicon seems to be invalid, and apparently, non-existing URLs are handled by your default view. This sets a different CSRF cookie, but the page that is displayed in the browser still has the initial CSRF token in the form.
Request loading the page:
Request loading the favicon:
You can fix this particular error by ensuring that the favicon exists.
Note this this bug will come back whenever any resource you link (e.g. an image) does not exist because your app renders the homepage instead of returning a 404 error.

CSRF failure on server using Remote Authentication (works when cookie is cleared)

I'm using Django's RemoteUserMiddleware to authenticate the users in one website. It works great until the next day. When I try to do anything using POST methods (even in Django's admin page) it gives me a 403 error.
I clear the cookies and it starts to work again.
There's a firewall in between my browser and the server which sends the headers to the server (so Django can know who is logged in).
Django's version: 1.9.6
I'm using NGINX in my server
When the error occurs django changes the csrftoken each time. It seems it is getting an Anonymous User when the cache is not cleared...
Any thoughts what could be causing this weird behavior?
This was due one invalid cookie being injected by the system. Django doesn't handle well whenever there is an invalid cookie... it kind wipes some of the keys in the meta tag (where the cookies are checked)

Clould9's Django out of the box: Admin page CSRF :443 error

Clould9's Django out of the box gives CSRF error when I attempt to login to the admin page.
Reason given is:
- Forbidden (403)
- CSRF verification failed. Request aborted.
- Referer checking failed - https://mysite.c9.io/admin/login/?next=/admin/ does
not match https://mysite.c9.io:443/
No changes were made to the instance, other than creating a superuser.
Commenting out setting.py MIDDLEWARE_CLASSES 'django.middleware.csrf.CsrfViewMiddleware' did not fix the issue.
Current workaround is through setattr(request, '_dont_enforce_csrf_checks', True), found here:
Django CSRF framework cannot be disabled and is breaking my site
The problem seems to be the way Cloud9 treats HTTPS on port 443. Is there a way to fix this without disabling CSRF for the whole site? I also seem to be sandboxed out of django-admin.py, so I can't limit the hack to just the admin page.
I have opened a ticket for this issue with the cloud9 support #eff M. They are great guys out there and great IDE as well with great support.
Meanwhile try this workaround for the time being:
access your admin page with http://mysite.c9.io/admin/login/?next=/admin/ and not with https://mysite.c9.io/admin/login/?next=/admin/ and see if you can login. it worked well on my side.

django: after upgrade to 1.2 CSRF raises 403 though I don't have CSRF protection enabled

I have just upgraded to Django 1.2 and I am trying to run my project. After I login I get
Forbidden (403)
CSRF verification failed. Request aborted.
which is strange because I haven't enabled CSRF protection previously. Do I have to configure something else to have my project work?
Yep, that's because you use contrib.admin view for login with a custom template. You should add csrf_token to your login template.
It seems, that CSRF protection is on for contrib.admin views, which I use for login. This is why I get 403.