Skip to content

Commit 27f0abe

Browse files
authored
Merge pull request #566 from rusq/i561
fix for filenames with tilda
2 parents 08fae9c + 9a68802 commit 27f0abe

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

internal/viewer/handlers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ func isHXRequest(r *http.Request) bool {
145145
return r.Header.Get("HX-Request") == "true"
146146
}
147147

148-
func isInvalid(path string) bool {
149-
return strings.Contains(path, "..") || strings.Contains(path, "~") || strings.Contains(path, "/") || strings.Contains(path, "\\")
148+
// isInvalid returns true if the provided path component is not web-safe.
149+
func isInvalid(pcomp string) bool {
150+
return strings.Contains(pcomp, "..") || strings.HasPrefix(pcomp, "~") || strings.Contains(pcomp, "/") || strings.Contains(pcomp, "\\")
150151
}
151152

152153
func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string) {

internal/viewer/handlers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
package viewer
2+
3+
import "testing"
4+
5+
func Test_isInvalid(t *testing.T) {
6+
type args struct {
7+
path string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want bool
13+
}{
14+
{"relative path", args{"../test.txt"}, true},
15+
{"home dir ref", args{"~/test.txt"}, true},
16+
{"filename with tilda #561", args{"test~1.txt"}, false},
17+
}
18+
for _, tt := range tests {
19+
t.Run(tt.name, func(t *testing.T) {
20+
if got := isInvalid(tt.args.path); got != tt.want {
21+
t.Errorf("isInvalid() = %v, want %v", got, tt.want)
22+
}
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)