mirror of
https://github.com/go-gitea/gitea
synced 2025-12-06 21:08:25 +00:00
Fix oauth2 session gob register (#36017)
`gob.Register` must be called before Sessioner Fix #36016
This commit is contained in:
@@ -5,6 +5,7 @@ package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -107,7 +108,11 @@ func ForwardedHeadersHandler(limit int, trustedProxies []string) func(h http.Han
|
||||
return proxy.ForwardedHeaders(opt)
|
||||
}
|
||||
|
||||
func Sessioner() (func(next http.Handler) http.Handler, error) {
|
||||
func MustInitSessioner() func(next http.Handler) http.Handler {
|
||||
// TODO: CHI-SESSION-GOB-REGISTER: chi-session has a design problem: it calls gob.Register for "Set"
|
||||
// But if the server restarts, then the first "Get" will fail to decode the previously stored session data because the structs are not registered yet.
|
||||
// So each package should make sure their structs are registered correctly during startup for session storage.
|
||||
|
||||
middleware, err := session.Sessioner(session.Options{
|
||||
Provider: setting.SessionConfig.Provider,
|
||||
ProviderConfig: setting.SessionConfig.ProviderConfig,
|
||||
@@ -120,8 +125,7 @@ func Sessioner() (func(next http.Handler) http.Handler, error) {
|
||||
Domain: setting.SessionConfig.Domain,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create session middleware: %w", err)
|
||||
log.Fatalf("common.Sessioner failed: %v", err)
|
||||
}
|
||||
|
||||
return middleware, nil
|
||||
return middleware
|
||||
}
|
||||
|
||||
@@ -55,8 +55,8 @@ func getSupportedDbTypeNames() (dbTypeNames []map[string]string) {
|
||||
return dbTypeNames
|
||||
}
|
||||
|
||||
// Contexter prepare for rendering installation page
|
||||
func Contexter() func(next http.Handler) http.Handler {
|
||||
// installContexter prepare for rendering installation page
|
||||
func installContexter() func(next http.Handler) http.Handler {
|
||||
rnd := templates.HTMLRenderer()
|
||||
dbTypeNames := getSupportedDbTypeNames()
|
||||
envConfigKeys := setting.CollectEnvConfigKeys()
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"html"
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/public"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
@@ -25,11 +24,8 @@ func Routes() *web.Router {
|
||||
base.Methods("GET, HEAD", "/assets/*", public.FileHandlerFunc())
|
||||
|
||||
r := web.NewRouter()
|
||||
if sessionMid, err := common.Sessioner(); err == nil && sessionMid != nil {
|
||||
r.Use(sessionMid, Contexter())
|
||||
} else {
|
||||
log.Fatal("common.Sessioner failed: %v", err)
|
||||
}
|
||||
r.Use(common.MustInitSessioner(), installContexter())
|
||||
|
||||
r.Get("/", Install) // it must be on the root, because the "install.js" use the window.location to replace the "localhost" AppURL
|
||||
r.Post("/", web.Bind(forms.InstallForm{}), SubmitInstall)
|
||||
r.Get("/post-install", InstallDone)
|
||||
|
||||
@@ -277,8 +277,11 @@ type LinkAccountData struct {
|
||||
GothUser goth.User
|
||||
}
|
||||
|
||||
func init() {
|
||||
gob.Register(LinkAccountData{}) // TODO: CHI-SESSION-GOB-REGISTER
|
||||
}
|
||||
|
||||
func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
|
||||
gob.Register(LinkAccountData{})
|
||||
v, ok := ctx.Session.Get("linkAccountData").(LinkAccountData)
|
||||
if !ok {
|
||||
return nil
|
||||
@@ -287,7 +290,6 @@ func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
|
||||
}
|
||||
|
||||
func Oauth2SetLinkAccountData(ctx *context.Context, linkAccountData LinkAccountData) error {
|
||||
gob.Register(LinkAccountData{})
|
||||
return updateSession(ctx, nil, map[string]any{
|
||||
"linkAccountData": linkAccountData,
|
||||
})
|
||||
|
||||
@@ -267,11 +267,7 @@ func Routes() *web.Router {
|
||||
routes.Get("/ssh_info", misc.SSHInfo)
|
||||
routes.Get("/api/healthz", healthcheck.Check)
|
||||
|
||||
if sessionMid, err := common.Sessioner(); err == nil && sessionMid != nil {
|
||||
mid = append(mid, sessionMid, context.Contexter())
|
||||
} else {
|
||||
log.Fatal("common.Sessioner failed: %v", err)
|
||||
}
|
||||
mid = append(mid, common.MustInitSessioner(), context.Contexter())
|
||||
|
||||
// Get user from session if logged in.
|
||||
mid = append(mid, webAuth(buildAuthGroup()))
|
||||
|
||||
Reference in New Issue
Block a user