Skip to content

Commit cf2267f

Browse files
authored
fix: make "file://" fail url validation (#1444)
1 parent 3fd4678 commit cf2267f

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

baked_in.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,15 +1461,6 @@ 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()
@@ -1483,16 +1474,13 @@ func isURL(fl FieldLevel) bool {
14831474
return false
14841475
}
14851476

1486-
if isFileURL(s) {
1487-
return true
1488-
}
1489-
14901477
url, err := url.Parse(s)
14911478
if err != nil || url.Scheme == "" {
14921479
return false
14931480
}
1481+
isFileScheme := url.Scheme == "file"
14941482

1495-
if url.Host == "" && url.Fragment == "" && url.Opaque == "" {
1483+
if (isFileScheme && (len(url.Path) == 0 || url.Path == "/")) || (!isFileScheme && len(url.Host) == 0 && len(url.Fragment) == 0 && len(url.Opaque) == 0) {
14961484
return false
14971485
}
14981486

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)