Debian Codegraph @

django/views/csrf.py

raw
1 from pathlib import Path
2
3 from django.conf import settings
4 from django.http import HttpResponseForbidden
5 from django.template import Context, Engine, TemplateDoesNotExist, loader
6 from django.utils.translation import gettext as _
7 from django.utils.version import get_docs_version
8
9 CSRF_FAILURE_TEMPLATE_NAME = "403_csrf.html"
10
11
12 def builtin_template_path(name):
13 """
14 Return a path to a builtin template.
15
16 Avoid calling this function at the module level or in a class-definition
17 because __file__ may not exist, e.g. in frozen environments.
18 """
19 return Path(__file__).parent / "templates" / name
20
21
22 def csrf_failure(request, reason="", template_name=CSRF_FAILURE_TEMPLATE_NAME):
23 """
24 Default view used when request fails CSRF protection
25 """
26 from django.middleware.csrf import REASON_NO_CSRF_COOKIE, REASON_NO_REFERER
27
28 c = {
29 "title": _("Forbidden"),
30 "main": _("CSRF verification failed. Request aborted."),
31 "reason": reason,
32 "no_referer": reason == REASON_NO_REFERER,
33 "no_referer1": _(
34 "You are seeing this message because this HTTPS site requires a "
35 "“Referer header” to be sent by your web browser, but none was "
36 "sent. This header is required for security reasons, to ensure "
37 "that your browser is not being hijacked by third parties."
38 ),
39 "no_referer2": _(
40 "If you have configured your browser to disable “Referer” headers, "
41 "please re-enable them, at least for this site, or for HTTPS "
42 "connections, or for “same-origin” requests."
43 ),
44 "no_referer3": _(
45 'If you are using the <meta name="referrer" '
46 'content="no-referrer"> tag or including the “Referrer-Policy: '
47 "no-referrer” header, please remove them. The CSRF protection "
48 "requires the “Referer” header to do strict referer checking. If "
49 "you’re concerned about privacy, use alternatives like "
50 '<a rel="noreferrer" …> for links to third-party sites.'
51 ),
52 "no_cookie": reason == REASON_NO_CSRF_COOKIE,
53 "no_cookie1": _(
54 "You are seeing this message because this site requires a CSRF "
55 "cookie when submitting forms. This cookie is required for "
56 "security reasons, to ensure that your browser is not being "
57 "hijacked by third parties."
58 ),
59 "no_cookie2": _(
60 "If you have configured your browser to disable cookies, please "
61 "re-enable them, at least for this site, or for “same-origin” "
62 "requests."
63 ),
64 "DEBUG": settings.DEBUG,
65 "docs_version": get_docs_version(),
66 "more": _("More information is available with DEBUG=True."),
67 }
68 try:
69 t = loader.get_template(template_name)
70 body = t.render(request=request)
71 except TemplateDoesNotExist:
72 if template_name == CSRF_FAILURE_TEMPLATE_NAME:
73 # If the default template doesn't exist, use the fallback template.
74 with builtin_template_path("csrf_403.html").open(encoding="utf-8") as fh:
75 t = Engine().from_string(fh.read())
76 body = t.render(Context(c))
77 else:
78 # Raise if a developer-specified template doesn't exist.
79 raise
80 return HttpResponseForbidden(body)