Skip to content

Commit

Permalink
[8.12] Fix issue with KQL wildcard queries not properly escaping back…
Browse files Browse the repository at this point in the history
…slashes (#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)](#174464)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Lukas
Olson","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-01-09T16:24:12Z","message":"Fix
issue with KQL wildcard queries not properly escaping backslashes
(#174464)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/169709.\r\n\r\nPrior to this
PR, KQL queries against keyword fields would not properly\r\nhandle
escaped special characters (such as backslash). This PR fixes
the\r\nbehavior to properly re-escape special characters before sending
them\r\nalong to the wildcard query.\r\n\r\n### Checklist\r\n\r\n- [x]
[Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### Release
note\r\n\r\nKQL queries against wildcards now properly handle escaped
special\r\ncharacters without requiring
double-escaping.\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"46fbfd3b13c80d49e51b6b1541fecebb7c628e02","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Feature:KQL","Team:DataDiscovery","v8.12.1","v8.13.0"],"title":"Fix
issue with KQL wildcard queries not properly escaping
backslashes","number":174464,"url":"https://github.com/elastic/kibana/pull/174464","mergeCommit":{"message":"Fix
issue with KQL wildcard queries not properly escaping backslashes
(#174464)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/169709.\r\n\r\nPrior to this
PR, KQL queries against keyword fields would not properly\r\nhandle
escaped special characters (such as backslash). This PR fixes
the\r\nbehavior to properly re-escape special characters before sending
them\r\nalong to the wildcard query.\r\n\r\n### Checklist\r\n\r\n- [x]
[Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### Release
note\r\n\r\nKQL queries against wildcards now properly handle escaped
special\r\ncharacters without requiring
double-escaping.\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"46fbfd3b13c80d49e51b6b1541fecebb7c628e02"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","branchLabelMappingKey":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/174464","number":174464,"mergeCommit":{"message":"Fix
issue with KQL wildcard queries not properly escaping backslashes
(#174464)\n\n## Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/169709.\r\n\r\nPrior to this
PR, KQL queries against keyword fields would not properly\r\nhandle
escaped special characters (such as backslash). This PR fixes
the\r\nbehavior to properly re-escape special characters before sending
them\r\nalong to the wildcard query.\r\n\r\n### Checklist\r\n\r\n- [x]
[Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### Release
note\r\n\r\nKQL queries against wildcards now properly handle escaped
special\r\ncharacters without requiring
double-escaping.\r\n\r\nCo-authored-by: Kibana Machine
<[email protected]>","sha":"46fbfd3b13c80d49e51b6b1541fecebb7c628e02"}}]}]
BACKPORT-->

Co-authored-by: Lukas Olson <[email protected]>
  • Loading branch information
kibanamachine and lukasolson authored Jan 9, 2024
1 parent 9fbf28e commit 6884fd1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/kbn-es-query/src/kuery/functions/is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-es-query/src/kuery/functions/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export function toElasticsearchQuery(
? {
wildcard: {
[field.name]: {
value,
value: wildcard.toQueryStringQuery(valueArg),
...(typeof config.caseInsensitive === 'boolean' && {
case_insensitive: config.caseInsensitive,
}),
Expand Down

0 comments on commit 6884fd1

Please sign in to comment.