Skip to content

Commit 81ac7d5

Browse files
authored
Add escape logic for header (#3500)
1 parent d07db17 commit 81ac7d5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

gin.go

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"html/template"
1010
"net"
1111
"net/http"
12+
"net/url"
1213
"os"
1314
"path"
1415
"strings"
@@ -668,6 +669,9 @@ func redirectTrailingSlash(c *Context) {
668669
req := c.Request
669670
p := req.URL.Path
670671
if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." {
672+
prefix = url.QueryEscape(prefix)
673+
prefix = strings.ReplaceAll(prefix, "%2F", "/")
674+
671675
p = prefix + "/" + req.URL.Path
672676
}
673677
req.URL.Path = p + "/"

routes_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,18 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
185185
w = PerformRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
186186
assert.Equal(t, 200, w.Code)
187187

188+
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../bug#?"})
189+
assert.Equal(t, "../../../bug%2523%253F/path", w.Header().Get("Location"))
190+
assert.Equal(t, 301, w.Code)
191+
192+
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "https://gin-gonic.com/#"})
193+
assert.Equal(t, "https%3A/gin-gonic.com/%23/https%253A/gin-gonic.com/%2523/path", w.Header().Get("Location"))
194+
assert.Equal(t, 301, w.Code)
195+
196+
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "#bug"})
197+
assert.Equal(t, "%23bug/%2523bug/path", w.Header().Get("Location"))
198+
assert.Equal(t, 301, w.Code)
199+
188200
router.RedirectTrailingSlash = false
189201

190202
w = PerformRequest(router, http.MethodGet, "/path/")

0 commit comments

Comments
 (0)