Skip to content

Commit 3f30b33

Browse files
Update ValidateMatcher in silence.go
Signed-off-by: George Robinson <[email protected]>
1 parent 16fb81c commit 3f30b33

File tree

3 files changed

+146
-3
lines changed

3 files changed

+146
-3
lines changed

cmd/alertmanager/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ func run() int {
180180
level.Error(logger).Log("msg", "error parsing the feature flag list", "err", err)
181181
return 1
182182
}
183-
184183
compat.InitFromFlags(logger, featureConfig)
184+
silence.InitFromFlags(logger, featureConfig)
185185

186186
err = os.MkdirAll(*dataDir, 0o777)
187187
if err != nil {

silence/silence.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package silence
1818
import (
1919
"bytes"
2020
"fmt"
21+
"github.com/prometheus/alertmanager/featurecontrol"
2122
"io"
2223
"math/rand"
2324
"os"
@@ -26,6 +27,7 @@ import (
2627
"sort"
2728
"sync"
2829
"time"
30+
"unicode/utf8"
2931

3032
"github.com/benbjohnson/clock"
3133
"github.com/go-kit/log"
@@ -469,6 +471,20 @@ func (s *Silences) GC() (int, error) {
469471

470472
// ValidateMatcher runs validation on the matcher name, type, and pattern.
471473
var ValidateMatcher = func(m *pb.Matcher) error {
474+
return validateClassicMatcher(m)
475+
}
476+
477+
// InitFromFlags initializes the validation function from the flagger.
478+
func InitFromFlags(l log.Logger, f featurecontrol.Flagger) {
479+
if !f.ClassicMatchersParsing() {
480+
ValidateMatcher = func(m *pb.Matcher) error {
481+
return validateUTF8Matcher(m)
482+
}
483+
}
484+
}
485+
486+
// validateClassicMatcher validates the matcher against the classic rules.
487+
func validateClassicMatcher(m *pb.Matcher) error {
472488
if !model.LabelName(m.Name).IsValid() {
473489
return fmt.Errorf("invalid label name %q", m.Name)
474490
}
@@ -487,6 +503,29 @@ var ValidateMatcher = func(m *pb.Matcher) error {
487503
return nil
488504
}
489505

506+
// validateUTF8Matcher validates the matcher against the UTF-8 rules.
507+
func validateUTF8Matcher(m *pb.Matcher) error {
508+
if !utf8.ValidString(m.Name) {
509+
return fmt.Errorf("invalid label name %q", m.Name)
510+
}
511+
switch m.Type {
512+
case pb.Matcher_EQUAL, pb.Matcher_NOT_EQUAL:
513+
if !utf8.ValidString(m.Pattern) {
514+
return fmt.Errorf("invalid label value %q", m.Pattern)
515+
}
516+
case pb.Matcher_REGEXP, pb.Matcher_NOT_REGEXP:
517+
if !utf8.ValidString(m.Pattern) {
518+
return fmt.Errorf("invalid regular expression %q", m.Pattern)
519+
}
520+
if _, err := regexp.Compile(m.Pattern); err != nil {
521+
return fmt.Errorf("invalid regular expression %q: %s", m.Pattern, err)
522+
}
523+
default:
524+
return fmt.Errorf("unknown matcher type %q", m.Type)
525+
}
526+
return nil
527+
}
528+
490529
func matchesEmpty(m *pb.Matcher) bool {
491530
switch m.Type {
492531
case pb.Matcher_EQUAL:

silence/silence_test.go

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ func TestSilencer(t *testing.T) {
11141114
require.True(t, s.Mutes(model.LabelSet{"foo": "bar"}), "expected alert silenced by activated second silence")
11151115
}
11161116

1117-
func TestValidateMatcher(t *testing.T) {
1117+
func TestValidateClassicMatcher(t *testing.T) {
11181118
cases := []struct {
11191119
m *pb.Matcher
11201120
err string
@@ -1154,6 +1154,103 @@ func TestValidateMatcher(t *testing.T) {
11541154
Type: pb.Matcher_EQUAL,
11551155
},
11561156
err: "invalid label name",
1157+
}, {
1158+
m: &pb.Matcher{
1159+
Name: "\xf0\x9f\x99\x82", // U+1F642
1160+
Pattern: "a",
1161+
Type: pb.Matcher_EQUAL,
1162+
},
1163+
err: "invalid label name",
1164+
}, {
1165+
m: &pb.Matcher{
1166+
Name: "a",
1167+
Pattern: "((",
1168+
Type: pb.Matcher_REGEXP,
1169+
},
1170+
err: "invalid regular expression",
1171+
}, {
1172+
m: &pb.Matcher{
1173+
Name: "a",
1174+
Pattern: "))",
1175+
Type: pb.Matcher_NOT_REGEXP,
1176+
},
1177+
err: "invalid regular expression",
1178+
}, {
1179+
m: &pb.Matcher{
1180+
Name: "a",
1181+
Pattern: "\xff",
1182+
Type: pb.Matcher_EQUAL,
1183+
},
1184+
err: "invalid label value",
1185+
}, {
1186+
m: &pb.Matcher{
1187+
Name: "a",
1188+
Pattern: "\xf0\x9f\x99\x82", // U+1F642
1189+
Type: pb.Matcher_EQUAL,
1190+
},
1191+
err: "",
1192+
}, {
1193+
m: &pb.Matcher{
1194+
Name: "a",
1195+
Pattern: "b",
1196+
Type: 333,
1197+
},
1198+
err: "unknown matcher type",
1199+
},
1200+
}
1201+
1202+
for _, c := range cases {
1203+
checkErr(t, c.err, validateClassicMatcher(c.m))
1204+
}
1205+
}
1206+
1207+
func TestValidateUTF8Matcher(t *testing.T) {
1208+
cases := []struct {
1209+
m *pb.Matcher
1210+
err string
1211+
}{
1212+
{
1213+
m: &pb.Matcher{
1214+
Name: "a",
1215+
Pattern: "b",
1216+
Type: pb.Matcher_EQUAL,
1217+
},
1218+
err: "",
1219+
}, {
1220+
m: &pb.Matcher{
1221+
Name: "a",
1222+
Pattern: "b",
1223+
Type: pb.Matcher_NOT_EQUAL,
1224+
},
1225+
err: "",
1226+
}, {
1227+
m: &pb.Matcher{
1228+
Name: "a",
1229+
Pattern: "b",
1230+
Type: pb.Matcher_REGEXP,
1231+
},
1232+
err: "",
1233+
}, {
1234+
m: &pb.Matcher{
1235+
Name: "a",
1236+
Pattern: "b",
1237+
Type: pb.Matcher_NOT_REGEXP,
1238+
},
1239+
err: "",
1240+
}, {
1241+
m: &pb.Matcher{
1242+
Name: "00",
1243+
Pattern: "a",
1244+
Type: pb.Matcher_EQUAL,
1245+
},
1246+
err: "",
1247+
}, {
1248+
m: &pb.Matcher{
1249+
Name: "\xf0\x9f\x99\x82", // U+1F642
1250+
Pattern: "a",
1251+
Type: pb.Matcher_EQUAL,
1252+
},
1253+
err: "",
11571254
}, {
11581255
m: &pb.Matcher{
11591256
Name: "a",
@@ -1175,6 +1272,13 @@ func TestValidateMatcher(t *testing.T) {
11751272
Type: pb.Matcher_EQUAL,
11761273
},
11771274
err: "invalid label value",
1275+
}, {
1276+
m: &pb.Matcher{
1277+
Name: "a",
1278+
Pattern: "\xf0\x9f\x99\x82", // U+1F642
1279+
Type: pb.Matcher_EQUAL,
1280+
},
1281+
err: "",
11781282
}, {
11791283
m: &pb.Matcher{
11801284
Name: "a",
@@ -1186,7 +1290,7 @@ func TestValidateMatcher(t *testing.T) {
11861290
}
11871291

11881292
for _, c := range cases {
1189-
checkErr(t, c.err, ValidateMatcher(c.m))
1293+
checkErr(t, c.err, validateUTF8Matcher(c.m))
11901294
}
11911295
}
11921296

0 commit comments

Comments
 (0)