Skip to content

fix(seedless-onboarding): update tests for optional refreshToken and proactive renewal#8148

Open
himanshuchawla009 wants to merge 7 commits intomainfrom
fix/seedless-onboarding-refresh-token-tests
Open

fix(seedless-onboarding): update tests for optional refreshToken and proactive renewal#8148
himanshuchawla009 wants to merge 7 commits intomainfrom
fix/seedless-onboarding-refresh-token-tests

Conversation

@himanshuchawla009
Copy link
Contributor

@himanshuchawla009 himanshuchawla009 commented Mar 9, 2026

  • Replace stale concurrent-rotation test with assertion that authenticate never receives refreshToken from the token-refresh path
  • Add tests verifying renewRefreshToken is called proactively after vault update when vault is unlocked, and skipped when locked
  • Add test covering the renewRefreshToken error-swallow path in refreshAuthTokens (catch block log call)
  • Add renewRefreshToken tests for vault-unlocked (no password) path and vault-locked-no-password error path
  • Fix two existing tests that captured state.refreshToken after the call; proactive renewal now rotates the token, so capture it before the call

Explanation

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Changes token refresh/re-authentication and refresh-token rotation logic in an authentication-adjacent controller, including a breaking removal of the public renewRefreshToken API. Risk is moderated by extensive test updates but could affect token lifecycle behavior when vault locked/unlocked.

Overview
Token refresh now re-authenticates via a stricter private #reAuthenticate path (only idTokens/accessToken/metadataAccessToken) and, when the vault is unlocked, proactively rotates the refresh/revoke token pair via a new private #rotateRefreshToken (with an opt-out refreshAuthTokens({ skipRenewRefreshToken }) and swallowed rotation errors).

Token expiry checks were unified around a new isTokenNearExpiry helper: access/metadata tokens refresh when entering the last 10% of lifetime (requires iat), while node auth tokens use exact expiry; fetchMetadataAccessCreds now uses the same threshold. Tests and changelog were updated accordingly, including new cases for lock skipping, missing revokeToken in vault, proactive rotation behavior, and updated expectations around refresh token mutation.

Written by Cursor Bugbot for commit 358e1b9. This will update automatically on new commits. Configure here.

…proactive renewal

- Replace stale concurrent-rotation test with assertion that authenticate
  never receives refreshToken from the token-refresh path
- Add tests verifying renewRefreshToken is called proactively after vault
  update when vault is unlocked, and skipped when locked
- Add test covering the renewRefreshToken error-swallow path in
  refreshAuthTokens (catch block log call)
- Add renewRefreshToken tests for vault-unlocked (no password) path and
  vault-locked-no-password error path
- Fix two existing tests that captured state.refreshToken after the call;
  proactive renewal now rotates the token, so capture it before the call
Token expiry helpers now use a 90% lifetime threshold:
- Access/metadata tokens refresh when <10% lifetime remains (uses iat)
- Node auth tokens fall back to exact-expiry check (no reliable iat)

Introduces module-level isTokenNearExpiry(exp, iat?) helper used by
checkNodeAuthTokenExpired, checkMetadataAccessTokenExpired, and
checkAccessTokenExpired.
Cover all source changes introduced in this branch:
- authenticate() optional refreshToken param
- proactive renewRefreshToken after JWT refresh in #doRefreshAuthTokens
- renewRefreshToken vault-unlock path (optional password + skipLock)
- 90% lifetime threshold for proactive token expiry checks
@himanshuchawla009 himanshuchawla009 marked this pull request as ready for review March 10, 2026 07:41
@himanshuchawla009 himanshuchawla009 requested review from a team as code owners March 10, 2026 07:41
@himanshuchawla009
Copy link
Contributor Author

@metamaskbot publish-preview

vaultData: updatedVaultData,
pwEncKey,
});

Copy link
Contributor

Choose a reason for hiding this comment

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

