-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* csp: nonce and unsafe-eval for scripts To kick things off, a rudimentary CSP implementation only allows dynamically loading new JavaScript if it includes an associated nonce that is generated on every load of the app. A more sophisticated content security policy is necessary, particularly one that bans eval for scripts, but one step at a time. * img-src is not necessary if the goal is not to restrict * configurable CSP owned by security team * smoke test * remove x-content-security-policy * document csp.rules * fix tsconfig for test * switch integration test back to regular js * stop looking for tsconfig in test * grrr, linting errors not caught by precommit * docs: people -> you for consistency sake Co-Authored-By: epixa <court@epixa.com>
- Loading branch information
Showing
12 changed files
with
195 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { createCSPRuleString, DEFAULT_CSP_RULES, generateCSPNonce } from './'; | ||
|
||
// CSP rules aren't strictly additive, so any change can potentially expand or | ||
// restrict the policy in a way we consider a breaking change. For that reason, | ||
// we test the default rules exactly so any change to those rules gets flagged | ||
// for manual review. In otherwords, this test is intentionally fragile to draw | ||
// extra attention if defaults are modified in any way. | ||
// | ||
// A test failure here does not necessarily mean this change cannot be made, | ||
// but any change here should undergo sufficient scrutiny by the Kibana | ||
// security team. | ||
// | ||
// The tests use inline snapshots to make it as easy as possible to identify | ||
// 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:", | ||
] | ||
`); | ||
}); | ||
|
||
test('generateCSPNonce() creates a 16 character string', async () => { | ||
const nonce = await generateCSPNonce(); | ||
|
||
expect(nonce).toHaveLength(16); | ||
}); | ||
|
||
test('generateCSPNonce() creates a new string on each call', async () => { | ||
const nonce1 = await generateCSPNonce(); | ||
const nonce2 = await generateCSPNonce(); | ||
|
||
expect(nonce1).not.toEqual(nonce2); | ||
}); | ||
|
||
test('createCSPRuleString() converts an array of rules into a CSP header string', () => { | ||
const csp = createCSPRuleString([`string-src 'self'`, 'worker-src blob:', 'img-src data: blob:']); | ||
|
||
expect(csp).toMatchInlineSnapshot(`"string-src 'self'; worker-src blob:; img-src data: blob:"`); | ||
}); | ||
|
||
test('createCSPRuleString() replaces all occurrences of {nonce} if provided', () => { | ||
const csp = createCSPRuleString( | ||
[`string-src 'self' 'nonce-{nonce}'`, 'img-src data: blob:', `default-src 'nonce-{nonce}'`], | ||
'foo' | ||
); | ||
|
||
expect(csp).toMatchInlineSnapshot( | ||
`"string-src 'self' 'nonce-foo'; img-src data: blob:; default-src 'nonce-foo'"` | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { randomBytes } from 'crypto'; | ||
import { promisify } from 'util'; | ||
|
||
const randomBytesAsync = promisify(randomBytes); | ||
|
||
export const DEFAULT_CSP_RULES = Object.freeze([ | ||
`script-src 'unsafe-eval' 'nonce-{nonce}'`, | ||
'worker-src blob:', | ||
'child-src blob:', | ||
]); | ||
|
||
export async function generateCSPNonce() { | ||
return (await randomBytesAsync(12)).toString('base64'); | ||
} | ||
|
||
export function createCSPRuleString(rules: string[], nonce?: string) { | ||
let ruleString = rules.join('; '); | ||
if (nonce) { | ||
ruleString = ruleString.replace(/\{nonce\}/g, nonce); | ||
} | ||
return ruleString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import expect from 'expect.js'; | ||
|
||
export default function ({ getService }) { | ||
const supertest = getService('supertest'); | ||
|
||
describe('csp smoke test', () => { | ||
it('app response sends content security policy headers', async () => { | ||
const response = await supertest.get('/app/kibana'); | ||
|
||
expect(response.headers).to.have.property('content-security-policy'); | ||
}); | ||
|
||
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'); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters