Loki: Fix parsing of escaped quotes in LogQL (#69584)

* fix parsing issue

* replace escaped quotes
pull/69621/head
Sven Grossmann 2 years ago committed by GitHub
parent 807c7d5b01
commit a81cee1d05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      public/app/plugins/datasource/loki/querybuilder/parsing.test.ts
  2. 5
      public/app/plugins/datasource/loki/querybuilder/parsing.ts

@ -76,6 +76,36 @@ describe('buildVisualQueryFromString', () => {
);
});
it('parses query with line filter and escaped quote', () => {
expect(buildVisualQueryFromString('{app="frontend"} |= "\\"line"')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.LineContains, params: ['"line'] }],
})
);
});
it('parses query with label filter and escaped quote', () => {
expect(buildVisualQueryFromString('{app="frontend"} | bar="\\"baz"')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.LabelFilter, params: ['bar', '=', '"baz'] }],
})
);
});
it('returns error for query with ip matching line filter', () => {
const context = buildVisualQueryFromString('{app="frontend"} |= ip("192.168.4.5/16") | logfmt');
expect(context).toEqual(
@ -320,7 +350,7 @@ describe('buildVisualQueryFromString', () => {
});
it('parses query with with decolorize and other operations', () => {
expect(buildVisualQueryFromString('{app="frontend"} | logfmt | decolorize | __error__="')).toEqual(
expect(buildVisualQueryFromString('{app="frontend"} | logfmt | decolorize | __error__=""')).toEqual(
noErrors({
labels: [
{

@ -599,7 +599,10 @@ function isIntervalVariableError(node: SyntaxNode) {
function handleQuotes(string: string) {
if (string[0] === `"` && string[string.length - 1] === `"`) {
return string.replace(/"/g, '').replace(/\\\\/g, '\\');
return string
.substring(1, string.length - 1)
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\');
}
return string.replace(/`/g, '');
}

Loading…
Cancel
Save