Skip to content

Commit 6e7d28c

Browse files
authored
Prevent double decoding of % in url params (#17997)
There was an unfortunate regression in #14293 which has led to the double decoding of url parameter elements if they contain a '%'. This is due to an issue with the way chi decodes its RoutePath. In detail the problem lies in mux.go where the routeHTTP path uses the URL.RawPath or even the URL.Path instead of the escaped path to do routing. This PR simply forcibly sets the routePath to that of the EscapedPath. Fix #17938 Signed-off-by: Andrew Thornton <[email protected]>
1 parent e0e3ba6 commit 6e7d28c

12 files changed

+41
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3a810dbf6b96afaa8c5f69a8b6ec1dabfca7368b
1+
59e2c41e8f5140bb0182acebec17c8ad9831cc62

integrations/nonascii_branches_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integrations
66

77
import (
88
"net/http"
9+
"net/url"
910
"path"
1011
"testing"
1112

@@ -159,6 +160,41 @@ func TestNonasciiBranches(t *testing.T) {
159160
to: "tag/%D0%81/%E4%BA%BA",
160161
status: http.StatusOK,
161162
},
163+
{
164+
from: "Plus+Is+Not+Space/%25%252525mightnotplaywell",
165+
to: "branch/Plus+Is+Not+Space/%25%252525mightnotplaywell",
166+
status: http.StatusOK,
167+
},
168+
{
169+
from: "Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
170+
to: "branch/Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
171+
status: http.StatusOK,
172+
},
173+
{
174+
from: "Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
175+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
176+
status: http.StatusOK,
177+
},
178+
{
179+
from: "Plus+Is+Not+Space/10%25.md",
180+
to: "branch/Plus+Is+Not+Space/10%25.md",
181+
status: http.StatusOK,
182+
},
183+
{
184+
from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
185+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
186+
status: http.StatusOK,
187+
},
188+
{
189+
from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
190+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
191+
status: http.StatusOK,
192+
},
193+
{
194+
from: "Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
195+
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
196+
status: http.StatusOK,
197+
},
162198
}
163199

164200
defer prepareTestEnv(t)()

modules/context/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ func Contexter() func(next http.Handler) http.Handler {
609609
var locale = middleware.Locale(resp, req)
610610
var startTime = time.Now()
611611
var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
612+
613+
chiCtx := chi.RouteContext(req.Context())
614+
chiCtx.RoutePath = req.URL.EscapedPath()
615+
612616
var ctx = Context{
613617
Resp: NewResponse(resp),
614618
Cache: mc.GetCache(),

0 commit comments

Comments
 (0)