Skip to content

Commit 48b92c6

Browse files
authored
Merge pull request #424 from rusq/source-detect
Add a path check to handlers
2 parents 1b21389 + 7edbb11 commit 48b92c6

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

internal/viewer/handlers.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"slices"
10+
"strings"
1011

1112
"github.com/davecgh/go-spew/spew"
1213
"github.com/rusq/slack"
@@ -111,12 +112,17 @@ func isHXRequest(r *http.Request) bool {
111112
return r.Header.Get("HX-Request") == "true"
112113
}
113114

115+
func isInvalid(path string) bool {
116+
return strings.Contains(path, "..") || strings.Contains(path, "~") || strings.Contains(path, "/") || strings.Contains(path, "\\")
117+
}
118+
114119
func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string) {
115120
ts := r.PathValue("ts")
116-
if ts == "" {
121+
if ts == "" || isInvalid(ts) {
117122
http.NotFound(w, r)
118123
return
119124
}
125+
120126
ctx := r.Context()
121127
lg := v.lg.With("in", "threadHandler", "channel", id, "thread", ts)
122128
mm, err := v.src.AllThreadMessages(id, ts)
@@ -167,7 +173,7 @@ func (v *Viewer) fileHandler(w http.ResponseWriter, r *http.Request) {
167173
filename = r.PathValue("filename")
168174
ctx = r.Context()
169175
)
170-
if id == "" || filename == "" {
176+
if id == "" || filename == "" || isInvalid(filename) || isInvalid(id) {
171177
http.NotFound(w, r)
172178
return
173179
}

0 commit comments

Comments
 (0)