Skip to content

Commit 2102f9d

Browse files
tamalsahatechknowlogick
authored andcommitted
Support setting cookie domain (#6288)
Signed-off-by: Tamal Saha <[email protected]>
1 parent d95237b commit 2102f9d

File tree

15 files changed

+58
-46
lines changed

15 files changed

+58
-46
lines changed

custom/conf/app.ini.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ IMPORT_LOCAL_PATHS = false
321321
DISABLE_GIT_HOOKS = false
322322
; Password Hash algorithm, either "pbkdf2", "argon2", "scrypt" or "bcrypt"
323323
PASSWORD_HASH_ALGO = pbkdf2
324+
; Set false to allow JavaScript to read CSRF cookie
325+
CSRF_COOKIE_HTTP_ONLY = true
324326

325327
[openid]
326328
;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
198198
- `INTERNAL_TOKEN`: **\<random at every install if no uri set\>**: Secret used to validate communication within Gitea binary.
199199
- `INTERNAL_TOKEN_URI`: **<empty>**: Instead of defining internal token in the configuration, this configuration option can be used to give Gitea a path to a file that contains the internal token (example value: `file:/etc/gitea/internal_token`)
200200
- `PASSWORD_HASH_ALGO`: **pbkdf2**: The hash algorithm to use \[pbkdf2, argon2, scrypt, bcrypt\].
201+
- `CSRF_COOKIE_HTTP_ONLY`: **true**: Set false to allow JavaScript to read CSRF cookie.
201202

202203
## OpenID (`openid`)
203204

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ require (
4747
github.com/go-macaron/cache v0.0.0-20151013081102-561735312776
4848
github.com/go-macaron/captcha v0.0.0-20190710000913-8dc5911259df
4949
github.com/go-macaron/cors v0.0.0-20190309005821-6fd6a9bfe14e9
50-
github.com/go-macaron/csrf v0.0.0-20180426211211-503617c6b372
51-
github.com/go-macaron/i18n v0.0.0-20160612092837-ef57533c3b0f
50+
github.com/go-macaron/csrf v0.0.0-20190131233648-3751b136073c
51+
github.com/go-macaron/i18n v0.0.0-20190131234336-56731837a73b
5252
github.com/go-macaron/inject v0.0.0-20160627170012-d8a0b8677191
5353
github.com/go-macaron/session v0.0.0-20190131233854-0a0a789bf193
5454
github.com/go-macaron/toolbox v0.0.0-20180818072302-a77f45a7ce90

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ github.com/go-macaron/captcha v0.0.0-20190710000913-8dc5911259df h1:MdgvtI3Y1u/D
117117
github.com/go-macaron/captcha v0.0.0-20190710000913-8dc5911259df/go.mod h1:j9TJ+0nwUOWBvNnm0bheHIPFf3cC62EQo7n7O6PbjZA=
118118
github.com/go-macaron/cors v0.0.0-20190309005821-6fd6a9bfe14e9 h1:A0QGzY6UHHEil0I2e7C21JenNNG0mmrj5d9SFWTlgr8=
119119
github.com/go-macaron/cors v0.0.0-20190309005821-6fd6a9bfe14e9/go.mod h1:utmMRnVIrXPSfA9MFcpIYKEpKawjKxf62vv62k4707E=
120-
github.com/go-macaron/csrf v0.0.0-20180426211211-503617c6b372 h1:acrx8CnDmlKl+BPoOOLEK9Ko+SrWFB5pxRuGkKj4iqo=
121-
github.com/go-macaron/csrf v0.0.0-20180426211211-503617c6b372/go.mod h1:oZGMxI7MBnicI0jJqJvH4qQzyrWKhtiKxLSJKHC+ydc=
122-
github.com/go-macaron/i18n v0.0.0-20160612092837-ef57533c3b0f h1:wDKrZFc9pYJlqFOf7EzGbFMrSFFtyHt3plr2uTdo8Rg=
123-
github.com/go-macaron/i18n v0.0.0-20160612092837-ef57533c3b0f/go.mod h1:MePM/dStkAh+PNzAdNSNl4SGDM2EZvZGken+KpJhM7s=
120+
github.com/go-macaron/csrf v0.0.0-20190131233648-3751b136073c h1:yCyrJuFaxKX/VoV9hHqYXhkFEMtg+Hxsiitk1z/lHfQ=
121+
github.com/go-macaron/csrf v0.0.0-20190131233648-3751b136073c/go.mod h1:oZGMxI7MBnicI0jJqJvH4qQzyrWKhtiKxLSJKHC+ydc=
122+
github.com/go-macaron/i18n v0.0.0-20190131234336-56731837a73b h1:p19t0uFyv0zkBwp4dafzU3EcpHilHNffTVDmxpn/M+w=
123+
github.com/go-macaron/i18n v0.0.0-20190131234336-56731837a73b/go.mod h1:MePM/dStkAh+PNzAdNSNl4SGDM2EZvZGken+KpJhM7s=
124124
github.com/go-macaron/inject v0.0.0-20160627170012-d8a0b8677191 h1:NjHlg70DuOkcAMqgt0+XA+NHwtu66MkTVVgR4fFWbcI=
125125
github.com/go-macaron/inject v0.0.0-20160627170012-d8a0b8677191/go.mod h1:VFI2o2q9kYsC4o7VP1HrEVosiZZTd+MVT3YZx4gqvJw=
126126
github.com/go-macaron/session v0.0.0-20190131233854-0a0a789bf193 h1:z/nqwd+ql/r6Q3QGnwNd6B89UjPytM0be5pDQV9TuWw=

modules/setting/session.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func newSessionService() {
3434
SessionConfig.Secure = Cfg.Section("session").Key("COOKIE_SECURE").MustBool(false)
3535
SessionConfig.Gclifetime = Cfg.Section("session").Key("GC_INTERVAL_TIME").MustInt64(86400)
3636
SessionConfig.Maxlifetime = Cfg.Section("session").Key("SESSION_LIFE_TIME").MustInt64(86400)
37+
SessionConfig.Domain = Cfg.Section("session").Key("DOMAIN").String()
3738

3839
shadowConfig, err := json.Marshal(SessionConfig)
3940
if err != nil {

modules/setting/setting.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ var (
287287
// Time settings
288288
TimeFormat string
289289

290-
CSRFCookieName = "_csrf"
290+
CSRFCookieName = "_csrf"
291+
CSRFCookieHTTPOnly = true
291292

292293
// Mirror settings
293294
Mirror struct {
@@ -781,6 +782,8 @@ func NewContext() {
781782
ImportLocalPaths = sec.Key("IMPORT_LOCAL_PATHS").MustBool(false)
782783
DisableGitHooks = sec.Key("DISABLE_GIT_HOOKS").MustBool(false)
783784
PasswordHashAlgo = sec.Key("PASSWORD_HASH_ALGO").MustString("pbkdf2")
785+
CSRFCookieHTTPOnly = sec.Key("CSRF_COOKIE_HTTP_ONLY").MustBool(true)
786+
784787
InternalToken = loadInternalToken(sec)
785788
IterateBufferSize = Cfg.Section("database").Key("ITERATE_BUFFER_SIZE").MustInt(50)
786789
LogSQL = Cfg.Section("database").Key("LOG_SQL").MustBool(true)

routers/routes/routes.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,13 @@ func NewMacaron() *macaron.Macaron {
181181
}
182182

183183
m.Use(i18n.I18n(i18n.Options{
184-
SubURL: setting.AppSubURL,
185-
Files: localFiles,
186-
Langs: setting.Langs,
187-
Names: setting.Names,
188-
DefaultLang: "en-US",
189-
Redirect: false,
184+
SubURL: setting.AppSubURL,
185+
Files: localFiles,
186+
Langs: setting.Langs,
187+
Names: setting.Names,
188+
DefaultLang: "en-US",
189+
Redirect: false,
190+
CookieDomain: setting.SessionConfig.Domain,
190191
}))
191192
m.Use(cache.Cacher(cache.Options{
192193
Adapter: setting.CacheService.Adapter,
@@ -202,8 +203,9 @@ func NewMacaron() *macaron.Macaron {
202203
Cookie: setting.CSRFCookieName,
203204
SetCookie: true,
204205
Secure: setting.SessionConfig.Secure,
205-
CookieHttpOnly: true,
206+
CookieHttpOnly: setting.CSRFCookieHTTPOnly,
206207
Header: "X-Csrf-Token",
208+
CookieDomain: setting.SessionConfig.Domain,
207209
CookiePath: setting.AppSubURL,
208210
}))
209211
m.Use(toolbox.Toolboxer(m, toolbox.Options{

routers/user/auth.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ func AutoSignIn(ctx *context.Context) (bool, error) {
5858
defer func() {
5959
if !isSucceed {
6060
log.Trace("auto-login cookie cleared: %s", uname)
61-
ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
62-
ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
61+
ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
62+
ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
6363
}
6464
}()
6565

@@ -85,7 +85,7 @@ func AutoSignIn(ctx *context.Context) (bool, error) {
8585
if err != nil {
8686
return false, err
8787
}
88-
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
88+
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
8989
return true, nil
9090
}
9191

@@ -475,9 +475,9 @@ func handleSignIn(ctx *context.Context, u *models.User, remember bool) {
475475
func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyRedirect bool) string {
476476
if remember {
477477
days := 86400 * setting.LogInRememberDays
478-
ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
478+
ctx.SetCookie(setting.CookieUserName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
479479
ctx.SetSuperSecureCookie(base.EncodeMD5(u.Rands+u.Passwd),
480-
setting.CookieRememberName, u.Name, days, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
480+
setting.CookieRememberName, u.Name, days, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
481481
}
482482

483483
_ = ctx.Session.Delete("openid_verified_uri")
@@ -507,10 +507,10 @@ func handleSignInFull(ctx *context.Context, u *models.User, remember bool, obeyR
507507
}
508508
}
509509

510-
ctx.SetCookie("lang", u.Language, nil, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
510+
ctx.SetCookie("lang", u.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
511511

512512
// Clear whatever CSRF has right now, force to generate a new one
513-
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
513+
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
514514

515515
// Register last login
516516
u.SetLastLogin()
@@ -610,7 +610,7 @@ func handleOAuth2SignIn(u *models.User, gothUser goth.User, ctx *context.Context
610610
}
611611

612612
// Clear whatever CSRF has right now, force to generate a new one
613-
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
613+
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
614614

615615
// Register last login
616616
u.SetLastLogin()
@@ -968,10 +968,10 @@ func handleSignOut(ctx *context.Context) {
968968
_ = ctx.Session.Delete("socialId")
969969
_ = ctx.Session.Delete("socialName")
970970
_ = ctx.Session.Delete("socialEmail")
971-
ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
972-
ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
973-
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
974-
ctx.SetCookie("lang", "", -1, setting.AppSubURL, "", setting.SessionConfig.Secure, true) // Setting the lang cookie will trigger the middleware to reset the language ot previous state.
971+
ctx.SetCookie(setting.CookieUserName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
972+
ctx.SetCookie(setting.CookieRememberName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
973+
ctx.SetCookie(setting.CSRFCookieName, "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
974+
ctx.SetCookie("lang", "", -1, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true) // Setting the lang cookie will trigger the middleware to reset the language ot previous state.
975975
}
976976

977977
// SignOut sign out from login status

routers/user/setting/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func ProfilePost(ctx *context.Context, form auth.UpdateProfileForm) {
104104
}
105105

106106
// Update the language to the one we just set
107-
ctx.SetCookie("lang", ctx.User.Language, nil, setting.AppSubURL, "", setting.SessionConfig.Secure, true)
107+
ctx.SetCookie("lang", ctx.User.Language, nil, setting.AppSubURL, setting.SessionConfig.Domain, setting.SessionConfig.Secure, true)
108108

109109
log.Trace("User settings updated: %s", ctx.User.Name)
110110
ctx.Flash.Success(i18n.Tr(ctx.User.Language, "settings.update_profile_success"))

vendor/github.com/go-macaron/csrf/.travis.yml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/go-macaron/csrf/csrf.go

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/go-macaron/csrf/xsrf.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/go-macaron/i18n/.travis.yml

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/go-macaron/i18n/i18n.go

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ github.com/go-macaron/cache/redis
128128
github.com/go-macaron/captcha
129129
# github.com/go-macaron/cors v0.0.0-20190309005821-6fd6a9bfe14e9
130130
github.com/go-macaron/cors
131-
# github.com/go-macaron/csrf v0.0.0-20180426211211-503617c6b372
131+
# github.com/go-macaron/csrf v0.0.0-20190131233648-3751b136073c
132132
github.com/go-macaron/csrf
133-
# github.com/go-macaron/i18n v0.0.0-20160612092837-ef57533c3b0f
133+
# github.com/go-macaron/i18n v0.0.0-20190131234336-56731837a73b
134134
github.com/go-macaron/i18n
135135
# github.com/go-macaron/inject v0.0.0-20160627170012-d8a0b8677191
136136
github.com/go-macaron/inject

0 commit comments

Comments
 (0)