Skip to content

Commit c53e75d

Browse files
authored
Adding "style-src 'unsafe-inline' 'self'" to default CSP rules (#41305) (#43065)
* Adding "style-src 'unsafe-inline' 'self'" to default CSP rules * Updating jest snapshot * Fixing api integration smoke test * Verifying all CSP responses * Fixing OIDC implicit flow test
1 parent c868741 commit c53e75d

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/legacy/server/csp/index.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ import {
3939
// the nature of a change in defaults during a PR review.
4040
test('default CSP rules', () => {
4141
expect(DEFAULT_CSP_RULES).toMatchInlineSnapshot(`
42-
Array [
43-
"script-src 'unsafe-eval' 'nonce-{nonce}'",
44-
"worker-src blob:",
45-
"child-src blob:",
46-
]
47-
`);
42+
Array [
43+
"script-src 'unsafe-eval' 'nonce-{nonce}'",
44+
"worker-src blob:",
45+
"child-src blob:",
46+
"style-src 'unsafe-inline' 'self'",
47+
]
48+
`);
4849
});
4950

5051
test('CSP strict mode defaults to disabled', () => {

src/legacy/server/csp/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const DEFAULT_CSP_RULES = Object.freeze([
2626
`script-src 'unsafe-eval' 'nonce-{nonce}'`,
2727
'worker-src blob:',
2828
'child-src blob:',
29+
`style-src 'unsafe-inline' 'self'`,
2930
]);
3031

3132
export const DEFAULT_CSP_STRICT = false;

test/api_integration/apis/general/csp.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,27 @@ export default function ({ getService }) {
2727
const response = await supertest.get('/app/kibana');
2828

2929
expect(response.headers).to.have.property('content-security-policy');
30-
});
30+
const header = response.headers['content-security-policy'];
31+
const parsed = new Map(header.split(';').map(rule => {
32+
const parts = rule.trim().split(' ');
33+
const key = parts.splice(0, 1)[0];
34+
return [key, parts];
35+
}));
3136

32-
it('csp header does not allow all inline scripts', async () => {
33-
const response = await supertest.get('/app/kibana');
37+
// ensure script-src uses a nonce, and remove it so we can .eql everything else
38+
const scriptSrc = parsed.get('script-src');
39+
expect(scriptSrc).to.be.an(Array);
40+
const nonceIndex = scriptSrc.findIndex(value => value.startsWith(`'nonce-`));
41+
expect(nonceIndex).greaterThan(-1);
42+
scriptSrc.splice(nonceIndex, 1);
3443

35-
expect(response.headers['content-security-policy']).to.contain('script-src');
36-
expect(response.headers['content-security-policy']).not.to.contain('unsafe-inline');
44+
const entries = Array.from(parsed.entries());
45+
expect(entries).to.eql([
46+
[ 'script-src', [ '\'unsafe-eval\'' ] ],
47+
[ 'worker-src', [ 'blob:' ] ],
48+
[ 'child-src', [ 'blob:' ] ],
49+
[ 'style-src', [ '\'unsafe-inline\'', '\'self\'' ] ]
50+
]);
3751
});
3852
});
3953
}

x-pack/test/oidc_api_integration/apis/implicit_flow/oidc_auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default function({ getService }: FtrProviderContext) {
5353
expect(response.headers['content-type']).to.be('text/html; charset=utf-8');
5454
expect(response.headers['cache-control']).to.be('private, no-cache, no-store');
5555
expect(response.headers['content-security-policy']).to.be(
56-
`script-src 'unsafe-eval' 'nonce-${scriptNonce}'; worker-src blob:; child-src blob:`
56+
`script-src 'unsafe-eval' 'nonce-${scriptNonce}'; worker-src blob:; child-src blob:; style-src 'unsafe-inline' 'self'`
5757
);
5858

5959
// Check that script that forwards URL fragment worked correctly.

0 commit comments

Comments
 (0)