From 8fa39258744635e8c28f61b96aca27a762dfa36b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 11 Feb 2025 19:46:27 +0800 Subject: [PATCH] Fix context usage (#33554) (#33557) Backport #33554 --- routers/web/web.go | 2 +- services/auth/auth.go | 2 +- services/auth/sspi.go | 2 +- services/context/context.go | 7 ++++--- services/markup/processorhelper.go | 4 ++-- services/markup/processorhelper_codepreview.go | 4 ++-- 6 files changed, 11 insertions(+), 10 deletions(-) diff --git a/routers/web/web.go b/routers/web/web.go index 70355448975..ae5f51d4038 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1635,7 +1635,7 @@ func registerRoutes(m *web.Router) { } m.NotFound(func(w http.ResponseWriter, req *http.Request) { - ctx := context.GetWebContext(req) + ctx := context.GetWebContext(req.Context()) routing.UpdateFuncInfo(ctx, routing.GetFuncInfo(ctx.NotFound, "WebNotFound")) ctx.NotFound("", nil) }) diff --git a/services/auth/auth.go b/services/auth/auth.go index 43ff95f0530..5e186d7e693 100644 --- a/services/auth/auth.go +++ b/services/auth/auth.go @@ -104,7 +104,7 @@ func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore middleware.SetLocaleCookie(resp, user.Language, 0) // force to generate a new CSRF token - if ctx := gitea_context.GetWebContext(req); ctx != nil { + if ctx := gitea_context.GetWebContext(req.Context()); ctx != nil { ctx.Csrf.PrepareForSessionUser(ctx) } } diff --git a/services/auth/sspi.go b/services/auth/sspi.go index 7f8a03a4c67..0152f18c748 100644 --- a/services/auth/sspi.go +++ b/services/auth/sspi.go @@ -89,7 +89,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore, store.GetData()["EnableSSPI"] = true // in this case, the Verify function is called in Gitea's web context // FIXME: it doesn't look good to render the page here, why not redirect? - gitea_context.GetWebContext(req).HTML(http.StatusUnauthorized, tplSignIn) + gitea_context.GetWebContext(req.Context()).HTML(http.StatusUnauthorized, tplSignIn) return nil, err } if outToken != "" { diff --git a/services/context/context.go b/services/context/context.go index 812a8c27eeb..2f6a4657405 100644 --- a/services/context/context.go +++ b/services/context/context.go @@ -77,9 +77,9 @@ type webContextKeyType struct{} var WebContextKey = webContextKeyType{} -func GetWebContext(req *http.Request) *Context { - ctx, _ := req.Context().Value(WebContextKey).(*Context) - return ctx +func GetWebContext(ctx context.Context) *Context { + webCtx, _ := ctx.Value(WebContextKey).(*Context) + return webCtx } // ValidateContext is a special context for form validation middleware. It may be different from other contexts. @@ -133,6 +133,7 @@ func NewWebContext(base *Base, render Render, session session.Store) *Context { } ctx.TemplateContext = NewTemplateContextForWeb(ctx) ctx.Flash = &middleware.Flash{DataStore: ctx, Values: url.Values{}} + ctx.AppendContextValue(WebContextKey, ctx) return ctx } diff --git a/services/markup/processorhelper.go b/services/markup/processorhelper.go index 1f1abf496a3..b4ad2de711b 100644 --- a/services/markup/processorhelper.go +++ b/services/markup/processorhelper.go @@ -20,8 +20,8 @@ func ProcessorHelper() *markup.RenderHelperFuncs { return false } - giteaCtx, ok := ctx.(*gitea_context.Context) - if !ok { + giteaCtx := gitea_context.GetWebContext(ctx) + if giteaCtx == nil { // when using general context, use user's visibility to check return mentionedUser.Visibility.IsPublic() } diff --git a/services/markup/processorhelper_codepreview.go b/services/markup/processorhelper_codepreview.go index 0500e57e461..cecfadae865 100644 --- a/services/markup/processorhelper_codepreview.go +++ b/services/markup/processorhelper_codepreview.go @@ -35,8 +35,8 @@ func renderRepoFileCodePreview(ctx context.Context, opts markup.RenderCodePrevie return "", err } - webCtx, ok := ctx.Value(gitea_context.WebContextKey).(*gitea_context.Context) - if !ok { + webCtx := gitea_context.GetWebContext(ctx) + if webCtx == nil { return "", fmt.Errorf("context is not a web context") } doer := webCtx.Doer