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

fix: pass environment into readConfig #274

Merged
merged 3 commits into from
Dec 11, 2021
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
2 changes: 1 addition & 1 deletion lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
run: function (options) {
let { environment, reportUri } = options;
let { project, ui } = this;
let ownConfig = readConfig(project);
let ownConfig = readConfig(project, environment);
let runConfig = project.config(environment);
let { reportOnly, policy } = calculateConfig(
environment,
Expand Down
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const getConfigPath = function (projectPkg, projectRoot) {
* Returns an empty object if that file does not exist.
*
* @param {string} project
* @param {string} environment
* @return {object}
*/
const readConfig = function (project, environment) {
Expand Down
35 changes: 35 additions & 0 deletions node-tests/e2e/cli-command-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,39 @@ describe('e2e: CLI command csp-headers', function () {
"default-src 'self'; script-src 'self' 'unsafe-inline';"
);
});

describe('passes environment into the configuration', function () {
beforeEach(async function () {
await setConfig(testProject, {
policy: {
'default-src': "'self'",
'script-src':
"{{\"'self'\" + (environment === 'development' ? \" 'unsafe-inline'\" : '')}}",
'font-src':
"{{\"'self'\" + (environment === 'production' ? \" http://fonts.gstatic.com\" : '')}}",
},
});
});

it('passes development as default', async function () {
let { stdout } = await testProject.runEmberCommand(
'csp-headers',
'--silent'
);
expect(stdout).to.equal(
"default-src 'self'; script-src 'self' 'unsafe-inline'; font-src 'self';"
);
});

it('passes specified environment', async function () {
let { stdout } = await testProject.runEmberCommand(
'csp-headers',
'--silent',
'--environment=production'
);
expect(stdout).to.equal(
"default-src 'self'; script-src 'self'; font-src 'self' http://fonts.gstatic.com;"
);
});
});
});
10 changes: 8 additions & 2 deletions node-tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ const fs = require('fs');
const CONFIG_PATH = 'config/content-security-policy.js';
const CSP_META_TAG_REG_EXP = /<meta http-equiv="Content-Security-Policy" content="(.*)">/i;

function parseExpressions(config) {
return config.replace(/"{{.*}}"/gs, (match) =>
match.replace(/"{{|}}"/g, '').replace(/\\"/g, '"')
);
}

async function setConfig(testProject, config) {
let content = `module.exports = function() { return ${JSON.stringify(
config
let content = `module.exports = function(environment) { return ${parseExpressions(
JSON.stringify(config)
)}; }`;

await testProject.writeFile(CONFIG_PATH, content);
Expand Down