Elasticsearch: Stop escaping backslash in regex adhoc filter (#94577)

pull/95735/head
Isabella Siu 8 months ago committed by GitHub
parent 0eb7b755e2
commit 5f4944117c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      devenv/docker/blocks/elastic/data/data.js
  2. 6
      public/app/plugins/datasource/elasticsearch/datasource.test.ts
  3. 11
      public/app/plugins/datasource/elasticsearch/modifyQuery.ts

@ -150,7 +150,8 @@ function getRandomLogItem(counter, timestamp) {
value: counter,
metric: chooseRandomElement(['cpu', 'memory', 'latency']),
description: "this is description",
slash: "Access to the path '\\\\tkasnpo\\KASNPO\\Files\\contacts.xml' is denied."
slash: "Access to the path '\\\\tkasnpo\\KASNPO\\Files\\contacts.xml' is denied.",
url: "/foo/blah"
};
}

@ -721,6 +721,12 @@ describe('ElasticDatasource', () => {
const query = ds.addAdHocFilters('', filters);
expect(query).toBe('field\\:name:"field \\"value\\""');
});
it('should not escape backslash in regex', () => {
const filters = [{ key: 'field:name', operator: '=~', value: 'field value\\/', condition: '' }];
const query = ds.addAdHocFilters('', filters);
expect(query).toBe('field\\:name:/field value\\//');
});
});
});

@ -104,13 +104,14 @@ export function addAddHocFilter(query: string, filter: AdHocVariableFilter): str
*/
const key = escapeFilter(filter.key);
const value = escapeFilterValue(filter.value);
const regexValue = escapeFilterValue(filter.value, false);
let addHocFilter = '';
switch (filter.operator) {
case '=~':
addHocFilter = `${key}:/${value}/`;
addHocFilter = `${key}:/${regexValue}/`;
break;
case '!~':
addHocFilter = `-${key}:/${value}/`;
addHocFilter = `-${key}:/${regexValue}/`;
break;
case '>':
addHocFilter = `${key}:>${value}`;
@ -186,8 +187,10 @@ export function escapeFilter(value: string) {
* Values can possibly reserved special characters such as quotes.
* Use this function to escape filter values.
*/
export function escapeFilterValue(value: string) {
value = value.replace(/\\/g, '\\\\');
export function escapeFilterValue(value: string, escapeBackslash = true) {
if (escapeBackslash) {
value = value.replace(/\\/g, '\\\\');
}
return lucene.phrase.escape(value);
}

Loading…
Cancel
Save