From 9dedc42b4ece1fbceae7d695ec51a508caa49710 Mon Sep 17 00:00:00 2001 From: AlexRV12 <71396855+AlexRV12@users.noreply.github.com> Date: Thu, 5 Nov 2026 23:40:38 +0100 Subject: [PATCH] fix: reserve the system usernames used for jobs and audit logs (#12236) * fix: reserve the usernames Windmill writes as job creators and audit authors * chore: update ee-repo-ref to 9f8892d5f110e844937f74433ad436fe4c5233f6 This commit updates the EE repository reference after PR #957 was merged in windmill-ee-private. Previous ee-repo-ref: 7dfe3cce0247ec29ebfe9bc8f0771827408027c4 New ee-repo-ref: 9f8892d5f110e844937f74433ad436fe4c5233f6 Automated by sync-ee-ref workflow. --------- Co-authored-by: windmill-internal-app[bot] Co-authored-by: Ruben Fiszel --- backend/ee-repo-ref.txt | 2 +- backend/windmill-api-auth/src/auth.rs | 3 +- .../tests/users.rs | 68 ++++++++++---- backend/windmill-api/src/users.rs | 30 +++--- backend/windmill-common/src/usernames.rs | 93 +++++++++++++++---- backend/windmill-queue/src/jobs.rs | 10 +- 6 files changed, 150 insertions(+), 56 deletions(-) diff --git a/backend/ee-repo-ref.txt b/backend/ee-repo-ref.txt index 3e1264822e..771591ea50 100644 --- a/backend/ee-repo-ref.txt +++ b/backend/ee-repo-ref.txt @@ -1 +1 @@ -cb6aa42ad515529506531051a3cbfa0e0867c605 +9f8892d5f110e844937f74433ad436fe4c5233f6 diff --git a/backend/windmill-api-auth/src/auth.rs b/backend/windmill-api-auth/src/auth.rs index 9a9ef691fb..1b53b546c4 100644 --- a/backend/windmill-api-auth/src/auth.rs +++ b/backend/windmill-api-auth/src/auth.rs @@ -793,7 +793,8 @@ impl AuthCache { { let authed = ApiAuthed { email: SUPERADMIN_SECRET_EMAIL.to_string(), - username: "superadmin_secret".to_string(), + username: windmill_common::usernames::SUPERADMIN_SECRET_USERNAME + .to_string(), is_admin: true, is_operator: false, groups: Vec::new(), diff --git a/backend/windmill-api-integration-tests/tests/users.rs b/backend/windmill-api-integration-tests/tests/users.rs index 290ec4568d..41488f34e1 100644 --- a/backend/windmill-api-integration-tests/tests/users.rs +++ b/backend/windmill-api-integration-tests/tests/users.rs @@ -1213,41 +1213,71 @@ async fn test_rename_keeps_other_workspaces_like_named_memberships( Ok(()) } -/// Logged-out visitors of public apps run as `anonymous`. Renaming an account that already holds -/// that name must not take their jobs with it, nor their logged-out access to them. +/// Renaming an account that holds a reserved job name keeps Windmill's jobs under that name: +/// `SUPERADMIN_SECRET` runs keep both columns, visitor runs keep `created_by = anonymous`. The +/// account's own `u/anonymous` principal, and every job of an audit-only name (`sso`), follow it. #[sqlx::test(migrations = "../migrations", fixtures("base"))] -async fn test_rename_from_anonymous_keeps_visitor_jobs(db: Pool) -> anyhow::Result<()> { +async fn test_rename_from_reserved_name_keeps_system_jobs( + db: Pool, +) -> anyhow::Result<()> { initialize_tracing().await; let server = ApiServer::start(db.clone()).await?; let port = server.addr.port(); sqlx::raw_sql( - "UPDATE usr SET username = 'anonymous' WHERE email = 'test3@windmill.dev'; - UPDATE password SET username = 'anonymous' WHERE email = 'test3@windmill.dev'; + "UPDATE usr SET username = 'superadmin_secret' WHERE email = 'test3@windmill.dev'; + UPDATE password SET username = 'superadmin_secret' WHERE email = 'test3@windmill.dev'; + UPDATE usr SET username = 'sso' WHERE email = 'test2@windmill.dev'; + UPDATE password SET username = 'sso' WHERE email = 'test2@windmill.dev'; + INSERT INTO password (email, password_hash, login_type, verified, username) + VALUES ('test4@windmill.dev', 'not-a-real-hash', 'password', true, 'anonymous'); + INSERT INTO usr (workspace_id, email, username, is_admin, role) + VALUES ('test-workspace', 'test4@windmill.dev', 'anonymous', false, 'User'); INSERT INTO v2_job (id, workspace_id, created_by, permissioned_as, kind, tag, args) - VALUES (gen_random_uuid(), 'test-workspace', 'anonymous', 'u/test-user', 'script', 'deno', '{}'::jsonb);", + VALUES (gen_random_uuid(), 'test-workspace', 'superadmin_secret', 'u/superadmin_secret', 'script', 'deno', '{}'::jsonb), + (gen_random_uuid(), 'test-workspace', 'sso', 'u/sso', 'script', 'deno', '{}'::jsonb), + (gen_random_uuid(), 'test-workspace', 'anonymous', 'u/test-user', 'script', 'deno', '{}'::jsonb), + (gen_random_uuid(), 'test-workspace', 'anonymous', 'u/anonymous', 'script', 'deno', '{}'::jsonb);", ) .execute(&db) .await?; - let resp = authed(client().post(format!( - "http://localhost:{port}/api/users/rename/test3@windmill.dev" - ))) - .json(&json!({ "new_username": "former_anonymous" })) - .send() - .await?; - assert_eq!(resp.status(), 200, "rename: {}", resp.text().await?); + for (email, new_username) in [ + ("test3@windmill.dev", "former_secret"), + ("test2@windmill.dev", "former_sso"), + ("test4@windmill.dev", "former_anonymous"), + ] { + let resp = + authed(client().post(format!("http://localhost:{port}/api/users/rename/{email}"))) + .json(&json!({ "new_username": new_username })) + .send() + .await?; + assert_eq!(resp.status(), 200, "rename: {}", resp.text().await?); + } let username: String = sqlx::query_scalar("SELECT username FROM usr WHERE email = 'test3@windmill.dev'") .fetch_one(&db) .await?; - assert_eq!(username, "former_anonymous"); - let created_by: Vec = - sqlx::query_scalar("SELECT created_by FROM v2_job WHERE workspace_id = 'test-workspace'") - .fetch_all(&db) - .await?; - assert_eq!(created_by, vec!["anonymous"]); + assert_eq!(username, "former_secret"); + let jobs: Vec<(String, String)> = sqlx::query_as( + "SELECT created_by, permissioned_as FROM v2_job WHERE workspace_id = 'test-workspace' + ORDER BY created_by, permissioned_as", + ) + .fetch_all(&db) + .await?; + assert_eq!( + jobs, + vec![ + ("anonymous".to_string(), "u/former_anonymous".to_string()), + ("anonymous".to_string(), "u/test-user".to_string()), + ("former_sso".to_string(), "u/former_sso".to_string()), + ( + "superadmin_secret".to_string(), + "u/superadmin_secret".to_string() + ), + ] + ); Ok(()) } diff --git a/backend/windmill-api/src/users.rs b/backend/windmill-api/src/users.rs index 8a46272a5e..7ad07e9995 100644 --- a/backend/windmill-api/src/users.rs +++ b/backend/windmill-api/src/users.rs @@ -29,7 +29,9 @@ use windmill_api_auth::{ use windmill_audit::audit_oss::audit_log; use windmill_audit::ActionKind; use windmill_common::audit::AuditAuthor; -use windmill_common::usernames::{validate_username, ANONYMOUS_USERNAME}; +use windmill_common::usernames::{ + validate_username, RESERVED_JOB_USERNAMES, SUPERADMIN_SECRET_USERNAME, +}; use windmill_common::{ error::{self, Error, Result}, DB, @@ -468,18 +470,22 @@ async fn update_username_in_workpsace<'c>( .execute(&mut **tx) .await?; - sqlx::query!( - "UPDATE v2_job SET permissioned_as = ('u/' || $1) WHERE permissioned_as = ('u/' || $2) AND workspace_id = $3", - new_username, - old_username, - w_id - ) - .execute(&mut **tx) - .await?; + // An account still holding a reserved job name shares it with jobs Windmill ran under that + // name (logged-out visitors, `SUPERADMIN_SECRET`, handlers), which must stay Windmill's: + // moving them would hand those runs to the account. Of those, only `SUPERADMIN_SECRET` jobs + // also run as `u/`, so for the others `permissioned_as` is the account's own. + if old_username != SUPERADMIN_SECRET_USERNAME { + sqlx::query!( + "UPDATE v2_job SET permissioned_as = ('u/' || $1) WHERE permissioned_as = ('u/' || $2) AND workspace_id = $3", + new_username, + old_username, + w_id + ) + .execute(&mut **tx) + .await?; + } - // An account still named `anonymous` shares that name with logged-out visitors' jobs, which - // must stay theirs: moving them would hand the visitors' runs to the account. - if old_username != ANONYMOUS_USERNAME { + if !RESERVED_JOB_USERNAMES.contains(&old_username) { sqlx::query!( "UPDATE v2_job SET created_by = $1 WHERE created_by = $2 AND workspace_id = $3", new_username, diff --git a/backend/windmill-common/src/usernames.rs b/backend/windmill-common/src/usernames.rs index abdc895121..54f2dbf682 100644 --- a/backend/windmill-common/src/usernames.rs +++ b/backend/windmill-common/src/usernames.rs @@ -24,10 +24,54 @@ pub const USERNAME_MAX_LEN: usize = 50; /// exactly, so only this exact spelling is reserved. pub const ANONYMOUS_USERNAME: &str = "anonymous"; -/// The rule for every username a caller chooses. It is exactly what the generators produce -/// (`INVALID_USERNAME_CHARS` stripped), so automatic creation always satisfies it. No hyphen: -/// Windmill's own job creators and identities are hyphenated (`group-` becomes `g/`, -/// `schedule-`, `email-…`), and a username must never equal or prefix-match one. +/// The name Windmill uses for requests made with the `SUPERADMIN_SECRET` token. +pub const SUPERADMIN_SECRET_USERNAME: &str = "superadmin_secret"; + +pub const ERROR_HANDLER_USERNAME: &str = "error_handler"; +pub const SCHEDULE_ERROR_HANDLER_USERNAME: &str = "schedule_error_handler"; +pub const GLOBAL_ERROR_HANDLER_USERNAME: &str = "global"; +pub const SUCCESS_HANDLER_USERNAME: &str = "success_handler"; +pub const VARIABLE_EXPIRATION_HANDLER_USERNAME: &str = "variable_expiration_handler"; +pub const SCHEDULE_RECOVERY_HANDLER_USERNAME: &str = "schedule_recovery_handler"; +pub const AUTOSCALING_USERNAME: &str = "autoscaling"; + +/// Bare names Windmill writes as a job's `created_by`. Job visibility matches that column by +/// username alone, so an account with one of these names would see (and could cancel) the jobs +/// Windmill ran under it. A new producer of such a name must be added here. +pub const RESERVED_JOB_USERNAMES: &[&str] = &[ + ANONYMOUS_USERNAME, + SUPERADMIN_SECRET_USERNAME, + ERROR_HANDLER_USERNAME, + SCHEDULE_ERROR_HANDLER_USERNAME, + GLOBAL_ERROR_HANDLER_USERNAME, + SUCCESS_HANDLER_USERNAME, + VARIABLE_EXPIRATION_HANDLER_USERNAME, + SCHEDULE_RECOVERY_HANDLER_USERNAME, + AUTOSCALING_USERNAME, +]; + +/// Bare names Windmill writes only as audit authors. Audit visibility matches the author's +/// username alone, so an account with one of these names would read those entries. A new +/// producer of such a name must be added here. +const RESERVED_AUDIT_USERNAMES: &[&str] = &[ + "scim", + "sso", + "system", + "storage_usage", + "instance_config_sync", + "git_sync_auto_pull", + "presigned", +]; + +/// Exact match: the visibility checks compare exactly. +pub fn is_reserved_username(username: &str) -> bool { + RESERVED_JOB_USERNAMES.contains(&username) || RESERVED_AUDIT_USERNAMES.contains(&username) +} + +/// The rule for every username a caller chooses; the generators produce only names it accepts. +/// It keeps usernames off Windmill's own identities: the hyphenated ones (`group-` becomes +/// `g/`, `schedule-`, `email-…`), neither equal nor prefix-matched, by the no-hyphen +/// rule, and the bare ones by the reserved lists above. pub fn validate_username(username: &str) -> error::Result<()> { if username.is_empty() || username.len() > USERNAME_MAX_LEN @@ -38,10 +82,10 @@ pub fn validate_username(username: &str) -> error::Result<()> { at most {USERNAME_MAX_LEN} characters" ))); } - if username == ANONYMOUS_USERNAME { - return Err(Error::BadRequest( - "The username anonymous is reserved".to_string(), - )); + if is_reserved_username(username) { + return Err(Error::BadRequest(format!( + "The username {username} is reserved" + ))); } Ok(()) } @@ -85,7 +129,7 @@ pub async fn generate_instance_wide_unique_username<'c>( ))); } username = fit_username(&base_username, i); - username_conflict = username == ANONYMOUS_USERNAME || sqlx::query_scalar!( + username_conflict = is_reserved_username(&username) || sqlx::query_scalar!( "SELECT EXISTS(SELECT 1 FROM usr WHERE username = $1 and email != $2 UNION SELECT 1 FROM password WHERE username = $1 UNION SELECT 1 FROM pending_user WHERE username = $1)", &username, &email @@ -221,17 +265,30 @@ mod tests { ] { assert!(validate_username(ok).is_ok(), "{ok}"); } - for bad in [ - "", - "jean-luc", - "group-ops", - "josé", - "a b", - "anonymous", - &"a".repeat(51), - ] { + for bad in ["", "jean-luc", "group-ops", "josé", "a b", &"a".repeat(51)] { assert!(validate_username(bad).is_err(), "{bad}"); } + // Spelled out rather than read from the lists, so dropping a name from them fails here. + for reserved in [ + "anonymous", + "superadmin_secret", + "error_handler", + "schedule_error_handler", + "global", + "success_handler", + "variable_expiration_handler", + "schedule_recovery_handler", + "autoscaling", + "scim", + "sso", + "system", + "storage_usage", + "instance_config_sync", + "git_sync_auto_pull", + "presigned", + ] { + assert!(validate_username(reserved).is_err(), "{reserved}"); + } } #[test] diff --git a/backend/windmill-queue/src/jobs.rs b/backend/windmill-queue/src/jobs.rs index d9c7470b46..ba5d7409eb 100644 --- a/backend/windmill-queue/src/jobs.rs +++ b/backend/windmill-queue/src/jobs.rs @@ -168,11 +168,11 @@ const MAX_FREE_EXECS: i32 = 1000; #[cfg(feature = "cloud")] const MAX_FREE_CONCURRENT_RUNS: i32 = 30; -const ERROR_HANDLER_USERNAME: &str = "error_handler"; -pub const SCHEDULE_ERROR_HANDLER_USERNAME: &str = "schedule_error_handler"; -const GLOBAL_ERROR_HANDLER_USERNAME: &str = "global"; -const SUCCESS_HANDLER_USERNAME: &str = "success_handler"; -const VARIABLE_EXPIRATION_HANDLER_USERNAME: &str = "variable_expiration_handler"; +pub use windmill_common::usernames::SCHEDULE_ERROR_HANDLER_USERNAME; +use windmill_common::usernames::{ + ERROR_HANDLER_USERNAME, GLOBAL_ERROR_HANDLER_USERNAME, SUCCESS_HANDLER_USERNAME, + VARIABLE_EXPIRATION_HANDLER_USERNAME, +}; pub const ERROR_HANDLER_USER_GROUP: &str = "g/error_handler"; pub const ERROR_HANDLER_USER_EMAIL: &str = "error_handler@windmill.dev";