Skip to content

Commit e2cfadc

Browse files
committed
fix
1 parent 688430e commit e2cfadc

File tree

7 files changed

+19
-24
lines changed

7 files changed

+19
-24
lines changed

modules/auth/webauthn/webauthn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var WebAuthn *webauthn.WebAuthn
2222

2323
// Init initializes the WebAuthn instance from the config.
2424
func Init() {
25-
gob.Register(&webauthn.SessionData{})
25+
gob.Register(&webauthn.SessionData{}) // TODO: CHI-SESSION-GOB-REGISTER.
2626

2727
appURL, _ := protocol.FullyQualifiedOrigin(setting.AppURL)
2828

routers/common/middleware.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package common
55

66
import (
77
"fmt"
8+
"log"
89
"net/http"
910
"strings"
1011

@@ -107,7 +108,11 @@ func ForwardedHeadersHandler(limit int, trustedProxies []string) func(h http.Han
107108
return proxy.ForwardedHeaders(opt)
108109
}
109110

110-
func Sessioner() (func(next http.Handler) http.Handler, error) {
111+
func MustInitSessioner() func(next http.Handler) http.Handler {
112+
// TODO: CHI-SESSION-GOB-REGISTER: chi-session has a design problem: it calls gob.Register for "Set"
113+
// 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.
114+
// So each package should make sure their structs are registered correctly during startup for session storage.
115+
111116
middleware, err := session.Sessioner(session.Options{
112117
Provider: setting.SessionConfig.Provider,
113118
ProviderConfig: setting.SessionConfig.ProviderConfig,
@@ -120,8 +125,7 @@ func Sessioner() (func(next http.Handler) http.Handler, error) {
120125
Domain: setting.SessionConfig.Domain,
121126
})
122127
if err != nil {
123-
return nil, fmt.Errorf("failed to create session middleware: %w", err)
128+
log.Fatalf("common.Sessioner failed: %v", err)
124129
}
125-
126-
return middleware, nil
130+
return middleware
127131
}

routers/install/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ func getSupportedDbTypeNames() (dbTypeNames []map[string]string) {
5555
return dbTypeNames
5656
}
5757

58-
// Contexter prepare for rendering installation page
59-
func Contexter() func(next http.Handler) http.Handler {
58+
// installContexter prepare for rendering installation page
59+
func installContexter() func(next http.Handler) http.Handler {
6060
rnd := templates.HTMLRenderer()
6161
dbTypeNames := getSupportedDbTypeNames()
6262
envConfigKeys := setting.CollectEnvConfigKeys()

routers/install/routes.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"html"
99
"net/http"
1010

11-
"code.gitea.io/gitea/modules/log"
1211
"code.gitea.io/gitea/modules/public"
1312
"code.gitea.io/gitea/modules/setting"
1413
"code.gitea.io/gitea/modules/web"
@@ -25,11 +24,8 @@ func Routes() *web.Router {
2524
base.Methods("GET, HEAD", "/assets/*", public.FileHandlerFunc())
2625

2726
r := web.NewRouter()
28-
if sessionMid, err := common.Sessioner(); err == nil && sessionMid != nil {
29-
r.Use(sessionMid, Contexter())
30-
} else {
31-
log.Fatal("common.Sessioner failed: %v", err)
32-
}
27+
r.Use(common.MustInitSessioner(), installContexter())
28+
3329
r.Get("/", Install) // it must be on the root, because the "install.js" use the window.location to replace the "localhost" AppURL
3430
r.Post("/", web.Bind(forms.InstallForm{}), SubmitInstall)
3531
r.Get("/post-install", InstallDone)

routers/web/auth/oauth.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,11 @@ type LinkAccountData struct {
277277
GothUser goth.User
278278
}
279279

280+
func init() {
281+
gob.Register(LinkAccountData{}) // TODO: CHI-SESSION-GOB-REGISTER
282+
}
283+
280284
func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
281-
gob.Register(LinkAccountData{})
282285
v, ok := ctx.Session.Get("linkAccountData").(LinkAccountData)
283286
if !ok {
284287
return nil
@@ -287,7 +290,6 @@ func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
287290
}
288291

289292
func Oauth2SetLinkAccountData(ctx *context.Context, linkAccountData LinkAccountData) error {
290-
gob.Register(LinkAccountData{})
291293
return updateSession(ctx, nil, map[string]any{
292294
"linkAccountData": linkAccountData,
293295
})

routers/web/web.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,7 @@ func Routes() *web.Router {
267267
routes.Get("/ssh_info", misc.SSHInfo)
268268
routes.Get("/api/healthz", healthcheck.Check)
269269

270-
if sessionMid, err := common.Sessioner(); err == nil && sessionMid != nil {
271-
mid = append(mid, sessionMid, context.Contexter())
272-
} else {
273-
log.Fatal("common.Sessioner failed: %v", err)
274-
}
270+
mid = append(mid, common.MustInitSessioner(), context.Contexter())
275271

276272
// Get user from session if logged in.
277273
mid = append(mid, webAuth(buildAuthGroup()))

services/auth/source/oauth2/init.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ import (
2222

2323
var gothRWMutex = sync.RWMutex{}
2424

25-
// UsersStoreKey is the key for the store
26-
const UsersStoreKey = "gitea-oauth2-sessions"
27-
2825
// ProviderHeaderKey is the HTTP header key
2926
const ProviderHeaderKey = "gitea-oauth2-provider"
3027

@@ -33,7 +30,7 @@ func Init(ctx context.Context) error {
3330
// Lock our mutex
3431
gothRWMutex.Lock()
3532

36-
gob.Register(&sessions.Session{})
33+
gob.Register(&sessions.Session{}) // TODO: CHI-SESSION-GOB-REGISTER. FIXME: it seems to be an abuse, why the Session struct itself is stored in session store again?
3734

3835
gothic.Store = &SessionsStore{
3936
maxLength: int64(setting.OAuth2.MaxTokenLength),

0 commit comments

Comments
 (0)