should we add option to skip proactive renewRefreshToken ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good suggestion, would be useful to skip in some cases.

*/
async renewRefreshToken(password: string): Promise<void> {
return await this.#withControllerLock(async () => {
async renewRefreshToken(
Copy link
Contributor

@ieow ieow Mar 10, 2026

Choose a reason for hiding this comment

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

should we consider to allow renewRefreshToken happend only when the seedless controller vault is unlocked?

we can always call submitPassword before renewRefreshToken if there are scenario where the vault is locked and we have password

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with this. We can get rid of password from the params and rely on the cached encryptionKey only (which is only available when the vault is unlocked).

// NOTE: refreshToken is intentionally omitted — renewRefreshToken is the
// sole owner of state.refreshToken. Passing it here would risk overwriting
// a token that renewRefreshToken rotated concurrently during the async
// toprfClient.authenticate() call below.
Copy link
Contributor

Choose a reason for hiding this comment

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

is it cleaner if we have re-authenticate function which only accept

    idTokens,
    accessToken,
    metadataAccessToken,

Instead making the authenticate accept different input structure type

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think refactor the authenticate would be better, so that we can make stricter validation, i.e. first time authentication must provide accessToken and revokeToken while re-authenticate may not.

*/
async renewRefreshToken(password: string): Promise<void> {
return await this.#withControllerLock(async () => {
async renewRefreshToken(
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed we call this method internally, inside #doRefreshAuthTokens() so, I think we can make this private already.

Clients should not decide when to call this method, imo.

// NOTE: refreshToken is intentionally omitted — renewRefreshToken is the
// sole owner of state.refreshToken. Passing it here would risk overwriting
// a token that renewRefreshToken rotated concurrently during the async
// toprfClient.authenticate() call below.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think refactor the authenticate would be better, so that we can make stricter validation, i.e. first time authentication must provide accessToken and revokeToken while re-authenticate may not.

- Make renewRefreshToken private (#rotateRefreshToken): vault-unlock only,
  no password param — callers must unlock first via submitPassword
- Add #reAuthenticate private method for JWT-refresh re-auth path, accepting
  only {idTokens, accessToken, metadataAccessToken} from the refresh service
- Add skipRenewRefreshToken option to refreshAuthTokens to allow callers to
  opt out of the proactive rotation
- Update tests: renewRefreshToken describe converted to #rotateRefreshToken
  (tested via refreshAuthTokens), add skipRenewRefreshToken test, add
  skipLock branch coverage for authenticate
- fetchMetadataAccessCreds: use isTokenNearExpiry(exp, iat) instead of
  exact expiry so it is consistent with checkMetadataAccessTokenExpired
- isTokenNearExpiry: guard against malformed tokens where iat >= exp
  (zero or negative lifetime) by falling back to exact-expiry check
… explicit options

Callers passing explicit options (e.g. skipRenewRefreshToken: true) must
not be served by an in-flight request that was started with different
options. Only default-options calls (no explicit flags) share the pending
promise; explicit-options calls each get their own independent request.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

revokeToken: newRevokeToken,
},
pwEncKey,
});
Copy link

Choose a reason for hiding this comment

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

State updated before vault persist, error silently swallowed

Medium Severity

In #rotateRefreshToken, this.update() writes newRefreshToken and newRevokeToken to persisted state before #updateVault re-encrypts the vault with the new revokeToken. If the vault update fails, state holds the new tokens while the vault retains the old revokeToken. Because the caller in #doRefreshAuthTokens swallows this error via .catch(), the inconsistency is silent — no rollback of the state update occurs and #addRefreshTokenToRevokeList is skipped. On next vault unlock, #cachedDecryptedVaultData will contain the old revokeToken while state.revokeToken holds the new one, creating a mismatch that could cause subsequent rotation attempts to use a stale revokeToken.

Additional Locations (1)
Fix in Cursor Fix in Web

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.

3 participants