Skip to content

Commit a7129df

Browse files
authored
fix: possible panic if refresh token has a null session_id (#1822)
## What kind of change does this PR introduce? * Prior to the `auth.sessions` table being created, some refresh tokens can contain a null `session_id`. In those cases, attempting to use those refresh tokens to obtain a new session will result in a panic. * This PR creates a new session for those refresh tokens that do not have a `session_id` to prevent panics from happening. ## What is the current behavior? Please link any relevant issues here. ## What is the new behavior? Feel free to include screenshots if it includes visual changes. ## Additional context Add any other context or screenshots.
1 parent fa020d0 commit a7129df

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

internal/api/token_refresh.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,25 @@ func (a *API) RefreshTokenGrant(ctx context.Context, w http.ResponseWriter, r *h
5656
return oauthError("invalid_grant", "Invalid Refresh Token: User Banned")
5757
}
5858

59-
if session != nil {
60-
result := session.CheckValidity(retryStart, &token.UpdatedAt, config.Sessions.Timebox, config.Sessions.InactivityTimeout)
59+
if session == nil {
60+
// a refresh token won't have a session if it's created prior to the sessions table introduced
61+
if err := db.Destroy(token); err != nil {
62+
return internalServerError("Error deleting refresh token with missing session").WithInternalError(err)
63+
}
64+
return badRequestError(ErrorCodeSessionNotFound, "Invalid Refresh Token: No Valid Session Found")
65+
}
6166

62-
switch result {
63-
case models.SessionValid:
64-
// do nothing
67+
result := session.CheckValidity(retryStart, &token.UpdatedAt, config.Sessions.Timebox, config.Sessions.InactivityTimeout)
6568

66-
case models.SessionTimedOut:
67-
return oauthError("invalid_grant", "Invalid Refresh Token: Session Expired (Inactivity)")
69+
switch result {
70+
case models.SessionValid:
71+
// do nothing
6872

69-
default:
70-
return oauthError("invalid_grant", "Invalid Refresh Token: Session Expired")
71-
}
73+
case models.SessionTimedOut:
74+
return oauthError("invalid_grant", "Invalid Refresh Token: Session Expired (Inactivity)")
75+
76+
default:
77+
return oauthError("invalid_grant", "Invalid Refresh Token: Session Expired")
7278
}
7379

7480
// Basic checks above passed, now we need to serialize access

0 commit comments

Comments
 (0)