Skip to content

Commit 6537838

Browse files
committed
Instead of using routerCtx just escape the url before routing
A consequence of forcibly setting the RoutePath to the escaped url is that the auto routing to endpoints without terminal slashes fails (Causing go-gitea#18060.) This failure raises the possibility that forcibly setting the RoutePath causes other unexpected behaviours too. Therefore, instead we should simply pre-escape the URL in the process registering handler. Then the request URL will be properly escaped for all the following calls. Fix go-gitea#17938 Fix go-gitea#18060 Replace go-gitea#18062 Replace go-gitea#17997 Signed-off-by: Andrew Thornton <[email protected]>
1 parent a5df7ba commit 6537838

File tree

3 files changed

+3
-8
lines changed

3 files changed

+3
-8
lines changed

modules/context/context.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,6 @@ func Contexter() func(next http.Handler) http.Handler {
610610
var startTime = time.Now()
611611
var link = setting.AppSubURL + strings.TrimSuffix(req.URL.EscapedPath(), "/")
612612

613-
chiCtx := chi.RouteContext(req.Context())
614-
chiCtx.RoutePath = req.URL.EscapedPath()
615-
616613
var ctx = Context{
617614
Resp: NewResponse(resp),
618615
Cache: mc.GetCache(),

routers/common/middleware.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func Middlewares() []func(http.Handler) http.Handler {
2323
var handlers = []func(http.Handler) http.Handler{
2424
func(next http.Handler) http.Handler {
2525
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
26+
// First of all escape the URL RawPath to ensure that all routing is done using a correctly escaped URL
27+
req.URL.RawPath = req.URL.EscapedPath()
28+
2629
ctx, _, finished := process.GetManager().AddContext(req.Context(), fmt.Sprintf("%s: %s", req.Method, req.RequestURI))
2730
defer finished()
2831
next.ServeHTTP(context.NewResponse(resp), req.WithContext(ctx))

routers/web/web.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,11 +1079,6 @@ func RegisterRoutes(m *web.Route) {
10791079
m.Get("/swagger.v1.json", SwaggerV1Json)
10801080
}
10811081
m.NotFound(func(w http.ResponseWriter, req *http.Request) {
1082-
escapedPath := req.URL.EscapedPath()
1083-
if len(escapedPath) > 1 && escapedPath[len(escapedPath)-1] == '/' {
1084-
http.Redirect(w, req, setting.AppSubURL+escapedPath[:len(escapedPath)-1], http.StatusTemporaryRedirect)
1085-
return
1086-
}
10871082
ctx := context.GetContext(req)
10881083
ctx.NotFound("", nil)
10891084
})

0 commit comments

Comments
 (0)