Skip to content

Commit 245ac32

Browse files
authored
Fix context usage (#33554)
Some old code use direct type-casting to get context, it causes problems. This PR fixes all legacy problems and use correct `ctx.Value` to get low-level contexts. Fix #33518
1 parent e9b98ae commit 245ac32

File tree

11 files changed

+18
-17
lines changed

11 files changed

+18
-17
lines changed

routers/install/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Contexter() func(next http.Handler) http.Handler {
6464
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
6565
base := context.NewBaseContext(resp, req)
6666
ctx := context.NewWebContext(base, rnd, session.GetSession(req))
67-
ctx.SetContextValue(context.WebContextKey, ctx)
67+
ctx.SetContextValue(context.WebContextKey, ctx) // FIXME: this should be removed because NewWebContext should already set it
6868
ctx.Data.MergeFrom(middleware.CommonTemplateContextData())
6969
ctx.Data.MergeFrom(reqctx.ContextData{
7070
"Title": ctx.Locale.Tr("install.install"),

routers/private/internal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Routes() *web.Router {
8888
// Fortunately, the LFS handlers are able to handle requests without a complete web context
8989
common.AddOwnerRepoGitLFSRoutes(r, func(ctx *context.PrivateContext) {
9090
webContext := &context.Context{Base: ctx.Base}
91-
ctx.SetContextValue(context.WebContextKey, webContext)
91+
ctx.SetContextValue(context.WebContextKey, webContext) // FIXME: this is not ideal but no other way at the moment
9292
})
9393
})
9494

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ func registerRoutes(m *web.Router) {
16371637
}
16381638

16391639
m.NotFound(func(w http.ResponseWriter, req *http.Request) {
1640-
ctx := context.GetWebContext(req)
1640+
ctx := context.GetWebContext(req.Context())
16411641
defer routing.RecordFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound"))()
16421642
ctx.NotFound("", nil)
16431643
})

services/auth/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore
149149
middleware.SetLocaleCookie(resp, user.Language, 0)
150150

151151
// force to generate a new CSRF token
152-
if ctx := gitea_context.GetWebContext(req); ctx != nil {
152+
if ctx := gitea_context.GetWebContext(req.Context()); ctx != nil {
153153
ctx.Csrf.PrepareForSessionUser(ctx)
154154
}
155155
}

services/auth/sspi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore,
8888
store.GetData()["EnableSSPI"] = true
8989
// in this case, the Verify function is called in Gitea's web context
9090
// FIXME: it doesn't look good to render the page here, why not redirect?
91-
gitea_context.GetWebContext(req).HTML(http.StatusUnauthorized, tplSignIn)
91+
gitea_context.GetWebContext(req.Context()).HTML(http.StatusUnauthorized, tplSignIn)
9292
return nil, err
9393
}
9494
if outToken != "" {

services/context/context.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ type webContextKeyType struct{}
7979

8080
var WebContextKey = webContextKeyType{}
8181

82-
func GetWebContext(req *http.Request) *Context {
83-
ctx, _ := req.Context().Value(WebContextKey).(*Context)
84-
return ctx
82+
func GetWebContext(ctx context.Context) *Context {
83+
webCtx, _ := ctx.Value(WebContextKey).(*Context)
84+
return webCtx
8585
}
8686

8787
// ValidateContext is a special context for form validation middleware. It may be different from other contexts.
@@ -135,6 +135,7 @@ func NewWebContext(base *Base, render Render, session session.Store) *Context {
135135
}
136136
ctx.TemplateContext = NewTemplateContextForWeb(ctx)
137137
ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}}
138+
ctx.SetContextValue(WebContextKey, ctx)
138139
return ctx
139140
}
140141

@@ -165,7 +166,7 @@ func Contexter() func(next http.Handler) http.Handler {
165166
ctx.PageData = map[string]any{}
166167
ctx.Data["PageData"] = ctx.PageData
167168

168-
ctx.Base.SetContextValue(WebContextKey, ctx)
169+
ctx.Base.SetContextValue(WebContextKey, ctx) // FIXME: this should be removed because NewWebContext should already set it
169170
ctx.Csrf = NewCSRFProtector(csrfOpts)
170171

171172
// get the last flash message from cookie

services/context/package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func PackageContexter() func(next http.Handler) http.Handler {
156156
base := NewBaseContext(resp, req)
157157
// it is still needed when rendering 500 page in a package handler
158158
ctx := NewWebContext(base, renderer, nil)
159-
ctx.SetContextValue(WebContextKey, ctx)
159+
ctx.SetContextValue(WebContextKey, ctx) // FIXME: this should be removed because NewWebContext should already set it
160160
next.ServeHTTP(ctx.Resp, ctx.Req)
161161
})
162162
}

services/contexttest/context_tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func MockContext(t *testing.T, reqPath string, opts ...MockContextOption) (*cont
6767

6868
chiCtx := chi.NewRouteContext()
6969
ctx := context.NewWebContext(base, opt.Render, nil)
70-
ctx.SetContextValue(context.WebContextKey, ctx)
70+
ctx.SetContextValue(context.WebContextKey, ctx) // FIXME: this should be removed because NewWebContext should already set it
7171
ctx.SetContextValue(chi.RouteCtxKey, chiCtx)
7272
if opt.SessionStore != nil {
7373
ctx.SetContextValue(session.MockStoreContextKey, opt.SessionStore)

services/markup/renderhelper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func FormalRenderHelperFuncs() *markup.RenderHelperFuncs {
2121
return false
2222
}
2323

24-
giteaCtx, ok := ctx.(*gitea_context.Context)
25-
if !ok {
24+
giteaCtx := gitea_context.GetWebContext(ctx)
25+
if giteaCtx == nil {
2626
// when using general context, use user's visibility to check
2727
return mentionedUser.Visibility.IsPublic()
2828
}

services/markup/renderhelper_codepreview.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePrevie
3636
return "", err
3737
}
3838

39-
webCtx, ok := ctx.Value(gitea_context.WebContextKey).(*gitea_context.Context)
40-
if !ok {
39+
webCtx := gitea_context.GetWebContext(ctx)
40+
if webCtx == nil {
4141
return "", fmt.Errorf("context is not a web context")
4242
}
4343
doer := webCtx.Doer

services/markup/renderhelper_issueicontitle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
)
1919

2020
func renderRepoIssueIconTitle(ctx context.Context, opts markup.RenderIssueIconTitleOptions) (_ template.HTML, err error) {
21-
webCtx, ok := ctx.Value(gitea_context.WebContextKey).(*gitea_context.Context)
22-
if !ok {
21+
webCtx := gitea_context.GetWebContext(ctx)
22+
if webCtx == nil {
2323
return "", fmt.Errorf("context is not a web context")
2424
}
2525

0 commit comments

Comments
 (0)