Skip to content

Commit 7683429

Browse files
committed
Prevent double decoding of % in url params (go-gitea#17997)
There was an unfortunate regression in go-gitea#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 go-gitea#17938 Signed-off-by: Andrew Thornton <[email protected]>
1 parent fc8c23e commit 7683429

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
@@ -669,6 +669,10 @@ func Contexter() func(next http.Handler) http.Handler {
669669
var locale = middleware.Locale(resp, req)
670670
var startTime = time.Now()
671671
var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
672+
673+
chiCtx := chi.RouteContext(req.Context())
674+
chiCtx.RoutePath = req.URL.EscapedPath()
675+
672676
var ctx = Context{
673677
Resp: NewResponse(resp),
674678
Cache: mc.GetCache(),

0 commit comments

Comments
 (0)