Skip to content
This repository has been archived by the owner on Feb 26, 2020. It is now read-only.

[stable] Electron security #136

Merged
merged 2 commits into from
Jun 11, 2018
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parity-ui",
"version": "0.1.2",
"version": "0.1.3",
"description": "The Electron app for Parity UI",
"main": ".build/electron.js",
"jsnext:main": ".build/electron.js",
Expand Down Expand Up @@ -28,7 +28,7 @@
"build": "npm run build:inject && npm run build:app && npm run build:electron && npm run build:embed",
"build:app": "webpack --config webpack/app",
"build:electron": "webpack --config webpack/electron",
"build:inject": "webpack --config webpack/inject",
"build:inject": "webpack --config webpack/inject && webpack --config webpack/preload",
"build:embed": "cross-env EMBED=1 node webpack/embed",
"build:i18n": "npm run clean && npm run build && babel-node ./scripts/build-i18n.js",
"ci:build": "cross-env NODE_ENV=production npm run build",
Expand Down Expand Up @@ -186,4 +186,4 @@
"solc": "ngotchac/solc-js",
"store": "1.3.20"
}
}
}
1 change: 0 additions & 1 deletion src/Dapp/dapp.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
flex-grow: 1;
margin: 0;
padding: 0;
opacity: 0;
width: 100%;
z-index: 1;
}
Expand Down
29 changes: 11 additions & 18 deletions src/Dapp/dapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,16 @@ export default class Dapp extends Component {
// Reput eventListeners when webview has finished loading dapp
webview.addEventListener('did-finish-load', () => {
this.setState({ loading: false });
// Listen to IPC messages from this webview
webview.addEventListener('ipc-message', event =>
this.requestsStore.receiveMessage({
...event.args[0],
source: event.target
}));
// Send ping message to tell dapp we're ready to listen to its ipc messages
webview.send('ping');
});

this.onDappLoad();
// Listen to IPC messages from this webview. More particularly, to IPC
// messages coming from the preload.js injected in this webview.
webview.addEventListener('ipc-message', event => {
this.requestsStore.receiveMessage({
...event.args[0],
source: event.target
});
});
};

