diff --git a/backend/tests/leave_instance_tokens.rs b/backend/tests/leave_instance_tokens.rs new file mode 100644 index 0000000000..7697eebb90 --- /dev/null +++ b/backend/tests/leave_instance_tokens.rs @@ -0,0 +1,45 @@ +//! Leaving the instance ends every credential of the account, as deleting it does: a token kept +//! past the leave would authenticate as whoever later takes the address. + +use sqlx::{Pool, Postgres}; + +use windmill_test_utils::*; + +#[sqlx::test(fixtures("base"))] +async fn test_leave_instance_deletes_tokens(db: Pool) -> anyhow::Result<()> { + initialize_tracing().await; + let server = ApiServer::start(db.clone()).await?; + let base = format!("http://localhost:{}/api/users", server.addr.port()); + let client = reqwest::Client::new(); + + sqlx::query( + "INSERT INTO token (token_hash, token_prefix, token, email, label, super_admin) + VALUES (encode(sha256('OTHER_TOKEN_2'::bytea), 'hex'), 'OTHER_TOKE', 'OTHER_TOKEN_2', + 'test2@windmill.dev', 'api', false)", + ) + .execute(&db) + .await?; + + let resp = client + .post(format!("{base}/leave_instance")) + .bearer_auth("SECRET_TOKEN_2") + .send() + .await?; + assert_eq!(resp.status(), 200, "{}", resp.text().await?); + + let left: i64 = + sqlx::query_scalar("SELECT count(*) FROM token WHERE email = 'test2@windmill.dev'") + .fetch_one(&db) + .await?; + assert_eq!(left, 0, "tokens of the account survived the leave"); + + let whoami = |token: &'static str| { + client + .get(format!("{base}/whoami")) + .bearer_auth(token) + .send() + }; + assert_eq!(whoami("OTHER_TOKEN_2").await?.status(), 401); + assert_eq!(whoami("SECRET_TOKEN_3").await?.status(), 200); + Ok(()) +} diff --git a/backend/windmill-api-users/src/users.rs b/backend/windmill-api-users/src/users.rs index ba1e4bc236..e5825679cc 100644 --- a/backend/windmill-api-users/src/users.rs +++ b/backend/windmill-api-users/src/users.rs @@ -1315,6 +1315,10 @@ async fn join_workspace<'c>( async fn leave_instance(Extension(db): Extension, authed: ApiAuthed) -> Result { forbid_job_token_account_destruction(&authed)?; let mut tx = db.begin().await?; + revoke_mcp_oauth_grants(&mut *tx, &authed.email, None).await?; + sqlx::query!("DELETE FROM token WHERE email = $1", &authed.email) + .execute(&mut *tx) + .await?; sqlx::query!("DELETE FROM password WHERE email = $1", &authed.email) .execute(&mut *tx) .await?; diff --git a/backend/windmill-api-workspaces/src/remote_deploy.rs b/backend/windmill-api-workspaces/src/remote_deploy.rs index a0f922a607..070e138154 100644 --- a/backend/windmill-api-workspaces/src/remote_deploy.rs +++ b/backend/windmill-api-workspaces/src/remote_deploy.rs @@ -462,9 +462,9 @@ async fn connect( // No lock against removals, target changes or key rotations: `load_connection` voids a row // that lands after one of them. A disconnect or a newer connect stamps the row itself, which // this one then leaves alone. Without a membership to bind to, the row is bound to the account - // through the credential making this request, which deleting the account removes (except - // through `leave_instance`, which leaves its tokens): the address could otherwise be deleted - // and taken by a new account while the remote call runs, and the foreign key would accept it. + // through the credential making this request, which deleting or leaving the account removes: + // the address could otherwise be deleted and taken by a new account while the remote call + // runs, and the foreign key would accept it. let mut tx = db.begin().await?; let connected_at = sqlx::query_scalar!( "INSERT INTO remote_deploy_token diff --git a/docs/auth-surface.md b/docs/auth-surface.md index 5e2070f3f7..0b7b7e9827 100644 --- a/docs/auth-surface.md +++ b/docs/auth-surface.md @@ -18,13 +18,13 @@ Symbols, not line numbers, are cited: they drift less. token's 7 days therefore has no row left to delete: what ends a user's access calls `revoke_mcp_oauth_grants` (`windmill-api-users`), which also deletes grants without a live token and codes not yet exchanged — a password change or reset, disabling or deleting the - account, offboarding, SCIM deactivation and removal, and (for the grants bound to that - workspace) removal from a workspace. The grant lock (`lock_mcp_oauth_grants`) is the user's - `password` row, shared while issuing a grant and exclusive while revoking, because every path + account, leaving the instance, offboarding, SCIM deactivation and removal, and (for the + grants bound to that workspace) removal from a workspace. The grant lock + (`lock_mcp_oauth_grants`) is the user's `password` row, shared while issuing a grant and exclusive while revoking, because every path that changes a user locks that row before their tokens: a refresh takes it, then its access-token row, then its grant, the order the trigger also follows. A new path that writes a - user's tokens and may revoke must lock that row first too. `leave_workspace` and `leave_instance` - delete no tokens and revoke nothing. + user's tokens and may revoke must lock that row first too. `leave_workspace` deletes no tokens + and revokes nothing; `leave_instance` deletes every token of the account, as `delete_user` does. - **Sessions** are `token` rows with `label='session'` plus the HttpOnly `token` cookie, minted only by `create_session_token` (`windmill-api-users/src/users.rs`). `GET /api/users/refresh_token` mints one for any non-job token but returns plain text, no redirect. @@ -140,11 +140,10 @@ Symbols, not line numbers, are cited: they drift less. the same (or the owner is a superadmin) and it decrypts. So a target set A → B → A, or a removal and re-add, voids it whatever the clocks say. A superadmin with no membership is bound through the credential making the request instead: the insert requires its `token` row to - still exist, and `delete_user`, offboarding and SCIM removal delete an account's tokens, so an - address deleted that way and re-created during the call does not inherit the connection (a JWT, - having no row, cannot connect a workspace its owner is not a member of). `leave_instance` - deletes only the `password` row, leaving the tokens and the memberships, which is not covered. - Neither is a member's connect across an account re-creation: an address is not expected to pass + still exist, and `delete_user`, `leave_instance`, offboarding and SCIM removal delete an + account's tokens, so an address deleted that way and re-created during the call does not + inherit the connection (a JWT, having no row, cannot connect a workspace its owner is not a + member of). A member's connect across an account re-creation is not covered: an address is not expected to pass to another person. A disconnect empties and stamps the row (inserting one if needed) rather than deleting it, `connected_at` is when a connect started, and the connect's upsert leaves a row stamped after that alone, so an older connect cannot undo