Server: Disambiguate redirects to server relative paths (#32788)

* Server: Disambiguate redirects to server relative paths

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen 2021-04-08 15:05:42 +02:00 committed by GitHub
parent 21879410ab
commit 7032ecce2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,7 @@
package httpstatic package httpstatic
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -149,7 +150,12 @@ func staticHandler(ctx *macaron.Context, log *log.Logger, opt StaticOptions) boo
if fi.IsDir() { if fi.IsDir() {
// Redirect if missing trailing slash. // Redirect if missing trailing slash.
if !strings.HasSuffix(ctx.Req.URL.Path, "/") { if !strings.HasSuffix(ctx.Req.URL.Path, "/") {
http.Redirect(ctx.Resp, ctx.Req.Request, ctx.Req.URL.Path+"/", http.StatusFound) path := fmt.Sprintf("%s/", ctx.Req.URL.Path)
if !strings.HasPrefix(path, "/") {
// Disambiguate that it's a path relative to this server
path = fmt.Sprintf("/%s", path)
}
http.Redirect(ctx.Resp, ctx.Req.Request, path, http.StatusFound)
return true return true
} }