Skip to content
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

Merged
merged 6 commits into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"chalk": "^1.1.1",
"cordova-registry-mapper": "^1.1.12",
"cordova-serve": "^1.0.0",
"csp-parse": "^0.0.2",
"glob": "^7.0.5",
"minimist": "^1.2.0",
"ncp": "^2.0.0",
Expand Down
36 changes: 33 additions & 3 deletions src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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
Expand Down Expand Up @@ -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
Copy link
Collaborator

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?

Copy link
Member Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification.

// 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, {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The call to send performs a critical task in order to make the simulation works: inject simulation files and makes an analyzisis of CSP in order to inject another configuration that allows connections to localhost for simulation. We could probably do a little refactor on _streamAppHostHtml and the promise chain at the prepare call. So it is easy to follow, smaller methods and more testable.
This is not needed to do it as part of this PR, it's just a comment :)

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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))
Expand Down