-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
CSP reporting #2154
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
Merged
Merged
CSP reporting #2154
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57b427e
CSP reporting
mattrobenolt e97b714
Clean up, comments, etc
mattrobenolt 0361792
Comments for CspReportView that explains auth logic
mattrobenolt 22a0553
Add a mess of tests
mattrobenolt 9c11568
Add integration tests
mattrobenolt f4894b5
derp derp derp
mattrobenolt 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 hidden or 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 hidden or 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 hidden or 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,119 @@ | ||
| """ | ||
| sentry.interfaces.csp | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| :copyright: (c) 2010-2015 by the Sentry Team, see AUTHORS for more details. | ||
| :license: BSD, see LICENSE for more details. | ||
| """ | ||
|
|
||
| from __future__ import absolute_import | ||
|
|
||
| __all__ = ('Csp',) | ||
|
|
||
| from urlparse import urlsplit | ||
| from sentry.interfaces.base import Interface | ||
| from sentry.utils.safe import trim | ||
|
|
||
|
|
||
| # Sourced from https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives | ||
| REPORT_KEYS = frozenset(( | ||
| 'blocked_uri', 'document_uri', 'effective_directive', 'original_policy', | ||
| 'referrer', 'status_code', 'violated_directive', 'source_file', | ||
| 'line_number', 'column_number', | ||
|
|
||
| # FireFox specific keys | ||
| 'script_sample', | ||
| )) | ||
|
|
||
| KEYWORDS = frozenset(( | ||
| "'none'", "'self'", "'unsafe-inline'", "'unsafe-eval'", | ||
| )) | ||
|
|
||
| ALL_SCHEMES = ( | ||
| 'data:', 'mediastream:', 'blob:', 'filesystem:', | ||
| 'http:', 'https:', 'file:', | ||
| ) | ||
|
|
||
|
|
||
| class Csp(Interface): | ||
| """ | ||
| A CSP violation report. | ||
|
|
||
| See also: http://www.w3.org/TR/CSP/#violation-reports | ||
|
|
||
| >>> { | ||
| >>> "document_uri": "http://example.com/", | ||
| >>> "violated_directive": "style-src cdn.example.com", | ||
| >>> "blocked_uri": "http://example.com/style.css", | ||
| >>> } | ||
| """ | ||
| @classmethod | ||
| def to_python(cls, data): | ||
| kwargs = {k: trim(data.get(k, None), 1024) for k in REPORT_KEYS} | ||
| # Inline script violations are confusing and don't say what uri blocked them | ||
| # because they're inline. FireFox sends along "blocked-uri": "self", which is | ||
| # vastly more useful, so we want to emulate that | ||
| if kwargs['effective_directive'] == 'script-src' and not kwargs['blocked_uri']: | ||
| kwargs['blocked_uri'] = 'self' | ||
| return cls(**kwargs) | ||
|
|
||
| def get_hash(self): | ||
| # The hash of a CSP report is it's normalized `violated-directive`. | ||
| # This normalization has to be done for FireFox because they send | ||
| # weird stuff compared to Safari and Chrome. | ||
| # NOTE: this may or may not be great, not sure until we see it in the wild | ||
| return [':'.join(self.get_violated_directive()), ':'.join(self.get_culprit_directive())] | ||
|
|
||
| def get_violated_directive(self): | ||
| return 'violated-directive', self._normalize_directive(self.violated_directive) | ||
|
|
||
| def get_culprit_directive(self): | ||
| if self.blocked_uri: | ||
| return 'blocked-uri', self.blocked_uri | ||
| return 'effective-directive', self._normalize_directive(self.effective_directive) | ||
|
|
||
| def get_path(self): | ||
| return 'sentry.interfaces.Csp' | ||
|
|
||
| def get_message(self): | ||
| return 'CSP Violation: %s %r' % self.get_culprit_directive() | ||
|
|
||
| def get_culprit(self): | ||
| return '%s in %r' % self.get_violated_directive() | ||
|
|
||
| def _normalize_directive(self, directive): | ||
| if not directive: | ||
| return directive | ||
| bits = filter(None, directive.split(' ')) | ||
| return ' '.join([bits[0]] + map(self._normalize_value, bits[1:])) | ||
|
|
||
| def _normalize_value(self, value): | ||
| # > If no scheme is specified, the same scheme as the one used to | ||
| # > access the protected document is assumed. | ||
| # Source: https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives | ||
| if value in KEYWORDS: | ||
| return value | ||
|
|
||
| # normalize a value down to 'self' if it matches the origin of document-uri | ||
| # FireFox transforms a 'self' value into the spelled out origin, so we | ||
| # want to reverse this and bring it back | ||
| if value.startswith(ALL_SCHEMES): | ||
| if _get_origin(self.document_uri) == value: | ||
| return "'self'" | ||
| return value | ||
|
|
||
| # Now we need to stitch on a scheme to the value | ||
| scheme = self.document_uri.split(':', 1)[0] | ||
| # These schemes need to have an additional '//' to be a url | ||
| if scheme in ('http', 'https', 'file'): | ||
| return '%s://%s' % (scheme, value) | ||
| # The others do not | ||
| return '%s:%s' % (scheme, value) | ||
|
|
||
|
|
||
| def _get_origin(value): | ||
| "Extract the origin out of a url, which is just scheme+host" | ||
| scheme, hostname = urlsplit(value)[:2] | ||
| if scheme in ('http', 'https', 'file'): | ||
| return '%s://%s' % (scheme, hostname) | ||
| return '%s:%s' % (scheme, hostname) |
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
src/sentry/static/sentry/app/components/events/interfaces/csp.jsx
This file contains hidden or 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,35 @@ | ||
| import React from "react"; | ||
| import _ from "underscore"; | ||
| import PropTypes from "../../../proptypes"; | ||
|
|
||
| import EventDataSection from "../eventDataSection"; | ||
| import DefinitionList from "./definitionList"; | ||
|
|
||
| var CSPInterface = React.createClass({ | ||
| propTypes: { | ||
| group: PropTypes.Group.isRequired, | ||
| event: PropTypes.Event.isRequired, | ||
| type: React.PropTypes.string.isRequired, | ||
| data: React.PropTypes.object.isRequired, | ||
| }, | ||
|
|
||
| render() { | ||
| let {group, event, data} = this.props; | ||
|
|
||
| let extraDataArray = _.chain(data) | ||
| .map((val, key) => [key.replace(/_/g, '-'), val]) | ||
| .value(); | ||
|
|
||
| return ( | ||
| <EventDataSection | ||
| group={group} | ||
| event={event} | ||
| type="csp" | ||
| title="CSP Report"> | ||
| <DefinitionList data={extraDataArray} isContextData={true}/> | ||
| </EventDataSection> | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| export default CSPInterface; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
trailing comma is invalid // @benvinegar how do we get lint for this
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.
It's not invalid. Only IE7 and below throw error on this.
On Monday, October 12, 2015, David Cramer notifications@github.com wrote:
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.
Oh, right. Yeah, @benvinegar, we talked about this before. You have a preference of no trailing commas just as convention, but a linter to enforce would be nice for those of us who don't do that instinctively. :)
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.
Yeah, I added the rule to raven-js. I can do the same here.