influxdb: influxql query editor: fix escaping problematic characters (#39170)

This commit is contained in:
Gábor Farkas 2021-09-14 12:55:43 +02:00 committed by GitHub
parent ad971cc9be
commit 27e3fda7ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -142,6 +142,7 @@ export default class InfluxQueryModel {
}
private renderTagCondition(tag: InfluxQueryTag, index: number, interpolate?: boolean) {
// FIXME: merge this function with query_builder/renderTagCondition
let str = '';
let operator = tag.operator;
let value = tag.value;

View File

@ -2,6 +2,7 @@ import { reduce } from 'lodash';
import kbn from 'app/core/utils/kbn';
function renderTagCondition(tag: { operator: any; value: string; condition: any; key: string }, index: number) {
// FIXME: merge this function with influx_query_model/renderTagCondition
let str = '';
let operator = tag.operator;
let value = tag.value;
@ -19,7 +20,7 @@ function renderTagCondition(tag: { operator: any; value: string; condition: any;
// quote value unless regex or number, or if empty-string
if (value === '' || (operator !== '=~' && operator !== '!~' && isNaN(+value))) {
value = "'" + value + "'";
value = "'" + value.replace(/\\/g, '\\\\').replace(/\'/g, "\\'") + "'";
}
return str + '"' + tag.key + '" ' + operator + ' ' + value;

View File

@ -178,6 +178,24 @@ describe('InfluxQueryBuilder', () => {
expect(query).toBe(`SHOW TAG KEYS WHERE "app" == 42`);
});
it('should handle tag-value-contains-backslash-character getting tag-keys', () => {
const builder = new InfluxQueryBuilder(
{ measurement: undefined, tags: [{ key: 'app', value: 'lab\\el', operator: '==' }] },
undefined
);
const query = builder.buildExploreQuery('TAG_KEYS');
expect(query).toBe(`SHOW TAG KEYS WHERE "app" == 'lab\\\\el'`);
});
it('should handle tag-value-contains-single-quote-character getting tag-keys', () => {
const builder = new InfluxQueryBuilder(
{ measurement: undefined, tags: [{ key: 'app', value: "lab'el", operator: '==' }] },
undefined
);
const query = builder.buildExploreQuery('TAG_KEYS');
expect(query).toBe(`SHOW TAG KEYS WHERE "app" == 'lab\\'el'`);
});
it('should handle tag-value=emptry-string when getting measurements', () => {
const builder = new InfluxQueryBuilder(
{ measurement: undefined, tags: [{ key: 'app', value: '', operator: '==' }] },