conf: overhaul security settings

This commit is contained in:
ᴜɴᴋɴᴡᴏɴ
2020-02-22 20:46:16 +08:00
parent a7e53b8134
commit 286fbc07e9
19 changed files with 151 additions and 126 deletions

View File

@@ -201,9 +201,9 @@ func Config(c *context.Context) {
c.Data["SSH"] = conf.SSH
c.Data["Repository"] = conf.Repository
c.Data["Database"] = conf.Database
c.Data["Security"] = conf.Security
c.Data["LogRootPath"] = conf.LogRootPath
c.Data["ReverseProxyAuthUser"] = conf.ReverseProxyAuthUser
c.Data["HTTP"] = conf.HTTP

View File

@@ -32,7 +32,7 @@ func Home(c *context.Context) {
}
// Check auto-login.
uname := c.GetCookie(conf.CookieUserName)
uname := c.GetCookie(conf.Security.CookieUsername)
if len(uname) != 0 {
c.Redirect(conf.Server.Subpath + "/user/login")
return

View File

@@ -31,7 +31,6 @@ import (
"gogs.io/gogs/internal/ssh"
"gogs.io/gogs/internal/template/highlight"
"gogs.io/gogs/internal/tool"
"gogs.io/gogs/internal/user"
)
const (
@@ -67,7 +66,7 @@ func GlobalInit(customConf string) error {
conf.NewServices()
mailer.NewContext()
if conf.InstallLock {
if conf.Security.InstallLock {
highlight.NewContext()
markup.NewSanitizer()
if err := db.NewEngine(); err != nil {
@@ -96,7 +95,7 @@ func GlobalInit(customConf string) error {
}
checkRunMode()
if !conf.InstallLock {
if !conf.Security.InstallLock {
return nil
}
@@ -116,7 +115,7 @@ func GlobalInit(customConf string) error {
}
func InstallInit(c *context.Context) {
if conf.InstallLock {
if conf.Security.InstallLock {
c.NotFound()
return
}
@@ -159,7 +158,7 @@ func Install(c *context.Context) {
// Note(unknwon): it's hard for Windows users change a running user,
// so just use current one if config says default.
if conf.IsWindowsRuntime() && conf.App.RunUser == "git" {
f.RunUser = user.CurrentUsername()
f.RunUser = osutil.CurrentUsername()
} else {
f.RunUser = conf.App.RunUser
}
@@ -265,7 +264,7 @@ func InstallPost(c *context.Context, f form.Install) {
return
}
currentUser, match := conf.IsRunUserMatchCurrentUser(f.RunUser)
currentUser, match := conf.CheckRunUser(f.RunUser)
if !match {
c.FormErr("RunUser")
c.RenderWithErr(c.Tr("install.run_user_not_match", f.RunUser, currentUser), INSTALL, &f)
@@ -406,7 +405,7 @@ func InstallPost(c *context.Context, f form.Install) {
}
if err := db.CreateUser(u); err != nil {
if !db.IsErrUserAlreadyExist(err) {
conf.InstallLock = false
conf.Security.InstallLock = false
c.FormErr("AdminName", "AdminEmail")
c.RenderWithErr(c.Tr("install.invalid_admin_setting", err), INSTALL, &f)
return

View File

@@ -36,7 +36,7 @@ func AutoLogin(c *context.Context) (bool, error) {
return false, nil
}
uname := c.GetCookie(conf.CookieUserName)
uname := c.GetCookie(conf.Security.CookieUsername)
if len(uname) == 0 {
return false, nil
}
@@ -45,9 +45,9 @@ func AutoLogin(c *context.Context) (bool, error) {
defer func() {
if !isSucceed {
log.Trace("auto-login cookie cleared: %s", uname)
c.SetCookie(conf.CookieUserName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.CookieRememberName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.LoginStatusCookieName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.CookieUsername, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.CookieRememberName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.LoginStatusCookieName, "", -1, conf.Server.Subpath)
}
}()
@@ -59,7 +59,7 @@ func AutoLogin(c *context.Context) (bool, error) {
return false, nil
}
if val, ok := c.GetSuperSecureCookie(u.Rands+u.Passwd, conf.CookieRememberName); !ok || val != u.Name {
if val, ok := c.GetSuperSecureCookie(u.Rands+u.Passwd, conf.Security.CookieRememberName); !ok || val != u.Name {
return false, nil
}
@@ -67,8 +67,8 @@ func AutoLogin(c *context.Context) (bool, error) {
c.Session.Set("uid", u.ID)
c.Session.Set("uname", u.Name)
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
if conf.EnableLoginStatusCookie {
c.SetCookie(conf.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
if conf.Security.EnableLoginStatusCookie {
c.SetCookie(conf.Security.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
}
return true, nil
}
@@ -119,9 +119,9 @@ func Login(c *context.Context) {
func afterLogin(c *context.Context, u *db.User, remember bool) {
if remember {
days := 86400 * conf.LoginRememberDays
c.SetCookie(conf.CookieUserName, u.Name, days, conf.Server.Subpath, "", conf.CookieSecure, true)
c.SetSuperSecureCookie(u.Rands+u.Passwd, conf.CookieRememberName, u.Name, days, conf.Server.Subpath, "", conf.CookieSecure, true)
days := 86400 * conf.Security.LoginRememberDays
c.SetCookie(conf.Security.CookieUsername, u.Name, days, conf.Server.Subpath, "", conf.Security.CookieSecure, true)
c.SetSuperSecureCookie(u.Rands+u.Passwd, conf.Security.CookieRememberName, u.Name, days, conf.Server.Subpath, "", conf.Security.CookieSecure, true)
}
c.Session.Set("uid", u.ID)
@@ -131,8 +131,8 @@ func afterLogin(c *context.Context, u *db.User, remember bool) {
// Clear whatever CSRF has right now, force to generate a new one
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
if conf.EnableLoginStatusCookie {
c.SetCookie(conf.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
if conf.Security.EnableLoginStatusCookie {
c.SetCookie(conf.Security.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
}
redirectTo, _ := url.QueryUnescape(c.GetCookie("redirect_to"))
@@ -283,8 +283,8 @@ func LoginTwoFactorRecoveryCodePost(c *context.Context) {
func SignOut(c *context.Context) {
c.Session.Flush()
c.Session.Destory(c.Context)
c.SetCookie(conf.CookieUserName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.CookieRememberName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.CookieUsername, "", -1, conf.Server.Subpath)
c.SetCookie(conf.Security.CookieRememberName, "", -1, conf.Server.Subpath)
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
c.SubURLRedirect("/")
}