adamchainz / django-cors-headers

Django app for handling the server headers required for Cross-Origin Resource Sharing (CORS)
MIT License
5.4k stars 536 forks source link

Incompatible with Daphne under ASGI #908

Closed captain828 closed 1 month ago

captain828 commented 1 year ago

Understanding CORS

Python Version

3.12

Django Version

5.0b1

Package Version

4.3.0

Description

Getting errors on Django 5 + Daphne ASGI:

Internal Server Error: /
Traceback (most recent call last):
  File "[root]\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\utils\deprecation.py", line 136, in __call__
    response = self.process_response(request, response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\middleware\security.py", line 47, in process_response
    response.headers.setdefault("X-Content-Type-Options", "nosniff")
    ^^^^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'headers'
Exception inside application: object HttpResponse can't be used in 'await' expression
Traceback (most recent call last):
  File "[root]\venv\Lib\site-packages\django\contrib\staticfiles\handlers.py", line 101, in __call__
    return await self.application(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 170, in __call__
    await self.handle(scope, receive, send)
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 209, in handle
    task.result()
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 192, in process_request
    response = await self.run_get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\core\handlers\asgi.py", line 237, in run_get_response
    response = await self.get_response_async(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[root]\venv\Lib\site-packages\django\core\handlers\base.py", line 162, in get_response_async
    response = await self._middleware_chain(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object HttpResponse can't be used in 'await' expression
HTTP GET / 500 [0.73, 127.0.0.1:50708]

settings.py

INSTALLED_APPS = [
    'daphne',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'apps.my_app',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    # 'django.middleware.locale.LocaleMiddleware',
    # 'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.cache.FetchFromCacheMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # 'django_brotli.middleware.BrotliMiddleware',
    # 'django.middleware.gzip.GZipMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
]

asgi.py - default from Django

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')

application = get_asgi_application()

runserver command

python manage.py runserver 127.0.0.1:8000

I tried removing as many variables as I could but the error only goes away when I remove the CORS middleware.

adamchainz commented 1 year ago

It looks like you're experiencing the same thing as this report in another of my packages: https://github.com/adamchainz/django-htmx/issues/381 . Would you be able to check out that issue and related PR and maybe test them? Then you or I could make an identical PR here.

captain828 commented 1 year ago

I'll have a look but I can't promise I'll have time to do it this week.

Royalflamejlh commented 1 year ago

This is likely affecting all ASGI servers, I'm having the same issue with uvicorn.

daggaz commented 1 year ago

This causes an incompatibility with asgiref < 3.6.0 as iscoroutinefunction was only added in this commit: https://github.com/django/asgiref/commit/36f37c9ee5bf2d1f240db23643734f1225a0b5fd

Maybe update the dependencies to require at least 3.6.0?

adamchainz commented 1 year ago

That was done: https://github.com/adamchainz/django-cors-headers/commit/7fe1d62f54997a33e2f7ad428c51b7978f2c1f9d#diff-50c86b7ed8ac2cf95bd48334961bf0530cdc77b5a56f852c5c61b89d735fd711R46

What tooling did you use to install an incompatible asgiref with? Can you provide a reproduction?

JPaulMora commented 2 months ago

Bumping this thread, can't use channels with this package.

adamchainz commented 1 month ago

I think this was fixed already back in #911. If you're still having issues with Channels please open a new issue @JPaulMora .