Skip to content

Commit 83112d2

Browse files
committed
fix handling of trailing commas in SecretStringSliceCSV.Set()
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent e5eadb1 commit 83112d2

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* [BUGFIX] Ring: Fix ring token conflict resolution only applied to updated instance and make constantly token conflict check during instance observe period.
5050
* [BUGFIX] Distributor: Fix a panic (`slice bounds out of range`) in the stream push path when the context deadline expires while the worker goroutine is still marshalling a `WriteRequest`. #7541
5151
* [BUGFIX] Query Frontend: Fix native histogram responses not being handled correctly in `minTime()` sort ordering for split_by_interval merge. #7555
52+
* [BUGFIX] Security: Fix `SecretStringSliceCSV.Set()` accepting empty entries from stray or trailing commas (e.g. `newkey,`). #7587
5253

5354
## 1.21.0 2026-04-24
5455

pkg/util/flagext/secretstringslicecsv.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package flagext
22

3-
import "strings"
3+
import (
4+
"fmt"
5+
"strings"
6+
)
47

58
// SecretStringSliceCSV is a slice of strings that is parsed from a comma-separated string.
69
// It implements flag.Value and yaml Marshalers, but masks the value when marshaled to YAML
@@ -15,12 +18,23 @@ func (v SecretStringSliceCSV) String() string {
1518
}
1619

1720
// Set implements flag.Value
21+
// Each comma-separated entry is trimmed of surrounding whitespace.
22+
// Empty entries (after trimming) are rejected with an error.
1823
func (v *SecretStringSliceCSV) Set(s string) error {
1924
if s == "" {
2025
v.values = nil
2126
return nil
2227
}
23-
v.values = strings.Split(s, ",")
28+
parts := strings.Split(s, ",")
29+
values := make([]string, 0, len(parts))
30+
for _, p := range parts {
31+
p = strings.TrimSpace(p)
32+
if p == "" {
33+
return fmt.Errorf("invalid key list %q: empty entry after trimming", s)
34+
}
35+
values = append(values, p)
36+
}
37+
v.values = values
2438
return nil
2539
}
2640

pkg/util/flagext/secretstringslicecsv_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,32 @@ func TestSecretStringSliceCSV(t *testing.T) {
4444
require.NoError(t, s.Keys.Set(""))
4545
assert.Equal(t, []string(nil), s.Keys.Value())
4646
})
47+
48+
t.Run("trailing comma is rejected", func(t *testing.T) {
49+
var s TestStruct
50+
err := s.Keys.Set("newkey,")
51+
require.Error(t, err, "trailing comma must produce an error")
52+
assert.Nil(t, s.Keys.Value(), "values must not be updated on error")
53+
})
54+
55+
t.Run("leading comma is rejected", func(t *testing.T) {
56+
var s TestStruct
57+
require.Error(t, s.Keys.Set(",newkey"))
58+
})
59+
60+
t.Run("double comma is rejected", func(t *testing.T) {
61+
var s TestStruct
62+
require.Error(t, s.Keys.Set("newkey,,oldkey"))
63+
})
64+
65+
t.Run("whitespace-only entry is rejected", func(t *testing.T) {
66+
var s TestStruct
67+
require.Error(t, s.Keys.Set("newkey, ,oldkey"))
68+
})
69+
70+
t.Run("surrounding whitespace is trimmed from valid entries", func(t *testing.T) {
71+
var s TestStruct
72+
require.NoError(t, s.Keys.Set(" key1 , key2 "))
73+
assert.Equal(t, []string{"key1", "key2"}, s.Keys.Value())
74+
})
4775
}

0 commit comments

Comments
 (0)