-
Notifications
You must be signed in to change notification settings - Fork 52
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
Changing to semantic CSP injection #131
Changes from 4 commits
abdd4d0
0372421
4b5073c
80d7e49
3dd7cdd
84b47ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ var fs = require('fs'), | |
path = require('path'), | ||
replaceStream = require('replacestream'), | ||
cordovaServe = require('cordova-serve'), | ||
cspParse = require('csp-parse'), | ||
send = require('send-transform'), | ||
url = require('url'), | ||
Q = require('q'), | ||
|
@@ -218,13 +219,35 @@ SimulationServer.prototype._streamAppHostHtml = function (request, response) { | |
return '<script src="' + scriptSource + '"></script>'; | ||
}).join(''); | ||
|
||
// Note we replace "default-src 'self'" with "default-src 'self' ws:" (in Content Security Policy) so that | ||
// websocket connections are allowed (this relies on a custom version of send that supports a 'transform' option). | ||
// Note we add "connect-src 'self' ws:" and "img-src 'blob:' (in Content Security Policy) so that | ||
// websocket connections are allowed and the camera plugin works (this relies on a custom version | ||
// of send that supports a 'transform' option). | ||
var metaTagRegex = /<\s*meta[^>]*>/; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tested these changes with the cordova mobilespec app, but with a little modification. The cordova mobilespec app analyzes the user agent to identify mobile runtime, and if it matches some of the OS like android, ios etc, it will inject CSP. That is done automatically when one of the scripts is loaded. Since for cordova-simulate with do not change the user agent yet, I took the CSP configuration and add it manually to the index.html, looking like this:
With the changes in this branch, and also with latest master, replacing CSP while streaming the index.html doesn't work. The regex to match the meta tag only matches the first meta tag, not all of them. But if we change the regex to use the global modifier,
it returns all of the matches and it works as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah good point! I'll make the change. |
||
var cspRegex = /http-equiv\s*=\s*(['"])Content-Security-Policy\1/; | ||
var cspContent = /(content\s*=\s*")([^"]*)"/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semi-colon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
send(request, filePath, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: The call to |
||
transform: function (stream) { | ||
return stream | ||
.pipe(replaceStream(/(<\s*head[^>]*>)/, '$1' + scriptTags)) | ||
.pipe(replaceStream('default-src \'self\'', 'default-src \'self\' ws: blob:')); | ||
.pipe(replaceStream(metaTagRegex, function (metaTag) { | ||
if (!cspRegex.test(metaTag)) { | ||
// Not a CSP tag; return unchanged | ||
return metaTag; | ||
} | ||
return metaTag.replace(cspContent, function (match, preamble, csp) { | ||
var policy = new cspParse(csp); | ||
var defaultCsp = policy.get('default-src'); | ||
if (!policy.get('connect-src')) { | ||
policy.add('connect-src', defaultCsp); | ||
} | ||
policy.add('connect-src', '\'self\' ws:'); | ||
if (!policy.get('img-src')) { | ||
policy.add('img-src', defaultCsp); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can add the comment related to the camera plugin and image handling at this point. |
||
} | ||
policy.add('img-src', 'blob:'); | ||
return preamble + policy.toString() + '"'; | ||
}); | ||
}, {maxMatchLen: 1024})); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we can keep |
||
} | ||
}).pipe(response); | ||
}.bind(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.
What do you mean that the camera plugin works? Is there any issue with 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.
My understanding is that the camera plugin works by providing
blob:
URLs, and that's the reason for adding it to the CSP.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.
Thanks for the clarification.