Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Both 403_csrf.html and 403.html should exist #3316

Closed
user52 opened this issue Sep 5, 2021 · 2 comments · Fixed by #4464
Closed

Both 403_csrf.html and 403.html should exist #3316

user52 opened this issue Sep 5, 2021 · 2 comments · Fixed by #4464
Labels

Comments

@user52
Copy link

user52 commented Sep 5, 2021

What happened?

The custom template (403.html) was not displayed for a 403 error. It was a csrf 403, not a regular 403 error.

What should've happened instead?

The 403.html template should have been displayed.

Additional details

Reproduce (all tests pass):

from unittest.mock import patch

from django.conf import settings
from django.test import Client, override_settings
from django.urls import reverse
from django.views import defaults as default_views


class TestCsrfTemplatesInUse:

    @override_settings(DEBUG=True)
    def test_403_csrf_debug_enabled(self):
        csrf_client = Client(enforce_csrf_checks=True)
        csrf_client.cookies.load({settings.CSRF_COOKIE_NAME: "notavalidtoken"})
        response = csrf_client.post(reverse("account_login"))
        # Show that CSRF_FAILURE_TEMPLATE is unexpectedly being used instead of using the project's custom "403.html".
        # https://github.com/django/django/blob/main/django/views/csrf.py#L15-L100
        assert 'CSRF verification failed. Request aborted.' in str(response.content)

    @override_settings(DEBUG=False)
    def test_403_csrf_debug_disabled(self):
        csrf_client = Client(enforce_csrf_checks=True)
        csrf_client.cookies.load({settings.CSRF_COOKIE_NAME: "notavalidtoken"})
        response = csrf_client.post(reverse("account_login"))
        # Show that CSRF_FAILURE_TEMPLATE is unexpectedly being used instead of using the project's custom "403.html".
        # https://github.com/django/django/blob/main/django/views/csrf.py#L15-L100
        assert 'CSRF verification failed. Request aborted.' in str(response.content)

    @patch("django.views.generic.base.TemplateView.get")
    def test_403_non_csrf(self, template_view_get):
        def get(request, *args, **kwargs):
            return default_views.permission_denied(request, Exception("Permission Denied"))
        template_view_get.side_effect = get

        client = Client()
        response = client.get(reverse("home"))
        # Show that non-csrf 403 errors are showing the 403.html template.
        assert response.templates[0].name == '403.html'

Fix:

$ cd {{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/templates/
$ cp 403.html 403_csrf.html
@user52 user52 added the bug label Sep 5, 2021
@user52
Copy link
Author

user52 commented Sep 18, 2021

with DEBUG=True (expected):
debug-true-403-csrf-forbidden

with DEBUG=False (production, 403_csrf.html shown, unexpected):
debug-false-403-csrf-forbidden

@infraredCoding
Copy link
Contributor

@user52 is this issue still open? I'd love to take a look at it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants