Skip to content

Agent Mesh

fix(api): sort label keys in ValidateLabels for deterministic errors

HosniBelfekie5a833835aec
diff --git a/api/labels.go b/api/labels.go
index 278c0098..fd3a21b0 100644
--- a/api/labels.go
+++ b/api/labels.go
@@ -17,6 +17,7 @@ package api
import (
"fmt"
"regexp"
+ "sort"
"strings"
)
@@ -63,13 +64,23 @@ func ValidateLabelValue(value string) error {
return nil
}
-// ValidateLabels checks every key and value in a label set.
+// ValidateLabels checks every key and value in a label set. Keys are visited
+// in lexicographic order, so the error returned for a given input is
+// deterministic and stable across runs (Go's map iteration is randomized).
func ValidateLabels(labels map[string]string) error {
- for k, v := range labels {
+ if len(labels) == 0 {
+ return nil
+ }
+ keys := make([]string, 0, len(labels))
+ for k := range labels {
+ keys = append(keys, k)
+ }
+ sort.Strings(keys)
+ for _, k := range keys {
if err := ValidateLabelKey(k); err != nil {
return err
}
- if err := ValidateLabelValue(v); err != nil {
+ if err := ValidateLabelValue(labels[k]); err != nil {
return err
}
}
diff --git a/api/labels_test.go b/api/labels_test.go
index 9f2e8027..4ad2d80a 100644
--- a/api/labels_test.go
+++ b/api/labels_test.go
@@ -65,3 +65,45 @@ func TestValidateLabels(t *testing.T) {
t.Error("ValidateLabels(bad key): expected error, got nil")
}
}
+
+// TestValidateLabels_ReportsFirstErrorDeterministically guards against
+// non-deterministic map iteration: the first reported error must always come
+// from the lexicographically smallest invalid key, regardless of iteration
+// order. Regression test for the prior bug where ValidateLabels iterated
+// the input map directly and returned whatever bad key Go visited first.
+func TestValidateLabels_ReportsFirstErrorDeterministically(t *testing.T) {
+ bad := map[string]string{
+ "region": "v",
+ "team": "v",
+ "zulu": "v",
+ "b_ bad": "v",
+ "c,key": "v",
+ "a!key": "v",
+ }
+ // Repeat the call many times. With deterministic ordering the reported
+ // key is always the same (the lexicographically smallest invalid one,
+ // here "a!key"; whitespace, comma, and bang are all disallowed by
+ // labelKeySyntax, but "a!key" < "b_ bad" < "c,key" lexicographically).
+ // With the previous nondeterministic ordering the reported key varied
+ // between runs and could be any of "a!key", "b_ bad", or "c,key".
+ const iterations = 200
+ for i := 0; i < iterations; i++ {
+ err := ValidateLabels(bad)
+ if err == nil {
+ t.Fatalf("iteration %d: expected error, got nil", i)
+ }
+ if !strings.Contains(err.Error(), `"a!key"`) {
+ t.Fatalf("iteration %d: expected error to mention the lexicographically smallest invalid key \"a!key\", got: %v", i, err)
+ }
+ }
+}
+
+// TestValidateLabels_AllKeysValidStaysSorted asserts the happy path still
+// accepts sets whose keys happen to be in any order — sorting must not
+// introduce a false positive on otherwise valid input.
+func TestValidateLabels_AllKeysValidStaysSorted(t *testing.T) {
+ labels := map[string]string{"zeta": "1", "alpha": "2", "mike": "3"}
+ if err := ValidateLabels(labels); err != nil {
+ t.Errorf("ValidateLabels(sorted happy path): unexpected error: %v", err)
+ }
+}