Skip to content

Commit

Permalink
Merge pull request #216 from dogancanbakir/add_fileexistsin_func
Browse files Browse the repository at this point in the history
add FileExistsIn func
  • Loading branch information
Mzack9999 committed Jul 31, 2023
2 parents 322768d + a910ab7 commit 2f541e0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
31 changes: 31 additions & 0 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/asaskevich/govalidator"
"github.com/pkg/errors"
sliceutil "github.com/projectdiscovery/utils/slice"
stringsutil "github.com/projectdiscovery/utils/strings"
"gopkg.in/yaml.v3"
)
Expand All @@ -34,6 +35,36 @@ func FileExists(filename string) bool {
return !info.IsDir()
}

// FileExistsIn checks if the file exists in the allowed paths
func FileExistsIn(file string, allowedPaths ...string) (string, error) {
fileAbsPath, err := filepath.Abs(file)
if err != nil {
return "", err
}

uniqAllowedPaths := sliceutil.Dedupe(allowedPaths)

for _, allowedPath := range uniqAllowedPaths {
allowedAbsPath, err := filepath.Abs(allowedPath)
if err != nil {
return "", err
}
// reject any path that for some reason was cleaned up and starts with .
if stringsutil.HasPrefixAny(allowedAbsPath, ".") {
return "", errors.New("invalid path")
}

allowedDirPath := allowedAbsPath
if filepath.Ext(allowedAbsPath) != "" {
allowedDirPath = filepath.Dir(allowedAbsPath)
}
if strings.HasPrefix(fileAbsPath, allowedDirPath) && FileExists(fileAbsPath) {
return allowedDirPath, nil
}
}
return "", errors.New("no allowed path found")
}

// FolderExists checks if the folder exists
func FolderExists(foldername string) bool {
info, err := os.Stat(foldername)
Expand Down
51 changes: 51 additions & 0 deletions file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,54 @@ func TestOpenOrCreateFile(t *testing.T) {
require.Error(t, err)
})
}

func TestFileExistsIn(t *testing.T) {
tempDir := t.TempDir()
anotherTempDir := t.TempDir()
tempFile := filepath.Join(tempDir, "file.txt")
err := os.WriteFile(tempFile, []byte("content"), 0644)
if err != nil {
t.Fatalf("failed to write to temporary file: %v", err)
}
defer os.RemoveAll(tempFile)

tests := []struct {
name string
file string
allowedFiles []string
expectedPath string
expectedErr bool
}{
{
name: "file exists in allowed directory",
file: tempFile,
allowedFiles: []string{filepath.Join(tempDir, "tempfile.txt")},
expectedPath: tempDir,
expectedErr: false,
},
{
name: "file does not exist in allowed directory",
file: tempFile,
allowedFiles: []string{anotherTempDir},
expectedPath: "",
expectedErr: true,
},
{
name: "path starting with .",
file: tempFile,
allowedFiles: []string{"."},
expectedPath: "",
expectedErr: true,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
allowedPath, err := FileExistsIn(tc.file, tc.allowedFiles...)
gotErr := err != nil
require.Equal(t, tc.expectedErr, gotErr, "expected err but got %v", gotErr)
require.Equal(t, tc.expectedPath, allowedPath)

})
}
}

0 comments on commit 2f541e0

Please sign in to comment.