Skip to content

Commit f4bada7

Browse files
committed
trim extensions too
1 parent ff5fccd commit f4bada7

4 files changed

Lines changed: 22 additions & 5 deletions

File tree

handlers/list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func List(app *config.App) http.HandlerFunc {
1313
if req == nil {
1414
return
1515
}
16-
app.UpdateTimeRemaining()
1716
files := app.ListFiles()
1817
app.Log.Info("serving file list",
1918
"files", len(files), "user", req)

storage/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func (s *Storage) ListFiles() []File {
1111
break
1212
}
1313

14+
s.UpdateTimeRemaining()
1415
f := File{
1516
Id: file.Id,
1617
Name: file.Name,

storage/sanitize.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ import (
77
"unicode"
88
)
99

10-
const defaultName = "default"
10+
const (
11+
defaultName = "default"
12+
maxExtLength = 5
13+
)
1114

1215
// SanitizeName validates strings for use as filename.
1316
func SanitizeName(input, extraChars string, maxLength int) string {
17+
if strings.TrimSpace(input) == "" {
18+
return defaultName
19+
}
1420
input, err := url.QueryUnescape(input)
1521
if err != nil {
1622
return defaultName
@@ -41,10 +47,11 @@ func removeInvalidChars(filename string, allowed string) string {
4147
// truncateName trims a filename string to max size,
4248
// preserving reasonably-sized original file extensions.
4349
func truncateName(base string, ext string, maxLength int) string {
44-
const maxExtensionLength = 5
45-
if len(ext) > maxExtensionLength {
46-
ext = ext[:maxExtensionLength]
50+
ext = strings.ReplaceAll(ext, " ", "")
51+
if len(ext) > maxExtLength {
52+
ext = ext[:maxExtLength]
4753
}
54+
base = strings.TrimSpace(base)
4855
totalLength := len(base) + len(ext)
4956
if totalLength <= maxLength {
5057
return base + ext

storage/sanitize_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ func TestTruncateName(t *testing.T) {
4747
{"base", ".longextension", 12, "base.long"},
4848
{"exactfit", ".jpeg", 13, "exactfit.jpeg"},
4949
{"longfilename", ".txt", 10, "longfi.txt"},
50+
{"file", ". . . long", 12, "file...lo"},
5051
{"truncatebase", ".png", 8, "trun.png"},
5152
{"onlybase", ".toolong", 8, "onl.tool"},
53+
{"test", "test test", 18, "testtestt"},
5254
{"short", ".dat", 9, "short.dat"},
5355
{"example", "", 5, "examp"},
5456
{"", ".zip", 5, ".zip"},
@@ -81,21 +83,29 @@ func TestSanitizeName(t *testing.T) {
8183
{"myfilenames", extraChars, 10, "myfilename"},
8284
{"@#$%^&*.png", extraChars, 20, defaultName},
8385
{"!@#$%^&*()[]{}<>", extraChars, 20, defaultName},
86+
{"!@# a copy.alongext", extraChars, 20, "a copy.alon"},
87+
{"<>$ a copy.m", extraChars, 10, "a copy.m"},
88+
{"a copy.my copy", extraChars, 15, "a copy.myco"},
8489
{"filename.", extraChars, 15, "filename."},
8590
{"/etc/passwd", extraChars, 15, "passwd"},
8691
{"name\u0000.txt", extraChars, 20, "name.txt"},
8792
{"control\ttest.txt", extraChars, 20, "controltest.txt"},
8893
{"/path/../file.txt", extraChars, 20, "file.txt"},
8994
{"<script>alert('xss')</script>", extraChars, 25, "script"},
95+
{" ", extraChars, 15, "default"},
9096
{"percent%encoded%name.doc", extraChars, 20, defaultName},
9197
{"filename%20with%20spaces.txt", extraChars, 20,
9298
"filename with sp.txt"},
9399
{"filename with spaces.txt", extraChars, 25,
94100
"filename with spaces.txt"},
95101
{"my%2Fcool%2Bdoc%26about%2Cstuff.md", extraChars + "/+&,", 40,
96102
"cool+doc&about,stuff.md"},
103+
{"example." + strings.Repeat("x", 1000), extraChars, 20,
104+
"example.xxxx"},
97105
{strings.Repeat("a", 1000) + ".txt", extraChars, 50,
98106
strings.Repeat("a", 46) + ".txt"},
107+
{strings.Repeat(".", 100) + strings.Repeat(".", 100), extraChars, 80,
108+
strings.Repeat(".", 79) + "."},
99109
}
100110
for _, test := range tests {
101111
t.Run(test.input, func(t *testing.T) {

0 commit comments

Comments
 (0)