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] <windmill-internal-app[bot]@users.noreply.github.com>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
This commit is contained in:
AlexRV12
2026-11-05 23:40:38 +01:00
committed by GitHub
co-authored by windmill-internal-app[bot] Ruben Fiszel
parent 087e79e6ed
commit 9dedc42b4e
6 changed files with 150 additions and 56 deletions
+1 -1
View File
@@ -1 +1 @@
cb6aa42ad515529506531051a3cbfa0e0867c605
9f8892d5f110e844937f74433ad436fe4c5233f6
+2 -1
View File
@@ -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(),
@@ -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<Postgres>) -> anyhow::Result<()> {
async fn test_rename_from_reserved_name_keeps_system_jobs(
db: Pool<Postgres>,
) -> 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<String> =
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(())
}
+18 -12
View File
@@ -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/<name>`, 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,
+75 -18
View File
@@ -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-<g>` becomes `g/<g>`,
/// `schedule-<path>`, `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-<g>` becomes
/// `g/<g>`, `schedule-<path>`, `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]
+5 -5
View File
@@ -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";