loadApp (id) {
Expand All @@ -115,7 +114,6 @@ export default class Dapp extends Component {
frameBorder={ 0 }
id='dappFrame'
name={ name }
onLoad={ this.onDappLoad }
ref={ this.handleIframe }
sandbox='allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation'
scrolling='auto'
Expand All @@ -135,16 +133,17 @@ export default class Dapp extends Component {
posixDirName,
'..',
'.build',
'inject.js'
'preload.js'
)}`;

// https://electronjs.org/docs/tutorial/security#3-enable-context-isolation-for-remote-content
return <webview
className={ styles.frame }
id='dappFrame'
nodeintegration='false'
preload={ preload }
ref={ this.handleWebview }
src={ `${src}${hash}` }
webpreferences='contextIsolation'
/>;
}

Expand Down Expand Up @@ -210,10 +209,4 @@ export default class Dapp extends Component {
? this.renderWebview(src, hash)
: this.renderIframe(src, hash);
}

onDappLoad = () => {
const frame = document.getElementById('dappFrame');

frame.style.opacity = 1;
}
}
35 changes: 35 additions & 0 deletions src/index.electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@ function createWindow () {
}
});

// Do not accept all kind of web permissions (camera, location...)
// https://electronjs.org/docs/tutorial/security#4-handle-session-permission-requests-from-remote-content
session.defaultSession
.setPermissionRequestHandler((webContents, permission, callback) => {
if (!webContents.getURL().startsWith('file:')) {
// Denies the permissions request for all non-file://. Currently all
// network dapps are loaded on http://127.0.0.1:8545, so they won't
// have any permissions.
return callback(false);
}

// All others loaded on file:// (shell, builtin, local) can have those
// permissions.
return callback(true);
});

// Verify WebView Options Before Creation
// https://electronjs.org/docs/tutorial/security#12-verify-webview-options-before-creation
mainWindow.webContents.on('will-attach-webview', (event, webPreferences, params) => {
// Strip away inline preload scripts, ours is at preloadURL
delete webPreferences.preload;
// Verify the location of our prelaod script is legitimate (unless uiDev has been passed)
if (webPreferences.preloadURL !== encodeURI(url.format({
pathname: path.join(__dirname, 'preload.js'),
protocol: 'file:',
slashes: true
}))) {
throw new Error(`Unknown preload.js is being injected, quitting for security reasons. ${webPreferences.preloadURL}`);
}

// Disable Node.js integration
webPreferences.nodeIntegration = false;
webPreferences.contextIsolation = true;
});

// Create the Application's main menu
// https://github.com/electron/electron/blob/master/docs/api/menu.md#examples
const template = [
Expand Down
70 changes: 39 additions & 31 deletions src/index.parity.ejs
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title><%= htmlWebpackPlugin.options.title %></title>
<style>
html {
background: white;
background-repeat: round;
}

html, body, #container {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: Sans-Serif;
}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Security-Policy" content="<%= htmlWebpackPlugin.options.csp %>">
<title>
<%= htmlWebpackPlugin.options.title %>
</title>
<style>
html {
background: white;
background-repeat: round;
}

.loading {
text-align: center;
padding-top: 5em;
font-size: 2em;
color: #999;
}
</style>
</head>
<body>
<div id="container">
<div class="loading">Loading</div>
</div>
</body>
</html>
html,
body,
#container {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: Sans-Serif;
}

.loading {
text-align: center;
padding-top: 5em;
font-size: 2em;
color: #999;
}
</style>
</head>

<body>
<div id="container">
<div class="loading">Loading</div>
</div>
</body>

</html>
22 changes: 18 additions & 4 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import Api from '@parity/api';
import isElectron from 'is-electron';
import qs from 'query-string';

console.log('This inject.js has been injected by the shell.');
Expand All @@ -26,9 +25,9 @@ function initProvider () {

let appId = match ? match[0] : query.appId;

const ethereum = isElectron()
? new Api.Provider.Ipc(appId)
: new Api.Provider.PostMessage(appId);
// The dapp will use the PostMessage provider, send postMessages to
// preload.js, and preload.js will relay those messages to the shell.
const ethereum = new Api.Provider.PostMessage(appId);

console.log(`Requesting API communications token for ${appId}`);

Expand Down Expand Up @@ -69,4 +68,19 @@ if (typeof window !== 'undefined' && !window.isParity) {
initParity(ethereum);

console.warn('Deprecation: Dapps should only used the exposed EthereumProvider on `window.ethereum`, the use of `window.parity` and `window.web3` will be removed in future versions of this injector');

// Disable eval() for dapps
// https://electronjs.org/docs/tutorial/security#7-override-and-disable-eval
//
// TODO Currently Web3 Console dapp needs eval(), and is the only builtin
// that needs it, so we cannot blindly disable it as per the recommendation.
// One idea is to check here in inject.js if allowJsEval is set to true, but
// this requires more work (future PR).
// For now we simply allow eval(). All builtin dapps are trusted and can use
// eval(), and all network dapps are served on 127.0.0.1:8545, which have CSP
// that disallow eval(). So security-wise it should be enough.
//
// window.eval = global.eval = function () { // eslint-disable-line
// throw new Error(`Sorry, this app does not support window.eval().`);
// };
}
38 changes: 38 additions & 0 deletions src/preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import fs from 'fs';
import { ipcRenderer, remote, webFrame } from 'electron';
import path from 'path';

// The following two lines is just a proxy between the webview and the renderer process.
// If we receive an IPC message from the shell, we relay it to the webview as
// postMessage.
ipcRenderer.on('PARITY_SHELL_IPC_CHANNEL', (_, data) => window.postMessage(data, '*'));
// If we receive a postMessage from the webview, we transfer it to the renderer
// as an IPC message.
window.addEventListener('message', (event) => {
ipcRenderer.sendToHost('PARITY_SHELL_IPC_CHANNEL', { data: event.data });
});

// Load inject.js as a string, and inject it into the webview with executeJavaScript
fs.readFile(path.join(remote.getGlobal('dirName'), '..', '.build', 'inject.js'), 'utf8', (err, injectFile) => {
if (err) {
console.error(err);
return;
}
webFrame.executeJavaScript(injectFile);
});
41 changes: 41 additions & 0 deletions src/util/csp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const shared = [
// Restrict everything to the same origin by default.
"default-src 'self';",
// Allow connecting to WS servers and HTTP(S) servers.
// We could be more restrictive and allow only RPC server URL.
'connect-src http: https: ws: wss:;',
// Allow framing any content from HTTP(S).
// Again we could only allow embedding from RPC server URL.
// (deprecated)
"frame-src 'none';",
// Allow framing and web workers from HTTP(S).
"child-src 'self' http: https:;",
// We allow data: blob: and HTTP(s) images.
// We could get rid of wildcarding HTTP and only allow RPC server URL.
// (http required for local dapps icons)
"img-src 'self' 'unsafe-inline' file: data: blob: http: https:;",
// Allow style from data: blob: and HTTPS.
"style-src 'self' 'unsafe-inline' data: blob: https:;",
// Allow fonts from data: and HTTPS.
"font-src 'self' data: https:;",
// Same restrictions as script-src with additional
// blob: that is required for camera access (worker)
"worker-src 'self' https: blob:;",
// Disallow submitting forms from any dapps
"form-action 'none';",
// Never allow mixed content
'block-all-mixed-content;'
];

// These are the CSP for the renderer process (aka the shell)
const rendererCsp = [
...shared,
// Allow <webview> which are objects
"object-src 'self';",
// Allow scripts
"script-src 'self';"
];

module.exports = {
rendererCsp
};
4 changes: 3 additions & 1 deletion webpack/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ServiceWorkerWebpackPlugin = require('serviceworker-webpack-plugin');

const { rendererCsp } = require('../src/util/csp');
const rulesEs6 = require('./rules/es6');
const rulesParity = require('./rules/parity');
const Shared = require('./shared');
Expand Down Expand Up @@ -54,7 +55,7 @@ module.exports = {
cache: !isProd,
devtool: isProd
? false
: isEmbed ? '#source-map' : '#eval',
: '#source-map',
context: path.join(__dirname, '../src'),
entry,
output: {
Expand Down Expand Up @@ -175,6 +176,7 @@ module.exports = {
plugins,

new HtmlWebpackPlugin({
csp: rendererCsp.join(' '),
title: 'Parity UI',
filename: 'index.html',
template: './index.parity.ejs',
Expand Down
Loading