-
-
Notifications
You must be signed in to change notification settings - Fork 4.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
Use CSP nonces #1871
Use CSP nonces #1871
Conversation
@LukasReschke, thanks for your PR! By analyzing the history of the files in this pull request, we identified @Henni, @BernhardPosselt and @DeepDiver1975 to be potential reviewers. |
CSP nonces are a feature available with CSP v2. Basically instead of saying "JS resources from the same domain are ok to be served" we now say "Ressources from everywhere are allowed as long as they add a `nonce` attribute to the script tag with the right nonce. At the moment the nonce is basically just a `<?php p(base64_encode($_['requesttoken'])) ?>`, we have to decode the requesttoken since `:` is not an allowed value in the nonce. So if somebody does on their own include JS files (instead of using the `addScript` public API, they now must also include that attribute.) IE does currently not implement CSP v2, thus there is a whitelist included that delivers the new CSP v2 policy to newer browsers. Check http://caniuse.com/#feat=contentsecuritypolicy2 for the current browser support list. An alternative approach would be to just add `'unsafe-inline'` as well as `'unsafe-inline'` is ignored by CSPv2 when a nonce is set. But this would make this security feature unusable at all in IE. Not worth it at the moment IMO. Implementing this offers the following advantages: 1. **Security:** As we host resources from the same domain by design we don't have to worry about 'self' anymore being in the whitelist 2. **Performance:** We can move oc.js again to inline JS. This makes the loading way quicker as we don't have to load on every load of a new web page a blocking dynamically non-cached JavaScript file. If you want to toy with CSP see also https://csp-evaluator.withgoogle.com/ Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
9d5bc31
to
9e66348
Compare
Awesome! 🎈 |
@@ -47,6 +47,7 @@ | |||
'script', | |||
[ | |||
'src' => $linkToJs, | |||
'nonce' => base64_encode(\OC::$server->getCsrfTokenManager()->getToken()->getEncryptedValue()) |
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.
Should we have some kind of CSPNounceManager? That if we ever need to change the way it is generated we just have to do so in 1 place and everything keeps working.
For now it could just be a simple wrapper of course.
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.
Good idea. Let's do that. 👍
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.
Done
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
4447353
to
38b3ac8
Compare
😢 how? This will break the mail app as we're using requirejs to dynamically load scripts in debug mode. On production, we use |
Fix it? 😉 … or tell me how to setup that mail app to that state to experience that together with that branch. Like with stupid commands including Everything that goes further than |
Running make should take care of exactly that |
Been there. Done that. 90% of the fancy makefiles that people include in apps explode totally on OS X. I won't execute random makefiles anymore ;-) |
Plus having to install That's like: "Sure you can contribute. You hopefully have the right OS in the right version and then you have to execute these bunch of tools so that it just works" 🙈 🙊 🙉 Honestly, my time is too rare for that and I do see that as a major contribution blocking bummer… But anyways, off topic. |
@LukasReschke should IMHO be addressed, I'd recommend to file bugs on the respective repos |
Ok. That should be what requireJS needs to work again: diff --git a/js/require_config.js b/js/require_config.js
index 7e90525..5b1a5d1 100644
--- a/js/require_config.js
+++ b/js/require_config.js
@@ -11,8 +11,8 @@
*/
(function() {
- 'use strict';
+ 'use strict';
requirejs.config({
baseUrl: './../../../apps/mail/js',
paths: {
@@ -44,6 +44,18 @@
baseUrl: OC.linkTo('mail', 'js')
});
+ requirejs.createNode = function (config, moduleName) {
+ var node = config.xhtml ?
+ document.createElementNS('http://www.w3.org/1999/xhtml', 'html:script') :
+ document.createElement('script');
+ node.type = config.scriptType || 'text/javascript';
+ node.charset = 'utf-8';
+ node.async = true;
+
+ node.setAttribute("nonce", btoa(OC.requestToken));
+ return node;
+ };
+
require([
'init'
]); |
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
Current coverage is 57.30% (diff: 92.68%)
|
Very cool especially the cool things we can do with it. |
Tested and works 👍 |
Required for nextcloud/server#1871
Required for nextcloud/server#1871
CSP nonces are a feature available with CSP v2. Basically instead of saying "JS resources from the same domain are ok to be served" we now say "Ressources from everywhere are allowed as long as they add a
nonce
attribute to the script tag with the right nonce.At the moment the nonce is basically just a
<?php p(base64_encode($_['requesttoken'])) ?>
, we have to decode the requesttoken since:
is not an allowed value in the nonce. So if somebody does on their own include JS files (instead of using theaddScript
public API, they now must also include that attribute.)IE does currently not implement CSP v2, thus there is a whitelist included that delivers the new CSP v2 policy to newer browsers. Check http://caniuse.com/#feat=contentsecuritypolicy2 for the current browser support list. An alternative approach would be to just add
'unsafe-inline'
as well as'unsafe-inline'
is ignored by CSPv2 when a nonce is set. But this would make this security feature unusable at all in IE. Not worth it at the moment IMO.Implementing this offers the following advantages:
If you want to toy with CSP see also https://csp-evaluator.withgoogle.com/
cc @rullzer