[APM] Invalid array value is permitted in Origin Headers for RUM configuration (#137228)

* adding allow origin validation

* addressing PR comments

* change invalid message
This commit is contained in:
Cauê Marcondes 2022-07-28 12:24:35 -04:00 committed by GitHub
parent bb93f02740
commit 21576233d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 1 deletions

View file

@ -0,0 +1,51 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { getRUMSettings, isRUMFormValid } from './rum_settings';
describe('rum_settings - isRUMFormValid', () => {
it('returns true when allowed origin is string', () => {
const settings = getRUMSettings();
const isValid = isRUMFormValid(
{
enable_rum: { value: true, type: 'bool' },
rum_allow_origins: { value: ['*', 'foo', '1'], type: 'text' },
},
settings
);
expect(isValid).toBe(true);
});
it('returns false when allowed origin is an array', () => {
const settings = getRUMSettings();
const isValid = isRUMFormValid(
{
enable_rum: { value: true, type: 'bool' },
rum_allow_origins: {
value: ['*', 'foo', '1', '["bar"', ']'],
type: 'text',
},
},
settings
);
expect(isValid).toBe(false);
});
it('returns true when rum is disabled', () => {
const settings = getRUMSettings();
const isValid = isRUMFormValid(
{
enable_rum: { value: false, type: 'bool' },
rum_allow_origins: {
value: ['*', 'foo', '1', '["bar"]'],
type: 'text',
},
},
settings
);
expect(isValid).toBe(true);
});
});

View file

@ -4,10 +4,38 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import * as t from 'io-ts';
import { either } from 'fp-ts/lib/Either';
import { i18n } from '@kbn/i18n';
import { PackagePolicyVars, SettingsRow } from '../typings';
import { isSettingsFormValid, OPTIONAL_LABEL } from '../settings_form/utils';
const arrayRegex = new RegExp(/[\[\]]/);
function getAllowedOriginsRt() {
return new t.Type<string, string, unknown>(
'allowedOriginsRt',
t.string.is,
(input, context) => {
return either.chain(
t.string.validate(input, context),
(inputAsString) => {
return arrayRegex.test(inputAsString)
? t.failure(
input,
context,
i18n.translate(
'xpack.apm.fleet_integration.settings.rum.allowedHeadersValidation',
{ defaultMessage: 'Square brackets not allowed' }
)
)
: t.success(inputAsString);
}
);
},
t.identity
);
}
const ENABLE_RUM_KEY = 'enable_rum';
export function getRUMSettings(): SettingsRow[] {
return [
@ -28,7 +56,7 @@ export function getRUMSettings(): SettingsRow[] {
type: 'combo',
label: i18n.translate(
'xpack.apm.fleet_integration.settings.rum.rumAllowOriginsLabel',
{ defaultMessage: 'Origin Headers' }
{ defaultMessage: 'Allowed Origins' }
),
labelAppend: OPTIONAL_LABEL,
helpText: i18n.translate(
@ -38,6 +66,7 @@ export function getRUMSettings(): SettingsRow[] {
'Allowed Origin headers to be sent by User Agents.',
}
),
validation: getAllowedOriginsRt(),
},
{
key: 'rum_allow_headers',