Skip to content

Commit 7900b6c

Browse files
committed
fix
1 parent 688430e commit 7900b6c

File tree

7 files changed

+21
-25
lines changed

7 files changed

+21
-25
lines changed

modules/auth/webauthn/webauthn.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package webauthn
66
import (
77
"context"
88
"encoding/binary"
9-
"encoding/gob"
109

1110
"code.gitea.io/gitea/models/auth"
1211
user_model "code.gitea.io/gitea/models/user"
@@ -22,8 +21,6 @@ var WebAuthn *webauthn.WebAuthn
2221

2322
// Init initializes the WebAuthn instance from the config.
2423
func Init() {
25-
gob.Register(&webauthn.SessionData{})
26-
2724
appURL, _ := protocol.FullyQualifiedOrigin(setting.AppURL)
2825

2926
WebAuthn = &webauthn.WebAuthn{

routers/common/middleware.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package common
55

66
import (
7+
"encoding/gob"
78
"fmt"
9+
"log"
810
"net/http"
911
"strings"
1012

@@ -14,11 +16,14 @@ import (
1416
"code.gitea.io/gitea/modules/reqctx"
1517
"code.gitea.io/gitea/modules/setting"
1618
"code.gitea.io/gitea/modules/web/routing"
19+
"code.gitea.io/gitea/routers/web/auth"
1720
"code.gitea.io/gitea/services/context"
1821

1922
"gitea.com/go-chi/session"
2023
"github.com/chi-middleware/proxy"
2124
"github.com/go-chi/chi/v5"
25+
"github.com/go-webauthn/webauthn/webauthn"
26+
"github.com/gorilla/sessions"
2227
)
2328

2429
// ProtocolMiddlewares returns HTTP protocol related middlewares, and it provides a global panic recovery
@@ -107,7 +112,14 @@ func ForwardedHeadersHandler(limit int, trustedProxies []string) func(h http.Han
107112
return proxy.ForwardedHeaders(opt)
108113
}
109114

110-
func Sessioner() (func(next http.Handler) http.Handler, error) {
115+
func MuseInitSessioner() func(next http.Handler) http.Handler {
116+
// TODO: chi-session has a design problem: it calls gob.Register for "Set"
117+
// 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.
118+
// So here we register all session structs ahead.
119+
gob.Register(auth.LinkAccountData{}) // used by oauth link account
120+
gob.Register(&webauthn.SessionData{}) // used by webauthn
121+
gob.Register(&sessions.Session{}) // used by oauth2's SessionsStore. FIXME: it seems to be an abuse, why the Session struct itself is stored in session store again?
122+
111123
middleware, err := session.Sessioner(session.Options{
112124
Provider: setting.SessionConfig.Provider,
113125
ProviderConfig: setting.SessionConfig.ProviderConfig,
@@ -120,8 +132,7 @@ func Sessioner() (func(next http.Handler) http.Handler, error) {
120132
Domain: setting.SessionConfig.Domain,
121133
})
122134
if err != nil {
123-
return nil, fmt.Errorf("failed to create session middleware: %w", err)
135+
log.Fatal("common.Sessioner failed: %v", err)
124136
}
125-
126-
return middleware, nil
137+
return middleware
127138
}

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.MuseInitSessioner(), 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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package auth
55

66
import (
7-
"encoding/gob"
87
"errors"
98
"fmt"
109
"html"
@@ -278,7 +277,7 @@ type LinkAccountData struct {
278277
}
279278

280279
func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
281-
gob.Register(LinkAccountData{})
280+
282281
v, ok := ctx.Session.Get("linkAccountData").(LinkAccountData)
283282
if !ok {
284283
return nil
@@ -287,7 +286,6 @@ func oauth2GetLinkAccountData(ctx *context.Context) *LinkAccountData {
287286
}
288287

289288
func Oauth2SetLinkAccountData(ctx *context.Context, linkAccountData LinkAccountData) error {
290-
gob.Register(LinkAccountData{})
291289
return updateSession(ctx, nil, map[string]any{
292290
"linkAccountData": linkAccountData,
293291
})

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.MuseInitSessioner(), 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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ func Init(ctx context.Context) error {
3333
// Lock our mutex
3434
gothRWMutex.Lock()
3535

36-
gob.Register(&sessions.Session{})
37-
3836
gothic.Store = &SessionsStore{
3937
maxLength: int64(setting.OAuth2.MaxTokenLength),
4038
}

0 commit comments

Comments
 (0)