-
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 all 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'), | ||
|
@@ -13,6 +14,13 @@ var fs = require('fs'), | |
SocketServer = require('./socket'), | ||
pluginSimulationFiles = require('./plugin-files'); | ||
|
||
/** | ||
* Maximum length of <meta> tag we search for when modifying CSP properties | ||
* Needed for when a tag crosses multiple chunk boundaries. | ||
* @const | ||
*/ | ||
var MAX_CSP_TAG_LENGTH = 1024; | ||
|
||
/** | ||
* The simulation server that encapsulates the HTTP server instance and | ||
* Web Socket server. It enables to start and stop the server to listening | ||
|
@@ -218,13 +226,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[^>]*>/g; | ||
var cspRegex = /http-equiv\s*=\s*(['"])Content-Security-Policy\1/; | ||
var cspContent = /(content\s*=\s*")([^"]*)"/; | ||
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: MAX_CSP_TAG_LENGTH})); | ||
} | ||
}).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.