@@ -18,6 +18,8 @@ package validation
18
18
19
19
import (
20
20
"fmt"
21
+ "strings"
22
+
21
23
"k8s.io/apimachinery/pkg/api/validation"
22
24
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
23
25
"k8s.io/apimachinery/pkg/util/sets"
@@ -191,8 +193,16 @@ func ValidateFlowSchemaNonResourcePolicyRule(rule *flowcontrol.NonResourcePolicy
191
193
192
194
if len (rule .NonResourceURLs ) == 0 {
193
195
allErrs = append (allErrs , field .Required (fldPath .Child ("nonResourceURLs" ), "nonResourceURLs must contain at least one value" ))
194
- } else if len (rule .NonResourceURLs ) > 1 && hasWildcard (rule .NonResourceURLs ) {
195
- allErrs = append (allErrs , field .Invalid (fldPath .Child ("nonResourceURLs" ), rule .NonResourceURLs , "if '*' is present, must not specify other non-resource URLs" ))
196
+ } else if hasWildcard (rule .NonResourceURLs ) {
197
+ if len (rule .NonResourceURLs ) > 1 {
198
+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("nonResourceURLs" ), rule .NonResourceURLs , "if '*' is present, must not specify other non-resource URLs" ))
199
+ }
200
+ } else {
201
+ for i , nonResourceURL := range rule .NonResourceURLs {
202
+ if err := ValidateNonResourceURLPath (nonResourceURL , fldPath .Child ("nonResourceURLs" ).Index (i )); err != nil {
203
+ allErrs = append (allErrs , err )
204
+ }
205
+ }
196
206
}
197
207
198
208
return allErrs
@@ -332,6 +342,35 @@ func ValidatePriorityLevelConfigurationCondition(condition *flowcontrol.Priority
332
342
return allErrs
333
343
}
334
344
345
+ // ValidateNonResourceURLPath validates non-resource-url path by following rules:
346
+ // 1. Slash must be the leading character of the path
347
+ // 2. White-space is forbidden in the path
348
+ // 3. Continuous/double slash is forbidden in the path
349
+ // 4. Wildcard "*" should only do suffix glob matching. Note that wildcard also matches slashes.
350
+ func ValidateNonResourceURLPath (path string , fldPath * field.Path ) * field.Error {
351
+ if len (path ) == 0 {
352
+ return field .Invalid (fldPath , path , "must not be empty" )
353
+ }
354
+ if path == "/" { // root path
355
+ return nil
356
+ }
357
+
358
+ if ! strings .HasPrefix (path , "/" ) {
359
+ return field .Invalid (fldPath , path , "must start with slash" )
360
+ }
361
+ if strings .Contains (path , " " ) {
362
+ return field .Invalid (fldPath , path , "must not contain white-space" )
363
+ }
364
+ if strings .Contains (path , "//" ) {
365
+ return field .Invalid (fldPath , path , "must not contain double slash" )
366
+ }
367
+ wildcardCount := strings .Count (path , "*" )
368
+ if wildcardCount > 1 || (wildcardCount == 1 && path [len (path )- 2 :] != "/*" ) {
369
+ return field .Invalid (fldPath , path , "wildcard can only do suffix matching" )
370
+ }
371
+ return nil
372
+ }
373
+
335
374
func hasWildcard (operations []string ) bool {
336
375
for _ , o := range operations {
337
376
if o == "*" {
0 commit comments