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

development server should use config for test if serving /tests/ #143

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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ module.exports = {
// Use policy for test environment if both of these conditions are met:
// 1. the request is for tests and
// 2. the build include tests
let isRequestForTests = req.originalUrl.startsWith('/tests') && app.tests;
let buildIncludeTests = this.app.tests;
let isRequestForTests = req.originalUrl.startsWith('/tests') && buildIncludeTests;
let config = isRequestForTests ? this._configForTest : this._config;
let policyString = isRequestForTests ? this._policyStringForTest : this._policyString;
let header = config.reportOnly ? CSP_HEADER_REPORT_ONLY : CSP_HEADER;
Expand Down
59 changes: 41 additions & 18 deletions node-tests/e2e/test-support-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,56 @@ describe('e2e: provides test support', function() {
expect(indexHtml).to.not.match(CSP_META_TAG_REG_EXP);
});

it('adds nonce to script-src when required by tests', async function() {
await setConfig(app, {
delivery: ['meta'],
describe('adds nonce to script-src if needed', function() {
afterEach(async function() {
await removeConfig(app);
await app.stopServer();
});

await app.runEmberCommand('build');
it('adds nonce to script-src when required by tests', async function() {
await setConfig(app, {
delivery: ['meta'],
});

let testsIndexHtml = await fs.readFile(app.filePath('dist/tests/index.html'), 'utf8');
let [,cspInTestsIndexHtml] = testsIndexHtml.match(CSP_META_TAG_REG_EXP);
await app.startServer();

expect(cspInTestsIndexHtml).to.include('nonce-');
});
let response = await request({
url: 'http://localhost:49741/tests/',
headers: {
'Accept': 'text/html'
}
});

it('does not add nonce to script-src when it already has \'unsafe-inline\'', async function() {
await setConfig(app, {
delivery: ['meta'],
policy: {
'script-src': ["'self'", "'unsafe-inline'"]
}
let cspInHeader = response.headers['content-security-policy-report-only'];
let cspInMetaElement = response.body.match(CSP_META_TAG_REG_EXP)[1];
[cspInHeader, cspInMetaElement].forEach((csp) => {
expect(csp).to.match(/script-src [^;]* 'nonce-/);
});
});

await app.runEmberCommand('build');
it('does not add nonce to script-src if directive contains \'unsafe-inline\'', async function() {
await setConfig(app, {
delivery: ['meta'],
policy: {
'script-src': ["'self'", "'unsafe-inline'"]
}
});

let testsIndexHtml = await fs.readFile(app.filePath('dist/tests/index.html'), 'utf8');
let [,cspInTestsIndexHtml] = testsIndexHtml.match(CSP_META_TAG_REG_EXP);
await app.startServer();

expect(cspInTestsIndexHtml).to.not.include('nonce-');
let response = await request({
url: 'http://localhost:49741/tests/',
headers: {
'Accept': 'text/html'
}
});

let cspInHeader = response.headers['content-security-policy-report-only'];
let cspInMetaElement = response.body.match(CSP_META_TAG_REG_EXP)[1];
[cspInHeader, cspInMetaElement].forEach((csp) => {
expect(csp).to.not.include('nonce-');
});
});
});

describe('it uses CSP configuration for test environment if running tests', function() {
Expand Down