Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(bitbucket): send redirect_uri on the authorize hop

Max Freedom Pollardbedf5640fd28
diff --git a/backend/app/services/source_connection_service.py b/backend/app/services/source_connection_service.py
index bcf2c4ea..c0e0b51f 100644
--- a/backend/app/services/source_connection_service.py
+++ b/backend/app/services/source_connection_service.py
@@ -618,8 +618,12 @@ class SourceConnectionService:
state = secrets.token_urlsafe(32)
session['source_bitbucket_state'] = state
+ # redirect_uri is what complete_bitbucket_callback posts to the token
+ # endpoint, so the authorize hop has to ask for the same one — the
+ # GitHub and GitLab generators above both send it.
params = {
'client_id': config['client_id'],
+ 'redirect_uri': redirect_uri,
'response_type': 'code',
'state': state,
}
diff --git a/backend/tests/test_source_connection_oauth.py b/backend/tests/test_source_connection_oauth.py
new file mode 100644
index 00000000..2718e36d
--- /dev/null
+++ b/backend/tests/test_source_connection_oauth.py
@@ -0,0 +1,48 @@
+"""Proving tests for the source-connection OAuth authorize URLs.
+
+The two halves of one flow have to name the same callback. `/source-connections
+/<provider>/authorize` requires a `redirect_uri` and hands it to the generator;
+`/source-connections/<provider>/callback` posts that same value to the provider's
+token endpoint (`complete_*_callback`). A generator that accepts the parameter
+and leaves it out of the authorize URL makes the provider return the user to
+whatever callback is registered on the OAuth app instead of the one the panel
+asked for, while the token exchange still claims the one it asked for.
+
+Parametrised across every provider so the next one added is covered by the same
+assertion instead of a fourth copy of it.
+"""
+from urllib.parse import parse_qs, urlsplit
+
+import pytest
+
+from app.services.settings_service import SettingsService
+from app.services.source_connection_service import SourceConnectionService as SC
+
+# provider -> (client-id setting, client-secret setting, generator method)
+PROVIDERS = [
+ ('github', 'source_github_client_id', 'source_github_client_secret',
+ 'generate_github_authorize_url'),
+ ('gitlab', 'source_gitlab_client_id', 'source_gitlab_client_secret',
+ 'generate_gitlab_authorize_url'),
+ ('bitbucket', 'source_bitbucket_client_id', 'source_bitbucket_client_secret',
+ 'generate_bitbucket_authorize_url'),
+]
+
+
[email protected]('provider,id_key,secret_key,generator', PROVIDERS)
+def test_authorize_url_carries_the_callers_redirect_uri(
+ app, provider, id_key, secret_key, generator):
+ redirect_uri = f'https://panel.example/connections/callback/{provider}'
+ with app.test_request_context():
+ SettingsService.set(id_key, 'cid')
+ SettingsService.set(secret_key, 'sec')
+ # Classic OAuth screen: the GitHub App install path is a deliberate
+ # exception with its own coverage in test_github_app_manifest.py.
+ SettingsService.set('source_github_app_slug', '')
+
+ url, state = getattr(SC, generator)(redirect_uri)
+
+ params = parse_qs(urlsplit(url).query)
+ assert params['redirect_uri'] == [redirect_uri]
+ assert params['client_id'] == ['cid']
+ assert params['state'] == [state]