Skip to content

Commit

Permalink
Avoid mutating KQL query when validating it (elastic#97081)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Apr 18, 2021
1 parent 27583f2 commit 0e7d0e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/core/server/saved_objects/service/lib/filter_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { cloneDeep } from 'lodash';
// @ts-expect-error no ts
import { esKuery } from '../../es_query';

Expand Down Expand Up @@ -105,6 +106,22 @@ describe('Filter Utils', () => {
)
).toEqual(esKuery.fromKueryExpression('foo.title: "best"'));
});

test('does not mutate the input KueryNode', () => {
const input = esKuery.nodeTypes.function.buildNode(
'is',
`foo.attributes.title`,
'best',
true
);

const inputCopy = cloneDeep(input);

validateConvertFilterToKueryNode(['foo'], input, mockMappings);

expect(input).toEqual(inputCopy);
});

test('Validate a simple KQL expression filter', () => {
expect(
validateConvertFilterToKueryNode(['foo'], 'foo.attributes.title: "best"', mockMappings)
Expand Down
5 changes: 3 additions & 2 deletions src/core/server/saved_objects/service/lib/filter_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*/

import { set } from '@elastic/safer-lodash-set';
import { get } from 'lodash';
import { get, cloneDeep } from 'lodash';
import { SavedObjectsErrorHelpers } from './errors';
import { IndexMapping } from '../../mappings';
// @ts-expect-error no ts
import { esKuery } from '../../es_query';

type KueryNode = any;

const astFunctionType = ['is', 'range', 'nested'];
Expand All @@ -23,7 +24,7 @@ export const validateConvertFilterToKueryNode = (
): KueryNode | undefined => {
if (filter && indexMapping) {
const filterKueryNode =
typeof filter === 'string' ? esKuery.fromKueryExpression(filter) : filter;
typeof filter === 'string' ? esKuery.fromKueryExpression(filter) : cloneDeep(filter);

const validationFilterKuery = validateFilterKueryNode({
astFilter: filterKueryNode,
Expand Down

0 comments on commit 0e7d0e9

Please sign in to comment.