Skip to content

fix(auth): rotate session on login and invalidate on logout (#925)#1447

Draft
krishankumar01 wants to merge 6 commits intomasterfrom
kkumar-gcc/#925
Draft

fix(auth): rotate session on login and invalidate on logout (#925)#1447
krishankumar01 wants to merge 6 commits intomasterfrom
kkumar-gcc/#925

Conversation

@krishankumar01
Copy link
Copy Markdown
Member

@krishankumar01 krishankumar01 commented Apr 20, 2026

📑 Description

Closes goravel/goravel#925

@coderabbitai summary

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings April 20, 2026 01:07
@krishankumar01 krishankumar01 requested a review from a team as a code owner April 20, 2026 01:07
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 15.38462% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.15%. Comparing base (06f6946) to head (b2739a3).
⚠️ Report is 12 commits behind head on master.

Files with missing lines Patch % Lines
session/cookie.go 0.00% 22 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##           master    #1447    +/-   ##
========================================
  Coverage   69.15%   69.15%            
========================================
  Files         362      364     +2     
  Lines       28315    28491   +176     
========================================
+ Hits        19581    19703   +122     
- Misses       7870     7916    +46     
- Partials      864      872     +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to improve session-based authentication security by rotating the session on login and invalidating it on logout (addressing goravel/goravel#225).

Changes:

  • Regenerate the session (destroying the old ID) during SessionGuard.LoginUsingID.
  • Invalidate the session during SessionGuard.Logout.
  • Update/add unit tests to assert Regenerate(true) and Invalidate() are called and to cover error paths.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
auth/session_guard.go Regenerates session on login and invalidates session on logout.
auth/session_guard_test.go Updates expectations for session rotation/invalidation and adds error-path tests.

Comment thread auth/session_guard.go
Comment on lines +80 to 85
if err := r.session.Regenerate(true); err != nil {
return "", err
}

r.session.Put(sessionName, key)

Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoginUsingID regenerates the session ID, but the session cookie is currently written in session/middleware/start_session.go before req.Next() (StartSession sets the cookie from the pre-regenerate s.GetID()). That means the response will still contain the old session ID after Regenerate(true), so the rotated ID won’t reach the client (login may not persist, and the fixation mitigation is ineffective). Consider updating the session middleware to set/overwrite the session cookie after req.Next() (or otherwise ensure the cookie value is refreshed whenever Regenerate is called).

Copilot uses AI. Check for mistakes.
Comment thread auth/session_guard.go
Comment on lines 89 to 91
func (r *SessionGuard) Logout() error {
sessionName := r.getSessionName()
r.session.Forget(sessionName)

return nil
return r.session.Invalidate()
}
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logout now calls session.Invalidate(), which also regenerates the session ID. With the current StartSession middleware writing the session cookie before req.Next(), the client may continue to receive/send the pre-invalidate session ID, so the new ID from Invalidate() won’t be propagated. This can lead to inconsistent logout behavior and undermines the intended invalidation. Align cookie-writing with post-handler session ID (e.g., write/overwrite the cookie after req.Next()).

Copilot uses AI. Check for mistakes.
Comment thread auth/session_guard_test.go Outdated
Comment on lines +223 to +229
func (s *SessionGuardTestSuite) TestLoginUsingID_RegenerateError() {
s.mockSession.EXPECT().Regenerate(true).Return(assert.AnError).Once()

token, err := s.sessionGuard.LoginUsingID(1)
s.Empty(token)
s.ErrorIs(err, assert.AnError)
}
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test name TestLoginUsingID_RegenerateError breaks the file’s prevailing naming pattern (Test_<Something> with underscores, e.g. Test_Login, Test_InvalidKey). Renaming it to match the existing convention will keep the suite consistent and easier to scan/filter.

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings April 20, 2026 01:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread auth/session_guard.go Outdated
Comment on lines +104 to +108
if configFacade == nil {
return
}

r.ctx.Response().Cookie(http.Cookie{
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reissueSessionCookie() unconditionally calls r.ctx.Response().Cookie(...). If Response() can be nil (e.g., in custom/test contexts), this will panic. Consider guarding by assigning resp := r.ctx.Response() and returning early when resp == nil before calling resp.Cookie(...) (similar for r.ctx if needed).

Suggested change
if configFacade == nil {
return
}
r.ctx.Response().Cookie(http.Cookie{
if configFacade == nil || r.ctx == nil {
return
}
resp := r.ctx.Response()
if resp == nil {
return
}
resp.Cookie(http.Cookie{

Copilot uses AI. Check for mistakes.
Comment thread auth/session_guard.go Outdated
Comment on lines +108 to +117
r.ctx.Response().Cookie(http.Cookie{
Name: r.session.GetName(),
Value: r.session.GetID(),
Expires: carbon.Now().AddMinutes(configFacade.GetInt("session.lifetime", 120)).StdTime(),
Path: configFacade.GetString("session.path"),
Domain: configFacade.GetString("session.domain"),
Secure: configFacade.GetBool("session.secure"),
HttpOnly: configFacade.GetBool("session.http_only"),
SameSite: configFacade.GetString("session.same_site"),
})
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cookie construction here duplicates the logic in session/middleware/start_session.go (same config keys and fields). To reduce drift (e.g., if new cookie attributes are added later), consider factoring cookie-building into a shared helper (likely in the session package) and reusing it from both places.

Copilot uses AI. Check for mistakes.
@krishankumar01 krishankumar01 marked this pull request as draft April 20, 2026 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Session ID not rotated on login/logout (session fixation risk)

2 participants