Skip to content

Commit

Permalink
rejecting allowed relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Jul 23, 2023
1 parent 99bb3bb commit a910ab7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func FileExistsIn(file string, allowedPaths ...string) (string, error) {
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)
Expand Down
8 changes: 8 additions & 0 deletions file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ func TestFileExistsIn(t *testing.T) {
if err != nil {
t.Fatalf("failed to write to temporary file: %v", err)
}
defer os.RemoveAll(tempFile)

tests := []struct {
name string
Expand All @@ -612,6 +613,13 @@ func TestFileExistsIn(t *testing.T) {
expectedPath: "",
expectedErr: true,
},
{
name: "path starting with .",
file: tempFile,
allowedFiles: []string{"."},
expectedPath: "",
expectedErr: true,
},
}

for _, tc := range tests {
Expand Down

0 comments on commit a910ab7

Please sign in to comment.