summaryrefslogtreecommitdiff
path: root/django/middleware/csrf.py
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2021-01-12 19:55:02 -0500
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-03-18 20:00:22 +0100
commitdba44a7a7a3581ec722e06fa0f9f33dfc00ed5cd (patch)
treea999c49d722b06cc70740b55be21c8f73ae343eb /django/middleware/csrf.py
parent9bf5e9418f425666726559c9f1981a516da30aab (diff)
Refs #16010 -- Required CSRF_TRUSTED_ORIGINS setting to include the scheme.
Diffstat (limited to 'django/middleware/csrf.py')
-rw-r--r--django/middleware/csrf.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/django/middleware/csrf.py b/django/middleware/csrf.py
index 368b51f316..10d678db41 100644
--- a/django/middleware/csrf.py
+++ b/django/middleware/csrf.py
@@ -15,6 +15,7 @@ from django.urls import get_callable
from django.utils.cache import patch_vary_headers
from django.utils.crypto import constant_time_compare, get_random_string
from django.utils.deprecation import MiddlewareMixin
+from django.utils.functional import cached_property
from django.utils.http import is_same_domain
from django.utils.log import log_response
@@ -136,6 +137,13 @@ class CsrfViewMiddleware(MiddlewareMixin):
This middleware should be used in conjunction with the {% csrf_token %}
template tag.
"""
+ @cached_property
+ def csrf_trusted_origins_hosts(self):
+ return [
+ urlparse(origin).netloc.lstrip('*')
+ for origin in settings.CSRF_TRUSTED_ORIGINS
+ ]
+
# The _accept and _reject methods currently only exist for the sake of the
# requires_csrf_token decorator.
def _accept(self, request):
@@ -272,7 +280,7 @@ class CsrfViewMiddleware(MiddlewareMixin):
# Create a list of all acceptable HTTP referers, including the
# current host if it's permitted by ALLOWED_HOSTS.
- good_hosts = list(settings.CSRF_TRUSTED_ORIGINS)
+ good_hosts = list(self.csrf_trusted_origins_hosts)
if good_referer is not None:
good_hosts.append(good_referer)