diff --git a/src/core/server/saved_objects/service/lib/filter_utils.test.ts b/src/core/server/saved_objects/service/lib/filter_utils.test.ts index 956a60b23809..2ef5219ccfff 100644 --- a/src/core/server/saved_objects/service/lib/filter_utils.test.ts +++ b/src/core/server/saved_objects/service/lib/filter_utils.test.ts @@ -6,6 +6,7 @@ * Side Public License, v 1. */ +import { cloneDeep } from 'lodash'; // @ts-expect-error no ts import { esKuery } from '../../es_query'; @@ -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) diff --git a/src/core/server/saved_objects/service/lib/filter_utils.ts b/src/core/server/saved_objects/service/lib/filter_utils.ts index b3bcef9a62e1..a41a25a27b70 100644 --- a/src/core/server/saved_objects/service/lib/filter_utils.ts +++ b/src/core/server/saved_objects/service/lib/filter_utils.ts @@ -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']; @@ -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,