diff --git a/swarm-authelia-bridge/src/main.rs b/swarm-authelia-bridge/src/main.rs index c15715320..adb14ebef 100644 --- a/swarm-authelia-bridge/src/main.rs +++ b/swarm-authelia-bridge/src/main.rs @@ -343,6 +343,13 @@ async fn handle( write_lock: &tokio::sync::Mutex<()>, name: String, ) -> Result { + // The rule the roster reads with: `list` keeps only subjects that parse as + // an `Ident`, so a name the store would accept but `Ident` refuses (an + // uppercase letter, `.`, `_`) would be written, then dropped from the + // roster for good. Refused here, before the store is touched. + hive_types::Ident::parse(&name).map_err(|e| { + anyhow::anyhow!("{name:?} is not an agent name (lower-case a-z, 0-9, `-`): {e}") + })?; store::validate_username(&name)?; // After the charset check and before anything touches the store: a // reserved name is refused on its own terms, not as a side effect of a @@ -664,6 +671,47 @@ mod tests { assert!(status.success(), "writing {} failed", path.display()); } + /// The roster keeps a subject only if it parses as an `Ident`, so a name + /// the store accepts but `Ident` refuses is refused up front, and nothing + /// is written: `"Atlas"` would otherwise be created and then missing from + /// every roster for good. The stand-in `authelia` mints a password, so a + /// create would succeed if the check were absent. + #[tokio::test] + async fn a_name_the_roster_would_drop_is_refused_and_nothing_is_written() { + let mut config = config_with_one_operator(); + let dir = config.users_file.parent().unwrap().to_owned(); + let authelia = dir.join("authelia"); + write_executable( + &authelia, + "#!/bin/sh\nprintf 'Random Password: pw\\nDigest: $argon2id$x\\n'\n", + ); + config.authelia_bin = authelia; + let before = std::fs::read_to_string(&config.users_file).unwrap(); + let write_lock = tokio::sync::Mutex::new(()); + + for name in ["Atlas", "at_las", "at.las", "ATLAS"] { + let err = handle(&config, &write_lock, name.to_owned()) + .await + .expect_err(name); + assert!( + format!("{err:#}").contains("not an agent name"), + "{name}: {err:#}" + ); + assert_eq!( + std::fs::read_to_string(&config.users_file).unwrap(), + before, + "{name} changed the users file" + ); + } + // The control: a legal name is created, so the refusals above are the + // check and not a broken fixture. + handle(&config, &write_lock, "atlas".to_owned()) + .await + .expect("a legal name is created"); + assert_ne!(std::fs::read_to_string(&config.users_file).unwrap(), before); + let _ = std::fs::remove_dir_all(&dir); + } + /// `handle` waits for the `flock` `swarmctl` takes, not only for the /// in-process lock: it is held here while `handle` runs with a lock of /// its own that nothing contends.