Skip to content

Commit a63ce34

Browse files
committed
Reset Session ID on login
When logging in the SessionID should be reset and the session cleaned up. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 9296baf commit a63ce34

File tree

1 file changed

+68
-17
lines changed

1 file changed

+68
-17
lines changed

routers/web/user/auth.go

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,23 @@ func AutoSignIn(ctx *context.Context) (bool, error) {
9090

9191
isSucceed = true
9292

93+
// Save the session first
94+
_ = ctx.Session.Release()
95+
96+
// Then regenerate the ID - which should copy the previous data
97+
newSess, _ := ctx.Session.RegenerateID(ctx.Resp, ctx.Req)
98+
99+
// Then flush the old session to delete it
100+
_ = ctx.Session.Flush()
101+
93102
// Set session IDs
94-
if err := ctx.Session.Set("uid", u.ID); err != nil {
103+
if err := newSess.Set("uid", u.ID); err != nil {
95104
return false, err
96105
}
97-
if err := ctx.Session.Set("uname", u.Name); err != nil {
106+
if err := newSess.Set("uname", u.Name); err != nil {
98107
return false, err
99108
}
100-
if err := ctx.Session.Release(); err != nil {
109+
if err := newSess.Release(); err != nil {
101110
return false, err
102111
}
103112

@@ -233,26 +242,35 @@ func SignInPost(ctx *context.Context) {
233242
return
234243
}
235244

245+
// Save the current session
246+
_ = ctx.Session.Release()
247+
248+
// Regenerate the session - this will copy the data from the old session
249+
newSess, _ := ctx.Session.RegenerateID(ctx.Resp, ctx.Req)
250+
251+
// Delete the old session
252+
_ = ctx.Session.Flush()
253+
236254
// User will need to use 2FA TOTP or U2F, save data
237-
if err := ctx.Session.Set("twofaUid", u.ID); err != nil {
255+
if err := newSess.Set("twofaUid", u.ID); err != nil {
238256
ctx.ServerError("UserSignIn: Unable to set twofaUid in session", err)
239257
return
240258
}
241259

242-
if err := ctx.Session.Set("twofaRemember", form.Remember); err != nil {
260+
if err := newSess.Set("twofaRemember", form.Remember); err != nil {
243261
ctx.ServerError("UserSignIn: Unable to set twofaRemember in session", err)
244262
return
245263
}
246264

247265
if hasTOTPtwofa {
248266
// User will need to use U2F, save data
249-
if err := ctx.Session.Set("totpEnrolled", u.ID); err != nil {
267+
if err := newSess.Set("totpEnrolled", u.ID); err != nil {
250268
ctx.ServerError("UserSignIn: Unable to set u2fEnrolled in session", err)
251269
return
252270
}
253271
}
254272

255-
if err := ctx.Session.Release(); err != nil {
273+
if err := newSess.Release(); err != nil {
256274
ctx.ServerError("UserSignIn: Unable to save session", err)
257275
return
258276
}
@@ -526,6 +544,7 @@ func handleSignInFull(ctx *context.Context, u *user_model.User, remember bool, o
526544
setting.CookieRememberName, u.Name, days)
527545
}
528546

547+
// Delete the openid, 2fa and linkaccount data
529548
_ = ctx.Session.Delete("openid_verified_uri")
530549
_ = ctx.Session.Delete("openid_signin_remember")
531550
_ = ctx.Session.Delete("openid_determined_email")
@@ -534,13 +553,25 @@ func handleSignInFull(ctx *context.Context, u *user_model.User, remember bool, o
534553
_ = ctx.Session.Delete("twofaRemember")
535554
_ = ctx.Session.Delete("u2fChallenge")
536555
_ = ctx.Session.Delete("linkAccount")
537-
if err := ctx.Session.Set("uid", u.ID); err != nil {
556+
557+
// Save the session
558+
_ = ctx.Session.Release()
559+
560+
// Regenerate the session copying the old data to the new one
561+
newSess, _ := ctx.Session.RegenerateID(ctx.Resp, ctx.Req)
562+
563+
// delete the old session
564+
_ = ctx.Session.Flush()
565+
566+
// Now set our login data
567+
if err := newSess.Set("uid", u.ID); err != nil {
538568
log.Error("Error setting uid %d in session: %v", u.ID, err)
539569
}
540-
if err := ctx.Session.Set("uname", u.Name); err != nil {
570+
if err := newSess.Set("uname", u.Name); err != nil {
541571
log.Error("Error setting uname %s session: %v", u.Name, err)
542572
}
543-
if err := ctx.Session.Release(); err != nil {
573+
574+
if err := newSess.Release(); err != nil {
544575
log.Error("Unable to store session: %v", err)
545576
}
546577

@@ -799,13 +830,23 @@ func handleOAuth2SignIn(ctx *context.Context, source *login.Source, u *user_mode
799830
// If this user is enrolled in 2FA and this source doesn't override it,
800831
// we can't sign the user in just yet. Instead, redirect them to the 2FA authentication page.
801832
if !needs2FA {
802-
if err := ctx.Session.Set("uid", u.ID); err != nil {
833+
// save the current session
834+
_ = ctx.Session.Release()
835+
836+
// regenerate the session - copying data from the old session to the new.
837+
newSess, _ := ctx.Session.RegenerateID(ctx.Resp, ctx.Req)
838+
839+
// delete the old session
840+
ctx.Session.Flush()
841+
842+
// Set session IDs
843+
if err := newSess.Set("uid", u.ID); err != nil {
803844
log.Error("Error setting uid in session: %v", err)
804845
}
805-
if err := ctx.Session.Set("uname", u.Name); err != nil {
846+
if err := newSess.Set("uname", u.Name); err != nil {
806847
log.Error("Error setting uname in session: %v", err)
807848
}
808-
if err := ctx.Session.Release(); err != nil {
849+
if err := newSess.Release(); err != nil {
809850
log.Error("Error storing session: %v", err)
810851
}
811852

@@ -1199,7 +1240,7 @@ func LinkAccountPostRegister(ctx *context.Context) {
11991240
return
12001241
}
12011242

1202-
ctx.Redirect(setting.AppSubURL + "/user/login")
1243+
handleSignIn(ctx, u, false)
12031244
}
12041245

12051246
// HandleSignOut resets the session and sets the cookies
@@ -1563,13 +1604,23 @@ func handleAccountActivation(ctx *context.Context, user *user_model.User) {
15631604

15641605
log.Trace("User activated: %s", user.Name)
15651606

1566-
if err := ctx.Session.Set("uid", user.ID); err != nil {
1607+
// save the old session
1608+
_ = ctx.Session.Release()
1609+
1610+
// Regenerate the session copying the old data to the new session
1611+
newSess, _ := ctx.Session.RegenerateID(ctx.Resp, ctx.Req)
1612+
1613+
// delete the old session
1614+
ctx.Session.Flush()
1615+
1616+
// Set session IDs
1617+
if err := newSess.Set("uid", user.ID); err != nil {
15671618
log.Error("Error setting uid in session[%s]: %v", ctx.Session.ID(), err)
15681619
}
1569-
if err := ctx.Session.Set("uname", user.Name); err != nil {
1620+
if err := newSess.Set("uname", user.Name); err != nil {
15701621
log.Error("Error setting uname in session[%s]: %v", ctx.Session.ID(), err)
15711622
}
1572-
if err := ctx.Session.Release(); err != nil {
1623+
if err := newSess.Release(); err != nil {
15731624
log.Error("Error storing session[%s]: %v", ctx.Session.ID(), err)
15741625
}
15751626

0 commit comments

Comments
 (0)