Alerting: do not unescape external AM label values (#45334)

This commit is contained in:
Gilles De Mey 2022-02-15 13:57:00 +01:00 committed by GitHub
parent e1ff4dc9fe
commit 651bb773db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 14 deletions

View File

@ -31,16 +31,23 @@ describe('Alertmanager utils', () => {
});
});
it('should parse escaped values correctly', () => {
expect(parseMatcher('foo=~"bar\\"baz\\""')).toEqual<Matcher>({
// Alertmanager has some strict requirements for label values;
// we should not automatically encode or decode any values sent
// and instead let AM return any errors like (matcher value contains unescaped double quote: bar"baz")
// and allow the user to update the values to the correct format
//
// see https://github.com/prometheus/alertmanager/blob/4030e3670b359b8814aa8340ea1144f32b1f5ab3/pkg/labels/parse.go#L55-L99
// and https://github.com/prometheus/alertmanager/blob/4030e3670b359b8814aa8340ea1144f32b1f5ab3/pkg/labels/parse.go#L101-L178
it('should not parse escaped values', () => {
expect(parseMatcher('foo="^[a-z0-9-]{1}[a-z0-9-]{0,30}$"')).toEqual<Matcher>({
name: 'foo',
value: 'bar"baz"',
isRegex: true,
value: '"^[a-z0-9-]{1}[a-z0-9-]{0,30}$"',
isRegex: false,
isEqual: true,
});
expect(parseMatcher('foo=~bar\\"baz\\"')).toEqual<Matcher>({
name: 'foo',
value: 'bar"baz"',
value: 'bar\\"baz\\"',
isRegex: true,
isEqual: true,
});

View File

@ -118,14 +118,6 @@ const matcherOperators = [
MatcherOperator.equal,
];
function unescapeMatcherValue(value: string) {
let trimmed = value.trim().replace(/\\"/g, '"');
if (trimmed.startsWith('"') && trimmed.endsWith('"') && !trimmed.endsWith('\\"')) {
trimmed = trimmed.substr(1, trimmed.length - 2);
}
return trimmed.replace(/\\"/g, '"');
}
export function parseMatcher(matcher: string): Matcher {
const trimmed = matcher.trim();
if (trimmed.startsWith('{') && trimmed.endsWith('}')) {
@ -141,7 +133,7 @@ export function parseMatcher(matcher: string): Matcher {
}
const [operator, idx] = operatorsFound[0];
const name = trimmed.substr(0, idx).trim();
const value = unescapeMatcherValue(trimmed.substr(idx + operator.length).trim());
const value = trimmed.substr(idx + operator.length).trim();
if (!name) {
throw new Error(`Invalid matcher: ${trimmed}`);
}