Preserve the bearer token when a refresh yields nothing (1.2.1) - #3
Merged
Conversation
BearerTokenAuth.onAuthError called store.setToken(newToken ?? null) after invoking the refresh callback, so a refresh that returned null -- no refresh configured, or an error a refresh cannot fix such as a permission 403 -- overwrote the still-valid token with null. Every subsequent request then went out with no Authorization header and failed with 401, turning one 401/403 into a cascade of unauthenticated failures across unrelated requests. Now the stored token is only replaced when the refresh actually produces a new one; otherwise it is left intact and the failing request surfaces its original error. Regression test asserts a null refresh preserves the token and that onRequest still attaches it. Full suite: 262 passing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bugfix (1.2.1) for a token-clobbering cascade in
BearerTokenAuth, surfaced by a real audit run against a live Keap sandbox.The bug
On a
401/403,onAuthErrorinvokes the refresh callback and then persists the result:When the refresh returns
null— no refresh configured, or an error a refresh can't fix, such as a permission403— this overwrites the still-valid token withnull. From then ononRequestfinds no token and sends every request with noAuthorizationheader, so unrelated later requests fail with401.Impact: a single permission-denied resource strips auth from every subsequent request. In the audit that surfaced this, one
403on/campaignscascaded into misleading empty401s on/users,/products,/orders, and/subscriptions— hiding each resource's real status and message.Reproduction
A
403on the first request, then a second request to a healthy endpoint:The second request went out unauthenticated.
Fix
Only persist a successful refresh; leave the stored token untouched otherwise:
The failing request still gives up and surfaces its original error (unchanged), but the valid token survives for later requests, so each resource reports its own accurate status.
Tests
Adds a regression test: a
nullrefresh result preserves the stored token,onAuthErrordoes not retry, andonRequeststill attaches the original token. Full suite: 262 passing.