From 6884fd1275e78df79462d419b76469985f85880d Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 9 Jan 2024 12:38:30 -0500 Subject: [PATCH] [8.12] Fix issue with KQL wildcard queries not properly escaping backslashes (#174464) (#174533) # Backport This will backport the following commits from `main` to `8.12`: - [Fix issue with KQL wildcard queries not properly escaping backslashes (#174464)](https://github.com/elastic/kibana/pull/174464) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Lukas Olson --- .../src/kuery/functions/is.test.ts | 23 +++++++++++++++++++ .../kbn-es-query/src/kuery/functions/is.ts | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/kbn-es-query/src/kuery/functions/is.test.ts b/packages/kbn-es-query/src/kuery/functions/is.test.ts index a19c951c184a9..5b52eff548ed3 100644 --- a/packages/kbn-es-query/src/kuery/functions/is.test.ts +++ b/packages/kbn-es-query/src/kuery/functions/is.test.ts @@ -253,6 +253,29 @@ describe('kuery functions', () => { expect(result).toEqual(expected); }); + test('should create a wildcard query with backslashes properly escaped', () => { + const expected = { + bool: { + should: [ + { + wildcard: { + 'machine.os.keyword': { value: '*\\\\*' }, + }, + }, + ], + minimum_should_match: 1, + }, + }; + const node = nodeTypes.function.buildNode( + 'is', + 'machine.os.keyword', + '*\\\\*' + ) as KqlIsFunctionNode; + const result = is.toElasticsearchQuery(node, indexPattern); + + expect(result).toEqual(expected); + }); + test('should support scripted fields', () => { const node = nodeTypes.function.buildNode( 'is', diff --git a/packages/kbn-es-query/src/kuery/functions/is.ts b/packages/kbn-es-query/src/kuery/functions/is.ts index 55bce74973741..46a6acfc56ff9 100644 --- a/packages/kbn-es-query/src/kuery/functions/is.ts +++ b/packages/kbn-es-query/src/kuery/functions/is.ts @@ -159,7 +159,7 @@ export function toElasticsearchQuery( ? { wildcard: { [field.name]: { - value, + value: wildcard.toQueryStringQuery(valueArg), ...(typeof config.caseInsensitive === 'boolean' && { case_insensitive: config.caseInsensitive, }),