Skip to content

Commit 5d148d9

Browse files
authored
Validating the label values against regex (#1345)
1 parent b55f22b commit 5d148d9

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

internal/config/config.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ const (
4646

4747
// Regular expression to match invalid characters in paths.
4848
// It matches whitespace, control characters, non-printable characters, and specific Unicode characters.
49-
regexInvalidPath = "\\s|[[:cntrl:]]|[[:space:]]|[[^:print:]]|ㅤ|\\.\\.|\\*"
49+
regexInvalidPath = "\\s|[[:cntrl:]]|[[:space:]]|[[^:print:]]|ㅤ|\\.\\.|\\*"
50+
regexLabelPattern = "^[a-zA-Z0-9]([a-zA-Z0-9-_]{0,254}[a-zA-Z0-9])?$"
5051
)
5152

5253
var viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
@@ -946,7 +947,9 @@ func resolveLabels() map[string]interface{} {
946947
result[trimmedKey] = parseJSON(trimmedValue)
947948

948949
default: // String
949-
result[trimmedKey] = trimmedValue
950+
if validateLabel(trimmedValue) {
951+
result[trimmedKey] = trimmedValue
952+
}
950953
}
951954
}
952955

@@ -955,6 +958,19 @@ func resolveLabels() map[string]interface{} {
955958
return result
956959
}
957960

961+
func validateLabel(labelValue string) bool {
962+
const maxLength = 256
963+
labelPattern := regexp.MustCompile(regexLabelPattern)
964+
if len(labelValue) > maxLength || !labelPattern.MatchString(labelValue) {
965+
slog.Warn("Label value contains unsupported character or exceed maximum length of 256 characters ",
966+
"label_value", labelValue)
967+
968+
return false
969+
}
970+
971+
return true
972+
}
973+
958974
func resolveEnvironmentVariableLabels() map[string]string {
959975
envLabels := make(map[string]string)
960976
envInput := viperInstance.GetString(LabelsRootKey)

internal/config/config_test.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ func createConfig() *Config {
12371237
{
12381238
Action: "insert",
12391239
Key: "label1",
1240-
Value: "label 1",
1240+
Value: "label-1",
12411241
},
12421242
{
12431243
Action: "insert",
@@ -1317,7 +1317,7 @@ func createConfig() *Config {
13171317
},
13181318
},
13191319
Labels: map[string]any{
1320-
"label1": "label 1",
1320+
"label1": "label-1",
13211321
"label2": "new-value",
13221322
"label3": 123,
13231323
},
@@ -1413,3 +1413,59 @@ func createDefaultCollectorConfig() *Collector {
14131413
},
14141414
}
14151415
}
1416+
1417+
func TestValidateLabel(t *testing.T) {
1418+
tests := []struct {
1419+
name string
1420+
input string
1421+
expected bool
1422+
}{
1423+
{
1424+
name: "Test 1: Valid label - simple",
1425+
input: "label123",
1426+
expected: true,
1427+
},
1428+
{
1429+
name: "Test 2: Valid label - with dash and underscore",
1430+
input: "label-123_abc",
1431+
expected: true,
1432+
},
1433+
{
1434+
name: "Test 3: Invalid label - too long",
1435+
input: strings.Repeat("a", 257),
1436+
expected: false,
1437+
},
1438+
{
1439+
name: "Test 4: Invalid label - special char",
1440+
input: "label$",
1441+
expected: false,
1442+
},
1443+
{
1444+
name: "Test 5: Invalid label - starts with dash",
1445+
input: "-label",
1446+
expected: false,
1447+
},
1448+
{
1449+
name: "Test 6: Invalid label - ends with dash",
1450+
input: "label-",
1451+
expected: false,
1452+
},
1453+
{
1454+
name: "Test 7: Invalid label - empty",
1455+
input: "",
1456+
expected: false,
1457+
},
1458+
{
1459+
name: "Test 8: Invalid label - contains spaces",
1460+
input: "label 123",
1461+
expected: false,
1462+
},
1463+
}
1464+
1465+
for _, tt := range tests {
1466+
t.Run(tt.name, func(t *testing.T) {
1467+
actual := validateLabel(tt.input)
1468+
assert.Equal(t, tt.expected, actual)
1469+
})
1470+
}
1471+
}

internal/config/testdata/nginx-agent.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ watchers:
1313
- \.*log$
1414

1515
labels:
16-
label1: label 1
16+
label1: label-1
1717
label2: new-value
1818
label3: 123
1919

0 commit comments

Comments
 (0)