From 842a7964621a03c4e284084f15c44213e845b365 Mon Sep 17 00:00:00 2001 From: nodivbyzero Date: Sun, 6 Aug 2023 09:58:56 -0700 Subject: [PATCH] Fix filepath validator panic if the value is an existing dir (#1133) ## Fixes Or Enhances This PR adds an additional check for directories in the isFilePath() function. --- baked_in.go | 4 ++++ validator_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/baked_in.go b/baked_in.go index ed0cf3bb..a0750ccb 100644 --- a/baked_in.go +++ b/baked_in.go @@ -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 { diff --git a/validator_test.go b/validator_test.go index 8ca2c2aa..97a50f6c 100644 --- a/validator_test.go +++ b/validator_test.go @@ -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 {