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

Adding "style-src 'unsafe-inline' 'self'" to default CSP rules #41305

Merged
merged 12 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from 8 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
13 changes: 7 additions & 6 deletions src/legacy/server/csp/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ import {
// the nature of a change in defaults during a PR review.
test('default CSP rules', () => {
expect(DEFAULT_CSP_RULES).toMatchInlineSnapshot(`
Array [
"script-src 'unsafe-eval' 'nonce-{nonce}'",
"worker-src blob:",
"child-src blob:",
]
`);
Array [
"script-src 'unsafe-eval' 'nonce-{nonce}'",
"worker-src blob:",
"child-src blob:",
"style-src 'unsafe-inline' 'self'",
]
`);
});

test('CSP strict mode defaults to disabled', () => {
Expand Down
1 change: 1 addition & 0 deletions src/legacy/server/csp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const DEFAULT_CSP_RULES = Object.freeze([
`script-src 'unsafe-eval' 'nonce-{nonce}'`,
'worker-src blob:',
'child-src blob:',
`style-src 'unsafe-inline' 'self'`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is there any reason we don't want to use nonce for inline styles as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the nonce has caused quite a few issues, as discussed here and I'd like to find a way to move toward self even for script-src. I could see the argument being made for using the nonce until we switch script-src to self, but it's quite a bit more work which we'd rather quickly get rid of.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I see, thanks for the explanation. Yeah, let's see how it goes then.

]);

export const DEFAULT_CSP_STRICT = true;
Expand Down
12 changes: 10 additions & 2 deletions test/api_integration/apis/general/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ export default function ({ getService }) {
it('csp header does not allow all inline scripts', async () => {
const response = await supertest.get('/app/kibana');

expect(response.headers['content-security-policy']).to.contain('script-src');
expect(response.headers['content-security-policy']).not.to.contain('unsafe-inline');
const header = response.headers['content-security-policy'];
const parsed = new Map(header.split(';').map(rule => {
const parts = rule.trim().split(' ');
const key = parts.splice(0, 1)[0];
return [key, parts];
}));

const scriptSrc = parsed.get('script-src');
expect(scriptSrc).to.be.an(Array);
expect(scriptSrc).not.to.contain('unsafe-inline');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I'm a bit concerned about check like this. Our parsing logic in this test may not work as we expect at some point (even with packages like content-security-policy-parser) and we'll get false negatives here. Is there any reason we don't want to use expect().to.eql with a full list of "directives" here? Is it because of the nonce-some-unguessable-value? If so, can we filter startsWith('nonce-') out before asserting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I initially was trying to keep this test similar to how it was originally written, but you've made a convincing argument. Changes will be forth-coming.

});
});
}