-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
Fixed #35328 - Improved debug messaging behind proxies. #18014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
0df060c
to
95759af
Compare
d857f02
to
d7f6022
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you Ryan ⭐ I have initial comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your work here @ryanhiebert ! I have some comments with small improvements.
Lint error:
Set up pre-commit for yourself: https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/#pre-commit-checks |
c6805e2
to
6e3a65e
Compare
755cbb3
to
dc5ce91
Compare
django/views/templates/csrf_403.html
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<p>The <code>Origin</code> header does not match | |
the expected server origin, | |
but common proxy headers are present in the request | |
and may include parts of the <code>Origin</code> header.</p> | |
<p>The <code>Origin</code> header does not match the expected server origin, | |
but common proxy headers are present in the request and may include parts of | |
the <code>Origin</code> header.</p> |
This file is roughly wrapped at 80 characters so let's apply similar wrapping here
django/views/templates/csrf_403.html
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe?
<p>If you’re sure that you are only running behind a secure proxy | |
that always set these headers to avoid spoofing as described in | |
<a href="https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#secure-proxy-ssl-header">this warning in the docs</a>, | |
you may wish to add one or more of the following | |
settings to permit Django to trust these headers.</p> | |
<p><pre> | |
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") | |
USE_X_FORWARDED_HOST = True | |
</pre></p> | |
<p>If you are running behind a secure proxy that sets these headers, you may | |
want to add one or more of the following settings to permit Django to trust | |
these headers.</p> | |
<p><pre> | |
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") | |
USE_X_FORWARDED_HOST = True | |
</pre> | |
Modifying these setting can compromise your site’s security. Ensure you fully | |
understand your setup before changing it. See <a href="https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#secure-proxy-ssl-header"><code>SECURE_PROXY_SSL_HEADER</code></a> | |
for more details.</p> |
django/views/templates/csrf_403.html
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the expected server origin looks correct, | |
you may wish to add the origin to the <code>CSRF_TRUSTED_ORIGINS</code> | |
setting. | |
If the expected server origin looks correct, you may wish to add the origin | |
to the <code>CSRF_TRUSTED_ORIGINS</code> setting. |
(wrapping nit)
django/views/csrf.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"bad_origin": reason.startswith("Origin checking failed"), | |
"forwarded_may_fix": ( | |
request.headers.get("X-Forwarded-Proto", "") == "https" | |
and not request.is_secure() | |
or request.headers.get("Origin", "").endswith( | |
f'://{request.headers.get("X-Forwarded-Host", "")}' | |
) | |
), | |
"x_forwarded_proto": request.headers.get("X-Forwarded-Proto"), | |
"x_forwarded_host": request.headers.get("X-Forwarded-Host"), | |
"bad_origin": True, | |
"forwarded_may_fix": ( | |
request.headers.get("X-Forwarded-Proto", "") == "https" | |
or request.headers.get("Origin", "").endswith( | |
f'://{request.headers.get("X-Forwarded-Host", "")}' | |
) | |
), | |
"x_forwarded_proto": None, | |
"x_forwarded_host": None, |
Thank you for the updates @ryanhiebert ⭐
There are a couple of things I think we should still add tests/assert for. If I make the above changes, nothing fails. These changes are:
bad_origin
alwaysTrue
and not request.is_secure()
is removed from"forwarded_may_fix"
- can we also assert the values of
x_forwarded_proto
andx_forwarded_host
rendering in the template?
docs/releases/5.1.txt
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* The error messaging is improved when ``Origin`` checking fails, | |
including better hints when likely proxy headers are detected. | |
* The ``'403_csrf.html'`` template now has improved messaging when ``Origin`` | |
checking fails, including better hints when likely proxy headers are detected. |
Docs are wrapped at 80 characters and we will now need to rebase and move this to the 5.2 release notes.
Trac ticket number
ticket-35328
Branch description
When in debug mode, we can make some better error messaging when the
Origin
header checking fails. In common scenarios, we can notice that they are likely missing headers from proxies. In any case, we can remove the verbose messaging around CSRF Tokens, which are not the way they have failed and are unhelpful to solving the error they are viewing.Checklist
main
branch.