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

* fix parsing issue

* replace escaped quotes
This commit is contained in:
Sven Grossmann 2023-06-06 11:07:21 +02:00 committed by GitHub
parent 807c7d5b01
commit a81cee1d05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -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: [
{

View File

@ -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, '');
}