Merge pull request #425 from hashicorp/feature-csp

Feature csp
This commit is contained in:
Randall Morey authored and GitHub committed 2021-01-08 17:31:22 -05:00
commit 7fa608ae25
7 files changed
+140 -12

No files matched your search

-1
View File
@@ -61,7 +61,6 @@ export default class OriginService extends Service {
assert(`setOrigin expects a string, you passed ${origin}`, typeof origin === 'string');
try {
this.adapter.host = origin;
await this.store.query('scope', {});
this.rendererOrigin = origin;
if (origin !== await this.mainOrigin) {
await this.ipc.invoke('setOrigin', origin);
+1
View File
@@ -48,6 +48,7 @@
}
},
"dependencies": {
"axios": "^0.21.1",
"electron-devtools-installer": "^3.1.1",
"electron-is-dev": "^1.2.0",
"electron-squirrel-startup": "^1.0.0",
@@ -0,0 +1,46 @@
const origin = require('./origin.js');
const isDev = require('electron-is-dev');
const csp = {
'default-src': ["'none'"],
'script-src': ["'self'"],
'frame-src': ["'self'"],
'font-src': ["'self'"],
'connect-src': ["'self'"],
'img-src': ["'self'", 'data:'],
'style-src': ["'self'"],
'media-src': ["'self'"],
'manifest-src': ["'self'"],
};
// Dev policy is necessary for Ember live reload.
// Unsafe policy is necessary in development and test environments, but should
// not be used in production.
const enableDevCSP = () => {
csp['style-src'].push("'unsafe-inline'");
csp['script-src'].push("'unsafe-eval'");
csp['script-src'].push("'unsafe-inline'");
csp['script-src'].push("http://localhost:7020");
csp['connect-src'].push("ws://localhost:7020");
};
const generateCSPHeader = () => {
const policy = Object.assign({}, csp);
// If an origin is specified, add it to the connect-src directive
if (origin.origin) {
policy['connect-src'] = policy['connect-src'].slice();
policy['connect-src'].push(origin.origin);
}
return Object
.keys(policy)
.map(key => `${key} ${policy[key].join(' ')};`)
.join(' ');
}
if (isDev) enableDevCSP();
module.exports = {
generateCSPHeader: generateCSPHeader
};
+8 -11
View File
@@ -1,24 +1,21 @@
const handle = require('./ipc-handler');
const boundaryCli = require('./boundary-cli');
let origin = '';
const origin = require('./origin.js');
/**
* Returns the current runtime origin, which is used by the main thread to
* rewrite the CSP to allow requests.
*/
handle('getOrigin', () => origin);
handle('getOrigin', () => origin.origin);
/**
* TODO: sets the main process origin to the specified value. This value will
* be used to rewrite the CSP to allow requests to that origin. If the origin
* was not previously set, the main process will set it and restart
* the renderer.
*
* For now, we update the internal origin but do not yet rewrite the CSP.
* Sets the origin to be used in the content security policy and triggers
* a main window reload.
*/
handle('setOrigin', (requestOrigin) => origin = requestOrigin);
handle('setOrigin', async (requestOrigin) => {
await origin.validateOrigin(requestOrigin);
origin.origin = requestOrigin;
});
/**
* Check for boundary cli existence
+17
View File
@@ -7,6 +7,9 @@ const {
const { session, app, protocol, BrowserWindow, ipcMain } = require('electron');
require('./handlers.js');
const origin = require('./origin.js');
const { generateCSPHeader } = require('./content-security-policy.js');
const isDev = require('electron-is-dev');
// Register the custom file protocol
@@ -58,6 +61,16 @@ app.on('ready', async () => {
return callback(true);
});
// Setup content security policy
ses.webRequest.onHeadersReceived((details, callback) => {
callback({
responseHeaders: {
...details.responseHeaders,
'Content-Security-Policy': [generateCSPHeader()]
}
});
});
if (isDev) {
try {
require('devtron').install();
@@ -88,6 +101,10 @@ app.on('ready', async () => {
},
});
// If the user-specified origin changes, reload the page so that
// the CSP can be refreshed with the this source allowed
origin.onOriginChange(() => mainWindow.loadURL(emberAppURL));
// If you want to open up dev tools programmatically, call
// mainWindow.openDevTools();
+56
View File
@@ -0,0 +1,56 @@
const axios = require('axios');
// Provides a singleton class instance to enable a consistent view of
// runtime settings across the application.
// Runtime settings are any configuration, especially user-specified,
// set up at runtime. For example, the Boundary origin is a runtime setting.
class RuntimeSettings {
// Internal private origin is exposed via getter/setter below.
#origin = undefined;
/**
* The user-specified Boundary origin, which should be allowed by CSP.
* @type {?string}
*/
get origin() { return this.#origin; }
set origin(origin) {
this.#origin = origin;
this.triggerOriginChanged();
}
/**
*
*/
async validateOrigin(origin) {
// If the origin is the Electron origin, it is automatically valid.
if (origin === 'serve://boundary') return true;
// Otherwise, check if scopes can be loaded from the specified origin.
// Scopes are required by the desktop client, so this is a simple smoke
// test to see if the origin is API-compatible with the desktop client.
const scopesEndpoint = `${origin}/v1/scopes`;
try {
await axios.get(scopesEndpoint)
} catch (e) {
throw new Error(`Origin ${origin} could not be validated.`);
}
}
// Quick and dirty event handler pattern to enable the application to respond
// when the origin is changed.
#originWatchers = [];
onOriginChange(fn) {
this.#originWatchers.push(fn);
}
triggerOriginChanged() {
this.#originWatchers.forEach(fn => fn(this.#origin));
}
}
const runtimeSettings = new RuntimeSettings();
module.exports = runtimeSettings;
+12
View File
@@ -494,6 +494,13 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==
axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
dependencies:
follow-redirects "^1.10.0"
balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
@@ -1267,6 +1274,11 @@ flora-colossus@^1.0.0:
debug "^4.1.1"
fs-extra "^7.0.0"
follow-redirects@^1.10.0:
version "1.13.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.1.tgz#5f69b813376cee4fd0474a3aba835df04ab763b7"
integrity sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg==
forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"