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

Refs #31949 -- Made @csrf_exempt decorator work with async functions. #17055

Merged
merged 1 commit into from Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions django/views/decorators/csrf.py
@@ -1,5 +1,7 @@
from functools import wraps

from asgiref.sync import iscoroutinefunction

from django.middleware.csrf import CsrfViewMiddleware, get_token
from django.utils.decorators import decorator_from_middleware

Expand Down Expand Up @@ -51,9 +53,17 @@ def csrf_exempt(view_func):

# view_func.csrf_exempt = True would also work, but decorators are nicer
# if they don't have side effects, so return a new function.
@wraps(view_func)
def wrapper_view(*args, **kwargs):
return view_func(*args, **kwargs)

wrapper_view.csrf_exempt = True
return wrapper_view
if iscoroutinefunction(view_func):

async def _view_wrapper(request, *args, **kwargs):
return await view_func(request, *args, **kwargs)

else:

def _view_wrapper(request, *args, **kwargs):
return view_func(request, *args, **kwargs)
felixxm marked this conversation as resolved.
Show resolved Hide resolved

_view_wrapper.csrf_exempt = True

return wraps(view_func)(_view_wrapper)
4 changes: 4 additions & 0 deletions docs/ref/csrf.txt
Expand Up @@ -150,6 +150,10 @@ class-based views<decorating-class-based-views>`.
def my_view(request):
return HttpResponse("Hello world")

.. versionchanged:: 5.0

Support for wrapping asynchronous view functions was added.

.. function:: csrf_protect(view)

Decorator that provides the protection of ``CsrfViewMiddleware`` to a view.
Expand Down
1 change: 1 addition & 0 deletions docs/releases/5.0.txt
Expand Up @@ -258,6 +258,7 @@ Decorators
* :func:`~django.views.decorators.cache.cache_control`
* :func:`~django.views.decorators.cache.never_cache`
* :func:`~django.views.decorators.common.no_append_slash`
* :func:`~django.views.decorators.csrf.csrf_exempt`
* :func:`~django.views.decorators.debug.sensitive_variables`
* :func:`~django.views.decorators.debug.sensitive_post_parameters`
* :func:`~django.views.decorators.http.condition`
Expand Down
1 change: 1 addition & 0 deletions docs/topics/async.txt
Expand Up @@ -84,6 +84,7 @@ view functions:
* :func:`~django.views.decorators.cache.cache_control`
* :func:`~django.views.decorators.cache.never_cache`
* :func:`~django.views.decorators.common.no_append_slash`
* :func:`~django.views.decorators.csrf.csrf_exempt`
* :func:`~django.views.decorators.http.condition`
* :func:`~django.views.decorators.http.etag`
* :func:`~django.views.decorators.http.last_modified`
Expand Down
37 changes: 37 additions & 0 deletions tests/decorators/test_csrf.py
@@ -0,0 +1,37 @@
from asgiref.sync import iscoroutinefunction

from django.http import HttpRequest, HttpResponse
from django.test import SimpleTestCase
from django.views.decorators.csrf import csrf_exempt


class CsrfExemptTests(SimpleTestCase):
def test_wrapped_sync_function_is_not_coroutine_function(self):
def sync_view(request):
return HttpResponse()

wrapped_view = csrf_exempt(sync_view)
self.assertIs(iscoroutinefunction(wrapped_view), False)

def test_wrapped_async_function_is_coroutine_function(self):
async def async_view(request):
return HttpResponse()

wrapped_view = csrf_exempt(async_view)
self.assertIs(iscoroutinefunction(wrapped_view), True)

def test_csrf_exempt_decorator(self):
@csrf_exempt
def sync_view(request):
return HttpResponse()

self.assertIs(sync_view.csrf_exempt, True)
self.assertIsInstance(sync_view(HttpRequest()), HttpResponse)

async def test_csrf_exempt_decorator_async_view(self):
@csrf_exempt
async def async_view(request):
return HttpResponse()

self.assertIs(async_view.csrf_exempt, True)
self.assertIsInstance(await async_view(HttpRequest()), HttpResponse)