Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
gitea.com/go-chi/binding v0.0.0-20240430071103-39a851e106ed
gitea.com/go-chi/cache v0.2.1
gitea.com/go-chi/captcha v0.0.0-20240315150714-fb487f629098
gitea.com/go-chi/session v0.0.0-20240316035857-16768d98ec96
gitea.com/go-chi/session v0.0.0-20250926004215-636cadd82e15
gitea.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96
gitea.com/lunny/levelqueue v0.4.2-0.20230414023320-3c0159fe0fe4
github.com/42wim/httpsig v1.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ gitea.com/go-chi/cache v0.2.1 h1:bfAPkvXlbcZxPCpcmDVCWoHgiBSBmZN/QosnZvEC0+g=
gitea.com/go-chi/cache v0.2.1/go.mod h1:Qic0HZ8hOHW62ETGbonpwz8WYypj9NieU9659wFUJ8Q=
gitea.com/go-chi/captcha v0.0.0-20240315150714-fb487f629098 h1:p2ki+WK0cIeNQuqjR98IP2KZQKRzJJiV7aTeMAFwaWo=
gitea.com/go-chi/captcha v0.0.0-20240315150714-fb487f629098/go.mod h1:LjzIOHlRemuUyO7WR12fmm18VZIlCAaOt9L3yKw40pk=
gitea.com/go-chi/session v0.0.0-20240316035857-16768d98ec96 h1:IFDiMBObsP6CZIRaDLd54SR6zPYAffPXiXck5Xslu0Q=
gitea.com/go-chi/session v0.0.0-20240316035857-16768d98ec96/go.mod h1:0iEpFKnwO5dG0aF98O4eq6FMsAiXkNBaDIlUOlq4BtM=
gitea.com/go-chi/session v0.0.0-20250926004215-636cadd82e15 h1:qFYmz05u/s9664o7+XEgrlHXSPQ4uHO8/ccZGUb1uxA=
gitea.com/go-chi/session v0.0.0-20250926004215-636cadd82e15/go.mod h1:0iEpFKnwO5dG0aF98O4eq6FMsAiXkNBaDIlUOlq4BtM=
gitea.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96 h1:+wWBi6Qfruqu7xJgjOIrKVQGiLUZdpKYCZewJ4clqhw=
gitea.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96/go.mod h1:VyMQP6ue6MKHM8UsOXfNfuMKD0oSAWZdXVcpHIN2yaY=
gitea.com/lunny/levelqueue v0.4.2-0.20230414023320-3c0159fe0fe4 h1:IFT+hup2xejHqdhS7keYWioqfmxdnfblFDTGoOwcZ+o=
Expand Down
13 changes: 7 additions & 6 deletions modules/session/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import (
"context"
"fmt"
"log"
"sync"

Expand Down Expand Up @@ -121,12 +122,12 @@
}

// Exist returns true if session with given ID exists.
func (p *DBProvider) Exist(sid string) bool {
func (p *DBProvider) Exist(sid string) (bool, error) {
has, err := auth.ExistSession(dbContext(), sid)
if err != nil {
panic("session/DB: error checking existence: " + err.Error())
return false, fmt.Errorf("session/DB: error checking existence: %w", err)
}
return has
return has, nil
}

// Destroy deletes a session by session ID.
Expand Down Expand Up @@ -155,12 +156,12 @@
}

