Merge pull request #3671 from grobinson-grafana/grobinson/fix-missing-check-valid-labels

Fix missing check for len(name) == 0
This commit is contained in:
Simon Pasquier 2024-01-16 10:05:02 +01:00 committed by GitHub
commit 30fa9cd44b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -244,6 +244,9 @@ func isValidClassicLabelName(_ log.Logger) func(model.LabelName) bool {
// isValidUTF8LabelName returns true if the string is a valid UTF-8 label name.
func isValidUTF8LabelName(_ log.Logger) func(model.LabelName) bool {
return func(name model.LabelName) bool {
if len(name) == 0 {
return false
}
return utf8.ValidString(string(name))
}
}

View File

@ -172,19 +172,23 @@ func TestIsValidClassicLabelName(t *testing.T) {
input model.LabelName
expected bool
}{{
name: "is accepted",
name: "foo is accepted",
input: "foo",
expected: true,
}, {
name: "is also accepted",
name: "starts with underscore and ends with number is accepted",
input: "_foo1",
expected: true,
}, {
name: "is not accepted",
name: "empty is not accepted",
input: "",
expected: false,
}, {
name: "starts with number is not accepted",
input: "0foo",
expected: false,
}, {
name: "is also not accepted",
name: "contains emoji is not accepted",
input: "foo🙂",
expected: false,
}}
@ -203,21 +207,25 @@ func TestIsValidUTF8LabelName(t *testing.T) {
input model.LabelName
expected bool
}{{
name: "is accepted",
name: "foo is accepted",
input: "foo",
expected: true,
}, {
name: "is also accepted",
name: "starts with underscore and ends with number is accepted",
input: "_foo1",
expected: true,
}, {
name: "is accepted in UTF-8",
name: "starts with number is accepted",
input: "0foo",
expected: true,
}, {
name: "is also accepted with UTF-8",
name: "contains emoji is accepted",
input: "foo🙂",
expected: true,
}, {
name: "empty is not accepted",
input: "",
expected: false,
}}
for _, test := range tests {