Skip to content

Commit

Permalink
Adding a warning when a developer users hostWhitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiupover committed Jun 10, 2021
1 parent dcb6d03 commit 48e44c6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 56 deletions.
32 changes: 17 additions & 15 deletions packages/ember-cli-fastboot/lib/broccoli/fastboot-config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-env node */
'use strict';

const fs = require('fs');
const fmt = require('util').format;
const uniq = require('ember-cli-lodash-subset').uniq;
const merge = require('ember-cli-lodash-subset').merge;
const md5Hex = require('md5-hex');
const path = require('path');
const Plugin = require('broccoli-plugin');

const fs = require('fs');
const fmt = require('util').format;
const uniq = require('ember-cli-lodash-subset').uniq;
const merge = require('ember-cli-lodash-subset').merge;
const md5Hex = require('md5-hex');
const path = require('path');
const Plugin = require('broccoli-plugin');
const stringify = require('json-stable-stringify');

const LATEST_SCHEMA_VERSION = 3;
Expand Down Expand Up @@ -50,7 +49,7 @@ module.exports = class FastBootConfig extends Plugin {
this.buildConfig();
this.buildDependencies();
this.buildManifest();
this.buildHostAllowlist();
this.buildHostAllowList();

let outputPath = path.join(this.outputPath, 'package.json');
this.writeFileIfContentChanged(outputPath, this.toJSONString());
Expand Down Expand Up @@ -160,9 +159,12 @@ module.exports = class FastBootConfig extends Plugin {
this.manifest = this.updateFastBootManifest(manifest);
}

buildHostAllowlist() {
buildHostAllowList() {
if (this.fastbootAppConfig) {
this.hostAllowlist = this.fastbootAppConfig.hostAllowlist;
if ('hostWhitelist' in this.fastbootAppConfig) {
this.ui.writeLine('Please update your fastboot config to use `hostAllowList` of the deprecated `hostWhitelist`');
}
this.hostAllowList = this.fastbootAppConfig.hostAllowList || this.fastbootAppConfig.hostWhitelist
}
}

Expand All @@ -173,19 +175,19 @@ module.exports = class FastBootConfig extends Plugin {
moduleAllowlist: this.moduleAllowlist,
schemaVersion: LATEST_SCHEMA_VERSION,
manifest: this.manifest,
hostAllowlist: this.normalizeHostAllowlist(),
hostAllowList: this.normalizeHostAllowList(),
config: this.fastbootConfig,
appName: this.appName,
}
}, null, 2);
}

normalizeHostAllowlist() {
if (!this.hostAllowlist) {
normalizeHostAllowList() {
if (!this.hostAllowList) {
return;
}

return this.hostAllowlist.map(function(entry) {
return this.hostAllowList.map(function(entry) {
// Is a regex
if (entry.source) {
return '/' + entry.source + '/';
Expand Down
1 change: 0 additions & 1 deletion packages/ember-cli-fastboot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"body-parser": "^1.18.3",
"broccoli-asset-rev": "^3.0.0",
"broccoli-test-helper": "^1.5.0",
"co": "4.6.0",
"chai": "^4.1.2",
"chai-fs": "^2.0.0",
"chai-string": "^1.4.0",
Expand Down
83 changes: 50 additions & 33 deletions packages/ember-cli-fastboot/test/fastboot-config-test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
/* eslint-env node */
'use strict';

const expect = require('chai').use(require('chai-string')).expect;
const RSVP = require('rsvp');
const request = RSVP.denodeify(require('request'));

const AddonTestApp = require('ember-cli-addon-tests').AddonTestApp;

describe('FastBoot config', function() {
this.timeout(400000);
const expect = require('chai').expect;
const helpers = require('broccoli-test-helper');
const MockUI = require('console-ui/mock')
const createBuilder = helpers.createBuilder;
const createTempDir = helpers.createTempDir;
const FastbootConfig = require('../lib/broccoli/fastboot-config');


describe('FastbootConfig', function() {
let input;
let output;
let subject;
let project;

beforeEach(async function() {
input = await createTempDir();
project = {
addons: [],
pkg: {},
};
subject = new FastbootConfig(input.path(), {
project,
outputPaths: {
app: { js: 'app.js' },
vendor: { js: 'vendor.js' },
},
appConfig: {
modulePrefix: 'app',
},
ui: new MockUI(),
fastbootAppConfig: {
hostWhitelist: ['example.com', 'subdomain.example.com']
}
});
output = createBuilder(subject);
});

let app;
afterEach(async function() {
await input.dispose();
await output.dispose();
});

before(function() {
app = new AddonTestApp();
it('it replace hostWhitelist with hostAllowList and warns user to update the config to hostAllowList', async function() {
input.write({});

return app.create('fastboot-config', { emberVersion: 'latest'})
.then(function() {
return app.startServer({
command: 'serve'
});
});
});
await output.build();

after(function() {
return app.stopServer();
});
expect(
output.read()
).to.deep.equal({
'package.json': `{"dependencies":{},"fastboot":{"appName":"app","config":{"app":{"modulePrefix":"app"}},"hostAllowList":["example.com","subdomain.example.com"],"manifest":{"appFiles":["app.js","app-fastboot.js"],"htmlFile":"index.html","vendorFiles":["vendor.js"]},"moduleAllowlist":[],"schemaVersion":3}}`
});

it('provides sandbox globals', function() {
return request({
url: 'http://localhost:49741/',
headers: {
'Accept': 'text/html'
}
})
.then(function(response) {
expect(response.statusCode).to.equal(200);
expect(response.headers['content-type']).to.equalIgnoreCase('text/html; charset=utf-8');
expect(response.body).to.contain('<h1>My Global</h1>');
});
expect(output.builder.outputNode.ui.output).to.contain('Please update your fastboot config to use `hostAllowList` of the deprecated `hostWhitelist`');
});
});
1 change: 0 additions & 1 deletion packages/ember-cli-fastboot/test/new-package-json-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const expect = require('chai').expect;
const helpers = require('broccoli-test-helper');
const createBuilder = helpers.createBuilder;
const createTempDir = helpers.createTempDir;
const co = require('co');
const FastbootConfig = require('../lib/broccoli/fastboot-config');

describe('FastbootConfig', function() {
Expand Down
2 changes: 1 addition & 1 deletion test-packages/basic-app/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function(environment) {
},

fastboot: {
hostWhitelist: [
hostAllowList: [
'example.com',
'subdomain.example.com',
'/localhost:\\d+/',
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5577,11 +5577,6 @@ clone@^2.0.0, clone@^2.1.2:
resolved "https://registry.yarnpkg.com/clone/-/clone-2.1.2.tgz#1b7f4b9f591f1e8f83670401600345a02887435f"
integrity sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=

co@4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
integrity sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=

code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
Expand Down

0 comments on commit 48e44c6

Please sign in to comment.