Skip to content

Cognito rotation of refresh tokens

2

asked 3 years ago898 views

1 Answer
0

Cognito seriously needs to implement refresh token rotation to provide proper security to SPA clients. Here are some relevant excerpts from BCPs:

OAuth 2.0 for Browser-Based Applications (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-browser-based-apps-19#name-refresh-tokens-3)

In particular, authorization servers:

  • MUST either rotate refresh tokens on each use OR use sender-constrained refresh tokens as described in [oauth-security-topics] Section 4.14.2
  • MUST either set a maximum lifetime on refresh tokens OR expire if the refresh token has not been used within some amount of time upon issuing a rotated refresh token, MUST NOT extend the lifetime of the new refresh token beyond the lifetime of the initial refresh token if the refresh token has a preestablished expiration time

Limiting the overall refresh token lifetime to the lifetime of the initial refresh token ensures a stolen refresh token cannot be used indefinitely.

For example:

  • A user authorizes an application, issuing an access token that lasts 10 minutes, and a refresh token that lasts 8 hours
  • After 10 minutes, the initial access token expires, so the application uses the refresh token to get a new access token
  • The authorization server returns a new access token that lasts 10 minutes, and a new refresh token that lasts 7 hours and 50 minutes
  • This continues until 8 hours pass from the initial authorization
  • At this point, when the application attempts to use the refresh token after 8 hours, the request will fail and the application will have to re-initialize an Authorization Code flow that relies on the user's authentication or previously established session

OAuth 2.0 Security Best Current Practice (https://datatracker.ietf.org/doc/html/draft-ietf-oauth-security-topics#name-refresh-token-protection)

Authorization servers MUST utilize one of these methods to detect refresh token replay by malicious actors for public clients:

  • Sender-constrained refresh tokens: the authorization server cryptographically binds the refresh token to a certain client instance, e.g., by utilizing [RFC8705] or [RFC9449].
  • Refresh token rotation: the authorization server issues a new refresh token with every access token refresh response. The previous refresh token is invalidated but information about the relationship is retained by the authorization server. If a refresh token is compromised and subsequently used by both the attacker and the legitimate client, one of them will present an invalidated refresh token, which will inform the authorization server of the breach. The authorization server cannot determine which party submitted the invalid refresh token, but it will revoke the active refresh token. This stops the attack at the cost of forcing the legitimate client to obtain a fresh authorization grant.

To further compound Cognito's lack of built-in support for automatically rotating access tokens is the fact that it's impossible to ask Cognito to issue a new refresh token with progressively shorter expiration periods and without forcing the user to re-authenticate (please correct me if I'm wrong). As a result, it seems Cognito is NOT following best practices with regard to securely handling refresh tokens for web clients. This is a serious issue, and it means application developers need to spend extra effort to work around this limitation (for example, securely storing the refresh token server-side and having extra logic to deal with it instead of simply sending it to the client, or implementing sender-constrained refresh tokens using something like DPoP (https://datatracker.ietf.org/doc/rfc9449/)).

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.