Skip to content

Commit 0f33be3

Browse files
committed
validating the value against regex
1 parent 383ce95 commit 0f33be3

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

internal/config/config.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,9 @@ func resolveLabels() map[string]interface{} {
931931
result[trimmedKey] = parseJSON(trimmedValue)
932932

933933
default: // String
934-
result[trimmedKey] = trimmedValue
934+
if validateLabel(trimmedValue) {
935+
result[trimmedKey] = trimmedValue
936+
}
935937
}
936938
}
937939

@@ -940,6 +942,17 @@ func resolveLabels() map[string]interface{} {
940942
return result
941943
}
942944

945+
func validateLabel(labelValue string) bool {
946+
const maxLength = 256
947+
labelPattern := regexp.MustCompile("^[a-zA-Z0-9]([a-zA-Z0-9-_]{0,254}[a-zA-Z0-9])?$")
948+
if len(labelValue) > maxLength || !labelPattern.MatchString(labelValue) {
949+
slog.Warn("Label value contains unsupported character ", "label_value", labelValue)
950+
return false
951+
}
952+
953+
return true
954+
}
955+
943956
func resolveEnvironmentVariableLabels() map[string]string {
944957
envLabels := make(map[string]string)
945958
envInput := viperInstance.GetString(LabelsRootKey)

internal/config/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ func createConfig() *Config {
12341234
{
12351235
Action: "insert",
12361236
Key: "label1",
1237-
Value: "label 1",
1237+
Value: "label-1",
12381238
},
12391239
{
12401240
Action: "insert",
@@ -1314,7 +1314,7 @@ func createConfig() *Config {
13141314
},
13151315
},
13161316
Labels: map[string]any{
1317-
"label1": "label 1",
1317+
"label1": "label-1",
13181318
"label2": "new-value",
13191319
"label3": 123,
13201320
},

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)