-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Adapt special character escape according to kuery…
… grammer (#198288) ## Summary As mentioned in https://github.com/elastic/kibana/security/code-scanning/349, this PR resolves the escaping issue. Additionally, it also adds more candiadates for escaping as mentioned in `kuery` grammar as shown below . https://github.com/elastic/kibana/blob/d6b4fe9e6ee2e18691b0fa818c4e2c4ed3c413fc/packages/kbn-es-query/grammar/grammar.peggy#L295-L298 ## Solution This PR replicates #128289 in `7.17` where `escape_kquery` was moved from `data` plugin to `es-query` package. This should have been backported to `7.17` but was not.
- Loading branch information
Showing
9 changed files
with
259 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/kbn-es-query/src/kuery/utils/escape_kuery.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { escapeQuotes, escapeKuery } from './escape_kuery'; | ||
|
||
describe('Kuery escape', () => { | ||
test('should escape quotes', () => { | ||
const value = 'I said, "Hello."'; | ||
const expected = 'I said, \\"Hello.\\"'; | ||
|
||
expect(escapeQuotes(value)).toBe(expected); | ||
}); | ||
|
||
test('should escape backslashes and quotes', () => { | ||
const value = 'Backslashes \\" in the middle and ends with quotes \\"'; | ||
const expected = 'Backslashes \\\\\\" in the middle and ends with quotes \\\\\\"'; | ||
|
||
expect(escapeQuotes(value)).toBe(expected); | ||
}); | ||
|
||
test('should escape special characters', () => { | ||
const value = `This \\ has (a lot of) <special> characters, don't you *think*? "Yes."`; | ||
const expected = `This \\\\ has \\(a lot of\\) \\<special\\> characters, don't you \\*think\\*? \\"Yes.\\"`; | ||
|
||
expect(escapeKuery(value)).toBe(expected); | ||
}); | ||
|
||
test('should escape keywords', () => { | ||
const value = 'foo and bar or baz not qux'; | ||
const expected = 'foo \\and bar \\or baz \\not qux'; | ||
|
||
expect(escapeKuery(value)).toBe(expected); | ||
}); | ||
|
||
test('should escape keywords next to each other', () => { | ||
const value = 'foo and bar or not baz'; | ||
const expected = 'foo \\and bar \\or \\not baz'; | ||
|
||
expect(escapeKuery(value)).toBe(expected); | ||
}); | ||
|
||
test('should not escape keywords without surrounding spaces', () => { | ||
const value = 'And this has keywords, or does it not?'; | ||
const expected = 'And this has keywords, \\or does it not?'; | ||
|
||
expect(escapeKuery(value)).toBe(expected); | ||
}); | ||
|
||
test('should escape uppercase keywords', () => { | ||
const value = 'foo AND bar'; | ||
const expected = 'foo \\AND bar'; | ||
|
||
expect(escapeKuery(value)).toBe(expected); | ||
}); | ||
|
||
test('should escape both keywords and special characters', () => { | ||
const value = 'Hello, world, and <nice> to meet you!'; | ||
const expected = 'Hello, world, \\and \\<nice\\> to meet you!'; | ||
|
||
expect(escapeKuery(value)).toBe(expected); | ||
}); | ||
|
||
test('should escape newlines and tabs', () => { | ||
const value = 'This\nhas\tnewlines\r\nwith\ttabs'; | ||
const expected = 'This\\nhas\\tnewlines\\r\\nwith\\ttabs'; | ||
|
||
expect(escapeKuery(value)).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { flow } from 'lodash'; | ||
|
||
/** | ||
* Escapes backslashes and double-quotes. (Useful when putting a string in quotes to use as a value | ||
* in a KQL expression. See the QuotedCharacter rule in kuery.peg.) | ||
*/ | ||
export function escapeQuotes(str: string) { | ||
return str.replace(/[\\"]/g, '\\$&'); | ||
} | ||
|
||
/** | ||
* Escapes a Kuery node value to ensure that special characters, operators, and whitespace do not result in a parsing error or unintended | ||
* behavior when using the value as an argument for the `buildNode` function. | ||
*/ | ||
export const escapeKuery = flow(escapeSpecialCharacters, escapeAndOr, escapeNot, escapeWhitespace); | ||
|
||
// See the SpecialCharacter rule in kuery.peg | ||
function escapeSpecialCharacters(str: string) { | ||
return str.replace(/[\\():<>"*]/g, '\\$&'); // $& means the whole matched string | ||
} | ||
|
||
// See the Keyword rule in kuery.peg | ||
function escapeAndOr(str: string) { | ||
return str.replace(/(\s+)(and|or)(\s+)/gi, '$1\\$2$3'); | ||
} | ||
|
||
function escapeNot(str: string) { | ||
return str.replace(/not(\s+)/gi, '\\$&'); | ||
} | ||
|
||
// See the Space rule in kuery.peg | ||
function escapeWhitespace(str: string) { | ||
return str.replace(/\t/g, '\\t').replace(/\r/g, '\\r').replace(/\n/g, '\\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.