mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-24 12:03:40 +08:00
fix(gin): keep encoded slashes in trailing slash redirects
With UseRawPath or UseEscapedPath the router matches the escaped path, but redirectTrailingSlash built the Location from the decoded URL.Path. A request for /files/a%2Fb/ was redirected to /files/a/b, which no longer matches /files/:name. Build the redirect from the escaped path when routing used it, and keep URL.RawPath in sync so the encoding survives. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0f09c3a9b4
commit
09b561866f
15
gin.go
15
gin.go
@ -9,6 +9,7 @@ import (
|
||||
"html/template"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
@ -810,16 +811,28 @@ func serveError(c *Context, code int, defaultMessage []byte) {
|
||||
func redirectTrailingSlash(c *Context) {
|
||||
req := c.Request
|
||||
p := req.URL.Path
|
||||
// Routing matched the escaped path, so build the redirect from it too;
|
||||
// otherwise an encoded slash (%2F) would turn into a real path separator.
|
||||
escaped := c.engine.UseEscapedPath || (c.engine.UseRawPath && req.URL.RawPath != "")
|
||||
if escaped {
|
||||
p = req.URL.EscapedPath()
|
||||
}
|
||||
if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." {
|
||||
prefix = sanitizePathChars(prefix)
|
||||
prefix = removeRepeatedChar(prefix, '/')
|
||||
|
||||
p = prefix + "/" + req.URL.Path
|
||||
p = prefix + "/" + p
|
||||
}
|
||||
req.URL.Path = p + "/"
|
||||
if length := len(p); length > 1 && p[length-1] == '/' {
|
||||
req.URL.Path = p[:length-1]
|
||||
}
|
||||
if escaped {
|
||||
if unescaped, err := url.PathUnescape(req.URL.Path); err == nil {
|
||||
req.URL.RawPath = req.URL.Path
|
||||
req.URL.Path = unescaped
|
||||
}
|
||||
}
|
||||
redirectRequest(c)
|
||||
}
|
||||
|
||||
|
||||
@ -303,6 +303,24 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
func TestRouteRedirectTrailingSlashKeepsEscapedPath(t *testing.T) {
|
||||
for _, useEscapedPath := range []bool{false, true} {
|
||||
router := New()
|
||||
router.UseRawPath = !useEscapedPath
|
||||
router.UseEscapedPath = useEscapedPath
|
||||
router.GET("/files/:name", func(c *Context) {})
|
||||
router.GET("/dirs/:name/", func(c *Context) {})
|
||||
|
||||
w := PerformRequest(router, http.MethodGet, "/files/a%2Fb/")
|
||||
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||
assert.Equal(t, "/files/a%2Fb", w.Header().Get("Location"))
|
||||
|
||||
w = PerformRequest(router, http.MethodGet, "/dirs/a%2Fb")
|
||||
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||
assert.Equal(t, "/dirs/a%2Fb/", w.Header().Get("Location"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteRedirectFixedPath(t *testing.T) {
|
||||
router := New()
|
||||
router.RedirectFixedPath = true
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user