Skip to content

Issue with CsrfViewMiddleware and "referer" same_origin checking for secure (https) subdomains #2982

@troygrosfield

Description

@troygrosfield

This issue is laid out in detail here:

The api url is using djangorestframework. Everything works as expected when trying to POST data from http://example.com to http://api.example.com. However, when we moved to prod over https, we see the following error:

detail: "CSRF Failed: Referer checking failed - https://example.com/some/url does not match https://api.example.com/."

Both sites are secure (https) and both sites are trusted. The same_origin check happens in the CsrfViewMiddleware (below) [1] which is subclassed in djangorestframework [2]:

if not same_origin(referer, good_referer):
    reason = REASON_BAD_REFERER % (referer, good_referer)
    return self._reject(request, reason)

However, this would never seem to allow me to post across subdomains since it won't be the same_origin. It seems logical for POST requests to be made across subdomains, right? Am I just missing something? All GET requests work as expected over https.

I've already added the necessary csrftoken headers via:

and I'm using the django-cors-headers lib as well.

[1] https://github.com/django/django/blob/eef95ea96faef0b7dbbe0c8092202b74f68a899b/django/middleware/csrf.py#L159
[2] https://github.com/tomchristie/django-rest-framework/blob/010f2ee9bd8696f8332e67d73b9ad488b4423d20/rest_framework/authentication.py#L27

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions