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 4 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
29 changes: 26 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 Down Expand Up @@ -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
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[^>]*>/;
Copy link
Collaborator

@albertinad albertinad Aug 19, 2016

Choose a reason for hiding this comment

The 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:

<html>
  <head>
    <!-- meta tag #1 -->
    <meta name="viewport" content="width=device-width,height=device-height,user-scalable=no,initial-scale=1.0" />
    <!-- meta tag #1 -->
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <!-- meta tag #3 - CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' https://ssl.gstatic.com/accessibility/javascript/android/; connect-src 'self' http://cordova-filetransfer.jitsu.com; media-src 'self' http://cordova.apache.org/downloads/ https://cordova.apache.org/downloads/; frame-src 'self' http://stealbridgesecret.test/ data: gap:; img-src 'self' data: cdvfile:; style-src 'self' 'unsafe-inline'"/>

    <!-- more content here -->

  </head>
  <body id="stage" class="theme">
    <!-- more content here -->
  </body>
</html>

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,

var metaTagRegex = /<\s*meta[^>]*>/g;

it returns all of the matches and it works as expected.

Copy link
Member Author

Choose a reason for hiding this comment

The 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*")([^"]*)"/
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing semi-colon

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

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

Choose a reason for hiding this comment

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

nit: we can keep maxMatchLen configuration value in a constant, with a representative name and maybe a comment that clarifies the reason of that specific value, so we don't forget about it.

}
}).pipe(response);
}.bind(this))
Expand Down