mirror of
https://github.com/go-gitea/gitea.git
synced 2025-10-04 15:56:28 +00:00

the middleware that creates the session provider just panics if on creation the config is wrong. this is not catched and so you just get an cryptic stacktrace with no point where to look at (as user). ## Before ``` 2025/09/16 03:56:37 ...xer/stats/indexer.go:87:populateRepoIndexer() [I] Done (re)populating the repo stats indexer with existing repositories 2025/09/16 03:56:37 modules/ssh/ssh.go:387:Listen() [I] Adding SSH host key: /var/lib/gitea/data/ssh/gitea.rsa 2025/09/16 03:56:37 modules/ssh/init.go:26:Init() [I] SSH server started on :1234. Cipher list ([chacha20-poly1305@openssh.com aes128-ctr aes192-ctr aes256-ctr aes128-gcm@openssh.com aes256-gcm@openssh.com]), key exchange algorithms ([curve25519-sha256 ecdh-sha2-nistp256 ecdh-sha2-nistp384 ecdh-sha2-nistp521 diffie-hellman-group14-sha256 diffie-hellman-group14-sha1]), MACs ([hmac-sha2-256-etm@openssh.com hmac-sha2-256 hmac-sha1]) 2025/09/16 03:56:37 ...s/graceful/server.go:50:NewServer() [I] Starting new SSH server: tcp::1234 on PID: 83337 2025/09/16 03:56:38 cmd/web.go:231:func1() [F] PANIC: dial tcp 127.0.0.1:6379: connect: connection refused gitea.com/go-chi/session@v0.0.0-20240316035857-16768d98ec96/session.go:239 (0x1cdb908) code.gitea.io/gitea/routers/common/middleware.go:108 (0x2547f5a) code.gitea.io/gitea/routers/web/web.go:270 (0x278b8e9) code.gitea.io/gitea/routers/init.go:185 (0x2850d89) code.gitea.io/gitea/cmd/web.go:211 (0x295c5ad) code.gitea.io/gitea/cmd/web.go:262 (0x295cacb) code.gitea.io/gitea/cmd/main.go:111 (0x2953422) github.com/urfave/cli/v2@v2.27.2/command.go:276 (0x1cc3dfd) github.com/urfave/cli/v2@v2.27.2/command.go:269 (0x1cc4084) github.com/urfave/cli/v2@v2.27.2/app.go:333 (0x1cc086a) github.com/urfave/cli/v2@v2.27.2/app.go:307 (0x2953f18) code.gitea.io/gitea/cmd/main.go:172 (0x2953efc) code.gitea.io/gitea/main.go:46 (0x2998498) runtime/proc.go:283 (0x4471ca) runtime/asm_amd64.s:1700 (0x484a20) ``` ## After ``` 2025/09/22 22:52:35 .../templates/htmlrenderer.go:118:initHTMLRenderer() [D] Creating static HTML Renderer 2025/09/22 22:52:35 routers/web/web.go:273:Routes() [F] common.Sessioner failed: failed to create session middleware: dial tcp 127.0.0.1:6379: connect: connection refused ``` --------- Signed-off-by: 6543 <6543@obermui.de>
204 lines
5.1 KiB
Go
204 lines
5.1 KiB
Go
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package session
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
"gitea.com/go-chi/session"
|
|
couchbase "gitea.com/go-chi/session/couchbase"
|
|
memcache "gitea.com/go-chi/session/memcache"
|
|
mysql "gitea.com/go-chi/session/mysql"
|
|
postgres "gitea.com/go-chi/session/postgres"
|
|
)
|
|
|
|
// VirtualSessionProvider represents a shadowed session provider implementation.
|
|
type VirtualSessionProvider struct {
|
|
lock sync.RWMutex
|
|
provider session.Provider
|
|
}
|
|
|
|
// Init initializes the cookie session provider with the given config.
|
|
func (o *VirtualSessionProvider) Init(gcLifetime int64, config string) error {
|
|
var opts session.Options
|
|
if err := json.Unmarshal([]byte(config), &opts); err != nil {
|
|
return err
|
|
}
|
|
// Note that these options are unprepared so we can't just use NewManager here.
|
|
// Nor can we access the provider map in session.
|
|
// So we will just have to do this by hand.
|
|
// This is only slightly more wrong than modules/setting/session.go:23
|
|
switch opts.Provider {
|
|
case "memory":
|
|
o.provider = &session.MemProvider{}
|
|
case "file":
|
|
o.provider = &session.FileProvider{}
|
|
case "redis":
|
|
o.provider = &RedisProvider{}
|
|
case "db":
|
|
o.provider = &DBProvider{}
|
|
case "mysql":
|
|
o.provider = &mysql.MysqlProvider{}
|
|
case "postgres":
|
|
o.provider = &postgres.PostgresProvider{}
|
|
case "couchbase":
|
|
o.provider = &couchbase.CouchbaseProvider{}
|
|
case "memcache":
|
|
o.provider = &memcache.MemcacheProvider{}
|
|
default:
|
|
return fmt.Errorf("VirtualSessionProvider: Unknown Provider: %s", opts.Provider)
|
|
}
|
|
return o.provider.Init(gcLifetime, opts.ProviderConfig)
|
|
}
|
|
|
|
// Read returns raw session store by session ID.
|
|
func (o *VirtualSessionProvider) Read(sid string) (session.RawStore, error) {
|
|
o.lock.RLock()
|
|
defer o.lock.RUnlock()
|
|
if exist, err := o.provider.Exist(sid); err == nil && exist {
|
|
return o.provider.Read(sid)
|
|
} else if err != nil {
|
|
return nil, fmt.Errorf("check if '%s' exist failed: %w", sid, err)
|
|
}
|
|
kv := make(map[any]any)
|
|
kv["_old_uid"] = "0"
|
|
return NewVirtualStore(o, sid, kv), nil
|
|
}
|
|
|
|
// Exist returns true if session with given ID exists.
|
|
func (o *VirtualSessionProvider) Exist(sid string) (bool, error) {
|
|
return true, nil
|
|
}
|
|
|
|
// Destroy deletes a session by session ID.
|
|
func (o *VirtualSessionProvider) Destroy(sid string) error {
|
|
o.lock.Lock()
|
|
defer o.lock.Unlock()
|
|
return o.provider.Destroy(sid)
|
|
}
|
|
|
|
// Regenerate regenerates a session store from old session ID to new one.
|
|
func (o *VirtualSessionProvider) Regenerate(oldsid, sid string) (session.RawStore, error) {
|
|
o.lock.Lock()
|
|
defer o.lock.Unlock()
|
|
return o.provider.Regenerate(oldsid, sid)
|
|
}
|
|
|
|
// Count counts and returns number of sessions.
|
|
func (o *VirtualSessionProvider) Count() (int, error) {
|
|
o.lock.RLock()
|
|
defer o.lock.RUnlock()
|
|
return o.provider.Count()
|
|
}
|
|
|
|
// GC calls GC to clean expired sessions.
|
|
func (o *VirtualSessionProvider) GC() {
|
|
o.provider.GC()
|
|
}
|
|
|
|
func init() {
|
|
session.Register("VirtualSession", &VirtualSessionProvider{})
|
|
}
|
|
|
|
// VirtualStore represents a virtual session store implementation.
|
|
type VirtualStore struct {
|
|
p *VirtualSessionProvider
|
|
sid string
|
|
lock sync.RWMutex
|
|
data map[any]any
|
|
released bool
|
|
}
|
|
|
|
// NewVirtualStore creates and returns a virtual session store.
|
|
func NewVirtualStore(p *VirtualSessionProvider, sid string, kv map[any]any) *VirtualStore {
|
|
return &VirtualStore{
|
|
p: p,
|
|
sid: sid,
|
|
data: kv,
|
|
}
|
|
}
|
|
|
|
// Set sets value to given key in session.
|
|
func (s *VirtualStore) Set(key, val any) error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
s.data[key] = val
|
|
return nil
|
|
}
|
|
|
|
// Get gets value by given key in session.
|
|
func (s *VirtualStore) Get(key any) any {
|
|
s.lock.RLock()
|
|
defer s.lock.RUnlock()
|
|
|
|
return s.data[key]
|
|
}
|
|
|
|
// Delete delete a key from session.
|
|
func (s *VirtualStore) Delete(key any) error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
delete(s.data, key)
|
|
return nil
|
|
}
|
|
|
|
// ID returns current session ID.
|
|
func (s *VirtualStore) ID() string {
|
|
return s.sid
|
|
}
|
|
|
|
// Release releases resource and save data to provider.
|
|
func (s *VirtualStore) Release() error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
// Now need to lock the provider
|
|
s.p.lock.Lock()
|
|
defer s.p.lock.Unlock()
|
|
if oldUID, ok := s.data["_old_uid"]; (ok && (oldUID != "0" || len(s.data) > 1)) || (!ok && len(s.data) > 0) {
|
|
// Now ensure that we don't exist!
|
|
realProvider := s.p.provider
|
|
|
|
if !s.released {
|
|
if exist, err := realProvider.Exist(s.sid); err == nil && exist {
|
|
// This is an error!
|
|
return fmt.Errorf("new sid '%s' already exists", s.sid)
|
|
} else if err != nil {
|
|
return fmt.Errorf("check if '%s' exist failed: %w", s.sid, err)
|
|
}
|
|
}
|
|
realStore, err := realProvider.Read(s.sid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := realStore.Flush(); err != nil {
|
|
return err
|
|
}
|
|
for key, value := range s.data {
|
|
if err := realStore.Set(key, value); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
err = realStore.Release()
|
|
if err == nil {
|
|
s.released = true
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Flush deletes all session data.
|
|
func (s *VirtualStore) Flush() error {
|
|
s.lock.Lock()
|
|
defer s.lock.Unlock()
|
|
|
|
s.data = make(map[any]any)
|
|
return nil
|
|
}
|