Skip to content

Commit 5a4374b

Browse files
fileserver: Preserve query during canonicalization redirect (#6109)
* fileserver: Preserve query during canonicalization redirect * Clarify that only a path should be passed
1 parent 0d44e3e commit 5a4374b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

modules/caddyhttp/fileserver/staticfiles.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,12 +639,18 @@ func calculateEtag(d os.FileInfo) string {
639639
return `"` + t + s + `"`
640640
}
641641

642-
func redirect(w http.ResponseWriter, r *http.Request, to string) error {
643-
for strings.HasPrefix(to, "//") {
642+
// redirect performs a redirect to a given path. The 'toPath' parameter
643+
// MUST be solely a path, and MUST NOT include a query.
644+
func redirect(w http.ResponseWriter, r *http.Request, toPath string) error {
645+
for strings.HasPrefix(toPath, "//") {
644646
// prevent path-based open redirects
645-
to = strings.TrimPrefix(to, "/")
647+
toPath = strings.TrimPrefix(toPath, "/")
646648
}
647-
http.Redirect(w, r, to, http.StatusPermanentRedirect)
649+
// preserve the query string if present
650+
if r.URL.RawQuery != "" {
651+
toPath += "?" + r.URL.RawQuery
652+
}
653+
http.Redirect(w, r, toPath, http.StatusPermanentRedirect)
648654
return nil
649655
}
650656

0 commit comments

Comments
 (0)