Skip to content

Commit 254dc4b

Browse files
committed
fix: make "file://" fail url validation
"file://" is not a valid URL since it has no path.
1 parent 3fd4678 commit 254dc4b

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

baked_in.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,19 +1461,11 @@ func isURI(fl FieldLevel) bool {
14611461
panic(fmt.Sprintf("Bad field type %s", field.Type()))
14621462
}
14631463

1464-
// isFileURL is the helper function for validating if the `path` valid file URL as per RFC8089
1465-
func isFileURL(path string) bool {
1466-
if !strings.HasPrefix(path, "file:/") {
1467-
return false
1468-
}
1469-
_, err := url.ParseRequestURI(path)
1470-
return err == nil
1471-
}
1472-
14731464
// isURL is the validation function for validating if the current field's value is a valid URL.
14741465
func isURL(fl FieldLevel) bool {
14751466
field := fl.Field()
14761467

1468+
var err error
14771469
switch field.Kind() {
14781470
case reflect.String:
14791471

@@ -1483,8 +1475,11 @@ func isURL(fl FieldLevel) bool {
14831475
return false
14841476
}
14851477

1486-
if isFileURL(s) {
1487-
return true
1478+
if strings.HasPrefix(s, "file://") {
1479+
var u *url.URL
1480+
u, err = url.ParseRequestURI(s)
1481+
1482+
return err == nil && u.Path != ""
14881483
}
14891484

14901485
url, err := url.Parse(s)

validator_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8255,7 +8255,9 @@ func TestUrl(t *testing.T) {
82558255
{"file:///c:/Windows/file.txt", true},
82568256
{"file://localhost/path/to/file.txt", true},
82578257
{"file://localhost/c:/WINDOWS/file.txt", true},
8258-
{"file://", true},
8258+
{"file:", false},
8259+
{"file:/", false},
8260+
{"file://", false},
82598261
{"file:////remotehost/path/file.txt", true},
82608262
}
82618263

0 commit comments

Comments
 (0)