-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix web UI when using session-based CSRF #6207
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
Changes from all commits
09f8ac2
b98d9ef
cee6823
9c23899
e7c6cc4
cc2be97
c71e472
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,7 +286,7 @@ <h1>{{ name }}</h1> | |
<script> | ||
window.drf = { | ||
csrfHeaderName: "{{ csrf_header_name|default:'X-CSRFToken' }}", | ||
csrfCookieName: "{{ csrf_cookie_name|default:'csrftoken' }}" | ||
csrfToken: "{% if request %}{{ csrf_token }}{% endif %}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How very odd... I am sure I used it this way when I was first figuring things out, clearly I was mistaken. OK, I'll have to re-work one test too, will take care of that in the next day or two. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, no, I do want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, and |
||
}; | ||
</script> | ||
<script src="{% static "rest_framework/js/jquery-3.3.1.min.js" %}"></script> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import re | ||
|
||
from django.shortcuts import render | ||
|
||
|
||
def test_base_template_with_context(): | ||
context = {'request': True, 'csrf_token': 'TOKEN'} | ||
result = render({}, 'rest_framework/base.html', context=context) | ||
assert re.search(r'\bcsrfToken: "TOKEN"', result.content.decode('utf-8')) | ||
|
||
|
||
def test_base_template_with_no_context(): | ||
# base.html should be renderable with no context, | ||
# so it can be easily extended. | ||
render({}, 'rest_framework/base.html') | ||
result = render({}, 'rest_framework/base.html') | ||
# note that this response will not include a valid CSRF token | ||
assert re.search(r'\bcsrfToken: ""', result.content.decode('utf-8')) |
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.
Again
{% csrf_token %}
? And do we need theif request
check?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.
We do need that check, because there is currently a unit test for rendering with no request and in that case the csrf_token call throws an exception (see https://code.djangoproject.com/ticket/29785, which you closed as "by design").
Uh oh!
There was an error while loading. Please reload this page.
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.
OK, read the comments out of order. Here we don't need the
{% if request %}
check because (presumably) there will always be a request when you load an admin page. In thebase.html
case, however, the check is only there because of the exception thrown if there is no request, this is to satisfytest_templates.test_base_template_with_no_context()
.