Skip to content

Commit

Permalink
hardening
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolaydubina committed Dec 17, 2024
1 parent 6571da5 commit 839e47a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
27 changes: 17 additions & 10 deletions metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,32 @@ func ParseMetric(s string) (name string, labels map[string]string, err error) {
return "", nil, ErrEmptyName
}

labels = make(map[string]string)

i := strings.IndexByte(s, '{')
if i == -1 {
if s[len(s)-1] == '}' {
return "", nil, ErrInvalidLabel
}
j := strings.IndexByte(s, '}')

if (i == -1) != (j == -1) {
return "", nil, ErrInvalidLabel
}

if i == -1 && j == -1 {
return s, nil, nil
}

if len(s) < 4 {
if i == 0 {
return "", nil, ErrInvalidLabel
}

if s[len(s)-1] != '}' {
if i >= j {
return "", nil, ErrInvalidLabel
}

if j != len(s)-1 {
return "", nil, ErrInvalidLabel
}

name = s[:i]

s = s[i+1 : len(s)-1]
for _, kv := range strings.Split(s, ",") {
for _, kv := range strings.Split(s[i+1:j], ",") {
p := strings.SplitN(kv, "=", 2)

if len(p) != 2 {
Expand All @@ -102,6 +106,9 @@ func ParseMetric(s string) (name string, labels map[string]string, err error) {
return "", nil, ErrLabelValueNotString
}

if labels == nil {
labels = make(map[string]string)
}
labels[label] = value[1 : len(p[1])-1]
}

Expand Down
3 changes: 3 additions & 0 deletions metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func TestParseMetric_Error(t *testing.T) {
"{}",
"asdf{",
"asdf}",
"asdf{a=\"1}\"}",
"asdf}a=\"1}\"{",
"asd}f",
"asdf{,}",
"asdf{=}",
"asdf{=a}",
Expand Down

0 comments on commit 839e47a

Please sign in to comment.