Skip to content

Commit fd76121

Browse files
unknwonachilleas-k
authored andcommitted
conf: overhaul sessions settings (#5952)
1 parent 2d358e1 commit fd76121

File tree

12 files changed

+117
-98
lines changed

12 files changed

+117
-98
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ All notable changes to Gogs are documented in this file.
3030
- Configuration option `[auth] RESET_PASSWD_CODE_LIVE_MINUTES` is deprecated and will end support in 0.13.0, please start using `[auth] RESET_PASSWORD_CODE_LIVES`.
3131
- Configuration option `[auth] ENABLE_CAPTCHA` is deprecated and will end support in 0.13.0, please start using `[auth] ENABLE_REGISTRATION_CAPTCHA`.
3232
- Configuration option `[auth] ENABLE_NOTIFY_MAIL` is deprecated and will end support in 0.13.0, please start using `[user] ENABLE_EMAIL_NOTIFICATION`.
33+
- Configuration option `[session] GC_INTERVAL_TIME` is deprecated and will end support in 0.13.0, please start using `[session] GC_INTERVAL`.
34+
- Configuration option `[session] SESSION_LIFE_TIME` is deprecated and will end support in 0.13.0, please start using `[session] MAX_LIFE_TIME`.
3335

3436
### Fixed
3537

@@ -49,6 +51,7 @@ All notable changes to Gogs are documented in this file.
4951
- Configuration option `[server] STATIC_ROOT_PATH`
5052
- Configuration option `[repository] MIRROR_QUEUE_LENGTH`
5153
- Configuration option `[repository] PULL_REQUEST_QUEUE_LENGTH`
54+
- Configuration option `[session] ENABLE_SET_COOKIE`
5255

5356
---
5457

conf/app.ini

+19-22
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,25 @@ REVERSE_PROXY_AUTHENTICATION_HEADER = X-WEBAUTH-USER
237237
; Whether to enable email notifications for users.
238238
ENABLE_EMAIL_NOTIFICATION = false
239239

240+
[session]
241+
; The session provider, either "memory", "file", or "redis".
242+
PROVIDER = memory
243+
; The configuration for respective provider:
244+
; - memory: does not need any config yet
245+
; - file: session file path, e.g. `data/sessions`
246+
; - redis: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180
247+
PROVIDER_CONFIG = data/sessions
248+
; The cookie name to store the session identifier.
249+
COOKIE_NAME = i_like_gogs
250+
; Whether to set cookie in HTTPS only.
251+
COOKIE_SECURE = false
252+
; The GC interval in seconds for session data.
253+
GC_INTERVAL = 3600
254+
; The maximum life time in seconds for a session.
255+
MAX_LIFE_TIME = 86400
256+
; The cookie name for CSRF token.
257+
CSRF_COOKIE_NAME = _csrf
258+
240259
; Attachment settings for releases
241260
[release.attachment]
242261
; Whether attachments are enabled. Defaults to `true`
@@ -297,28 +316,6 @@ INTERVAL = 60
297316
; memcache: `127.0.0.1:11211`
298317
HOST =
299318

300-
[session]
301-
; Either "memory", "file", or "redis", default is "memory"
302-
PROVIDER = memory
303-
; Provider config options
304-
; memory: not have any config yet
305-
; file: session file path, e.g. `data/sessions`
306-
; redis: network=tcp,addr=:6379,password=macaron,db=0,pool_size=100,idle_timeout=180
307-
; mysql: go-sql-driver/mysql dsn config string, e.g. `root:password@/session_table`
308-
PROVIDER_CONFIG = data/sessions
309-
; Session cookie name
310-
COOKIE_NAME = i_like_gogs
311-
; If you use session in https only, default is false
312-
COOKIE_SECURE = false
313-
; Enable set cookie, default is true
314-
ENABLE_SET_COOKIE = true
315-
; Session GC time interval, default is 3600
316-
GC_INTERVAL_TIME = 3600
317-
; Session life time, default is 86400
318-
SESSION_LIFE_TIME = 86400
319-
; Cookie name for CSRF
320-
CSRF_COOKIE_NAME = _csrf
321-
322319
[picture]
323320
; Path to store user uploaded avatars
324321
AVATAR_UPLOAD_PATH = data/avatars

conf/locale/locale_en-US.ini

+9-10
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,15 @@ config.auth.reverse_proxy_authentication_header = Reverse proxy authentication h
12841284
config.user_config = User configuration
12851285
config.user.enable_email_notify = Enable email notification
12861286
1287+
config.session_config = Session configuration
1288+
config.session.provider = Provider
1289+
config.session.provider_config = Provider config
1290+
config.session.cookie_name = Cookie
1291+
config.session.https_only = HTTPS only
1292+
config.session.gc_interval = GC interval
1293+
config.session.max_life_time = Max life time
1294+
config.session.csrf_cookie_name = CSRF cookie
1295+
12871296
config.log_file_root_path = Log File Root Path
12881297
12891298
config.http_config = HTTP Configuration
@@ -1303,16 +1312,6 @@ config.cache_adapter = Cache Adapter
13031312
config.cache_interval = Cache Interval
13041313
config.cache_conn = Cache Connection
13051314
1306-
config.session_config = Session Configuration
1307-
config.session_provider = Session Provider
1308-
config.provider_config = Provider Config
1309-
config.cookie_name = Cookie Name
1310-
config.enable_set_cookie = Enable Set Cookie
1311-
config.gc_interval_time = GC Interval Time
1312-
config.session_life_time = Session Life Time
1313-
config.https_only = HTTPS Only
1314-
config.cookie_life_time = Cookie Life Time
1315-
13161315
config.picture_config = Picture Configuration
13171316
config.picture_service = Picture Service
13181317
config.disable_gravatar = Disable Gravatar

internal/assets/conf/conf_gen.go

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/assets/public/public_gen.go

+4-4
Large diffs are not rendered by default.

internal/assets/templates/templates_gen.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cmd/web.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,18 @@ func newMacaron() *macaron.Macaron {
144144
m.Use(captcha.Captchaer(captcha.Options{
145145
SubURL: conf.Server.Subpath,
146146
}))
147-
m.Use(session.Sessioner(conf.SessionConfig))
147+
m.Use(session.Sessioner(session.Options{
148+
Provider: conf.Session.Provider,
149+
ProviderConfig: conf.Session.ProviderConfig,
150+
CookieName: conf.Session.CookieName,
151+
CookiePath: conf.Server.Subpath,
152+
Gclifetime: conf.Session.GCInterval,
153+
Maxlifetime: conf.Session.MaxLifeTime,
154+
Secure: conf.Session.CookieSecure,
155+
}))
148156
m.Use(csrf.Csrfer(csrf.Options{
149157
Secret: conf.Security.SecretKey,
150-
Cookie: conf.CSRFCookieName,
158+
Cookie: conf.Session.CSRFCookieName,
151159
SetCookie: true,
152160
Header: "X-Csrf-Token",
153161
CookiePath: conf.Server.Subpath,

internal/conf/conf.go

+8-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717

1818
_ "github.com/go-macaron/cache/memcache"
1919
_ "github.com/go-macaron/cache/redis"
20-
"github.com/go-macaron/session"
2120
_ "github.com/go-macaron/session/redis"
2221
"github.com/mcuadros/go-version"
2322
"github.com/pkg/errors"
@@ -254,6 +253,14 @@ func Init(customConf string) error {
254253
return errors.Wrap(err, "mapping [user] section")
255254
}
256255

256+
// ***********************************
257+
// ----- Session settings -----
258+
// ***********************************
259+
260+
if err = File.Section("session").MapTo(&Session); err != nil {
261+
return errors.Wrap(err, "mapping [session] section")
262+
}
263+
257264
handleDeprecated()
258265

259266
// TODO
@@ -468,10 +475,6 @@ var (
468475
CacheInterval int
469476
CacheConn string
470477

471-
// Session settings
472-
SessionConfig session.Options
473-
CSRFCookieName string
474-
475478
// Cron tasks
476479
Cron struct {
477480
UpdateMirror struct {
@@ -727,23 +730,8 @@ func newCacheService() {
727730
log.Trace("Cache service is enabled")
728731
}
729732

730-
func newSessionService() {
731-
SessionConfig.Provider = File.Section("session").Key("PROVIDER").In("memory",
732-
[]string{"memory", "file", "redis", "mysql"})
733-
SessionConfig.ProviderConfig = strings.Trim(File.Section("session").Key("PROVIDER_CONFIG").String(), "\" ")
734-
SessionConfig.CookieName = File.Section("session").Key("COOKIE_NAME").MustString("i_like_gogs")
735-
SessionConfig.CookiePath = Server.Subpath
736-
SessionConfig.Secure = File.Section("session").Key("COOKIE_SECURE").MustBool()
737-
SessionConfig.Gclifetime = File.Section("session").Key("GC_INTERVAL_TIME").MustInt64(3600)
738-
SessionConfig.Maxlifetime = File.Section("session").Key("SESSION_LIFE_TIME").MustInt64(86400)
739-
CSRFCookieName = File.Section("session").Key("CSRF_COOKIE_NAME").MustString("_csrf")
740-
741-
log.Trace("Session service is enabled")
742-
}
743-
744733
func NewServices() {
745734
newCacheService()
746-
newSessionService()
747735
}
748736

749737
// HookMode indicates whether program starts as Git server-side hook callback.

internal/conf/static.go

+25
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ var (
218218
User struct {
219219
EnableEmailNotification bool
220220
}
221+
222+
// Session settings
223+
Session struct {
224+
Provider string
225+
ProviderConfig string
226+
CookieName string
227+
CookieSecure bool
228+
GCInterval int64 `ini:"GC_INTERVAL"`
229+
MaxLifeTime int64
230+
CSRFCookieName string `ini:"CSRF_COOKIE_NAME"`
231+
232+
// Deprecated: Use GCInterval instead, will be removed in 0.13.
233+
GCIntervalTime int64 `ini:"GC_INTERVAL_TIME"`
234+
// Deprecated: Use MaxLifeTime instead, will be removed in 0.13.
235+
SessionLifeTime int64
236+
}
221237
)
222238

223239
// handleDeprecated transfers deprecated values to the new ones when set.
@@ -275,4 +291,13 @@ func handleDeprecated() {
275291
User.EnableEmailNotification = true
276292
Auth.EnableNotifyMail = false
277293
}
294+
295+
if Session.GCIntervalTime > 0 {
296+
Session.GCInterval = Session.GCIntervalTime
297+
Session.GCIntervalTime = 0
298+
}
299+
if Session.SessionLifeTime > 0 {
300+
Session.MaxLifeTime = Session.SessionLifeTime
301+
Session.SessionLifeTime = 0
302+
}
278303
}

internal/route/admin/admin.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func Config(c *context.Context) {
210210
c.Data["Email"] = conf.Email
211211
c.Data["Auth"] = conf.Auth
212212
c.Data["User"] = conf.User
213+
c.Data["Session"] = conf.Session
213214

214215
c.Data["LogRootPath"] = conf.LogRootPath
215216

@@ -221,8 +222,6 @@ func Config(c *context.Context) {
221222
c.Data["CacheInterval"] = conf.CacheInterval
222223
c.Data["CacheConn"] = conf.CacheConn
223224

224-
c.Data["SessionConfig"] = conf.SessionConfig
225-
226225
c.Data["DisableGravatar"] = conf.DisableGravatar
227226
c.Data["EnableFederatedAvatar"] = conf.EnableFederatedAvatar
228227

internal/route/user/auth.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func AutoLogin(c *context.Context) (bool, error) {
6666
isSucceed = true
6767
c.Session.Set("uid", u.ID)
6868
c.Session.Set("uname", u.Name)
69-
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
69+
c.SetCookie(conf.Session.CSRFCookieName, "", -1, conf.Server.Subpath)
7070
if conf.Security.EnableLoginStatusCookie {
7171
c.SetCookie(conf.Security.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
7272
}
@@ -130,7 +130,7 @@ func afterLogin(c *context.Context, u *db.User, remember bool) {
130130
c.Session.Delete("twoFactorUserID")
131131

132132
// Clear whatever CSRF has right now, force to generate a new one
133-
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
133+
c.SetCookie(conf.Session.CSRFCookieName, "", -1, conf.Server.Subpath)
134134
if conf.Security.EnableLoginStatusCookie {
135135
c.SetCookie(conf.Security.LoginStatusCookieName, "true", 0, conf.Server.Subpath)
136136
}
@@ -285,7 +285,7 @@ func SignOut(c *context.Context) {
285285
c.Session.Destory(c.Context)
286286
c.SetCookie(conf.Security.CookieUsername, "", -1, conf.Server.Subpath)
287287
c.SetCookie(conf.Security.CookieRememberName, "", -1, conf.Server.Subpath)
288-
c.SetCookie(conf.CSRFCookieName, "", -1, conf.Server.Subpath)
288+
c.SetCookie(conf.Session.CSRFCookieName, "", -1, conf.Server.Subpath)
289289
c.SubURLRedirect("/")
290290
}
291291

templates/admin/config.tmpl

+23-23
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@
289289
</dl>
290290
</div>
291291

292-
293292
{{/* User settings */}}
294293
<h4 class="ui top attached header">
295294
{{.i18n.Tr "admin.config.user_config"}}
@@ -301,6 +300,29 @@
301300
</dl>
302301
</div>
303302

303+
{{/* Session settings */}}
304+
<h4 class="ui top attached header">
305+
{{.i18n.Tr "admin.config.session_config"}}
306+
</h4>
307+
<div class="ui attached table segment">
308+
<dl class="dl-horizontal admin-dl-horizontal">
309+
<dt>{{.i18n.Tr "admin.config.session_provider"}}</dt>
310+
<dd>{{.Session.Provider}}</dd>
311+
<dt>{{.i18n.Tr "admin.config.session.provider_config"}}</dt>
312+
<dd><code>{{.Session.ProviderConfig}}</code></dd>
313+
<dt>{{.i18n.Tr "admin.config.session.cookie_name"}}</dt>
314+
<dd>{{.Session.CookieName}}</dd>
315+
<dt>{{.i18n.Tr "admin.config.session.https_only"}}</dt>
316+
<dd><i class="fa fa{{if .Session.CookieSecure}}-check{{end}}-square-o"></i></dd>
317+
<dt>{{.i18n.Tr "admin.config.session.gc_interval"}}</dt>
318+
<dd>{{.Session.GCInterval}} {{.i18n.Tr "tool.raw_seconds"}}</dd>
319+
<dt>{{.i18n.Tr "admin.config.session.max_life_time"}}</dt>
320+
<dd>{{.Session.MaxLifeTime}} {{.i18n.Tr "tool.raw_seconds"}}</dd>
321+
<dt>{{.i18n.Tr "admin.config.session.csrf_cookie_name"}}</dt>
322+
<dd>{{.Session.CSRFCookieName}}</dd>
323+
</dl>
324+
</div>
325+
304326
<!-- HTTP Configuration -->
305327
<h4 class="ui top attached header">
306328
{{.i18n.Tr "admin.config.http_config"}}
@@ -348,28 +370,6 @@
348370
</dl>
349371
</div>
350372

351-
<h4 class="ui top attached header">
352-
{{.i18n.Tr "admin.config.session_config"}}
353-
</h4>
354-
<div class="ui attached table segment">
355-
<dl class="dl-horizontal admin-dl-horizontal">
356-
<dt>{{.i18n.Tr "admin.config.session_provider"}}</dt>
357-
<dd>{{.SessionConfig.Provider}}</dd>
358-
<dt>{{.i18n.Tr "admin.config.provider_config"}}</dt>
359-
<dd><code>{{.SessionConfig.ProviderConfig}}</code></dd>
360-
<dt>{{.i18n.Tr "admin.config.cookie_name"}}</dt>
361-
<dd>{{.SessionConfig.CookieName}}</dd>
362-
<dt>{{.i18n.Tr "admin.config.gc_interval_time"}}</dt>
363-
<dd>{{.SessionConfig.Gclifetime}} {{.i18n.Tr "tool.raw_seconds"}}</dd>
364-
<dt>{{.i18n.Tr "admin.config.session_life_time"}}</dt>
365-
<dd>{{.SessionConfig.Maxlifetime}} {{.i18n.Tr "tool.raw_seconds"}}</dd>
366-
<dt>{{.i18n.Tr "admin.config.https_only"}}</dt>
367-
<dd><i class="fa fa{{if .SessionConfig.Secure}}-check{{end}}-square-o"></i></dd>
368-
<dt>{{.i18n.Tr "admin.config.cookie_life_time"}}</dt>
369-
<dd>{{.SessionConfig.CookieLifeTime}} {{.i18n.Tr "tool.raw_seconds"}}</dd>
370-
</dl>
371-
</div>
372-
373373
<h4 class="ui top attached header">
374374
{{.i18n.Tr "admin.config.picture_config"}}
375375
</h4>

0 commit comments

Comments
 (0)