-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial code parts for CSP implementation for sentry and self-hosted (#…
…48699) Another attempt of #47980 and #48507 (which were reverted due to bugs). This PR adds some preliminary code for adding a Content-Security-Policy-Report-Only header with minimal required permissions (at least I could not find any violations on sentry devserver and self-hosted). The CSP middleware is disabled (commented in the MIDDLEWARE) There is no report collecting enabled by default (CSP_REPORT_URI is not set), the intent is to customize it depending on the use case.
- Loading branch information
Showing
6 changed files
with
141 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.conf import settings | ||
from django.test.utils import override_settings | ||
|
||
from sentry.testutils import APITestCase | ||
from sentry.utils.http import absolute_uri | ||
|
||
|
||
def provision_middleware(): | ||
return ["csp.middleware.CSPMiddleware"] + list(settings.MIDDLEWARE) | ||
|
||
|
||
class JiraCSPTest(APITestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.issue_key = "APP-123" | ||
self.path = absolute_uri(f"extensions/jira/issue/{self.issue_key}/") + "?xdm_e=base_url" | ||
self.middleware = provision_middleware() | ||
|
||
def _split_csp_policy(self, policy): | ||
csp = {} | ||
for directive in policy.split("; "): | ||
parts = directive.split(" ") | ||
csp[parts[0]] = parts[1:] | ||
return csp | ||
|
||
def test_csp_frame_ancestors(self): | ||
with override_settings(MIDDLEWARE=tuple(self.middleware)): | ||
response = self.client.get(self.path) | ||
assert "Content-Security-Policy-Report-Only" in response | ||
|
||
csp = self._split_csp_policy(response["Content-Security-Policy-Report-Only"]) | ||
assert "base_url" in csp["frame-ancestors"] | ||
assert "http://testserver" in csp["frame-ancestors"] | ||
|
||
@override_settings(STATIC_FRONTEND_APP_URL="https://sentry.io/_static/dist/") | ||
def test_csp_remote_style(self): | ||
with override_settings(MIDDLEWARE=tuple(self.middleware)): | ||
response = self.client.get(self.path) | ||
assert "Content-Security-Policy-Report-Only" in response | ||
|
||
csp = self._split_csp_policy(response["Content-Security-Policy-Report-Only"]) | ||
assert "https://sentry.io" in csp["style-src"] | ||
|
||
@override_settings(CSP_REPORT_ONLY=False) | ||
def test_csp_enforce(self): | ||
with override_settings(MIDDLEWARE=tuple(self.middleware)): | ||
response = self.client.get(self.path) | ||
assert "Content-Security-Policy" in response |