-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Quick hack for including csp_nonces from requests into script tags #1975
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fbbfca9
Quick hack for including csp_nonces from requests into script tags
karolyi ba4ae50
Add rendering tests & nonces on link/style tags
karolyi 64041b6
Fixing for py38
karolyi 260d2fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 073748c
Improve testing of django-csp integration
tim-schilling ad4b463
Use IntegrationTestCase as it clears the store.
tim-schilling 7da5f26
Fix typing errors
karolyi 1a6ff11
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ geckodriver.log | |
coverage.xml | ||
.direnv/ | ||
.envrc | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ html5lib | |
selenium | ||
tox | ||
black | ||
django-csp # Used in tests/test_csp_rendering | ||
|
||
# Integration support | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
from typing import Dict, cast | ||
from xml.etree.ElementTree import Element | ||
|
||
from django.conf import settings | ||
from django.http.response import HttpResponse | ||
from django.test.utils import ContextList, override_settings | ||
from html5lib.constants import E | ||
from html5lib.html5parser import HTMLParser | ||
|
||
from debug_toolbar.toolbar import DebugToolbar | ||
|
||
from .base import IntegrationTestCase | ||
|
||
|
||
def get_namespaces(element: Element) -> Dict[str, str]: | ||
""" | ||
Return the default `xmlns`. See | ||
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces | ||
""" | ||
if not element.tag.startswith("{"): | ||
return {} | ||
return {"": element.tag[1:].split("}", maxsplit=1)[0]} | ||
|
||
|
||
@override_settings(DEBUG=True) | ||
class CspRenderingTestCase(IntegrationTestCase): | ||
"""Testing if `csp-nonce` renders.""" | ||
|
||
def setUp(self): | ||
super().setUp() | ||
self.parser = HTMLParser() | ||
|
||
def _fail_if_missing( | ||
self, root: Element, path: str, namespaces: Dict[str, str], nonce: str | ||
): | ||
""" | ||
Search elements, fail if a `nonce` attribute is missing on them. | ||
""" | ||
elements = root.findall(path=path, namespaces=namespaces) | ||
for item in elements: | ||
if item.attrib.get("nonce") != nonce: | ||
raise self.failureException(f"{item} has no nonce attribute.") | ||
|
||
def _fail_if_found(self, root: Element, path: str, namespaces: Dict[str, str]): | ||
""" | ||
Search elements, fail if a `nonce` attribute is found on them. | ||
""" | ||
elements = root.findall(path=path, namespaces=namespaces) | ||
for item in elements: | ||
if "nonce" in item.attrib: | ||
raise self.failureException(f"{item} has a nonce attribute.") | ||
|
||
def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser): | ||
"""Fail if the passed HTML is invalid.""" | ||
if parser.errors: | ||
default_msg = ["Content is invalid HTML:"] | ||
lines = content.split(b"\n") | ||
for position, error_code, data_vars in parser.errors: | ||
default_msg.append(" %s" % E[error_code] % data_vars) | ||
default_msg.append(" %r" % lines[position[0] - 1]) | ||
msg = self._formatMessage(None, "\n".join(default_msg)) | ||
raise self.failureException(msg) | ||
|
||
@override_settings( | ||
MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"] | ||
) | ||
def test_exists(self): | ||
"""A `nonce` should exist when using the `CSPMiddleware`.""" | ||
response = cast(HttpResponse, self.client.get(path="/regular/basic/")) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
html_root: Element = self.parser.parse(stream=response.content) | ||
self._fail_on_invalid_html(content=response.content, parser=self.parser) | ||
self.assertContains(response, "djDebug") | ||
|
||
namespaces = get_namespaces(element=html_root) | ||
toolbar = list(DebugToolbar._store.values())[0] | ||
nonce = str(toolbar.request.csp_nonce) | ||
self._fail_if_missing( | ||
root=html_root, path=".//link", namespaces=namespaces, nonce=nonce | ||
) | ||
self._fail_if_missing( | ||
root=html_root, path=".//script", namespaces=namespaces, nonce=nonce | ||
) | ||
|
||
@override_settings( | ||
DEBUG_TOOLBAR_CONFIG={"DISABLE_PANELS": set()}, | ||
MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"], | ||
) | ||
def test_redirects_exists(self): | ||
response = cast(HttpResponse, self.client.get(path="/regular/basic/")) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
html_root: Element = self.parser.parse(stream=response.content) | ||
self._fail_on_invalid_html(content=response.content, parser=self.parser) | ||
self.assertContains(response, "djDebug") | ||
|
||
namespaces = get_namespaces(element=html_root) | ||
context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue] | ||
nonce = str(context["toolbar"].request.csp_nonce) | ||
self._fail_if_missing( | ||
root=html_root, path=".//link", namespaces=namespaces, nonce=nonce | ||
) | ||
self._fail_if_missing( | ||
root=html_root, path=".//script", namespaces=namespaces, nonce=nonce | ||
) | ||
|
||
@override_settings( | ||
MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"] | ||
) | ||
def test_panel_content_nonce_exists(self): | ||
response = cast(HttpResponse, self.client.get(path="/regular/basic/")) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
toolbar = list(DebugToolbar._store.values())[0] | ||
panels_to_check = ["HistoryPanel", "TimerPanel"] | ||
for panel in panels_to_check: | ||
content = toolbar.get_panel_by_id(panel).content | ||
html_root: Element = self.parser.parse(stream=content) | ||
namespaces = get_namespaces(element=html_root) | ||
nonce = str(toolbar.request.csp_nonce) | ||
self._fail_if_missing( | ||
root=html_root, path=".//link", namespaces=namespaces, nonce=nonce | ||
) | ||
self._fail_if_missing( | ||
root=html_root, path=".//script", namespaces=namespaces, nonce=nonce | ||
) | ||
|
||
def test_missing(self): | ||
"""A `nonce` should not exist when not using the `CSPMiddleware`.""" | ||
response = cast(HttpResponse, self.client.get(path="/regular/basic/")) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
html_root: Element = self.parser.parse(stream=response.content) | ||
self._fail_on_invalid_html(content=response.content, parser=self.parser) | ||
self.assertContains(response, "djDebug") | ||
|
||
namespaces = get_namespaces(element=html_root) | ||
self._fail_if_found(root=html_root, path=".//link", namespaces=namespaces) | ||
self._fail_if_found(root=html_root, path=".//script", namespaces=namespaces) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ deps = | |
pygments | ||
selenium>=4.8.0 | ||
sqlparse | ||
django-csp | ||
passenv= | ||
CI | ||
COVERAGE_ARGS | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
Same issue here as above.
It might be inconvenient for others, but I can assure you I have to fight with these kinda errors throughout everywhere when working with django, even when having the stubs installed.
It is not perfect but it's the best we have. I have already made the compromise to have my code "autocorrected" (in the sense of might makes right), so I suggest others make compromises here too.