Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(github): fail the sign-in visibly when authorize cannot send the browser on

Petr Bauch7b88fa7fc5ae
diff --git a/core/switch_core/gateway/github_connections.py b/core/switch_core/gateway/github_connections.py
index 568b88967..4f9944ac5 100644
--- a/core/switch_core/gateway/github_connections.py
+++ b/core/switch_core/gateway/github_connections.py
@@ -1,4 +1,5 @@
import asyncio
+import html
import logging
import secrets
from typing import Annotated, cast
@@ -51,6 +52,7 @@ STORE = ServiceConnectionStore()
KIND = "github"
INTERRUPTED = "Sign-in was interrupted. Start it again from Switch Console."
+APP_CHANGED = "The GitHub App changed during your sign-in. Start it again."
def service(request: Request) -> GitHubConnections:
@@ -116,7 +118,7 @@ def page(message: str, status: int) -> HTMLResponse:
return HTMLResponse(
"<!doctype html><html lang=en><meta charset=utf-8><meta name=viewport content='width=device-width'>"
"<title>Switch · GitHub</title><body><main><h1>Switch · GitHub</h1><p>"
- + message
+ + html.escape(message)
+ "</p></main></body></html>",
status_code=status,
headers={
@@ -189,6 +191,17 @@ async def start(
}
+async def _failed_before_leaving(
+ flows: SignInFlows, flow: StoredFlow, message: str, status: int
+) -> HTMLResponse:
+ """The browser cannot leave for GitHub: the flow fails with `message`,
+ for Console's poll to read, and the browser is told the same."""
+ await flows.advance(
+ flow, expect=("pending",), to="failed", changes={"error": message}
+ )
+ return page(message, status)
+
+
@router.get("/authorize")
async def authorize(
state: str,
@@ -203,12 +216,15 @@ async def authorize(
flow = await browser_flow(flows, state)
except GitHubError:
return page(INTERRUPTED, 400)
- github = app_for_workspace(
- request, await broker.organization_app_of(flow.tenant_id, "github")
- )
+ try:
+ github = app_for_workspace(
+ request, await broker.organization_app_of(flow.tenant_id, "github")
+ )
+ except HTTPException as refusal:
+ return await _failed_before_leaving(flows, flow, str(refusal.detail), 503)
# Started under another App: the code GitHub issued would be refused.
if flow.details.get("client_id") not in (None, github.client_id):
- return page(INTERRUPTED, 400)
+ return await _failed_before_leaving(flows, flow, APP_CHANGED, 400)
try:
taken = await flows.advance(
flow,
@@ -313,11 +329,10 @@ async def complete(
if recorded is not None and recorded != github.client_id:
# The code was issued to the App the flow started with: with another
# in its place, GitHub would refuse it, and less clearly.
- changed = "The GitHub App changed during your sign-in. Start it again."
await flows.advance(
- flow, expect=("delivered",), to="failed", changes={"error": changed}
+ flow, expect=("delivered",), to="failed", changes={"error": APP_CHANGED}
)
- raise HTTPException(400, changed)
+ raise HTTPException(400, APP_CHANGED)
checking = await flows.advance(
flow, expect=("delivered",), to="checking", changes={}
)
diff --git a/core/tests/switch_core/gateway/test_github_connections.py b/core/tests/switch_core/gateway/test_github_connections.py
index ab29e16df..0ea6211d6 100644
--- a/core/tests/switch_core/gateway/test_github_connections.py
+++ b/core/tests/switch_core/gateway/test_github_connections.py
@@ -28,7 +28,7 @@ from switch_core.db.stores.service_connection_store import ServiceConnectionStor
from switch_core.db.stores.workspace_service_store import WorkspaceServiceStore
from switch_core.gateway.auth import get_current_user
from switch_core.gateway.dependencies import get_config, get_session
-from switch_core.gateway.github_connections import router
+from switch_core.gateway.github_connections import APP_CHANGED, router
from switch_core.keys import Keyring
from switch_core.providers.github import GitHubConnections, GitHubError, GitHubReach
from switch_core.providers.github_installation import GitHubInstallationCredentials
@@ -824,5 +824,35 @@ class TestOrganizationsApp:
# The organization saves its own App while the browser is on its way.
organizations.app = _saved(_organization_github("organization-client"))
left = await client.get(started.json()["url"])
+ polled = (await client.get(BASE + f"/flows/{state}")).json()
assert left.status_code == 400
+ assert APP_CHANGED in left.text
+ assert polled["status"] == "failed"
+ assert polled["error"] == APP_CHANGED
+
+ async def test_an_app_unusable_by_authorize_fails_the_flow_with_its_problem(
+ self, github_app
+ ) -> None:
+ _, server_github, identity, factory = github_app
+ organizations = _Organizations(None)
+ app = _process(server_github, identity, factory, False, organizations)
+ problem = "Your organization's GitHub app can't be used: it is <broken>."
+
+ async with await _client(app) as client:
+ state = secrets.token_urlsafe(32)
+ started = await client.post(
+ BASE + "/flows",
+ json={"port": 12345, "state": state, "completion_secret": SECRET},
+ )
+ assert started.status_code == 200
+ organizations.app = _saved(None, problem)
+ left = await client.get(started.json()["url"])
+ polled = (await client.get(BASE + f"/flows/{state}")).json()
+
+ assert left.status_code == 503
+ assert left.headers["content-type"].startswith("text/html")
+ assert left.headers["cache-control"] == "no-store"
+ assert "it is &lt;broken&gt;." in left.text
+ assert polled["status"] == "failed"
+ assert polled["error"] == problem