fix(oauth): only re-register on invalid_client, not all 4xx#168
Draft
fix(oauth): only re-register on invalid_client, not all 4xx#168
Conversation
The introspection and code-exchange retry paths were treating every 401/403 as "client credentials are bad" and re-registering. But 401/403 has many other causes (invalid_token, invalid_grant, AS hiccups, empty bodies). Each false-positive register rotates the shared client_secret and amplifies failure across users in multi-user deployments (atxp-pics, atxp-video, atxp-music — see auth dataset showing ~1.2M /register/day with oauth4webapi user-agent). Fix: introduce isInvalidClientError(response) that parses the response body and WWW-Authenticate header per RFC 6749 §5.2 and RFC 6750. Re-register only when the failure is explicitly identified as `error=invalid_client`. Other 4xx errors propagate to the caller as before. Auto-DCR on first use and self-healing on genuine credential failure are both preserved. Tests: 4 unit tests + 4 real-HTTP integration tests + 2 shared-client multi-user scenarios verifying that 50 requests with 10 false-positive 401s produce zero /register calls (pre-fix would have produced ~10). 2 new false-positive guard tests on the client-side token-exchange path. Existing "should re-register if code exchange fails" test updated to use proper invalid_client error body (matching the live auth.atxp.ai contract, verified via curl probes).
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
Stops the SDK from treating every 401/403 from the auth server as "my client credentials are bad" and re-registering. Re-registration is now scoped to the one error code that actually means client creds are bad: `invalid_client` (per RFC 6749 §5.2 / RFC 6750).
This is the dominant driver of the ~1.2M /register/day we see in the `auth` dataset (oauth4webapi user-agent). Each false-positive register rotates the shared `client_secret`, which then makes other in-flight users' tokens fail introspection — triggering more registers. Fix preserves auto-DCR on first use and self-healing on genuine credential failure; only the false-positive path is removed.
Changes
Verification
Live auth.atxp.ai contract (curl probes, no DB writes):
The AS already returns the right error format. The fix relies on it.
Unit tests (`atxp-common` 113 pass, `atxp-client` 210 pass, `atxp-server` 190 pass):
Real-HTTP integration tests (`oAuthResource.introspectRetry.integration.test.ts`):
A local Express AS exercises the full oauth4webapi → fetch → real HTTP path. Same 4 scenarios pass end-to-end.
Shared-client (multi-user atxp-pics-style) scenarios in the same integration file:
The second scenario is the one that maps to the prod volume — that exact pattern (occasional 401 invalid_token amplified into mass DCR via the false-positive path) explains the observed 1.2M/day.
Out of scope (follow-up)
Test plan