Skip to content

Commit

Permalink
ui: CSP Improvements (#9847)
Browse files Browse the repository at this point in the history
* Configure ember-auto-import so we can use a stricter CSP

* Create a fake filesystem using JSON to avoid inline scripts in index

We used to have inline scripts in index.html in order to support embers
filepath fingerprinting and our configurable rootURL.

Instead of using inline scripts we use application/json plus a JSON blob
to create a fake filesystem JSON blob/hash/map to hold all of the
rootURL'ed fingerprinted file paths which we can then retrive later in
non-inline scripts.

We move our inlined polyfills script into the init.js external script,
and we move the CodeMirror syntax highlighting configuration inline
script into the main app itself - into the already existing CodeMirror
initializer (this has been moved so we can lookup a service located
document using ember's DI container)

* Set a strict-ish CSP policy during development
  • Loading branch information
johncowen authored and dizzyup committed Apr 21, 2021
1 parent f1982a6 commit 3b02e02
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .changelog/9847.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
ui: support stricter content security policies
```
11 changes: 0 additions & 11 deletions ui/packages/consul-ui/app/initializers/ivy-codemirror.js

This file was deleted.

30 changes: 30 additions & 0 deletions ui/packages/consul-ui/app/instance-initializers/ivy-codemirror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* globals CodeMirror */
export function initialize(application) {
const appName = application.application.name;
const doc = application.lookup('service:-document');
// pick codemirror syntax highlighting paths out of index.html
const fs = JSON.parse(doc.querySelector(`[data-${appName}-fs]`).textContent);
// configure syntax highlighting for CodeMirror
CodeMirror.modeURL = {
replace: function(n, mode) {
switch (mode) {
case 'javascript':
return fs['codemirror/mode/javascript/javascript.js'];
case 'ruby':
return fs['codemirror/mode/ruby/ruby.js'];
case 'yaml':
return fs['codemirror/mode/yaml/yaml.js'];
}
},
};

const IvyCodeMirrorComponent = application.resolveRegistration('component:ivy-codemirror');
// Make sure ivy-codemirror respects/maintains a `name=""` attribute
IvyCodeMirrorComponent.reopen({
attributeBindings: ['name'],
});
}

export default {
initialize,
};
4 changes: 4 additions & 0 deletions ui/packages/consul-ui/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module.exports = function(defaults) {
plugins: ['@babel/plugin-proposal-object-rest-spread'],
sourceMaps: sourcemaps ? 'inline' : false,
},
autoImport: {
// allows use of a CSP without 'unsafe-eval' directive
forbidEval: true,
},
codemirror: {
keyMaps: ['sublime'],
addonFiles: [
Expand Down
38 changes: 10 additions & 28 deletions ui/packages/consul-ui/lib/startup/templates/body.html.js

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

8 changes: 8 additions & 0 deletions ui/packages/consul-ui/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ module.exports = function(app, options) {
}
next();
});

// sets the base CSP policy for the UI
app.use(function(request, response, next) {
response.set({
'Content-Security-Policy': `default-src 'self' ws: localhost:${options.liveReloadPort} http: localhost:${options.liveReloadPort}; img-src 'self' data: ; style-src 'self' 'unsafe-inline'`,
});
next();
});
// Serve the coverage folder for easy viewing during development
app.use('/coverage', express.static('coverage'));
};
16 changes: 16 additions & 0 deletions ui/packages/consul-ui/vendor/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
(function(doc, appName) {
const fs = JSON.parse(doc.querySelector(`[data-${appName}-fs]`).textContent);
const appendScript = function(src) {
var $script = doc.createElement('script');
$script.src = src;
doc.body.appendChild($script);
};

// polyfills
if (!('TextDecoder' in window)) {
appendScript(fs['text-encoding/encoding-indexes.js']);
appendScript(fs['text-encoding/encoding.js']);
}
if (!(window.CSS && window.CSS.escape)) {
appendScript(fs['css.escape/css.escape.js']);
}

try {
const $appMeta = doc.querySelector(`[name="${appName}/config/environment"]`);
// pick out the operatorConfig from our application/json script tag
Expand Down

0 comments on commit 3b02e02

Please sign in to comment.