Commit bf835af7 by Ivana Huckova Committed by GitHub

Prometheus: Add backslash escaping for template variables (#26205)

* Run query on splitOpen action

* Escape \ in prometheusRegularEscape

* Revert "Run query on splitOpen action"

This reverts commit 3559b6c5739f351f4e797c57069bc937bd06c065.

Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
parent bf5d52c5
......@@ -415,9 +415,17 @@ describe('PrometheusDatasource', () => {
expect(prometheusRegularEscape("looking'glass")).toEqual("looking\\\\'glass");
});
it('should escape \\', () => {
expect(prometheusRegularEscape('looking\\glass')).toEqual('looking\\\\glass');
});
it('should escape multiple characters', () => {
expect(prometheusRegularEscape("'looking'glass'")).toEqual("\\\\'looking\\\\'glass\\\\'");
});
it('should escape multiple different characters', () => {
expect(prometheusRegularEscape("'loo\\king'glass'")).toEqual("\\\\'loo\\\\king\\\\'glass\\\\'");
});
});
describe('Prometheus regexes escaping', () => {
......
......@@ -764,11 +764,9 @@ export function extractRuleMappingFromGroups(groups: any[]) {
}
export function prometheusRegularEscape(value: any) {
return typeof value === 'string' ? value.replace(/'/g, "\\\\'") : value;
return typeof value === 'string' ? value.replace(/\\/g, '\\\\').replace(/'/g, "\\\\'") : value;
}
export function prometheusSpecialRegexEscape(value: any) {
return typeof value === 'string'
? prometheusRegularEscape(value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]+?.()|]/g, '\\\\$&'))
: value;
return typeof value === 'string' ? value.replace(/\\/g, '\\\\\\\\').replace(/[$^*{}\[\]\'+?.()|]/g, '\\\\$&') : value;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment