Apply security patch security-patch-202505051005.patch (#105754)
* Fix static handler redirect logic to ensure proper clean up URLs before redirection. (cherry picked from commit f50ec8e0d10c24fd79f6c454974a2fc6e9694ef2)
This commit is contained in:
@@ -159,16 +159,17 @@ func staticHandler(ctx *web.Context, log log.Logger, opt StaticOptions) bool {
|
||||
if fi.IsDir() {
|
||||
// Redirect if missing trailing slash.
|
||||
if !strings.HasSuffix(ctx.Req.URL.Path, "/") {
|
||||
path := fmt.Sprintf("%s/", ctx.Req.URL.Path)
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
redirectPath := path.Clean(ctx.Req.URL.Path)
|
||||
redirectPath = fmt.Sprintf("%s/", redirectPath)
|
||||
if !strings.HasPrefix(redirectPath, "/") {
|
||||
// Disambiguate that it's a path relative to this server
|
||||
path = fmt.Sprintf("/%s", path)
|
||||
redirectPath = fmt.Sprintf("/%s", redirectPath)
|
||||
} else {
|
||||
// A string starting with // or /\ is interpreted by browsers as a URL, and not a server relative path
|
||||
rePrefix := regexp.MustCompile(`^(?:/\\|/+)`)
|
||||
path = rePrefix.ReplaceAllString(path, "/")
|
||||
redirectPath = rePrefix.ReplaceAllString(redirectPath, "/")
|
||||
}
|
||||
http.Redirect(ctx.Resp, ctx.Req, path, http.StatusFound)
|
||||
http.Redirect(ctx.Resp, ctx.Req, redirectPath, http.StatusFound)
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user