Skip to content

Commit

Permalink
[v10.0.x] Loki: Fix parsing of escaped quotes in LogQL (#69584) (#69615)
Browse files Browse the repository at this point in the history
Loki: Fix parsing of escaped quotes in LogQL (#69584)

* fix parsing issue

* replace escaped quotes

(cherry picked from commit a81cee1)
  • Loading branch information
svennergr committed Jun 6, 2023
1 parent 1d5f90b commit 04330a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
30 changes: 30 additions & 0 deletions public/app/plugins/datasource/loki/querybuilder/parsing.test.ts
Expand Up @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion public/app/plugins/datasource/loki/querybuilder/parsing.ts
Expand Up @@ -576,7 +576,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, '');
}
Expand Down

0 comments on commit 04330a0

Please sign in to comment.