github-app-oauth: refresh_token rotation breaks on 2nd refresh — connection_config.userCredentials never updated
Summary
For any provider with auth_mode: CUSTOM whose refresh path returns plain OAuth2Credentials (no user/app wrapper), Nango updates connection.credentials after a successful refresh but never updates connection_config.userCredentials. Since getFreshOAuth2Credentials reads the refresh_token from connection_config.userCredentials (not credentials) when auth_mode === 'CUSTOM', the next refresh re-sends the original refresh_token — which the provider has invalidated by rotation, returning a BAD_REFRESH_TOKEN-shaped error that Nango surfaces as HTTP 500 from GET /connection/{id}.
The visible failure mode for github-app-oauth in self-hosted deployments: every connection deterministically breaks ~8 hours after its first successful refresh (GitHub's access_token TTL). Each user gets one working session, then breaks.
Repro
- Self-hosted Nango with a
github-app-oauthintegration (any GitHub App inproviders.yaml—auth_mode: CUSTOM). - User completes OAuth via Connect UI. Connection row written; post-connection hook seeds
connection_config.userCredentialswith the initial OAuth response (correct). - User triggers any operation that calls
GET /connection/{id}after the access_token expires (default 8h for GitHub Apps). Nango callsgetFreshOAuth2Credentials, sendsconnection_config.userCredentials.refresh_tokentohttps://github.com/login/oauth/access_token, gets back rotated tokens, writes them toconnection.credentials.connection_config.userCredentialsremains untouched. - Wait until the new access_token expires (another 8h). Trigger
GET /connection/{id}again. - Nango sends the original refresh_token (still in
connection_config.userCredentials.refresh_token). GitHub rejects it (already rotated and invalidated in step 3).GET /connection/{id}returns 500.
Empirically verified by inspecting _nango_connections directly in a self-hosted Nango PG. Two connections at different points in their refresh lifecycle:
| connection state | credentials_expires_at column |
connection_config.userCredentials.expires_at |
|---|---|---|
| One refresh completed (now broken on next refresh) | refresh+8h | original OAuth completion |
| One refresh completed (next will be the broken one) | refresh+8h | original OAuth completion |
The drift between the two expires_at values is the smoking gun: the credentials column advances with each refresh; userCredentials is frozen at the post-connection-hook write. Both connections will then fail on their next refresh.
Root cause
Two paths read/write asymmetrically.
Read — packages/shared/lib/clients/oauth2.client.ts:112:
const tokenSource = provider.auth_mode === 'CUSTOM' ? connection.connection_config['userCredentials'] : credentials;
For CUSTOM, reads refresh_token from userCredentials.
Write — packages/shared/lib/services/connections/credentials/refresh.ts:461-471:
if ('user' in newCredentials && newCredentials.user && 'app' in newCredentials && newCredentials.app) {
connectionToRefresh.connection_config['userCredentials'] = newCredentials.user;
connectionToRefresh.credentials = newCredentials.app;
} else if ('user' in newCredentials && newCredentials.user) {
connectionToRefresh.connection_config['userCredentials'] = newCredentials.user;
} else if ('app' in newCredentials && newCredentials.app) {
connectionToRefresh.credentials = newCredentials.app;
} else {
// Use newCredentials as fallback when neither user nor app are specifically present
connectionToRefresh.credentials = newCredentials;
}
The else branch fires for github-app-oauth (plain OAuth2Credentials from getFreshOAuth2Credentials, no user/app wrapper). It writes only to credentials, never to userCredentials. Next refresh reads the stale userCredentials.refresh_token per oauth2.client.ts:112.
#4341 (merged 2025-07-24, "feat(refresh): implement refresh logic for GitHub OAuth app user credentials") introduced the CUSTOM read-from-userCredentials path without the corresponding write-back. #3995 was a previous attempt in the same area; reverted next day (commit b20e70d0).
Proposed fix
Mirror the userCredentials write in the fallback else-branch for auth_mode === 'CUSTOM' providers receiving plain OAuth2 credentials, keeping the read source and write source in lockstep.
PR submitted with the 4-line patch and a regression test (currently no test in refresh.integration.test.ts covers the userCredentials write path).
Environment
- Nango self-hosted, latest
masterverified. - Provider:
github-app-oauth(default frompackages/providers/providers.yaml). - Symptom appears for every user, ~8h after their first successful refresh.
Workaround for affected self-hosters
Until the fix lands, the user-recovery path is to delete the broken connection (DELETE /connection/{id}?provider_config_key=<key>). The user re-OAuths through Connect, the post-connection hook seeds userCredentials correctly, and they get one more working session before the bug recurs.
This issue is stale because it has been open for 30 days with no activity.
I ran into a related failure pattern while reading this issue and the other OAuth reports (#6825, #7098, #6416, #5849): the token refresh fails or goes stale, but nothing shows the developer which step went wrong.
For this issue specifically, the cause is that the rotated refresh token is not saved back to connection_config, so the second refresh uses the old token.
I built a small CLI, nango-doctor, that detects this pattern from the connection data and prints the likely root cause. It might help when debugging or when writing a test for a fix: https://github.com/Shyam525/nango-doctor
Happy to open a PR or write a test case if that would help.
This issue is stale because it has been open for 30 days with no activity.