Skip to content

Commit

Permalink
prevent more filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Jul 25, 2024
1 parent 9235c34 commit 4f1a6a8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ func UnzipDirectory(destination string, source string) error {
// ValidFileName checks if a filename is valid
// by making sure it has no invisible characters
func ValidFileName(fname string) (err error) {
// make sure it doesn't contain unicode or invisible characters
for _, r := range fname {
if !unicode.IsGraphic(r) {
err = fmt.Errorf("non-graphical unicode: %x U+%d in '%s'", string(r), r, fname)
Expand All @@ -582,5 +583,20 @@ func ValidFileName(fname string) (err error) {
return
}
}
// make sure basename does not include ".." or path separators
_, basename := filepath.Split(fname)
if strings.Contains(basename, "..") {
err = fmt.Errorf("basename cannot contain '..': '%s'", basename)

This comment has been minimized.

Copy link
@q--

q-- Oct 1, 2024

Why is this blocked? This is a completely valid filename. It prevented me from transferring a folder because it had some file in it which I named something like Database export with some test data (users, posts, comments, ...) platformname.sql, and I'm sure there are other legitimate reasons to have .. in the file name.

If you're trying to prevent path traversal, wouldn't be checking if a file name equals .. be enough?

This comment has been minimized.

Copy link
@schollz

schollz Oct 1, 2024

Author Owner

can you give me full name of the file you wanted to transfer?

return
}
if strings.Contains(basename, string(os.PathSeparator)) {
err = fmt.Errorf("basename cannot contain path separators: '%s'", basename)
return
}
// make sure the filename is not an absolute path
if filepath.IsAbs(fname) {
err = fmt.Errorf("filename cannot be an absolute path: '%s'", fname)
return
}
return
}
2 changes: 2 additions & 0 deletions src/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,6 @@ func TestValidFileName(t *testing.T) {
err := ValidFileName("D中文.cslouglas​")
assert.NotNil(t, err)
assert.Equal(t, "non-graphical unicode: e2808b U+8203 in 'D中文.cslouglas​'", err.Error())
assert.NotNil(t, ValidFileName("hi..txt"))
assert.NotNil(t, ValidFileName("/hi/something.txt"))
}

0 comments on commit 4f1a6a8

Please sign in to comment.