summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2023-10-14 02:56:41 +0200
committerGitHub <noreply@github.com>2023-10-14 00:56:41 +0000
commitc6c829fe3fde5d55b2115eb006b427288e381158 (patch)
treed2429a0bfd72836375262137e0709995889c6924 /routers
parentee6a390675638b9c0f587d861e7063b9e633540a (diff)
downloadgitea-c6c829fe3fde5d55b2115eb006b427288e381158.tar.gz
gitea-c6c829fe3fde5d55b2115eb006b427288e381158.zip
Enhanced auth token / remember me (#27606)
Closes #27455 > The mechanism responsible for long-term authentication (the 'remember me' cookie) uses a weak construction technique. It will hash the user's hashed password and the rands value; it will then call the secure cookie code, which will encrypt the user's name with the computed hash. If one were able to dump the database, they could extract those two values to rebuild that cookie and impersonate a user. That vulnerability exists from the date the dump was obtained until a user changed their password. > > To fix this security issue, the cookie could be created and verified using a different technique such as the one explained at https://paragonie.com/blog/2015/04/secure-authentication-php-with-long-term-persistence#secure-remember-me-cookies. The PR removes the now obsolete setting `COOKIE_USERNAME`.
Diffstat (limited to 'routers')
-rw-r--r--routers/install/install.go12
-rw-r--r--routers/web/auth/2fa.go6
-rw-r--r--routers/web/auth/auth.go64
-rw-r--r--routers/web/auth/openid.go19
-rw-r--r--routers/web/auth/webauthn.go3
-rw-r--r--routers/web/home.go3
-rw-r--r--routers/web/web.go2
7 files changed, 54 insertions, 55 deletions
diff --git a/routers/install/install.go b/routers/install/install.go
index 185e4bf6bf..5c0290d2cc 100644
--- a/routers/install/install.go
+++ b/routers/install/install.go
@@ -27,12 +27,14 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
+ "code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/translation"
"code.gitea.io/gitea/modules/user"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/routers/common"
+ auth_service "code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/forms"
"gitea.com/go-chi/session"
@@ -547,11 +549,13 @@ func SubmitInstall(ctx *context.Context) {
u, _ = user_model.GetUserByName(ctx, u.Name)
}
- days := 86400 * setting.LogInRememberDays
- ctx.SetSiteCookie(setting.CookieUserName, u.Name, days)
+ nt, token, err := auth_service.CreateAuthTokenForUserID(ctx, u.ID)
+ if err != nil {
+ ctx.ServerError("CreateAuthTokenForUserID", err)
+ return
+ }
- ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
- setting.CookieRememberName, u.Name, days)
+ ctx.SetSiteCookie(setting.CookieRememberName, nt.ID+":"+token, setting.LogInRememberDays*timeutil.Day)
// Auto-login for admin
if err = ctx.Session.Set("uid", u.ID); err != nil {
diff --git a/routers/web/auth/2fa.go b/routers/web/auth/2fa.go
index bc3cb4907c..dc0062ebaa 100644
--- a/routers/web/auth/2fa.go
+++ b/routers/web/auth/2fa.go
@@ -26,8 +26,7 @@ var (
func TwoFactor(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("twofa")
- // Check auto-login.
- if checkAutoLogin(ctx) {
+ if CheckAutoLogin(ctx) {
return
}
@@ -99,8 +98,7 @@ func TwoFactorPost(ctx *context.Context) {
func TwoFactorScratch(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("twofa_scratch")
- // Check auto-login.
- if checkAutoLogin(ctx) {
+ if CheckAutoLogin(ctx) {
return
}
diff --git a/routers/web/auth/auth.go b/routers/web/auth/auth.go
index df835a2fa1..1238e52755 100644
--- a/routers/web/auth/auth.go
+++ b/routers/web/auth/auth.go
@@ -43,41 +43,52 @@ const (
TplActivate base.TplName = "user/auth/activate"
)
-// AutoSignIn reads cookie and try to auto-login.
-func AutoSignIn(ctx *context.Context) (bool, error) {
+// autoSignIn reads cookie and try to auto-login.
+func autoSignIn(ctx *context.Context) (bool, error) {
if !db.HasEngine {
return false, nil
}
- uname := ctx.GetSiteCookie(setting.CookieUserName)
- if len(uname) == 0 {
- return false, nil
- }
-
isSucceed := false
defer func() {
if !isSucceed {
- log.Trace("auto-login cookie cleared: %s", uname)
- ctx.DeleteSiteCookie(setting.CookieUserName)
ctx.DeleteSiteCookie(setting.CookieRememberName)
}
}()
- u, err := user_model.GetUserByName(ctx, uname)
+ if err := auth.DeleteExpiredAuthTokens(ctx); err != nil {
+ log.Error("Failed to delete expired auth tokens: %v", err)
+ }
+
+ t, err := auth_service.CheckAuthToken(ctx, ctx.GetSiteCookie(setting.CookieRememberName))
if err != nil {
- if !user_model.IsErrUserNotExist(err) {
- return false, fmt.Errorf("GetUserByName: %w", err)
+ switch err {
+ case auth_service.ErrAuthTokenInvalidFormat, auth_service.ErrAuthTokenExpired:
+ return false, nil
}
+ return false, err
+ }
+ if t == nil {
return false, nil
}
- if val, ok := ctx.GetSuperSecureCookie(
- base.EncodeMD5(u.Rands+u.Passwd), setting.CookieRememberName); !ok || val != u.Name {
+ u, err := user_model.GetUserByID(ctx, t.UserID)
+ if err != nil {
+ if !user_model.IsErrUserNotExist(err) {
+ return false, fmt.Errorf("GetUserByID: %w", err)
+ }
return false, nil
}
isSucceed = true
+ nt, token, err := auth_service.RegenerateAuthToken(ctx, t)
+ if err != nil {
+ return false, err
+ }
+
+ ctx.SetSiteCookie(setting.CookieRememberName, nt.ID+":"+token, setting.LogInRememberDays*timeutil.Day)
+
if err := updateSession(ctx, nil, map[string]any{
// Set session IDs
"uid": u.ID,
@@ -113,11 +124,15 @@ func resetLocale(ctx *context.Context, u *user_model.User) error {
return nil
}
-func checkAutoLogin(ctx *context.Context) bool {
+func CheckAutoLogin(ctx *context.Context) bool {
// Check auto-login
- isSucceed, err := AutoSignIn(ctx)
+ isSucceed, err := autoSignIn(ctx)
if err != nil {
- ctx.ServerError("AutoSignIn", err)
+ if errors.Is(err, auth_service.ErrAuthTokenInvalidHash) {
+ ctx.Flash.Error(ctx.Tr("auth.remember_me.compromised"), true)
+ return false
+ }
+ ctx.ServerError("autoSignIn", err)
return true
}
@@ -141,8 +156,7 @@ func checkAutoLogin(ctx *context.Context) bool {
func SignIn(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("sign_in")
- // Check auto-login
- if checkAutoLogin(ctx) {
+ if CheckAutoLogin(ctx) {
return
}
@@ -290,10 +304,13 @@ func handleSignIn(ctx *context.Context, u *user_model.User, remember bool) {
func handleSignInFull(ctx *context.Context, u *user_model.User, remember, obeyRedirect bool) string {
if remember {
- days := 86400 * setting.LogInRememberDays
- ctx.SetSiteCookie(setting.CookieUserName, u.Name, days)
- ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
- setting.CookieRememberName, u.Name, days)
+ nt, token, err := auth_service.CreateAuthTokenForUserID(ctx, u.ID)
+ if err != nil {
+ ctx.ServerError("CreateAuthTokenForUserID", err)
+ return setting.AppSubURL + "/"
+ }
+
+ ctx.SetSiteCookie(setting.CookieRememberName, nt.ID+":"+token, setting.LogInRememberDays*timeutil.Day)
}
if err := updateSession(ctx, []string{
@@ -368,7 +385,6 @@ func getUserName(gothUser *goth.User) string {
func HandleSignOut(ctx *context.Context) {
_ = ctx.Session.Flush()
_ = ctx.Session.Destroy(ctx.Resp, ctx.Req)
- ctx.DeleteSiteCookie(setting.CookieUserName)
ctx.DeleteSiteCookie(setting.CookieRememberName)
ctx.Csrf.DeleteCookie(ctx)
middleware.DeleteRedirectToCookie(ctx.Resp)
diff --git a/routers/web/auth/openid.go b/routers/web/auth/openid.go
index aa07129632..29ef772b1c 100644
--- a/routers/web/auth/openid.go
+++ b/routers/web/auth/openid.go
@@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
- "code.gitea.io/gitea/modules/web/middleware"
"code.gitea.io/gitea/services/auth"
"code.gitea.io/gitea/services/forms"
)
@@ -36,23 +35,7 @@ func SignInOpenID(ctx *context.Context) {
return
}
- // Check auto-login.
- isSucceed, err := AutoSignIn(ctx)
- if err != nil {
- ctx.ServerError("AutoSignIn", err)
- return
- }
-
- redirectTo := ctx.FormString("redirect_to")
- if len(redirectTo) > 0 {
- middleware.SetRedirectToCookie(ctx.Resp, redirectTo)
- } else {
- redirectTo = ctx.GetSiteCookie("redirect_to")
- }
-
- if isSucceed {
- middleware.DeleteRedirectToCookie(ctx.Resp)
- ctx.RedirectToFirst(redirectTo)
+ if CheckAutoLogin(ctx) {
return
}
diff --git a/routers/web/auth/webauthn.go b/routers/web/auth/webauthn.go
index 9b516ce396..95c8d262a5 100644
--- a/routers/web/auth/webauthn.go
+++ b/routers/web/auth/webauthn.go
@@ -26,8 +26,7 @@ var tplWebAuthn base.TplName = "user/auth/webauthn"
func WebAuthn(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("twofa")
- // Check auto-login.
- if checkAutoLogin(ctx) {
+ if CheckAutoLogin(ctx) {
return
}
diff --git a/routers/web/home.go b/routers/web/home.go
index ab3fbde2c9..2321b00efe 100644
--- a/routers/web/home.go
+++ b/routers/web/home.go
@@ -54,8 +54,7 @@ func Home(ctx *context.Context) {
}
// Check auto-login.
- uname := ctx.GetSiteCookie(setting.CookieUserName)
- if len(uname) != 0 {
+ if ctx.GetSiteCookie(setting.CookieRememberName) != "" {
ctx.Redirect(setting.AppSubURL + "/user/login")
return
}
diff --git a/routers/web/web.go b/routers/web/web.go
index d2179a0008..6449f7716c 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -187,7 +187,7 @@ func verifyAuthWithOptions(options *common.VerifyOptions) func(ctx *context.Cont
// Redirect to log in page if auto-signin info is provided and has not signed in.
if !options.SignOutRequired && !ctx.IsSigned &&
- len(ctx.GetSiteCookie(setting.CookieUserName)) > 0 {
+ ctx.GetSiteCookie(setting.CookieRememberName) != "" {
if ctx.Req.URL.Path != "/user/events" {
middleware.SetRedirectToCookie(ctx.Resp, setting.AppSubURL+ctx.Req.URL.RequestURI())
}