Skip to content

Commit

Permalink
Fix filepath validator panic if the value is an existing dir (#1133)
Browse files Browse the repository at this point in the history
## Fixes Or Enhances
This PR adds an additional check for directories in the isFilePath()
function.
  • Loading branch information
nodivbyzero authored Aug 6, 2023
1 parent 63657cf commit 842a796
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,10 @@ func isFilePath(fl FieldLevel) bool {

field := fl.Field()

// Not valid if it is a directory.
if isDir(fl) {
return false
}
// If it exists, it obviously is valid.
// This is done first to avoid code duplication and unnecessary additional logic.
if exists = isFile(fl); exists {
Expand Down
1 change: 1 addition & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5852,6 +5852,7 @@ func TestFilePathValidation(t *testing.T) {
{"valid filepath", filepath.Join("testdata", "a.go"), true},
{"invalid filepath", filepath.Join("testdata", "no\000.go"), false},
{"directory, not a filepath", "testdata" + string(os.PathSeparator), false},
{"directory", "testdata", false},
}

for _, test := range tests {
Expand Down

0 comments on commit 842a796

Please sign in to comment.