Bitbucket Cloud import: refreshed OAuth credentials are never persisted to the session, permanently breaking imports with refresh_token is invalid
Summary
Import::BitbucketController refreshes expired Bitbucket OAuth tokens but never persists the refreshed credentials back into the session. Bitbucket Cloud rotates refresh tokens, invalidating the previous one on every refresh, so the token stored in the session is dead immediately after the first refresh. Every subsequent request then fails with unauthorized_client: refresh_token is invalid.
Once this happens the user's session is permanently unusable for Bitbucket Cloud imports, and there is no way to recover from the interface.
Confirmed on GitLab.com.
Root cause
Bitbucket::OauthConnection#perform_refresh! updates credentials in memory only, in gems/gitlab-bitbucket/lib/bitbucket/oauth_connection.rb:
def perform_refresh!
response = connection.refresh!
@token = response.token
@expires_at = response.expires_at
@expires_in = response.expires_in
@refresh_token = response.refresh_token
@connection = nil
endImport::BitbucketController rebuilds the client from the session on every request, and nothing writes the rotated values back:
def client
@client ||= Bitbucket::Client.new(credentials.merge(client_options), http_client: Import::Clients::HTTP)
end
def credentials
{
token: session[:bitbucket_token],
expires_at: session[:bitbucket_expires_at],
expires_in: session[:bitbucket_expires_in],
refresh_token: session[:bitbucket_refresh_token]
}
endThe session keys are written in exactly one place, #callback, after the initial authorization code exchange.
The gem already exposes the hooks needed to close this gap and neither is used on the controller path:
Bitbucket::OauthConnection#adopt_credentials- the
refresh_strategy:constructor argument, whichrefresh!delegates to when present
Sequence
- The access token expires. GitLab refreshes successfully. Bitbucket issues new access and refresh tokens and invalidates the old refresh token.
- The refreshed credentials exist only on the
OauthConnectioninstance, which is discarded at the end of the request. The session still holds the invalidated refresh token. - Every later request attempts a refresh with the dead token and receives
unauthorized_client: refresh_token is invalid.
Observed exception
OAuth2::Error: unauthorized_client: refresh_token is invalid
{"error": "unauthorized_client", "error_description": "refresh_token is invalid"}Abridged backtrace:
oauth2 (2.0.25) lib/oauth2/access_token.rb:222:in `refresh'
gems/gitlab-bitbucket/lib/bitbucket/oauth_connection.rb:54:in `perform_refresh!'
gems/gitlab-bitbucket/lib/bitbucket/oauth_connection.rb:50:in `refresh_if_expired!'
gems/gitlab-bitbucket/lib/bitbucket/oauth_connection.rb:76:in `get_with_retry'
gems/gitlab-bitbucket/lib/bitbucket/paginator.rb:62:in `fetch_next_page'
gems/gitlab-bitbucket/lib/bitbucket/client.rb:175:in `all_workspaces'
gems/gitlab-bitbucket/lib/bitbucket/client.rb:114:in `multi_workspace_repos'
app/controllers/import/bitbucket_controller.rb:172:in `bitbucket_repos'
app/controllers/import/bitbucket_controller.rb:39:in `status'Accounts with many workspaces are affected sooner and more often, because all_workspaces paginates through every workspace and widens the window in which a refresh occurs mid-request.
Steps to reproduce
- Authorize a Bitbucket Cloud account on the import page.
- Wait for the Bitbucket access token to expire, or use an account with enough workspaces that a refresh happens during pagination.
- Load the repository list again.
Current behaviour
Every request fails with refresh_token is invalid. The user cannot recover by reloading the page, because bitbucket_auth only tests whether the token is blank:
def bitbucket_auth
go_to_bitbucket_for_permissions if session[:bitbucket_token].blank?
endThe invalidated token is present, not blank, so the re-authorization path is never entered from the before_action.
Expected behaviour
Refreshed credentials are written back to the session so the rotated refresh token is used on the next request, and a session survives token expiry without manual intervention.
Proposed fix
Persist the refreshed credentials on the controller path, either by injecting a refresh_strategy that writes to the session, or by copying the connection's credentials back into the session after a refresh. Both hooks already exist in the gem.
A secondary consideration: when the refresh token really is unrecoverable, the session copies should be cleared so that bitbucket_auth sees a blank token and performs a clean re-authorization, rather than retrying a dead token indefinitely.
Impact
Reported by a customer on GitLab.com whose users could not load the Bitbucket Cloud repository list across repeated attempts. The failure is permanent for the affected session and invisible to the user, because the resulting error is rescued into a redirect that the browser blocks under CSP. See the companion issue for that behaviour.
Workaround
Sign out of GitLab and sign back in, which clears the session-stored Bitbucket credentials and lets the import page perform a clean OAuth authorization. A private browsing window has the same effect. Reloading the import page does not help.