Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add FileExistsIn func #216

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

})
}
}
Loading