-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[csp] Telemetry for csp configuration #43223
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/csp/csp_collector.test.ts
This file contains hidden or 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,78 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import sinon from 'sinon'; | ||
| import { DEFAULT_CSP_RULES } from '../../../../../../../../src/legacy/server/csp'; | ||
| import { | ||
| getMockCallWithInternal, | ||
| getMockKbnServer, | ||
| getMockTaskFetch, | ||
| } from '../../../../test_utils'; | ||
| import { createCspCollector } from './csp_collector'; | ||
|
|
||
| test('fetches whether strict mode is enabled', async () => { | ||
| const { collector, mockConfig } = setupCollector(); | ||
|
|
||
| expect((await collector.fetch()).strict).toEqual(true); | ||
|
|
||
| mockConfig.get.withArgs('csp.strict').returns(false); | ||
| expect((await collector.fetch()).strict).toEqual(false); | ||
| }); | ||
|
|
||
| test('fetches whether the legacy browser warning is enabled', async () => { | ||
| const { collector, mockConfig } = setupCollector(); | ||
|
|
||
| expect((await collector.fetch()).warnLegacyBrowsers).toEqual(true); | ||
|
|
||
| mockConfig.get.withArgs('csp.warnLegacyBrowsers').returns(false); | ||
| expect((await collector.fetch()).warnLegacyBrowsers).toEqual(false); | ||
| }); | ||
|
|
||
| test('fetches whether the csp rules have been changed or not', async () => { | ||
| const { collector, mockConfig } = setupCollector(); | ||
|
|
||
| expect((await collector.fetch()).rulesChangedFromDefault).toEqual(false); | ||
|
|
||
| mockConfig.get.withArgs('csp.rules').returns(['not', 'default']); | ||
| expect((await collector.fetch()).rulesChangedFromDefault).toEqual(true); | ||
| }); | ||
|
|
||
| test('does not include raw csp.rules under any property names', async () => { | ||
| const { collector } = setupCollector(); | ||
|
|
||
| // It's important that we do not send the value of csp.rules here as it | ||
| // can be customized with values that can be identifiable to given | ||
| // installs, such as URLs | ||
| // | ||
| // We use a snapshot here to ensure csp.rules isn't finding its way into the | ||
| // payload under some new and unexpected variable name (e.g. cspRules). | ||
| expect(await collector.fetch()).toMatchInlineSnapshot(` | ||
| Object { | ||
| "rulesChangedFromDefault": false, | ||
| "strict": true, | ||
| "warnLegacyBrowsers": true, | ||
| } | ||
| `); | ||
| }); | ||
|
|
||
| test('does not arbitrarily fetch other csp configurations (e.g. whitelist only)', async () => { | ||
| const { collector, mockConfig } = setupCollector(); | ||
|
|
||
| mockConfig.get.withArgs('csp.foo').returns('bar'); | ||
|
|
||
| expect(await collector.fetch()).not.toHaveProperty('foo'); | ||
| }); | ||
|
|
||
| function setupCollector() { | ||
| const mockConfig = { get: sinon.stub() }; | ||
| mockConfig.get.withArgs('csp.rules').returns(DEFAULT_CSP_RULES); | ||
| mockConfig.get.withArgs('csp.strict').returns(true); | ||
| mockConfig.get.withArgs('csp.warnLegacyBrowsers').returns(true); | ||
|
|
||
| const mockKbnServer = getMockKbnServer(getMockCallWithInternal(), getMockTaskFetch(), mockConfig); | ||
|
|
||
| return { mockConfig, collector: createCspCollector(mockKbnServer) }; | ||
| } |
39 changes: 39 additions & 0 deletions
39
x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/csp/csp_collector.ts
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import { | ||
| createCSPRuleString, | ||
| DEFAULT_CSP_RULES, | ||
| } from '../../../../../../../../src/legacy/server/csp'; | ||
| import { HapiServer } from '../../../../'; | ||
|
|
||
| export function createCspCollector(server: HapiServer) { | ||
| return { | ||
| type: 'csp', | ||
| isReady: () => true, | ||
| async fetch() { | ||
| const config = server.config(); | ||
|
|
||
| // It's important that we do not send the value of csp.rules here as it | ||
| // can be customized with values that can be identifiable to given | ||
| // installs, such as URLs | ||
| const defaultRulesString = createCSPRuleString([...DEFAULT_CSP_RULES]); | ||
| const actualRulesString = createCSPRuleString(config.get('csp.rules')); | ||
|
epixa marked this conversation as resolved.
|
||
|
|
||
| return { | ||
| strict: config.get('csp.strict'), | ||
| warnLegacyBrowsers: config.get('csp.warnLegacyBrowsers'), | ||
| rulesChangedFromDefault: defaultRulesString !== actualRulesString, | ||
| }; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export function registerCspCollector(server: HapiServer): void { | ||
| const { usage } = server; | ||
| const collector = usage.collectorSet.makeUsageCollector(createCspCollector(server)); | ||
| usage.collectorSet.register(collector); | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
x-pack/legacy/plugins/oss_telemetry/server/lib/collectors/csp/index.ts
This file contains hidden or 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,7 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| export { registerCspCollector } from './csp_collector'; |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.