Skip to content

Commit

Permalink
Fix gin-gonic#3500 Add escape logic for header
Browse files Browse the repository at this point in the history
  • Loading branch information
t0rchwo0d committed Feb 17, 2023
1 parent 81ac7d5 commit c22614f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,11 @@ func redirectTrailingSlash(c *Context) {
p := req.URL.Path
if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." {
prefix = url.QueryEscape(prefix)
prefix = strings.ReplaceAll(prefix, ".", "")
prefix = strings.ReplaceAll(prefix, "%2F", "/")

fmt.Println(prefix)

p = prefix + "/" + req.URL.Path
}
req.URL.Path = p + "/"
Expand Down
4 changes: 2 additions & 2 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,11 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
assert.Equal(t, 200, w.Code)

w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../bug#?"})
assert.Equal(t, "../../../bug%2523%253F/path", w.Header().Get("Location"))
assert.Equal(t, "//bug%2523%253F//path", w.Header().Get("Location"))
assert.Equal(t, 301, w.Code)

w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "https://gin-gonic.com/#"})
assert.Equal(t, "https%3A/gin-gonic.com/%23/https%253A/gin-gonic.com/%2523/path", w.Header().Get("Location"))
assert.Equal(t, "https%3A/gin-goniccom/%23/https%253A/gin-goniccom/%2523/path", w.Header().Get("Location"))
assert.Equal(t, 301, w.Code)

w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "#bug"})
Expand Down

0 comments on commit c22614f

Please sign in to comment.