From 7c6507823f24a172464d6b239a36386fcf9da4bc Mon Sep 17 00:00:00 2001 From: Nam Rodriguez Date: Tue, 3 Nov 2026 14:56:43 -0500 Subject: [PATCH] GitHub sign-in only where it works The sign-in page offered Continue with GitHub on every install, and an install with no GitHub OAuth client of its own (hybrid, on CoderHelm's app) sent people to GitHub with an empty client_id, which 404s. - GET /auth/methods (public, outside the sign-in rate limit) says which sign-in methods work: password always, Google on the hosted side only, GitHub only with the install's own client id and secret. - GET /auth/github and its callback with no client go back to the sign-in screen with signin_error=not_set_up instead of to GitHub. - New github_sign_in setup check: warns when the install's own app has no client (or half of one), skip on CoderHelm's app; never blocks an update. - Hybrid sign-in through CoderHelm stays off: it would let the hosted side vouch for people on the install. --- docs/self-host/check-your-install.md | 2 +- docs/self-host/sign-in.md | 4 +- services/gateway/src/main.rs | 3 + services/gateway/src/routes/auth.rs | 31 ++++++ services/gateway/src/routes/oauth_signin.rs | 90 ++++++++++++++++ services/gateway/src/routes/setup_checks.rs | 107 +++++++++++++++++++- 6 files changed, 232 insertions(+), 5 deletions(-) diff --git a/docs/self-host/check-your-install.md b/docs/self-host/check-your-install.md index c437529b4..fe79078c5 100644 --- a/docs/self-host/check-your-install.md +++ b/docs/self-host/check-your-install.md @@ -210,7 +210,7 @@ Install admins (`admins` in the config) who are team admins can call these while } ``` -`status` is `pass`, `warn`, `fail` or `skip` (an optional integration that isn't set up). `fix` has `text` and any of `command` (to copy), `url` (outside CoderHelm) and `href` (a dashboard page). `ready` is true when nothing fails. Keys: `url`, `domain`, `github_keys`, `github_app`, `github_permissions`, `github_installations`, `github_webhook`, `email_sender`, `email_identity`, `email_sandbox`, `email_cognito`, `model`, `dlq`, `updates`, `slack`, `teams`, `jira`, `aws_connections`. A self-hosted install has no Google sign-in, so there is no `google_sign_in` check. On an install that uses CoderHelm's GitHub App (hybrid), `github_app` and `github_installations` say whether the app is linked instead, and never block an update. +`status` is `pass`, `warn`, `fail` or `skip` (an optional integration that isn't set up). `fix` has `text` and any of `command` (to copy), `url` (outside CoderHelm) and `href` (a dashboard page). `ready` is true when nothing fails. Keys: `url`, `domain`, `github_keys`, `github_app`, `github_permissions`, `github_installations`, `github_webhook`, `github_sign_in`, `email_sender`, `email_identity`, `email_sandbox`, `email_cognito`, `model`, `dlq`, `updates`, `slack`, `teams`, `jira`, `aws_connections`. A self-hosted install has no Google sign-in, so there is no `google_sign_in` check. On an install that uses CoderHelm's GitHub App (hybrid), `github_app` and `github_installations` say whether the app is linked instead, and never block an update. `github_sign_in` warns when the install's own GitHub App has no OAuth client id and secret stored (or only one of them): without both, the sign-in page doesn't offer Continue with GitHub. On an install that uses CoderHelm's GitHub App it is `skip`, since CoderHelm's app can't sign anyone in to the install; people sign in with email and password. It never blocks an update. ### `GET /api/system/domain`, `PUT /api/system/domain`, `DELETE /api/system/domain` diff --git a/docs/self-host/sign-in.md b/docs/self-host/sign-in.md index cbdfee2c5..2ed6c64a1 100644 --- a/docs/self-host/sign-in.md +++ b/docs/self-host/sign-in.md @@ -97,11 +97,11 @@ An install that had Google sign-in on loses it at its next update, which removes ## GitHub accounts -People connect their GitHub account in their own settings, so pull requests and reviews are credited to them. Signing in with GitHub follows the same rules as everything else: it lands on the organization only for someone already in it, invited, or at an allowed domain (by the email on their GitHub account). It uses the GitHub App's own OAuth client, which the GitHub App step stores; nothing to set up. +People connect their GitHub account in their own settings, so pull requests and reviews are credited to them. Signing in with GitHub follows the same rules as everything else: it lands on the organization only for someone already in it, invited, or at an allowed domain (by the email on their GitHub account). It uses the GitHub App's own OAuth client, which the GitHub App step stores; nothing to set up. The sign-in page offers **Continue with GitHub** only while that client's id and secret are both stored (it asks the API, `GET /auth/methods`). An install on CoderHelm's GitHub App (hybrid) has no client of its own, so its sign-in page shows email and password only: signing in through CoderHelm would hand your people's identities to CoderHelm. The `github_sign_in` check in **Settings, Setup** says which applies. ## Single sign-on (SAML, OIDC) -Not available yet: CoderHelm's sign-in page offers email and password, and GitHub. The Cognito user pool is in your account, so an identity provider added there by hand is not shown on the sign-in page and isn't supported. +Not available yet: CoderHelm's sign-in page offers email and password, and GitHub when the install has its own GitHub App. The Cognito user pool is in your account, so an identity provider added there by hand is not shown on the sign-in page and isn't supported. ## Install admins and team roles diff --git a/services/gateway/src/main.rs b/services/gateway/src/main.rs index ae47ee076..7d3035636 100644 --- a/services/gateway/src/main.rs +++ b/services/gateway/src/main.rs @@ -1433,6 +1433,9 @@ async fn main() -> Result<(), Error> { "/integrations/jira/forge-register", post(routes::api::forge_register_urls), ) + // The sign-in methods that work here: the sign-in page reads it on + // every load, so it stays out of the sign-in attempts' rate limit. + .route("/auth/methods", get(routes::auth::methods)) .nest("/auth", auth_routes) .nest("/email", email_routes) .nest("/agent-approvals", approval_link_routes) diff --git a/services/gateway/src/routes/auth.rs b/services/gateway/src/routes/auth.rs index 5f7bc0295..c35fdbb2a 100644 --- a/services/gateway/src/routes/auth.rs +++ b/services/gateway/src/routes/auth.rs @@ -1234,8 +1234,36 @@ async fn google_callback_inner( // ── GitHub OAuth (connect GitHub, not identity) ────────────────────── +/// GET /auth/methods: the sign-in methods that work on this deployment +/// (public, no session). The sign-in page shows these and no others. +pub async fn methods(State(state): State>) -> Response { + let m = oauth_signin::sign_in_methods(super::setup_checks::self_hosted(), github_ready(&state)); + Json(m).into_response() +} + +fn github_ready(state: &AppState) -> bool { + oauth_signin::github_sign_in_ready( + &state.secrets.github_client_id, + &state.secrets.github_client_secret, + ) +} + +/// Without an OAuth client of the install's own, the trip to GitHub would +/// end on GitHub's 404: back to the sign-in screen with a plain sentence. +fn github_not_set_up(state: &AppState, step: &'static str) -> Option { + oauth_signin::github_not_set_up( + &state.secrets.github_client_id, + &state.secrets.github_client_secret, + common::public_urls::app_url(&state.config.stage), + step, + ) +} + /// GET /auth/github — Redirect to GitHub OAuth (for connecting GitHub account). pub async fn github_login(State(state): State>) -> Response { + if let Some(r) = github_not_set_up(&state, "start") { + return r; + } let client_id = &state.secrets.github_client_id; let redirect_uri = format!( "{}/auth/github/callback", @@ -1267,6 +1295,9 @@ pub async fn github_callback( headers: HeaderMap, Query(params): Query, ) -> Response { + if let Some(r) = github_not_set_up(&state, "callback") { + return r; + } let app = common::public_urls::app_url(&state.config.stage); match github_callback_inner(&state, &ip.0, &headers, ¶ms).await { Ok(resp) => resp, diff --git a/services/gateway/src/routes/oauth_signin.rs b/services/gateway/src/routes/oauth_signin.rs index 28755a643..9e9d690f3 100644 --- a/services/gateway/src/routes/oauth_signin.rs +++ b/services/gateway/src/routes/oauth_signin.rs @@ -56,6 +56,9 @@ pub enum Reason { Provider, /// Something on our side failed. Server, + /// This install has no sign-in client for the provider, so the trip to + /// the provider would only end on its error page. + NotSetUp, } impl Reason { @@ -66,6 +69,7 @@ impl Reason { Reason::Cancelled => "cancelled", Reason::Provider => "provider", Reason::Server => "server", + Reason::NotSetUp => "not_set_up", } } } @@ -212,6 +216,51 @@ fn cookie_value<'a>(cookie_header: &'a str, name: &str) -> Option<&'a str> { .filter(|v| !v.is_empty()) } +/// GitHub sign-in works only with an OAuth client of the install's own: its +/// client id and secret. A hybrid install on CoderHelm's GitHub App has +/// neither, and CoderHelm's client can't sign anyone in to it (the hosted +/// side never vouches for a person on an install). +pub fn github_sign_in_ready(client_id: &str, client_secret: &str) -> bool { + !client_id.trim().is_empty() && !client_secret.trim().is_empty() +} + +/// `GET /auth/github` (and its callback) with no client: back to the sign-in +/// screen with `not_set_up`, never to GitHub with an empty `client_id`. +pub fn github_not_set_up( + client_id: &str, + client_secret: &str, + app_url: &str, + step: &'static str, +) -> Option { + (!github_sign_in_ready(client_id, client_secret)).then(|| { + SignInFailure::new( + Provider::Github, + Reason::NotSetUp, + step, + "no GitHub OAuth client id and secret stored for this install", + ) + .into_redirect(app_url) + }) +} + +/// The sign-in methods that work here, which the sign-in page shows and +/// nothing else: email and password always, Google only on the hosted +/// deployment, GitHub only with the install's own OAuth client. +#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)] +pub struct SignInMethods { + pub password: bool, + pub google: bool, + pub github: bool, +} + +pub fn sign_in_methods(self_hosted: bool, github_ready: bool) -> SignInMethods { + SignInMethods { + password: true, + google: !self_hosted, + github: github_ready, + } +} + /// An app's built-in browser (a web view inside another app), where Google /// shows "This browser or app may not be secure" instead of a sign-in page. /// @@ -259,6 +308,47 @@ mod tests { } } + #[test] + fn github_sign_in_needs_both_halves_of_the_client() { + assert!(github_sign_in_ready("Iv1.abc", "secret")); + // A hybrid install on CoderHelm's app: no client of its own. + assert!(!github_sign_in_ready("", "")); + assert!(!github_sign_in_ready("Iv1.abc", "")); + assert!(!github_sign_in_ready("", "secret")); + assert!(!github_sign_in_ready(" ", " \n")); + } + + #[test] + fn the_methods_list_has_only_what_works() { + let m = sign_in_methods(true, false); + assert_eq!((m.password, m.google, m.github), (true, false, false)); + let m = sign_in_methods(true, true); + assert_eq!((m.password, m.google, m.github), (true, false, true)); + let m = sign_in_methods(false, true); + assert_eq!((m.password, m.google, m.github), (true, true, true)); + assert_eq!( + serde_json::to_value(sign_in_methods(true, false)).unwrap(), + serde_json::json!({ "password": true, "google": false, "github": false }) + ); + } + + #[test] + fn github_with_no_client_goes_back_to_sign_in_not_to_github() { + let app = "https://coderhelm.acme.example"; + for (id, secret) in [("", ""), ("Iv1.abc", ""), ("", "secret")] { + let r = github_not_set_up(id, secret, app, "start").expect("refused"); + assert_eq!(r.status(), StatusCode::FOUND); + let to = r.headers()[header::LOCATION].to_str().unwrap(); + assert_eq!( + to, + "https://coderhelm.acme.example/?signin_error=not_set_up&provider=github" + ); + assert!(!to.contains("github.com"), "{to}"); + } + // With the install's own client, sign-in goes on to GitHub. + assert!(github_not_set_up("Iv1.abc", "secret", app, "start").is_none()); + } + #[test] fn a_matching_state_returns_the_code() { let query = q(Some("abc"), Some("S1"), None); diff --git a/services/gateway/src/routes/setup_checks.rs b/services/gateway/src/routes/setup_checks.rs index 962b35fc4..27a5be63c 100644 --- a/services/gateway/src/routes/setup_checks.rs +++ b/services/gateway/src/routes/setup_checks.rs @@ -229,6 +229,59 @@ pub fn github_keys(app_id: &str, private_key: &str) -> Check { ) } +/// GitHub sign-in: the sign-in page offers "Continue with GitHub" only with +/// an OAuth client of the install's own (its id and secret). `hosted_app` is +/// a hybrid install on CoderHelm's GitHub App, which has no client to store: +/// CoderHelm's can't sign anyone in to the install, so sign-in there is +/// email and password, and that's expected. +pub fn github_sign_in(client_id: &str, client_secret: &str, hosted_app: bool) -> Check { + const KEY: &str = "github_sign_in"; + const LABEL: &str = "GitHub sign-in"; + let (id, secret) = ( + !client_id.trim().is_empty(), + !client_secret.trim().is_empty(), + ); + if id && secret { + return Check::pass( + KEY, + G, + LABEL, + "The OAuth client is stored, so the sign-in page offers Continue with GitHub.", + ); + } + if hosted_app && !id && !secret { + return Check::not_ok( + Status::Skip, + KEY, + G, + LABEL, + "Off: this install uses CoderHelm's GitHub App, which can't sign people in to this install.", + "People sign in with email and password; the sign-in page doesn't offer GitHub.", + Fix::text("Nothing to do. To offer GitHub sign-in, create a GitHub App of this install's own.") + .command(format!("{CLI} github-app")), + ); + } + let detail = if id || secret { + format!( + "Only the OAuth client {} is stored; the {} is missing.", + if id { "id" } else { "secret" }, + if id { "secret" } else { "id" } + ) + } else { + "The GitHub App's OAuth client id and secret aren't stored.".to_string() + }; + Check::not_ok( + Status::Warn, + KEY, + G, + LABEL, + detail, + "The sign-in page hides Continue with GitHub until both are stored, and connecting a GitHub account doesn't work.", + Fix::text("Generate a new client secret on the app's page and store the app again.") + .command(format!("{CLI} github-app --force")), + ) +} + /// GitHub's answer to `GET /app` signed with the stored id and key. pub fn github_app(status: Option) -> Check { match status { @@ -1928,11 +1981,19 @@ pub async fn run_all(state: &AppState, team_id: &str) -> (Vec, Option