Skip to content

Commit

Permalink
fileserver: Preserve query during canonicalization redirect (#6109)
Browse files Browse the repository at this point in the history
* fileserver: Preserve query during canonicalization redirect

* Clarify that only a path should be passed
  • Loading branch information
francislavoie committed Mar 6, 2024
1 parent 0d44e3e commit 5a4374b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions modules/caddyhttp/fileserver/staticfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,18 @@ func calculateEtag(d os.FileInfo) string {
return `"` + t + s + `"`
}

func redirect(w http.ResponseWriter, r *http.Request, to string) error {
for strings.HasPrefix(to, "//") {
// redirect performs a redirect to a given path. The 'toPath' parameter
// MUST be solely a path, and MUST NOT include a query.
func redirect(w http.ResponseWriter, r *http.Request, toPath string) error {
for strings.HasPrefix(toPath, "//") {
// prevent path-based open redirects
to = strings.TrimPrefix(to, "/")
toPath = strings.TrimPrefix(toPath, "/")
}
http.Redirect(w, r, to, http.StatusPermanentRedirect)
// preserve the query string if present
if r.URL.RawQuery != "" {
toPath += "?" + r.URL.RawQuery
}
http.Redirect(w, r, toPath, http.StatusPermanentRedirect)
return nil
}

Expand Down

0 comments on commit 5a4374b

Please sign in to comment.