Skip to content

Commit

Permalink
Add validation for feature label values
Browse files Browse the repository at this point in the history
Prevents NFD errors e.g. in the case custom hooks happen to output
invalid label values.
  • Loading branch information
marquiz committed Feb 20, 2019
1 parent ab5b286 commit 7009fa2
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
var (
version = "" // Must not be const, set using ldflags at build time
validFeatureNameRe = regexp.MustCompile(`^([-.\w]*)?[A-Za-z0-9]$`)
validFeatureValRe = regexp.MustCompile(`^(([A-Za-z0-9][-.\w]*)?[A-Za-z0-9])?$`)
)

// package loggers
Expand Down Expand Up @@ -359,20 +360,28 @@ func getFeatureLabels(source source.FeatureSource) (labels Labels, err error) {
if err != nil {
return nil, err
}
for k := range features {
// Validate label
for k, v := range features {
// Validate label name
if !validFeatureNameRe.MatchString(k) {
stderrLogger.Printf("Invalid feature name '%s', ignoring...", k)
continue
}

prefix := source.Name() + "-"
switch source.(type) {
case local.Source:
// Do not prefix labels from the hooks
prefix = ""
}
labels[fmt.Sprintf("%s%s", prefix, k)] = fmt.Sprintf("%v", features[k])
label := prefix + k

// Validate label value
value := fmt.Sprintf("%v", v)
if !validFeatureValRe.MatchString(value) {
stderrLogger.Printf("Invalid value '%s' for label '%s', ignoring...", value, label)
continue
}

labels[label] = value
}
return labels, nil
}
Expand Down

0 comments on commit 7009fa2

Please sign in to comment.