Code Monkey home page Code Monkey logo

Comments (6)

adamchainz avatar adamchainz commented on August 19, 2024 1

Since Django 1.8 support will be dropped shortly, I don't want to include it in the library. Copying that snippet should be easy enough for most projects.

from django-cors-headers.

rvanlaar avatar rvanlaar commented on August 19, 2024

Using this SessionAuthentication class for DRF mitigates the problem.
Could this be included in cors-headers?

from corsheaders import defaults as settings
from corsheaders.middleware import (
    CorsPostCsrfMiddleware, CorsMiddleware)
from rest_framework.authentication import (
    SessionAuthentication as RFSessionAuthentication)


class PatchDepatchRefererCsrf(CorsMiddleware, CorsPostCsrfMiddleware):
    """
    Helper class to provide access to _https_referer_replace
    and _https_referer_replace_reverse.
    """

    def patch(self, request):
        if self.is_enabled(request) and settings.CORS_REPLACE_HTTPS_REFERER:
            self._https_referer_replace(request)

    def depatch(self, request):
        self._https_referer_replace_reverse(request)


class SessionAuthentication(RFSessionAuthentication):
    """
    SessionAuthentication that patchess the HTTP_REFERER before checking
    CSRF and depatch it after checking it.

    corsheaders supplies middleware but DRF doesn't use the middleware
    when checking CSRF for itself. This creates a problem when using https.
    """
    def enforce_csrf(self, request):
        patch_depatch = PatchDepatchRefererCsrf()
        patch_depatch.patch(request)
        super(SessionAuthentication, self).enforce_csrf(request)
        patch_depatch.depatch(request)

from django-cors-headers.

adamchainz avatar adamchainz commented on August 19, 2024

I'm sorry, I'm not familiar with the REST framework. Are you saying it bypasses the middleware stack entirely?

from django-cors-headers.

avorio avatar avorio commented on August 19, 2024

Are you saying it bypasses the middleware stack entirely?

It looks like turning CORS_REPLACE_HTTPS_REFERER on has no effect on Django REST Framework. I was still getting errors relating to the HTTP referer not matching the original hostname.

from django-cors-headers.

adamchainz avatar adamchainz commented on August 19, 2024

I think CORS_REPLACE_HTTPS_REFERER is only needed on Django 1.8, since CSRF_TRUSTED_ORIGINS can be used on 1.9+ (this is from re-reading the docs, I've forgotten how this feature ever worked). Does that setting work for your situation @avorio ?

from django-cors-headers.

avorio avatar avorio commented on August 19, 2024

I think CORS_REPLACE_HTTPS_REFERER is only needed on Django 1.8, since CSRF_TRUSTED_ORIGINS can be used on 1.9+ (this is from re-reading the docs, I've forgotten how this feature ever worked). Does that setting work for your situation @avorio ?

I am indeed using Django 1.8, so that feature would be great there. The problem is: it's not working as it is, so I ended up using something along the lines of @rvanlaar 's solution.

Could we bake that into the core django-cors-headers somehow?

from django-cors-headers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.