// Count counts and returns number of sessions.
func (p *DBProvider) Count() int {
func (p *DBProvider) Count() (int, error) {
total, err := auth.CountSessions(dbContext())
if err != nil {
panic("session/DB: error counting records: " + err.Error())
fmt.Errorf("session/DB: error counting records: %w", err)

Check failure on line 162 in modules/session/db.go

View workflow job for this annotation

GitHub Actions / lint-backend

Error return value of `fmt.Errorf` is not checked (errcheck)

Check failure on line 162 in modules/session/db.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

Error return value of `fmt.Errorf` is not checked (errcheck)

Check failure on line 162 in modules/session/db.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

Error return value of `fmt.Errorf` is not checked (errcheck)
}
return int(total)
return int(total), nil
}

// GC calls GC to clean expired sessions.
Expand Down
31 changes: 19 additions & 12 deletions modules/session/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,12 @@
// Read returns raw session store by session ID.
func (p *RedisProvider) Read(sid string) (session.RawStore, error) {
psid := p.prefix + sid
if !p.Exist(sid) {
if exist, err := p.Exist(sid); err == nil && !exist {
if err := p.c.Set(graceful.GetManager().HammerContext(), psid, "", p.duration).Err(); err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}

var kv map[any]any
Expand All @@ -159,9 +161,9 @@
}

// Exist returns true if session with given ID exists.
func (p *RedisProvider) Exist(sid string) bool {
func (p *RedisProvider) Exist(sid string) (bool, error) {
v, err := p.c.Exists(graceful.GetManager().HammerContext(), p.prefix+sid).Result()
return err == nil && v == 1
return err == nil && v == 1, err
}

// Destroy deletes a session by session ID.
Expand All @@ -174,11 +176,19 @@
poldsid := p.prefix + oldsid
psid := p.prefix + sid

if p.Exist(sid) {
exist, err := p.Exist(sid)
if err != nil {
return nil, err
}
if exist {
return nil, fmt.Errorf("new sid '%s' already exists", sid)
} else if !p.Exist(oldsid) {
// Make a fake old session.
if err = p.c.Set(graceful.GetManager().HammerContext(), poldsid, "", p.duration).Err(); err != nil {
} else {

Check failure on line 185 in modules/session/redis.go

View workflow job for this annotation

GitHub Actions / lint-backend

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 185 in modules/session/redis.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)

Check failure on line 185 in modules/session/redis.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (revive)
if exist, err := p.Exist(oldsid); err == nil && !exist {
// Make a fake old session.
if err = p.c.Set(graceful.GetManager().HammerContext(), poldsid, "", p.duration).Err(); err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -211,12 +221,9 @@
}

// Count counts and returns number of sessions.
func (p *RedisProvider) Count() int {
func (p *RedisProvider) Count() (int, error) {
size, err := p.c.DBSize(graceful.GetManager().HammerContext()).Result()
if err != nil {
return 0
}
return int(size)
return int(size), err
}

// GC calls GC to clean expired sessions.
Expand Down
20 changes: 13 additions & 7 deletions modules/session/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ func (o *VirtualSessionProvider) Init(gcLifetime int64, config string) error {
func (o *VirtualSessionProvider) Read(sid string) (session.RawStore, error) {
o.lock.RLock()
defer o.lock.RUnlock()
if o.provider.Exist(sid) {
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 {
return true
func (o *VirtualSessionProvider) Exist(sid string) (bool, error) {
return true, nil
}

// Destroy deletes a session by session ID.
Expand All @@ -87,7 +89,7 @@ func (o *VirtualSessionProvider) Regenerate(oldsid, sid string) (session.RawStor
}

// Count counts and returns number of sessions.
func (o *VirtualSessionProvider) Count() int {
func (o *VirtualSessionProvider) Count() (int, error) {
o.lock.RLock()
defer o.lock.RUnlock()
return o.provider.Count()
Expand Down Expand Up @@ -162,9 +164,13 @@ func (s *VirtualStore) Release() error {
// Now ensure that we don't exist!
realProvider := s.p.provider

if !s.released && realProvider.Exist(s.sid) {
// This is an error!
return fmt.Errorf("new sid '%s' already exists", s.sid)
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 {
Expand Down
9 changes: 7 additions & 2 deletions routers/common/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func ForwardedHeadersHandler(limit int, trustedProxies []string) func(h http.Han
return proxy.ForwardedHeaders(opt)
}

func Sessioner() func(next http.Handler) http.Handler {
return session.Sessioner(session.Options{
func Sessioner() (func(next http.Handler) http.Handler, error) {
middleware, err := session.Sessioner(session.Options{
Provider: setting.SessionConfig.Provider,
ProviderConfig: setting.SessionConfig.ProviderConfig,
CookieName: setting.SessionConfig.CookieName,
Expand All @@ -119,4 +119,9 @@ func Sessioner() func(next http.Handler) http.Handler {
SameSite: setting.SessionConfig.SameSite,
Domain: setting.SessionConfig.Domain,
})
if err != nil {
return nil, fmt.Errorf("failed to create session middleware: %w", err)
}

return middleware, nil
}
7 changes: 6 additions & 1 deletion routers/install/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
Expand All @@ -23,7 +24,11 @@ func Routes() *web.Router {
base.Methods("GET, HEAD", "/assets/*", public.FileHandlerFunc())

r := web.NewRouter()
r.Use(common.Sessioner(), Contexter())
if sessionMid, err := common.Sessioner(); err == nil && sessionMid != nil {
r.Use(sessionMid, Contexter())
} else {
log.Fatal("common.Sessioner failed: %v", err)
}
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)
Expand Down
6 changes: 5 additions & 1 deletion routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ func Routes() *web.Router {
routes.Get("/ssh_info", misc.SSHInfo)
routes.Get("/api/healthz", healthcheck.Check)

mid = append(mid, common.Sessioner(), context.Contexter())
if sessionMid, err := common.Sessioner(); err == nil && sessionMid != nil {
mid = append(mid, sessionMid, context.Contexter())
} else {
log.Fatal("common.Sessioner failed: %v", err)
}

// Get user from session if logged in.
mid = append(mid, webAuth(buildAuthGroup()))
Expand Down
Loading