5082 - Make content-security-policy more strict #5220
Merged
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.
Description
I believe this is our most tightly constrained CSP without breaking anything. It requires a few "unsafe" configurations:
imgSrc: data:
- "developers SHOULD NOT include either 'unsafe-inline', or data: as valid sources in their policies. Both enable XSS attacks by allowing code to be included directly in the document itself; they are best avoided completely." https://www.w3.org/TR/CSP3/#csp-directivesscriptSrc: unsafe-eval
- We useeval()
about 88 times and usenew Function()
about 17 times in inbox.js. "AngularJS makes use of this in the $parse service to provide a 30% increase in the speed of evaluating AngularJS expressions." https://docs.angularjs.org/api/ng/directive/ngCspstyleSrc: unsafe-inline
- Our dependency,angular-ui-bootstrap
injects about 8 inline style blocks. Here is an example issue where they fix one of them Angular-ui is throwing csp error on uib-popover angular-ui/bootstrap#5470. The risks from unsafe css are reduced by fully constraining imgSrc (not done here) and by avoiding unsafe-eval (not done here).#5082
Questions for Reviewer
window.startupTimes
- through a whitelisted hash. Another option would be to move that script into its own file and appcache it. As-is, if the content of this script ever changes, the corresponding hash will need to be updated. If we keep it as a hash, I'm unsure of the best method to keep the hash and script in sync (new test, calculate the hash in api, just pray, etc.).unsafe-inline
- I didn't do it because I'm not sure they are always the same for all browsers but they probably are and can follow-up if we close on that direction.Additional considerations
This change also makes the header larger. Including it on all resources (js, css, application/json, etc.) isn't required so one minor performance tweak we might consider here is setting it for
Accept: html
requests only.CSP also supports
report-uri
which can alert us when the security policy is violated (due to bugs or successful attacks). Might be worth opening an issue to enable this down the road.Testing
I went through some basic workflows in webapp + tried each actions in admin pages. In general though, this has risks of breaking edge-case functionalities or workflows I don't know about. Best to AT across all browsers as there are apparently some differences.
Review checklist
License
The software is provided under AGPL-3.0. Contributions to this project are accepted under the same license.