1"""
2Cross Site Request Forgery Middleware.
3
4This module provides a middleware that implements protection
5against request forgeries from other sites.
6"""
7
8import logging
9import string
10from collections import defaultdict
11from urllib.parse import urlsplit
12
13from django.conf import settings
14from django.core.exceptions import DisallowedHost, ImproperlyConfigured
15from django.http import HttpHeaders, UnreadablePostError
16from django.urls import get_callable
17from django.utils.cache import patch_vary_headers
18from django.utils.crypto import constant_time_compare, get_random_string
19from django.utils.deprecation import MiddlewareMixin
20from django.utils.functional import cached_property
21from django.utils.http import is_same_domain
22from django.utils.log import log_response
23from django.utils.regex_helper import _lazy_re_compile
24
…lines 25–150 unchanged (126 lines)
151 length CSRF_TOKEN_LENGTH, it is a masked secret.
152 """
153 # Only unmask tokens that are exactly CSRF_TOKEN_LENGTH characters long.
154 if len(request_csrf_token) == CSRF_TOKEN_LENGTH:
155 request_csrf_token = _unmask_cipher_token(request_csrf_token)
156 assert len(request_csrf_token) == CSRF_SECRET_LENGTH
157 return constant_time_compare(request_csrf_token, csrf_secret)
158
159
160class RejectRequest(Exception):
161 def __init__(self, reason):
162 self.reason = reason
163
…lines 164–485 unchanged (322 lines)