Skip to content

Commit 16adaae

Browse files
authored
Instead of using routerCtx just escape the url before routing (#18086)
A consequence of forcibly setting the RoutePath to the escaped url is that the auto routing to endpoints without terminal slashes fails (Causing #18060.) This failure raises the possibility that forcibly setting the RoutePath causes other unexpected behaviors 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 #17938 Fix #18060 Replace #18062 Replace #17997 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 26070eb commit 16adaae

File tree

4 files changed

+4
-18
lines changed

4 files changed

+4
-18
lines changed

integrations/links_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestLinksNoLogin(t *testing.T) {
3333
"/user/forgot_password",
3434
"/api/swagger",
3535
"/user2/repo1",
36+
"/user2/repo1/",
3637
"/user2/repo1/projects",
3738
"/user2/repo1/projects/1",
3839
"/assets/img/404.png",
@@ -61,16 +62,6 @@ func TestRedirectsNoLogin(t *testing.T) {
6162
resp := MakeRequest(t, req, http.StatusFound)
6263
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
6364
}
64-
65-
var temporaryRedirects = map[string]string{
66-
"/user2/repo1/": "/user2/repo1",
67-
}
68-
for link, redirectLink := range temporaryRedirects {
69-
req := NewRequest(t, "GET", link)
70-
resp := MakeRequest(t, req, http.StatusTemporaryRedirect)
71-
assert.EqualValues(t, path.Join(setting.AppSubURL, redirectLink), test.RedirectURL(resp))
72-
}
73-
7465
}
7566

7667
func TestNoLoginNotExist(t *testing.T) {

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)