diff --git a/AGENTS.md b/AGENTS.md index a0d3a8e6..c7c3bbc9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -36,7 +36,7 @@ crates/ egui craft-cloud-egui Craft Cloud's piece of an app's window: the account line, the shared recents, a live document's badge, and `CloudWatch` that keeps them current app craft-cloud-app what apps link: a blocking client of the agent's local API (status, sign-in, joining, computers, pause, autostart, recents, live documents at the level of bytes: `live.rs`) apps/ - craft-cloud-server HTTP API (`/api/v1`), the live service (`/api/v1/live`, `live.rs`: logs, documents by import, snapshots and compaction, presence), join codes (`join.rs`: pairings, join, the `/join/{code}` page), auth, admin CLI (`init`, `serve`, `user` with `invite`, `device`, `reset-epoch`, `live`) + craft-cloud-server HTTP API (`/api/v1`), the live service (`/api/v1/live`, `live.rs`: logs, documents by import, snapshots and compaction, presence), join codes (`join.rs`: pairings, join, the `/join/{code}` page), auth, the admin pages (`web/`: server-rendered HTML with browser sessions, off unless `[web_admin]` is on), admin CLI (`init`, `serve`, `user` with `invite`, `device`, `reset-epoch`, `live`, `web-sessions`) craft-cloud-agent the sync agent: real disk `Fs`, HTTP `Transport` over a `Connection`, saved account and state, root marker, CLI (`login`, `join`, `run`, `sync`, `status`, `logout`, `recents`, `doc` (`doc.rs`), `add-computer`, `devices`, `pause`, `resume`, `quit`, `autostart`), the `run` loop, the local API (`local_api.rs`), the live hub (`live/`: a document's Loro copy, recents document, replica (recents and documents of files), store (with the documents' journals), link, hub, the local API's docs socket), start at login (`autostart/`: Windows, macOS, Linux), the background build (`src/bin/bg.rs`, logging to `runlog.rs`) tests/network/ the simulation over a simulated network (turmoil): `main.rs` histories and scenarios, `harness.rs` server and agent hosts, `live.rs` live documents over WebSockets on it tests/recents_sim.rs the recents convergence simulation: replicas against the server's live sessions diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index e0b2760c..a0c69d3c 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -14,3 +14,4 @@ next to them. | `apps/craft-cloud-desktop/assets/tray-*.png` | Craft Cloud's tray icon in each state (synced, syncing, offline, needs attention, paused): a cloud with a badge | Craft Cloud contributors | Drawn by `cargo xtask icons` (`xtask/src/icons.rs`) for this repository | MIT OR Apache-2.0 | | `apps/craft-cloud-desktop/tests/autostart/*` | Expected start-at-login entries of the app per OS (golden files) | Craft Cloud contributors | Written for this repository | MIT OR Apache-2.0 | | `apps/craft-cloud-desktop/tests/snapshots/*.png` | The Craft Cloud app's screens as its snapshot tests expect them (the text in egui's default fonts, see `epaint_default_fonts`) | Craft Cloud contributors | Rendered by the tests (`UPDATE_SNAPSHOTS=1`) | MIT OR Apache-2.0 | +| `apps/craft-cloud-server/src/web/style.css` | The admin pages' stylesheet: system fonts, no images, no external files | Craft Cloud contributors | Written for this repository | MIT OR Apache-2.0 | diff --git a/Cargo.lock b/Cargo.lock index 7b4cbe7c..5cb966cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -121,6 +121,41 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common 0.1.7", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + +[[package]] +name = "aes-gcm" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831010a0f742e1209b3bcea8fab6a8e149051ba6099432c8cb2cc117dec3ead1" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ghash", + "subtle", +] + [[package]] name = "ahash" version = "0.8.12" @@ -910,6 +945,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common 0.1.7", + "inout", +] + [[package]] name = "clap" version = "4.6.7" @@ -1028,6 +1073,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "const-oid" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" + [[package]] name = "constant_time_eq" version = "0.4.2" @@ -1260,6 +1311,7 @@ dependencies = [ "tempfile", "thiserror 2.0.21", "tokio", + "tracing", "uuid", ] @@ -1283,6 +1335,7 @@ dependencies = [ name = "craft-cloud-server" version = "0.1.0" dependencies = [ + "aes-gcm", "argon2", "axum", "base64 0.23.1", @@ -1291,13 +1344,17 @@ dependencies = [ "clap", "craft-cloud-meta", "craft-cloud-proto", + "craft-cloud-server", "craft-cloud-store", "futures-util", "getrandom 0.4.3", + "hmac", "reqwest", "serde", "serde_json", + "sha1 0.11.0", "sqlx", + "subtle", "tempfile", "thiserror 2.0.21", "tokio", @@ -1306,6 +1363,7 @@ dependencies = [ "tracing", "tracing-subscriber", "uuid", + "zeroize", ] [[package]] @@ -1429,6 +1487,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", + "rand_core 0.6.4", "typenum", ] @@ -1441,6 +1500,15 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + [[package]] name = "ctutils" version = "0.4.3" @@ -1562,6 +1630,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" dependencies = [ "block-buffer 0.12.1", + "const-oid", "crypto-common 0.2.2", "ctutils", ] @@ -2286,6 +2355,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "ghash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0d8a4362ccb29cb0b265253fb0a2728f592895ee6854fd9bc13f2ffda266ff1" +dependencies = [ + "opaque-debug", + "polyval", +] + [[package]] name = "gl_generator" version = "0.14.0" @@ -2894,6 +2973,15 @@ dependencies = [ "libc", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -4107,6 +4195,12 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "opaque-debug" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" + [[package]] name = "openssl-probe" version = "0.2.1" @@ -4395,6 +4489,18 @@ dependencies = [ "arrayvec", ] +[[package]] +name = "polyval" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d1fe60d06143b2430aa532c94cfe9e29783047f06c0d7fd359a9a51b729fa25" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "opaque-debug", + "universal-hash", +] + [[package]] name = "portable-atomic" version = "1.15.0" @@ -6230,6 +6336,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +[[package]] +name = "universal-hash" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc1de2c688dc15305988b563c3854064043356019f97a4b46276fe734c4f07ea" +dependencies = [ + "crypto-common 0.1.7", + "subtle", +] + [[package]] name = "untrusted" version = "0.9.0" diff --git a/Cargo.toml b/Cargo.toml index 3c65eb18..9ec3a766 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,13 @@ craft-cloud-agent = { path = "apps/craft-cloud-agent" } craft-cloud-app = { path = "crates/app" } craft-cloud-kit = { path = "crates/kit" } craft-cloud-egui = { path = "crates/egui" } +# Two-factor sign-in: AES-256-GCM for the stored TOTP secrets (RustCrypto, pure Rust), and HMAC-SHA-1 for the codes +# themselves (authenticator apps speak SHA-1). `subtle` compares codes in constant time, `zeroize` wipes secrets. +aes-gcm = "0.10.3" +hmac = "0.13" +sha1 = "0.11" +subtle = "2.6" +zeroize = "1.9" argon2 = "0.6" async-trait = "0.1.89" # `ws`: the server's live service and the agent's local API are WebSockets (tokio-tungstenite, MIT). diff --git a/apps/craft-cloud-agent/src/cli.rs b/apps/craft-cloud-agent/src/cli.rs index 194d4713..9027e44b 100644 --- a/apps/craft-cloud-agent/src/cli.rs +++ b/apps/craft-cloud-agent/src/cli.rs @@ -33,11 +33,11 @@ use jiff::tz::TimeZone; use crate::account::{ACCOUNT_SCHEMA, Account, AccountError, DataDir, LastPass, LoopState, Outcome, PassSummary, STATE_SCHEMA, SavedState}; use crate::disk::{AGENT_DIR, DiskFs, create_private_dir}; -use crate::http::{self, DeviceToken, HttpTransport, JoinError, ReachError, ServerUrl}; +use crate::http::{self, CallError, DeviceToken, Factor, HttpTransport, JoinError, LoginStep, ReachError, Refusal, ServerUrl}; use crate::lock::{self, DATA_LOCK, FOLDER_LOCK, Lock, LockError}; use crate::root::{self, RootError, RootMarker}; use crate::settle::Stability; -use craft_cloud_proto::api::LoginResponse; +use craft_cloud_proto::api::{LoginResponse, TwoFactorChallenge}; use craft_cloud_proto::join::JoinString; #[derive(Debug, Parser)] @@ -336,6 +336,24 @@ pub enum AgentError { .0.display() )] FolderHasFiles(PathBuf), + /// `login`: the account has two-factor sign-in. The challenge goes back with a code from its authenticator app (or a + /// recovery code) to finish the sign-in. For `join` the challenge is empty: the same join goes again with the code. + #[error("this account has two-factor sign-in: a code from its authenticator app (or a recovery code) is needed")] + TwoFactorRequired(TwoFactorChallenge), + /// The code or recovery code was wrong (the server's message). Never a revoked device. + #[error("{0}")] + TwoFactorInvalid(String), + /// Too many wrong codes: the second factor is locked for a while (`retry_after_secs`, the server's `Retry-After`). + #[error("{message}")] + TwoFactorLocked { message: String, retry_after_secs: Option }, + /// The second step of a sign-in found its challenge gone (it works five minutes and takes five tries, and a password + /// change ends it): the sign-in starts again. + #[error("this sign-in step has run out (it works for five minutes and takes five tries): sign in again")] + ChallengeGone, + /// The server refused a call of the account (a wrong password, a conflict, a lock, a limit): its status, code and + /// message as it said them, and its `Retry-After`. Never a revoked device (that is a 401, [`AgentError::Revoked`]). + #[error("{message}")] + Refused { status: u16, code: String, message: String, retry_after_secs: Option }, /// Something found as `run` started that only a person can put right (the message says what); the agent waits, /// serving the local API, until it is. #[error("{0}")] @@ -352,6 +370,61 @@ impl From for AgentError { } } +impl From for AgentError { + /// For a signed-in call of the account: the server's refusal with its status, code and message; anything else as sync's + /// transport errors are (a 401 is a revoked device, offline is offline). + fn from(e: CallError) -> Self { + match e { + CallError::Refused(Refusal { status, error, message, retry_after_secs, .. }) => { + AgentError::Refused { status, code: error, message, retry_after_secs } + } + CallError::Transport(t) => t.into(), + } + } +} + +/// What a refusal said, as the transport error the sign-in used to see for it (a refusal with a code of its own is handled +/// before this), so that messages and exits of the first step don't change. +fn legacy(refusal: &Refusal) -> TransportError { + if refusal.status == 429 { + TransportError::Unavailable(format!("the server is busy: {}", refusal.message)) + } else { + TransportError::Protocol(format!("the server or a proxy refused the request (HTTP {}): {}", refusal.status, refusal.message)) + } +} + +/// A wait in words, for a lock: "about 14 minutes". +fn wait_in_words(secs: u64) -> String { + match secs { + 0..=90 => format!("{secs} seconds"), + 91..=5_400 => format!("about {} minutes", secs.div_ceil(60)), + _ => format!("about {} hours", secs.div_ceil(3_600)), + } +} + +/// The errors of a sign-in's second step and of a join with a code, from what the server said. +fn factor_error(e: CallError) -> AgentError { + match e { + CallError::Refused(r) if r.error == craft_cloud_proto::api::codes::TWO_FACTOR_INVALID => AgentError::TwoFactorInvalid(r.message), + CallError::Refused(r) if r.error == craft_cloud_proto::api::codes::TWO_FACTOR_LOCKED => locked(r.message, r.retry_after_secs), + CallError::Refused(r) if r.error == craft_cloud_proto::api::codes::TWO_FACTOR_REQUIRED => { + AgentError::TwoFactorRequired(r.two_factor.unwrap_or(TwoFactorChallenge { challenge: String::new(), expires_ms: 0 })) + } + // The challenge doesn't work any more: not a revoked device (nothing is signed in yet). + CallError::Transport(TransportError::Unauthorized) => AgentError::ChallengeGone, + CallError::Refused(r) => legacy(&r).into(), + CallError::Transport(t) => t.into(), + } +} + +fn locked(message: String, retry_after_secs: Option) -> AgentError { + let message = match retry_after_secs { + Some(secs) => format!("{message} (try again in {})", wait_in_words(secs)), + None => message, + }; + AgentError::TwoFactorLocked { message, retry_after_secs } +} + impl From for AgentError { fn from(e: SyncError) -> Self { match e { @@ -384,15 +457,40 @@ pub async fn run(cli: Cli, input: &mut impl BufRead, out: &mut impl Write) -> Re return Err(already_signed_in(&account)); } let password = read_password(input)?; - match login(&data, &args, &password, out).await { + let mut through_agent = false; + let first = match login(&data, &args, &password, out).await { // `run` holds the data folder: it signs in, through its local API. - Err(AgentError::Lock(LockError::Busy(path))) => login_through_agent(&data, &args, password, out).await.map_err(|e| match e { - AgentError::Local(craft_cloud_app::Error::NotRunning(_)) => AgentError::Lock(LockError::Busy(path)), - other => other, - })?, - other => { - other?; + Err(AgentError::Lock(LockError::Busy(path))) => { + through_agent = true; + login_through_agent(&data, &args, password, out).await.map_err(|e| match e { + AgentError::Local(craft_cloud_app::Error::NotRunning(_)) => AgentError::Lock(LockError::Busy(path)), + other => other, + }) + } + other => other.map(|_| ()), + }; + match first { + // An account with two-factor sign-in: the code comes from standard input, like the password (never from the + // command line), and a wrong one may be typed again twice. + Err(AgentError::TwoFactorRequired(challenge)) => { + let mut tries = 0; + loop { + let factor = read_factor(input, out)?; + let done = if through_agent { + second_step_through_agent(&data, &args, &challenge, &factor, out).await + } else { + login_second_factor(&data, &args, &challenge, &factor, out).await.map(|_| ()) + }; + match done { + Err(AgentError::TwoFactorInvalid(message)) if tries < MAX_CODE_TRIES - 1 => { + tries += 1; + say!(out, "{message}")?; + } + other => break other?, + } + } } + other => other?, } Ok(Exit::Done) } @@ -440,11 +538,13 @@ pub async fn run(cli: Cli, input: &mut impl BufRead, out: &mut impl Write) -> Re return Err(already_signed_in(&account)); } let mut password: Option = None; + let mut factor: Option = None; + let mut wrong_codes = 0; loop { - let joined = match join(&data, &args, password.as_deref(), out).await { + let joined = match join_with(&data, &args, password.as_deref(), factor.as_ref(), out).await { // `run` holds the data folder: it joins, through its local API. Err(AgentError::Lock(LockError::Busy(path))) => { - join_through_agent(&data, &args, password.clone(), out).await.map_err(|e| match e { + join_through_agent(&data, &args, password.clone(), factor.clone(), out).await.map_err(|e| match e { AgentError::Local(craft_cloud_app::Error::NotRunning(_)) => AgentError::Lock(LockError::Busy(path)), other => other, }) @@ -453,6 +553,13 @@ pub async fn run(cli: Cli, input: &mut impl BufRead, out: &mut impl Write) -> Re }; match joined { Err(AgentError::PasswordRequired) if password.is_none() => password = Some(new_password(input, out)?), + // The code was not used up: the same join goes again with the second factor. + Err(AgentError::TwoFactorRequired(_)) if factor.is_none() => factor = Some(read_factor(input, out)?), + Err(AgentError::TwoFactorInvalid(message)) if wrong_codes < MAX_CODE_TRIES - 1 => { + wrong_codes += 1; + say!(out, "{message}")?; + factor = Some(read_factor(input, out)?); + } other => break other?, } } @@ -572,8 +679,29 @@ fn new_password(input: &mut impl BufRead, out: &mut impl Write) -> Result Result { + say!(out, "This account has two-factor sign-in. Type the code from your authenticator app (or a recovery code):")?; + out.flush().map_err(io_at(Path::new("standard output")))?; + let mut line = String::new(); + input.take(200).read_line(&mut line).map_err(|e| AgentError::Usage(format!("can't read the code from standard input: {e}")))?; + if line.trim().is_empty() { + return Err(AgentError::Usage("no code on standard input".into())); + } + Ok(Factor::typed(&line)) +} + /// `join` while `run` runs: the running agent joins. -async fn join_through_agent(data: &DataDir, args: &JoinArgs, password: Option, out: &mut impl Write) -> Result<(), AgentError> { +async fn join_through_agent( + data: &DataDir, + args: &JoinArgs, + password: Option, + factor: Option, + out: &mut impl Write, +) -> Result<(), AgentError> { let folder = args.folder.as_ref().map(|f| std::path::absolute(f).map_err(io_at(f))).transpose()?; let request = craft_cloud_app::JoinRequest { join: args.join.clone(), @@ -581,6 +709,8 @@ async fn join_through_agent(data: &DataDir, args: &JoinArgs, password: Option(f: impl FnOnce() -> Result AgentError { use craft_cloud_proto::local::codes; + // A refusal that carries the challenge or the wait of a lock. + if let AgentError::Local(inner @ craft_cloud_app::Error::RefusedWith { .. }) = &e { + let message = inner.to_string(); + return match inner.code() { + Some(codes::TWO_FACTOR_REQUIRED) => { + AgentError::TwoFactorRequired(inner.challenge().cloned().unwrap_or(TwoFactorChallenge { challenge: String::new(), expires_ms: 0 })) + } + Some(codes::TWO_FACTOR_LOCKED) => locked(message, inner.retry_after_secs()), + _ => AgentError::Usage(message), + }; + } match e { AgentError::Local(craft_cloud_app::Error::Refused { code, message }) => match code.as_str() { + codes::TWO_FACTOR_INVALID => AgentError::TwoFactorInvalid(message), + codes::TWO_FACTOR_EXPIRED => AgentError::ChallengeGone, + codes::TWO_FACTOR_REQUIRED => AgentError::TwoFactorRequired(TwoFactorChallenge { challenge: String::new(), expires_ms: 0 }), codes::WRONG_PASSWORD => AgentError::WrongPassword, codes::NOT_SIGNED_IN => AgentError::NotSignedIn, codes::OFFLINE => AgentError::Offline(message), @@ -749,6 +893,36 @@ async fn login_through_agent(data: &DataDir, args: &LoginArgs, password: String, ) } +/// The second step of `login` while `run` runs: the running agent finishes the sign-in. +async fn second_step_through_agent( + data: &DataDir, + args: &LoginArgs, + challenge: &TwoFactorChallenge, + factor: &Factor, + out: &mut impl Write, +) -> Result<(), AgentError> { + let folder = args.folder.as_ref().map(|f| std::path::absolute(f).map_err(io_at(f))).transpose()?; + let request = craft_cloud_app::SecondFactorLoginRequest { + server: args.url.clone(), + user: args.user.clone(), + folder: folder.map(|f| f.display().to_string()), + device_name: args.device_name.clone(), + adopt: args.adopt, + challenge: challenge.challenge.clone(), + totp_code: factor.totp_code.clone(), + recovery_code: factor.recovery_code.clone(), + }; + let data_path = data.path().to_owned(); + let status = blocking(move || craft_cloud_app::Client::connect_in(&data_path)?.login_second_factor(&request)).await.map_err(local_error)?; + say!( + out, + "signed in as {} on {} (through the running agent, which syncs {} now)", + status.account.unwrap_or_default(), + status.server.unwrap_or_default(), + status.folder.unwrap_or_default() + ) +} + /// `recents [add ] [--watch]`: a client of the running agent like an app. async fn recents(data: &DataDir, args: RecentsArgs, out: &mut impl Write) -> Result { let data_path = data.path().to_owned(); @@ -1095,20 +1269,65 @@ pub(crate) async fn login_locked(data: &DataDir, args: &LoginArgs, password: &st let server = ServerUrl::parse(&args.url).map_err(|e| AgentError::Usage(e.to_string()))?; let prepared = prepare(data, args.folder.as_deref(), args.device_name.as_deref(), args.adopt)?; ask_server(&server, |_, message| AgentError::Offline(message)).await?; - let signed_in = match http::login(&server, &args.user, password, &prepared.device_name).await { - Err(TransportError::Unauthorized) => return Err(AgentError::WrongPassword), - other => other?, + let signed_in = match http::login_step(&server, &args.user, password, &prepared.device_name).await { + Ok(LoginStep::Device(signed_in)) => signed_in, + // An account with two-factor sign-in: no device yet. The caller asks for the code and finishes with + // [`login_second_factor_locked`], which needs nothing of this call but its arguments. + Ok(LoginStep::SecondFactor(challenge)) => return Err(AgentError::TwoFactorRequired(challenge)), + Err(CallError::Transport(TransportError::Unauthorized)) => return Err(AgentError::WrongPassword), + Err(CallError::Refused(refusal)) => return Err(legacy(&refusal).into()), + Err(CallError::Transport(e)) => return Err(e.into()), }; set_up(data, &server, signed_in, prepared).await } +/// The second step of a sign-in: the folder's checks again (the agent keeps nothing between the steps), then the challenge and +/// the second factor make the device, and the folder is set up as after [`login`]. Prints nothing. +pub(crate) async fn login_second_factor_locked(data: &DataDir, args: &LoginArgs, challenge: &str, factor: &Factor) -> Result { + if let Some(account) = data.load_account()? { + return Err(already_signed_in(&account)); + } + let server = ServerUrl::parse(&args.url).map_err(|e| AgentError::Usage(e.to_string()))?; + let prepared = prepare(data, args.folder.as_deref(), args.device_name.as_deref(), args.adopt)?; + ask_server(&server, |_, message| AgentError::Offline(message)).await?; + let signed_in = http::second_factor(&server, challenge, factor).await.map_err(factor_error)?; + set_up(data, &server, signed_in, prepared).await +} + +/// [`login_second_factor_locked`] for the command line: holds the data folder and says it signed in. +pub async fn login_second_factor( + data: &DataDir, + args: &LoginArgs, + challenge: &TwoFactorChallenge, + factor: &Factor, + out: &mut impl Write, +) -> Result { + let _data_lock = hold_data(data)?; + let account = login_second_factor_locked(data, args, &challenge.challenge, factor).await?; + say!(out, "signed in as {} on {} (device \"{}\")", account.username, account.server, account.device_name)?; + say!(out, "syncing {}; run `craft-cloud-agent sync` to sync it", account.folder.display())?; + Ok(account) +} + /// Joins with a join string (an invite or an add-a-computer code), creates a device and sets up the folder, as /// [`login`] does. Everything that can be checked here is checked before the code is spent (the folder, the device /// name, what the server speaks); a failure after it (the folder is set up for another space) removes the new device /// again, and says the code is used up. `password`: the new account's, for an invite that needs one. pub async fn join(data: &DataDir, args: &JoinArgs, password: Option<&str>, out: &mut impl Write) -> Result { + join_with(data, args, password, None, out).await +} + +/// [`join`] with the second factor of an account that has two-factor sign-in (without one, such an account answers +/// [`AgentError::TwoFactorRequired`] and the code is not used up). +pub async fn join_with( + data: &DataDir, + args: &JoinArgs, + password: Option<&str>, + factor: Option<&Factor>, + out: &mut impl Write, +) -> Result { let _data_lock = hold_data(data)?; - let account = join_locked(data, args, password).await?; + let account = join_locked(data, args, password, factor).await?; say!(out, "joined as {} on {} (device \"{}\")", account.username, account.server, account.device_name)?; say!(out, "syncing {}; run `craft-cloud-agent run` to keep it in sync", account.folder.display())?; Ok(account) @@ -1116,7 +1335,7 @@ pub async fn join(data: &DataDir, args: &JoinArgs, password: Option<&str>, out: /// [`join`] for a caller that holds the data folder's lock already: the `run` loop, joining for the local API. /// Prints nothing. -pub(crate) async fn join_locked(data: &DataDir, args: &JoinArgs, password: Option<&str>) -> Result { +pub(crate) async fn join_locked(data: &DataDir, args: &JoinArgs, password: Option<&str>, factor: Option<&Factor>) -> Result { if let Some(account) = data.load_account()? { return Err(already_signed_in(&account)); } @@ -1125,8 +1344,13 @@ pub(crate) async fn join_locked(data: &DataDir, args: &JoinArgs, password: Optio let prepared = prepare(data, args.folder.as_deref(), args.device_name.as_deref(), args.adopt)?; let unreachable = |server: &ServerUrl, message| AgentError::Unreachable { server: server.to_string(), message }; ask_server(&server, unreachable).await?; - let signed_in = match http::join(&server, join.code.as_str(), &prepared.device_name, password).await { + let signed_in = match http::join_with_factor(&server, join.code.as_str(), &prepared.device_name, password, factor).await { Ok(signed_in) => signed_in, + Err(JoinError::TwoFactorRequired) => { + return Err(AgentError::TwoFactorRequired(TwoFactorChallenge { challenge: String::new(), expires_ms: 0 })); + } + Err(JoinError::TwoFactorInvalid(message)) => return Err(AgentError::TwoFactorInvalid(message)), + Err(JoinError::TwoFactorLocked { message, retry_after_secs }) => return Err(locked(message, retry_after_secs)), Err(JoinError::InvalidCode) => return Err(AgentError::InvalidCode), Err(JoinError::PasswordRequired) => return Err(AgentError::PasswordRequired), Err(JoinError::WeakPassword(message)) => return Err(AgentError::WeakPassword(message)), diff --git a/apps/craft-cloud-agent/src/daemon.rs b/apps/craft-cloud-agent/src/daemon.rs index 0841eea7..ab63bdc0 100644 --- a/apps/craft-cloud-agent/src/daemon.rs +++ b/apps/craft-cloud-agent/src/daemon.rs @@ -994,7 +994,7 @@ pub async fn run_loop(data: &DataDir, options: RunOptions, mut stop: mpsc::Unbou None => stop_open = false, }, command = commands.recv(), if commands_open => match command { - Some(Command::Login(_, reply) | Command::Join(_, reply)) => { + Some(Command::Login(_, reply) | Command::Join(_, reply) | Command::LoginSecondFactor(_, reply)) => { let _ = reply.send(Err(crate::cli::already_signed_in(&account))); } // Signed out on the server first (an unreachable server changes nothing, and syncing goes on); then @@ -1178,7 +1178,7 @@ async fn wait_needing_you( let _ = reply.send(Err(e)); } }, - Some(Command::Login(_, reply) | Command::Join(_, reply) | Command::Pause(reply) | Command::Resume(reply)) => { + Some(Command::Login(_, reply) | Command::Join(_, reply) | Command::LoginSecondFactor(_, reply) | Command::Pause(reply) | Command::Resume(reply)) => { let _ = reply.send(Err(AgentError::NeedsYou(reason.to_owned()))); } Some(Command::Quit(reply)) => { @@ -1227,6 +1227,20 @@ async fn wait_for_sign_in( return SignedOut::SignedIn; } } + Some(Command::LoginSecondFactor(request, reply)) => { + let args = LoginArgs { + url: request.server.clone(), + user: request.user.clone(), + folder: request.folder.as_ref().map(std::path::PathBuf::from), + device_name: request.device_name.clone(), + adopt: request.adopt, + }; + let factor = crate::http::Factor { totp_code: request.totp_code.clone(), recovery_code: request.recovery_code.clone() }; + let result = crate::cli::login_second_factor_locked(data, &args, &request.challenge, &factor).await; + if signed_in(result, "signed in", control, reply, out) { + return SignedOut::SignedIn; + } + } Some(Command::Join(request, reply)) => { let args = JoinArgs { join: request.join.clone(), @@ -1236,7 +1250,9 @@ async fn wait_for_sign_in( // The caller asks for it with `POST /v1/autostart` once joined. autostart: false, }; - let result = crate::cli::join_locked(data, &args, request.password.as_deref()).await; + let factor = (request.totp_code.is_some() || request.recovery_code.is_some()) + .then(|| crate::http::Factor { totp_code: request.totp_code.clone(), recovery_code: request.recovery_code.clone() }); + let result = crate::cli::join_locked(data, &args, request.password.as_deref(), factor.as_ref()).await; if signed_in(result, "joined", control, reply, out) { return SignedOut::SignedIn; } diff --git a/apps/craft-cloud-agent/src/http.rs b/apps/craft-cloud-agent/src/http.rs index 16efff9f..d2eb5530 100644 --- a/apps/craft-cloud-agent/src/http.rs +++ b/apps/craft-cloud-agent/src/http.rs @@ -35,8 +35,10 @@ use std::time::Duration; use async_trait::async_trait; use bytes::{Bytes, BytesMut}; use craft_cloud_proto::api::{ - CommitRequest, CommitResponse, DeviceInfo, ErrorBody, InfoResponse, JoinRequest, LoginRequest, LoginResponse, MAX_MISSING_IDS, MissingRequest, - MissingResponse, PROTOCOL_MAX, PROTOCOL_MIN, PairingCreated, PairingStatus, SpaceResponse, codes, + ChangePasswordRequest, CommitRequest, CommitResponse, DeviceInfo, ErrorBody, InfoResponse, JoinRequest, LoginRequest, LoginResponse, + MAX_MISSING_IDS, MissingRequest, MissingResponse, PROTOCOL_MAX, PROTOCOL_MIN, PairingCreated, PairingStatus, RecoveryCodes, RenameDeviceRequest, + RevokedOthers, SecondFactorRequest, SpaceResponse, TwoFactorChallenge, TwoFactorConfirmRequest, TwoFactorProofRequest, TwoFactorSetupRequest, + TwoFactorSetupResponse, TwoFactorStatus, codes, }; use craft_cloud_proto::limits::{MAX_BATCH_OPS, MAX_CHUNK_BYTES, MAX_MANIFEST_BYTES, MAX_PAGE_NODES}; use craft_cloud_proto::local::MAX_MACHINE_BYTES; @@ -492,6 +494,25 @@ pub fn check_compatible(info: &InfoResponse) -> Result<(), TransportError> { Ok(()) } +/// The most bytes the server takes of a device's `platform` or `app_version`. +const MAX_DEVICE_DETAIL_BYTES: usize = 32; + +/// What this agent says about itself when it signs in or joins: `agent`, the operating system and the crate's +/// version, for the account's device list (display only). The server takes at most 32 bytes without control +/// characters per field, so a longer value is cut at a character boundary rather than refused. +fn device_details() -> (Option, Option, Option) { + fn limited(text: &str) -> String { + text.chars() + .filter(|c| !c.is_control()) + .scan(0usize, |len, c| { + *len += c.len_utf8(); + (*len <= MAX_DEVICE_DETAIL_BYTES).then_some(c) + }) + .collect() + } + (Some("agent".to_owned()), Some(limited(std::env::consts::OS)), Some(limited(env!("CARGO_PKG_VERSION")))) +} + /// Signs in and creates a device. `Unauthorized` means a wrong username or password. pub async fn login(server: &ServerUrl, username: &str, password: &str, device_name: &str) -> Result { login_over(&Reqwest::new(server)?, server, username, password, device_name).await @@ -505,11 +526,178 @@ pub async fn login_over( password: &str, device_name: &str, ) -> Result { - let body = LoginRequest { username: username.to_owned(), password: password.to_owned(), device_name: device_name.to_owned() }; + let (client_kind, platform, app_version) = device_details(); + let body = LoginRequest { + username: username.to_owned(), + password: password.to_owned(), + device_name: device_name.to_owned(), + client_kind, + platform, + app_version, + }; let req = request(Method::POST, server.endpoint("auth/login"), None, 0).json(&body)?; json(connection.send(req).await?, MAX_SMALL_JSON_BYTES).await } +/// A server's refusal of a call that the agent keeps whole (its status, code and message, the challenge of +/// `two_factor_required`, the `Retry-After` of a lock), unlike [`TransportError`], which sync relies on and which has no +/// room for them. Only the calls that need it return this ([`CallError`]); the sync paths, [`error_for`] and +/// `TransportError` are as they were. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Refusal { + pub status: u16, + pub error: String, + pub message: String, + pub two_factor: Option, + pub retry_after_secs: Option, +} + +/// Why a call that returns a [`Refusal`] failed: the server refused it (a wrong password or code, a conflict, a lock: never a +/// reason to think this device was revoked), or it failed as any call does (offline, a 401 on a signed-in call, a 404). +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum CallError { + #[error("{}", .0.message)] + Refused(Refusal), + #[error(transparent)] + Transport(TransportError), +} + +impl From for CallError { + fn from(e: TransportError) -> Self { + CallError::Transport(e) + } +} + +/// The server's `Retry-After` in seconds, cut to a sane size. +fn retry_after_of(headers: &HeaderMap) -> Option { + headers.get(RETRY_AFTER).and_then(|v| v.to_str().ok()).and_then(|s| s.trim().parse::().ok()).map(|secs| secs.min(24 * 60 * 60)) +} + +/// A failed answer as a [`CallError`]: a 400, 403, 409, 422 or 429 with an error body, a 401 that is `two_factor_required` or +/// `two_factor_invalid` (the not-signed-in routes' answers; on a signed-in route a 401 is only ever a revoked device) and a +/// 503 `two_factor_unavailable` are a [`Refusal`]; everything else is what [`error_for`] makes of it. +async fn failure(resp: Answer) -> CallError { + let status = resp.status; + let retry = retry_after_of(&resp.headers); + let body = body_capped(resp, MAX_SMALL_JSON_BYTES).await.ok().and_then(|b| serde_json::from_slice::(&b).ok()); + if let Some(b) = &body { + let refused = match status.as_u16() { + 400 | 403 | 409 | 422 | 429 => true, + 401 => matches!(b.error.as_str(), codes::TWO_FACTOR_REQUIRED | codes::TWO_FACTOR_INVALID), + 503 => b.error == codes::TWO_FACTOR_UNAVAILABLE, + _ => false, + }; + if refused { + return CallError::Refused(Refusal { + status: status.as_u16(), + error: b.error.clone(), + message: clean(&b.message), + two_factor: b.two_factor.clone(), + retry_after_secs: retry, + }); + } + } + CallError::Transport(error_for(status, body)) +} + +/// The second factor a person gave: a code from the authenticator app, or a recovery code. Its `Debug` output hides both. +#[derive(Clone, Default, PartialEq, Eq)] +pub struct Factor { + pub totp_code: Option, + pub recovery_code: Option, +} + +impl Factor { + /// Reads what a person typed: six digits (spaces allowed) are a code, anything else a recovery code. + pub fn typed(text: &str) -> Self { + let trimmed = text.trim(); + let digits: String = trimmed.chars().filter(|c| !c.is_whitespace()).collect(); + if digits.len() == 6 && digits.bytes().all(|b| b.is_ascii_digit()) { + Self { totp_code: Some(digits), recovery_code: None } + } else { + Self { totp_code: None, recovery_code: Some(trimmed.to_owned()) } + } + } +} + +impl std::fmt::Debug for Factor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Factor") + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +/// The first step of a sign-in: a device, or the challenge of an account that has two-factor sign-in. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum LoginStep { + Device(LoginResponse), + SecondFactor(TwoFactorChallenge), +} + +/// Signs in as [`login`] does, but tells the account's second factor from a wrong password: `Unauthorized` (a wrong username +/// or password) is [`CallError::Transport`], a challenge is [`LoginStep::SecondFactor`]. +pub async fn login_step(server: &ServerUrl, username: &str, password: &str, device_name: &str) -> Result { + login_step_over(&Reqwest::new(server)?, server, username, password, device_name).await +} + +/// [`login_step`] over another connection (a simulated network, in tests). +pub async fn login_step_over( + connection: &C, + server: &ServerUrl, + username: &str, + password: &str, + device_name: &str, +) -> Result { + let (client_kind, platform, app_version) = device_details(); + let body = LoginRequest { + username: username.to_owned(), + password: password.to_owned(), + device_name: device_name.to_owned(), + client_kind, + platform, + app_version, + }; + let req = request(Method::POST, server.endpoint("auth/login"), None, 0).json(&body)?; + let resp = connection.send(req).await?; + if resp.status.is_success() { + return Ok(LoginStep::Device(json(resp, MAX_SMALL_JSON_BYTES).await?)); + } + match failure(resp).await { + CallError::Refused(Refusal { error, two_factor, .. }) if error == codes::TWO_FACTOR_REQUIRED => match two_factor { + Some(challenge) => Ok(LoginStep::SecondFactor(challenge)), + None => Err(CallError::Transport(TransportError::Protocol("the server asked for a second factor without a challenge".into()))), + }, + other => Err(other), + } +} + +/// The second step of a sign-in: the `challenge` and the second factor make the device. A challenge that doesn't work any +/// more (unknown, expired, used, from before a password change) is `Transport(Unauthorized)` here, which is no revoked device +/// (the caller says the sign-in has to start again); a wrong code is `Refused` with `two_factor_invalid`, a lock with +/// `two_factor_locked` and its wait. +pub async fn second_factor(server: &ServerUrl, challenge: &str, factor: &Factor) -> Result { + second_factor_over(&Reqwest::new(server)?, server, challenge, factor).await +} + +/// [`second_factor`] over another connection (a simulated network, in tests). +pub async fn second_factor_over( + connection: &C, + server: &ServerUrl, + challenge: &str, + factor: &Factor, +) -> Result { + let body = + SecondFactorRequest { challenge: challenge.to_owned(), totp_code: factor.totp_code.clone(), recovery_code: factor.recovery_code.clone() }; + let req = request(Method::POST, server.endpoint("auth/login/second-factor"), None, 0).json(&body)?; + let resp = connection.send(req).await?; + if resp.status.is_success() { + return Ok(json(resp, MAX_SMALL_JSON_BYTES).await?); + } + Err(failure(resp).await) +} + /// Why a join ([`join`]) was refused. #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] pub enum JoinError { @@ -525,13 +713,34 @@ pub enum JoinError { /// Too many failed joins from this address; the server's message. #[error("{0}")] RateLimited(String), + /// The account has two-factor sign-in: join again with a code from its authenticator app or a recovery code. The code + /// was not used up. + #[error("the account has two-factor sign-in: a code from its authenticator app is needed")] + TwoFactorRequired, + /// The code or recovery code was wrong (the join code still works); the server's message. + #[error("{0}")] + TwoFactorInvalid(String), + /// Too many wrong codes: the second factor is locked for `retry_after_secs`. + #[error("{message}")] + TwoFactorLocked { message: String, retry_after_secs: Option }, #[error(transparent)] Transport(TransportError), } /// Redeems a join code (normalised) for a new device named `device_name`; `password` for an invite to a new account. pub async fn join(server: &ServerUrl, code: &str, device_name: &str, password: Option<&str>) -> Result { - join_over(&Reqwest::new(server).map_err(JoinError::Transport)?, server, code, device_name, password).await + join_with_factor(server, code, device_name, password, None).await +} + +/// [`join`] with the second factor of an account that has two-factor sign-in. +pub async fn join_with_factor( + server: &ServerUrl, + code: &str, + device_name: &str, + password: Option<&str>, + factor: Option<&Factor>, +) -> Result { + join_over_with(&Reqwest::new(server).map_err(JoinError::Transport)?, server, code, device_name, password, factor).await } /// [`join`] over another connection (a simulated network, in tests). @@ -542,15 +751,43 @@ pub async fn join_over( device_name: &str, password: Option<&str>, ) -> Result { - let body = JoinRequest { code: code.to_owned(), device_name: device_name.to_owned(), password: password.map(str::to_owned) }; + join_over_with(connection, server, code, device_name, password, None).await +} + +/// [`join_over`] with a second factor. +pub async fn join_over_with( + connection: &C, + server: &ServerUrl, + code: &str, + device_name: &str, + password: Option<&str>, + factor: Option<&Factor>, +) -> Result { + let (client_kind, platform, app_version) = device_details(); + let body = JoinRequest { + code: code.to_owned(), + device_name: device_name.to_owned(), + password: password.map(str::to_owned), + totp_code: factor.and_then(|f| f.totp_code.clone()), + recovery_code: factor.and_then(|f| f.recovery_code.clone()), + client_kind, + platform, + app_version, + }; let req = request(Method::POST, server.endpoint("join"), None, 0).json(&body).map_err(JoinError::Transport)?; let resp = connection.send(req).await.map_err(JoinError::Transport)?; if resp.status.is_success() { return json(resp, MAX_SMALL_JSON_BYTES).await.map_err(JoinError::Transport); } let status = resp.status; + let retry = retry_after_of(&resp.headers); let body = body_capped(resp, MAX_SMALL_JSON_BYTES).await.ok().and_then(|b| serde_json::from_slice::(&b).ok()); Err(match body.as_ref().map(|b| b.error.as_str()) { + Some(codes::TWO_FACTOR_REQUIRED) => JoinError::TwoFactorRequired, + Some(codes::TWO_FACTOR_INVALID) => JoinError::TwoFactorInvalid(body.as_ref().map(|b| clean(&b.message)).unwrap_or_default()), + Some(codes::TWO_FACTOR_LOCKED) => { + JoinError::TwoFactorLocked { message: body.as_ref().map(|b| clean(&b.message)).unwrap_or_default(), retry_after_secs: retry } + } Some(codes::INVALID_CODE) => JoinError::InvalidCode, Some(codes::PASSWORD_REQUIRED) => JoinError::PasswordRequired, Some(codes::WEAK_PASSWORD) => JoinError::WeakPassword(body.as_ref().map(|b| clean(&b.message)).unwrap_or_default()), @@ -660,6 +897,67 @@ impl HttpTransport { } } + /// A signed-in call whose refusals the agent keeps whole. + async fn account_call(&self, req: Request) -> Result { + let resp = self.send(req).await?; + if resp.status.is_success() { + return Ok(json(resp, MAX_SMALL_JSON_BYTES).await?); + } + Err(failure(resp).await) + } + + /// [`HttpTransport::account_call`] for an answer without a body. + async fn account_unit(&self, req: Request) -> Result<(), CallError> { + let resp = self.send(req).await?; + if resp.status.is_success() { + return Ok(()); + } + Err(failure(resp).await) + } + + /// Whether the account has two-factor sign-in on (`GET /account/two-factor`). + pub async fn two_factor_status(&self) -> Result { + self.account_call(self.request(Method::GET, "account/two-factor", 0)).await + } + + /// Starts a set-up with the account's password: the new secret, shown once. + pub async fn two_factor_setup(&self, password: &str) -> Result { + let req = self.request(Method::POST, "account/two-factor/setup", 0).json(&TwoFactorSetupRequest { password: password.to_owned() })?; + self.account_call(req).await + } + + /// Turns it on with the first code: the recovery codes, shown once. + pub async fn two_factor_confirm(&self, totp_code: &str) -> Result { + let req = self.request(Method::POST, "account/two-factor/confirm", 0).json(&TwoFactorConfirmRequest { totp_code: totp_code.to_owned() })?; + self.account_call(req).await + } + + /// Turns it off: the password and a code or recovery code. + pub async fn two_factor_disable(&self, body: &TwoFactorProofRequest) -> Result<(), CallError> { + self.account_unit(self.request(Method::POST, "account/two-factor/disable", 0).json(body)?).await + } + + /// New recovery codes: the password and a code from the authenticator app. + pub async fn two_factor_recovery_codes(&self, body: &TwoFactorProofRequest) -> Result { + self.account_call(self.request(Method::POST, "account/two-factor/recovery-codes", 0).json(body)?).await + } + + /// Changes the account's password. + pub async fn change_password(&self, body: &ChangePasswordRequest) -> Result<(), CallError> { + self.account_unit(self.request(Method::POST, "account/password", 0).json(body)?).await + } + + /// Signs every other device of the account out. + pub async fn revoke_other_devices(&self) -> Result { + self.account_call(self.request(Method::POST, "devices/revoke-others", 0)).await + } + + /// Renames one of the account's devices. + pub async fn rename_device(&self, device: DeviceId, name: &str) -> Result { + let req = self.request(Method::PATCH, &format!("devices/{device}"), 0).json(&RenameDeviceRequest { name: name.to_owned() })?; + self.account_call(req).await + } + /// Revokes a device of this account (this one, to sign out). pub async fn revoke(&self, device: DeviceId) -> Result<(), TransportError> { self.no_content(self.request(Method::DELETE, &format!("devices/{device}"), 0)).await @@ -763,6 +1061,26 @@ mod tests { use super::*; use craft_cloud_proto::{Conflict, ConflictKind}; + #[test] + fn a_second_factor_is_read_as_typed_and_hidden_in_debug() { + assert_eq!(Factor::typed(" 123 456 "), Factor { totp_code: Some("123456".into()), recovery_code: None }); + assert_eq!(Factor::typed("abcd-efgh-jkmn-pqrs"), Factor { totp_code: None, recovery_code: Some("abcd-efgh-jkmn-pqrs".into()) }); + assert_eq!(Factor::typed("12345").recovery_code.as_deref(), Some("12345"), "five digits are not a code"); + let shown = format!("{:?}", Factor::typed("123456")); + assert!(!shown.contains("123456"), "{shown}"); + let shown = format!( + "{:?}", + Refusal { + status: 401, + error: "x".into(), + message: "m".into(), + two_factor: Some(TwoFactorChallenge { challenge: "chal-secret".into(), expires_ms: 1 }), + retry_after_secs: None + } + ); + assert!(!shown.contains("chal-secret"), "{shown}"); + } + #[test] fn https_is_required_except_on_this_machine() { assert!(ServerUrl::parse("https://cloud.example.com").is_ok()); @@ -811,7 +1129,12 @@ mod tests { #[test] fn requests_keep_bodies_and_tokens_out_of_debug_output() { let server = ServerUrl::parse("http://127.0.0.1:1").unwrap(); - let body = LoginRequest { username: "ana".into(), password: "correct horse battery".into(), device_name: "laptop".into() }; + let body = LoginRequest { + username: "ana".into(), + password: "correct horse battery".into(), + device_name: "laptop".into(), + ..LoginRequest::default() + }; let login = request(Method::POST, server.endpoint("auth/login"), None, 0).json(&body).unwrap(); let shown = format!("{login:?}"); assert!(!shown.contains("correct horse") && !shown.contains("ana"), "{shown}"); diff --git a/apps/craft-cloud-agent/src/local_api.rs b/apps/craft-cloud-agent/src/local_api.rs index 3e2f8bb5..8359dc7d 100644 --- a/apps/craft-cloud-agent/src/local_api.rs +++ b/apps/craft-cloud-agent/src/local_api.rs @@ -25,11 +25,14 @@ use axum::response::{IntoResponse, Response}; use axum::routing::{delete, get, post}; use axum::{Json, Router}; use craft_cloud_proto::DeviceId; -use craft_cloud_proto::api::{DeviceInfo, ErrorBody, PairingStatus, codes as api_codes}; +use craft_cloud_proto::api::{ + DeviceInfo, PairingStatus, RecoveryCodes, RevokedOthers, TwoFactorChallenge, TwoFactorSetupResponse, TwoFactorStatus, codes as api_codes, +}; use craft_cloud_proto::join::{Code, JoinString, MAX_JOIN_BYTES, PAIRING_CHARS}; use craft_cloud_proto::local::{ - AutostartRequest, JoinRequest, LOCAL_API_FILE, LOCAL_API_MAX, LOCAL_API_SCHEMA, LocalApiFile, LoginRequest, LogoutRequest, MAX_LOCAL_JSON_BYTES, - PairingInfo, RunState, Status, codes, paths, + AutostartRequest, ChangePasswordRequest, JoinRequest, LOCAL_API_FILE, LOCAL_API_MAX, LOCAL_API_SCHEMA, LocalApiFile, LoginRequest, LogoutRequest, + MAX_LOCAL_JSON_BYTES, PairingInfo, PasswordRequest, RenameDeviceRequest, RunState, SecondFactorLoginRequest, Status, TotpRequest, + TwoFactorProofRequest, codes, paths, }; use tokio::sync::{mpsc, oneshot}; @@ -46,6 +49,8 @@ use crate::live::app_blobs::{self, BlobError}; pub enum Command { Login(LoginRequest, oneshot::Sender>), Join(JoinRequest, oneshot::Sender>), + /// The second step of a sign-in that answered `two_factor_required`. + LoginSecondFactor(SecondFactorLoginRequest, oneshot::Sender>), Logout(LogoutRequest, oneshot::Sender>), /// Hold syncing and the live connection until [`Command::Resume`], also after a restart. Pause(oneshot::Sender>), @@ -191,23 +196,35 @@ impl Control { } } -/// An error answer: the HTTP status and the [`ErrorBody`]. +/// An error answer: the HTTP status, the code and message, and what a two-factor answer carries beyond them (the challenge +/// of `two_factor_required`, the wait of a lock, also as `Retry-After`). #[derive(Debug)] pub struct LocalError { pub status: StatusCode, - pub code: &'static str, + pub code: String, pub message: String, + pub two_factor: Option, + pub retry_after_secs: Option, } impl LocalError { - pub fn new(status: StatusCode, code: &'static str, message: impl Into) -> Self { - Self { status, code, message: message.into() } + pub fn new(status: StatusCode, code: impl Into, message: impl Into) -> Self { + Self { status, code: code.into(), message: message.into(), two_factor: None, retry_after_secs: None } } } impl IntoResponse for LocalError { fn into_response(self) -> Response { - (self.status, Json(ErrorBody { error: self.code.to_owned(), message: self.message, ..ErrorBody::default() })).into_response() + let extras = craft_cloud_proto::local::ErrorExtras { two_factor: self.two_factor, retry_after_secs: self.retry_after_secs }; + let mut body = serde_json::json!({ "error": self.code, "message": self.message }); + if let (Some(object), Ok(serde_json::Value::Object(more))) = (body.as_object_mut(), serde_json::to_value(&extras)) { + object.extend(more); + } + let mut response = (self.status, Json(body)).into_response(); + if let Some(secs) = self.retry_after_secs { + response.headers_mut().insert(axum::http::header::RETRY_AFTER, axum::http::HeaderValue::from(secs)); + } + response } } @@ -219,6 +236,32 @@ impl From for LocalError { let inner = LocalError::from(*inner); return Self::new(inner.status, inner.code, message); } + // The second factor: a challenge, a wrong code, a lock and a server's refusal keep what they carry. None of them is a + // revoked device (that is `Revoked`, from a 401 on a signed-in call). + match &e { + AgentError::TwoFactorRequired(challenge) => { + let mut error = Self::new(StatusCode::UNAUTHORIZED, codes::TWO_FACTOR_REQUIRED, message); + // `join` has no challenge to hand on: the same join goes again with the code. + error.two_factor = Some(challenge.clone()); + return error; + } + AgentError::TwoFactorInvalid(_) => return Self::new(StatusCode::UNAUTHORIZED, codes::TWO_FACTOR_INVALID, message), + AgentError::ChallengeGone => return Self::new(StatusCode::CONFLICT, codes::TWO_FACTOR_EXPIRED, message), + AgentError::TwoFactorLocked { retry_after_secs, .. } => { + let mut error = Self::new(StatusCode::TOO_MANY_REQUESTS, codes::TWO_FACTOR_LOCKED, message); + error.retry_after_secs = *retry_after_secs; + return error; + } + AgentError::Refused { status, code, retry_after_secs, .. } => { + // The server's own status and code (never a 401: that is `Revoked`). + let status = + StatusCode::from_u16(*status).ok().filter(|s| (s.is_client_error() || s.is_server_error()) && *s != StatusCode::UNAUTHORIZED); + let mut error = Self::new(status.unwrap_or(StatusCode::BAD_REQUEST), code.clone(), message); + error.retry_after_secs = *retry_after_secs; + return error; + } + _ => {} + } let (status, code) = match &e { AgentError::WrongPassword => (StatusCode::UNAUTHORIZED, codes::WRONG_PASSWORD), AgentError::InvalidCode => (StatusCode::NOT_FOUND, codes::INVALID_CODE), @@ -343,14 +386,22 @@ fn router(state: Api) -> Router { Router::new() .route(paths::STATUS, get(status)) .route(paths::LOGIN, post(login)) + .route(paths::LOGIN_SECOND_FACTOR, post(login_second_factor).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) .route(paths::LOGOUT, post(logout)) + .route(paths::TWO_FACTOR, get(two_factor_status)) + .route(paths::TWO_FACTOR_SETUP, post(two_factor_setup).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route(paths::TWO_FACTOR_CONFIRM, post(two_factor_confirm).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route(paths::TWO_FACTOR_DISABLE, post(two_factor_disable).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route(paths::TWO_FACTOR_RECOVERY_CODES, post(two_factor_recovery_codes).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route(paths::ACCOUNT_PASSWORD, post(change_password).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route(paths::DEVICES_REVOKE_OTHERS, post(revoke_other_devices)) .route(paths::RECENTS, get(crate::live::recents_socket)) .route(paths::DOCS, get(crate::live::docs::docs_socket)) .route(paths::JOIN, post(join)) .route(paths::PAIRINGS, post(create_pairing)) .route(&format!("{}/{{id}}", paths::PAIRINGS), get(pairing_status).delete(delete_pairing)) .route(paths::DEVICES, get(devices)) - .route(&format!("{}/{{id}}", paths::DEVICES), delete(remove_device)) + .route(&format!("{}/{{id}}", paths::DEVICES), delete(remove_device).patch(rename_device).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) .route(paths::AUTOSTART, post(set_autostart)) .route(paths::PAUSE, post(pause)) .route(paths::RESUME, post(resume)) @@ -408,6 +459,101 @@ async fn login(State(state): State, body: Result, JsonRe ask(&state, |reply| Command::Login(request, reply)).await } +/// Most bytes of the small bodies (a password, a code, a name): far less than a join string's megabyte. +const TINY_JSON_BYTES: usize = 4 * 1024; + +async fn login_second_factor( + State(state): State, + body: Result, JsonRejection>, +) -> Result, LocalError> { + let Json(request) = body?; + ask(&state, |reply| Command::LoginSecondFactor(request, reply)).await +} + +/// A call of the signed-in account to the server, its refusals kept whole: never mistaken for a revoked device. +fn account_call_error(e: crate::http::CallError) -> LocalError { + AgentError::from(e).into() +} + +async fn two_factor_status(State(state): State) -> Result, LocalError> { + let (_, transport) = server_api(&state)?; + Ok(Json(transport.two_factor_status().await.map_err(account_call_error)?)) +} + +async fn two_factor_setup( + State(state): State, + body: Result, JsonRejection>, +) -> Result, LocalError> { + let Json(request) = body?; + let (_, transport) = server_api(&state)?; + Ok(Json(transport.two_factor_setup(&request.password).await.map_err(account_call_error)?)) +} + +async fn two_factor_confirm(State(state): State, body: Result, JsonRejection>) -> Result, LocalError> { + let Json(request) = body?; + let (_, transport) = server_api(&state)?; + Ok(Json(transport.two_factor_confirm(&request.totp_code).await.map_err(account_call_error)?)) +} + +async fn two_factor_disable(State(state): State, body: Result, JsonRejection>) -> Result { + let Json(request) = body?; + let (_, transport) = server_api(&state)?; + transport + .two_factor_disable(&craft_cloud_proto::api::TwoFactorProofRequest { + password: request.password, + totp_code: request.totp_code, + recovery_code: request.recovery_code, + }) + .await + .map_err(account_call_error)?; + Ok(StatusCode::NO_CONTENT) +} + +async fn two_factor_recovery_codes( + State(state): State, + body: Result, JsonRejection>, +) -> Result, LocalError> { + let Json(request) = body?; + let (_, transport) = server_api(&state)?; + let sent = craft_cloud_proto::api::TwoFactorProofRequest { + password: request.password, + totp_code: request.totp_code, + recovery_code: request.recovery_code, + }; + Ok(Json(transport.two_factor_recovery_codes(&sent).await.map_err(account_call_error)?)) +} + +async fn change_password(State(state): State, body: Result, JsonRejection>) -> Result { + let Json(request) = body?; + let (_, transport) = server_api(&state)?; + let sent = craft_cloud_proto::api::ChangePasswordRequest { current_password: request.current_password, new_password: request.new_password }; + transport.change_password(&sent).await.map_err(account_call_error)?; + Ok(StatusCode::NO_CONTENT) +} + +async fn revoke_other_devices(State(state): State) -> Result, LocalError> { + let (_, transport) = server_api(&state)?; + Ok(Json(transport.revoke_other_devices().await.map_err(account_call_error)?)) +} + +async fn rename_device( + State(state): State, + id: Result, PathRejection>, + body: Result, JsonRejection>, +) -> Result, LocalError> { + let id = path_id(id)?; + let Json(request) = body?; + let (_, transport) = server_api(&state)?; + let renamed = transport.rename_device(id, &request.name).await.map_err(|e| match e { + // A device that isn't there (or isn't ours, or is signed out): the server's `not_found`. + crate::http::CallError::Transport(craft_cloud_sync::TransportError::NotFound) => { + LocalError::new(StatusCode::NOT_FOUND, api_codes::NOT_FOUND, "no such computer in this cloud") + } + other => account_call_error(other), + })?; + Ok(Json(renamed)) +} + async fn logout(State(state): State, body: Result, JsonRejection>) -> Result, LocalError> { let Json(request) = body?; ask(&state, |reply| Command::Logout(request, reply)).await diff --git a/apps/craft-cloud-agent/tests/end_to_end.rs b/apps/craft-cloud-agent/tests/end_to_end.rs index 92d2fd97..44a6ad8e 100644 --- a/apps/craft-cloud-agent/tests/end_to_end.rs +++ b/apps/craft-cloud-agent/tests/end_to_end.rs @@ -761,6 +761,39 @@ async fn a_failed_pass_keeps_the_deletes_the_user_was_shown() { assert_eq!((exit, summary.removed_remotely), (Exit::Done, 12), "{summary:?}"); } +/// The agent tells the server what it is when it signs in and when it joins, so the account's device list shows it. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn login_and_join_send_the_agents_device_details() { + use craft_cloud_agent::cli::JoinArgs; + use craft_cloud_proto::join::{Code, INVITE_CHARS, INVITE_TTL_MS, JoinString}; + let server = server().await; + let ana = server.meta.user_by_name("ana").await.unwrap().unwrap(); + async fn details(server: &Server, user: craft_cloud_meta::UserId, name: &str) -> (String, String, String) { + let devices = server.meta.list_devices(user).await.unwrap(); + let device = devices.into_iter().find(|d| d.name == name).unwrap_or_else(|| panic!("no device {name}")); + (device.client.kind, device.client.platform, device.client.app_version) + } + let expected = ("agent".to_owned(), std::env::consts::OS.to_owned(), env!("CARGO_PKG_VERSION").to_owned()); + + let a = Agent::signed_in(&server, "desk").await; + assert_eq!(details(&server, ana.id, "desk").await, expected, "after login"); + drop(a); + + let invite = Code::generate(INVITE_CHARS).unwrap(); + let now = i64::try_from(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis()).unwrap(); + server.meta.create_invite("ana", &invite.hash(), now, INVITE_TTL_MS).await.unwrap(); + let b = Agent::new(); + let args = JoinArgs { + join: JoinString::new(server.url.clone(), invite).link(), + folder: Some(b.folder.clone()), + device_name: Some("laptop".into()), + adopt: false, + autostart: false, + }; + cli::join(&b.data, &args, None, &mut Vec::new()).await.unwrap(); + assert_eq!(details(&server, ana.id, "laptop").await, expected, "after join"); +} + #[cfg(unix)] #[tokio::test(flavor = "multi_thread", worker_threads = 4)] async fn login_makes_the_agent_folder_owner_only() { diff --git a/apps/craft-cloud-agent/tests/local_api.rs b/apps/craft-cloud-agent/tests/local_api.rs index 2839d706..0b48d145 100644 --- a/apps/craft-cloud-agent/tests/local_api.rs +++ b/apps/craft-cloud-agent/tests/local_api.rs @@ -174,6 +174,15 @@ const ROUTES: &[(&str, &str)] = &[ ("GET", "/v1/devices"), ("DELETE", "/v1/devices/0192f0c4-0000-7000-8000-000000000000"), ("POST", "/v1/autostart"), + ("POST", "/v1/login/second-factor"), + ("GET", "/v1/account/two-factor"), + ("POST", "/v1/account/two-factor/setup"), + ("POST", "/v1/account/two-factor/confirm"), + ("POST", "/v1/account/two-factor/disable"), + ("POST", "/v1/account/two-factor/recovery-codes"), + ("POST", "/v1/account/password"), + ("POST", "/v1/devices/revoke-others"), + ("PATCH", "/v1/devices/0192f0c4-0000-7000-8000-000000000000"), ]; /// A user session without systemd: Linux's entry is the XDG autostart file. @@ -694,6 +703,8 @@ impl Machine { device_name: Some("lptp1".into()), adopt, password: password.map(str::to_owned), + totp_code: None, + recovery_code: None, }) .unwrap() } diff --git a/apps/craft-cloud-agent/tests/two_factor.rs b/apps/craft-cloud-agent/tests/two_factor.rs new file mode 100644 index 00000000..105ba9f9 --- /dev/null +++ b/apps/craft-cloud-agent/tests/two_factor.rs @@ -0,0 +1,631 @@ +//! Two-factor sign-in through the agent: the second step of a sign-in and the code of a join, the account's two-factor calls +//! passed on to the server, and the command line's prompts, against a real server (with a key for two-factor secrets) on a +//! loopback port and the server's clock held still, so that codes don't change under a test. +//! +//! What they pin down: a challenge, its expiry and a lock's `Retry-After` reach the app typed; a wrong code, a wrong +//! password or a lock never sign the agent out (a 403 or 429 on the account's calls, a 401 at the second step), while a real +//! 401 still does; nothing secret is kept on disk or printed. + +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::sync::atomic::{AtomicI64, Ordering}; +use std::time::{Duration, Instant}; + +use clap::Parser; +use craft_cloud_agent::account::DataDir; +use craft_cloud_agent::cli::{self, AgentError, Exit}; +use craft_cloud_agent::daemon::{RunOptions, run_loop}; +use craft_cloud_agent::schedule::Timing; +use craft_cloud_app::{Client, Error, JoinRequest, LoginRequest, SecondFactorLoginRequest, TwoFactorProofRequest}; +use craft_cloud_meta::{Meta, Role}; +use craft_cloud_proto::join::{Code, INVITE_CHARS, INVITE_TTL_MS}; +use craft_cloud_proto::local::{LOCAL_API_FILE, LocalApiFile}; +use craft_cloud_server::AppState; +use craft_cloud_server::auth::hash_password; +use craft_cloud_server::secretbox::{SecretBox, TwoFactorKey}; +use craft_cloud_server::totp; +use craft_cloud_store::Store; +use craft_cloud_store::disk::DiskBackend; +use tokio::sync::mpsc; + +const PASSWORD: &str = "correct horse battery"; +const PATIENCE: Duration = Duration::from_secs(30); + +struct Server { + url: String, + meta: Meta, + clock: Arc, + _dirs: (tempfile::TempDir, tempfile::TempDir), +} + +impl Server { + async fn start() -> Self { + let (db, blobs) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap()); + let meta = Meta::open(&format!("sqlite:{}", db.path().join("meta.db").display())).await.unwrap(); + meta.create_user("ana", &hash_password(PASSWORD).unwrap(), Role::Member, 0).await.unwrap(); + // The clock stands still at the start of a TOTP step, until a test moves it. + let clock = Arc::new(AtomicI64::new(totp::step_at(craft_cloud_server::state::now_ms()) * 30_000 + 1_000)); + let ticking = clock.clone(); + let state = AppState::new(meta.clone(), Store::new(DiskBackend::new(blobs.path()).unwrap()), vec![]) + .unwrap() + .with_two_factor(Some(SecretBox::new(&TwoFactorKey::from_bytes([9; 32]), None).unwrap())) + .with_clock(Arc::new(move || ticking.load(Ordering::SeqCst))); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + tokio::spawn(craft_cloud_server::serve(state, listener, std::future::pending())); + Server { url, meta, clock, _dirs: (db, blobs) } + } + + fn now(&self) -> i64 { + self.clock.load(Ordering::SeqCst) + } + + fn advance(&self, ms: i64) { + self.clock.fetch_add(ms, Ordering::SeqCst); + } +} + +fn quick() -> RunOptions { + RunOptions { + timing: Timing { + debounce: Duration::from_millis(100), + max_delay: Duration::from_secs(1), + stability: Duration::from_millis(200), + periodic: Duration::from_secs(600), + unwatched_periodic: Duration::from_millis(500), + backoff_first: Duration::from_millis(200), + backoff_max: Duration::from_secs(1), + recheck: Duration::from_millis(300), + recheck_slow: Duration::from_secs(5), + approval_check: Duration::from_millis(100), + held_check: Duration::from_millis(300), + }, + heartbeat: Duration::from_millis(200), + poll_wait: Duration::from_secs(1), + poll_retry_first: Duration::from_millis(100), + poll_retry_max: Duration::from_secs(1), + poll_min_spacing: Duration::from_millis(100), + poll_busy: Duration::from_millis(200), + fail_watch: false, + record_delay: Duration::ZERO, + session_grace: Duration::from_secs(10), + announce_delay: Duration::ZERO, + autostart: craft_cloud_agent::autostart::Target::Off, + } +} + +/// A running agent (`run` on a thread of its own), signed out, and a client of its local API. +struct Machine { + data: DataDir, + folder: PathBuf, + stop: mpsc::UnboundedSender<()>, + thread: Option, String)>>, + _dir: tempfile::TempDir, +} + +impl Machine { + async fn start() -> Self { + let dir = tempfile::tempdir().unwrap(); + let data = DataDir::new(dir.path().join("data")); + let (stop, stops) = mpsc::unbounded_channel(); + let loop_data = data.clone(); + let thread = std::thread::spawn(move || { + let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap(); + let mut out = Vec::new(); + let exit = runtime.block_on(run_loop(&loop_data, quick(), stops, &mut out)); + (exit, String::from_utf8_lossy(&out).into_owned()) + }); + let file = data.path().join(LOCAL_API_FILE); + let deadline = Instant::now() + PATIENCE; + while !file.exists() { + assert!(Instant::now() < deadline, "the local API never came up"); + tokio::time::sleep(Duration::from_millis(50)).await; + } + Self { folder: dir.path().join("Craft Cloud"), data, stop, thread: Some(thread), _dir: dir } + } + + fn client(&self) -> Client { + Client::connect_in(self.data.path()).unwrap() + } + + fn api(&self) -> LocalApiFile { + serde_json::from_slice(&std::fs::read(self.data.path().join(LOCAL_API_FILE)).unwrap()).unwrap() + } + + fn login_request(&self, server: &Server) -> LoginRequest { + LoginRequest { + server: server.url.clone(), + user: "ana".into(), + password: PASSWORD.into(), + folder: Some(self.folder.display().to_string()), + device_name: Some("desktop".into()), + adopt: true, + } + } + + fn second_step(&self, server: &Server, challenge: &str, totp_code: Option<&str>, recovery_code: Option<&str>) -> SecondFactorLoginRequest { + SecondFactorLoginRequest { + server: server.url.clone(), + user: "ana".into(), + folder: Some(self.folder.display().to_string()), + device_name: Some("desktop".into()), + adopt: true, + challenge: challenge.into(), + totp_code: totp_code.map(str::to_owned), + recovery_code: recovery_code.map(str::to_owned), + } + } + + async fn stop(mut self) -> (Result, String) { + let _ = self.stop.send(()); + let thread = self.thread.take().unwrap(); + tokio::task::spawn_blocking(move || thread.join().unwrap()).await.unwrap() + } +} + +impl Drop for Machine { + fn drop(&mut self) { + if self.thread.is_some() { + let _ = self.stop.send(()); + let _ = self.stop.send(()); + } + } +} + +/// A blocking call of the app client, off the runtime. +async fn off(f: impl FnOnce() -> Result + Send + 'static) -> Result { + tokio::task::spawn_blocking(f).await.unwrap() +} + +fn code_for(secret: &[u8], at_ms: i64) -> String { + totp::code_for(secret, totp::step_at(at_ms)).unwrap() +} + +fn wrong_code(code: &str) -> String { + format!("{:06}", (code.parse::().unwrap() + 1) % 1_000_000) +} + +/// Signs the machine in and turns two-factor sign-in on through the agent. The secret and the recovery codes come back. +async fn protect(server: &Server, machine: &Machine) -> (Vec, Vec) { + let client = machine.client(); + let request = machine.login_request(server); + let c = client.clone(); + assert!(off(move || c.login(&request)).await.unwrap().signed_in); + let c = client.clone(); + let setup = off(move || c.two_factor_setup(PASSWORD)).await.unwrap(); + let secret = totp::base32_decode(&setup.secret).unwrap(); + let code = code_for(&secret, server.now()); + let c = client.clone(); + let codes = off(move || c.two_factor_confirm(&code)).await.unwrap(); + assert_eq!(codes.recovery_codes.len(), 10); + (secret, codes.recovery_codes) +} + +async fn sign_out(machine: &Machine) { + let c = machine.client(); + assert!(!off(move || c.logout(false)).await.unwrap().signed_in); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_second_step_of_a_sign_in_goes_through_the_agent_typed() { + let server = Server::start().await; + let machine = Machine::start().await; + let (secret, recovery) = protect(&server, &machine).await; + sign_out(&machine).await; + let client = machine.client(); + + // The first step: a challenge, typed, with its expiry; the agent is still signed out and keeps nothing. + let request = machine.login_request(&server); + let c = client.clone(); + let refused = off(move || c.login(&request)).await.unwrap_err(); + assert_eq!(refused.code(), Some("two_factor_required")); + let challenge = refused.challenge().cloned().expect("a challenge"); + assert_eq!(challenge.challenge.len(), 43); + assert_eq!(challenge.expires_ms, server.now() + 5 * 60_000); + assert!(!client.status().unwrap().signed_in); + // A wrong password is still a wrong password, with nothing about two-factor sign-in. + let wrong = LoginRequest { password: "wrong password".into(), ..machine.login_request(&server) }; + let c = client.clone(); + let refused = off(move || c.login(&wrong)).await.unwrap_err(); + assert!(matches!(&refused, Error::Refused { code, .. } if code == "wrong_password"), "{refused:?}"); + + // A wrong code is `two_factor_invalid` and signs nothing in or out; a challenge takes five. + let right = code_for(&secret, server.now() + 30_000); + server.advance(30_000); + let step = machine.second_step(&server, &challenge.challenge, Some(&wrong_code(&right)), None); + let c = client.clone(); + let invalid = off(move || c.login_second_factor(&step)).await.unwrap_err(); + assert!(matches!(&invalid, Error::Refused { code, .. } if code == "two_factor_invalid"), "{invalid:?}"); + assert!(!client.status().unwrap().signed_in); + // The right code finishes it, and the agent syncs as after a password sign-in. + let step = machine.second_step(&server, &challenge.challenge, Some(&right), None); + let c = client.clone(); + let status = off(move || c.login_second_factor(&step)).await.unwrap(); + assert!(status.signed_in && status.account.as_deref() == Some("ana"), "{status:?}"); + // The challenge is used up: the same step again is `two_factor_expired`, never a signed-out agent. + let step = machine.second_step(&server, &challenge.challenge, Some(&right), None); + let c = client.clone(); + let again = off(move || c.login_second_factor(&step)).await.unwrap_err(); + assert!(client.status().unwrap().signed_in); + assert!(matches!(&again, Error::Refused { .. } | Error::RefusedWith { .. }), "{again:?}"); + + // A recovery code, once. + sign_out(&machine).await; + let request = machine.login_request(&server); + let c = client.clone(); + let challenge = off(move || c.login(&request)).await.unwrap_err().challenge().cloned().unwrap(); + let step = machine.second_step(&server, &challenge.challenge, None, Some(&recovery[0].to_lowercase().replace('-', " "))); + let c = client.clone(); + assert!(off(move || c.login_second_factor(&step)).await.unwrap().signed_in); + sign_out(&machine).await; + let request = machine.login_request(&server); + let c = client.clone(); + let challenge = off(move || c.login(&request)).await.unwrap_err().challenge().cloned().unwrap(); + let step = machine.second_step(&server, &challenge.challenge, None, Some(&recovery[0])); + let c = client.clone(); + let used = off(move || c.login_second_factor(&step)).await.unwrap_err(); + assert!(matches!(&used, Error::Refused { code, .. } if code == "two_factor_invalid"), "{used:?}"); + + // A challenge that ran out is `two_factor_expired` (a 401 here would read as a revoked device). + server.advance(5 * 60_000 + 1); + let step = machine.second_step(&server, &challenge.challenge, Some(&code_for(&secret, server.now())), None); + let c = client.clone(); + let expired = off(move || c.login_second_factor(&step)).await.unwrap_err(); + assert!(matches!(&expired, Error::Refused { code, .. } if code == "two_factor_expired"), "{expired:?}"); + let (exit, out) = machine.stop().await; + assert_eq!(exit.unwrap(), Exit::Done, "{out}"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_accounts_calls_are_passed_on_and_a_refusal_never_signs_the_agent_out() { + let server = Server::start().await; + let machine = Machine::start().await; + let client = machine.client(); + let request = machine.login_request(&server); + let c = client.clone(); + assert!(off(move || c.login(&request)).await.unwrap().signed_in); + + let c = client.clone(); + let off_state = off(move || c.two_factor_status()).await.unwrap(); + assert_eq!((off_state.enabled, off_state.recovery_codes_left, off_state.pending), (false, 0, false)); + // A wrong password is 403 `wrong_password`, and the agent stays signed in, not even "removed". + let c = client.clone(); + let wrong = off(move || c.two_factor_setup("wrong password!")).await.unwrap_err(); + assert!(matches!(&wrong, Error::Refused { code, .. } if code == "wrong_password"), "{wrong:?}"); + let status = client.status().unwrap(); + assert!(status.signed_in && !status.removed, "{status:?}"); + let c = client.clone(); + let setup = off(move || c.two_factor_setup(PASSWORD)).await.unwrap(); + assert_eq!(setup.secret.len(), 32); + assert!(setup.otpauth_uri.starts_with("otpauth://totp/Craft%20Cloud:ana?secret="), "{}", setup.otpauth_uri); + let secret = totp::base32_decode(&setup.secret).unwrap(); + let c = client.clone(); + assert!(off(move || c.two_factor_status()).await.unwrap().pending); + // A wrong first code: 403 `two_factor_invalid`; the agent is not signed out. + let right = code_for(&secret, server.now()); + let bad = wrong_code(&right); + let c = client.clone(); + let invalid = off(move || c.two_factor_confirm(&bad)).await.unwrap_err(); + assert!(matches!(&invalid, Error::Refused { code, .. } if code == "two_factor_invalid"), "{invalid:?}"); + assert!(client.status().unwrap().signed_in); + let c = client.clone(); + let codes = off(move || c.two_factor_confirm(&right)).await.unwrap(); + assert_eq!(codes.recovery_codes.len(), 10); + let c = client.clone(); + let on = off(move || c.two_factor_status()).await.unwrap(); + assert_eq!((on.enabled, on.recovery_codes_left), (true, 10)); + // Already on: a conflict, said as one. + let c = client.clone(); + let conflict = off(move || c.two_factor_setup(PASSWORD)).await.unwrap_err(); + assert!(matches!(&conflict, Error::Refused { code, .. } if code == "conflict"), "{conflict:?}"); + + // New recovery codes with a fresh step; the old ones die. + server.advance(30_000); + let fresh = code_for(&secret, server.now()); + let c = client.clone(); + let renewed = off(move || c.two_factor_recovery_codes(PASSWORD, &fresh)).await.unwrap(); + assert!(renewed.recovery_codes.iter().all(|code| !codes.recovery_codes.contains(code))); + + // Password change and the other computers: passed on too. + let c = client.clone(); + let weak = off(move || c.change_password(PASSWORD, "short")).await.unwrap_err(); + assert!(matches!(&weak, Error::Refused { code, .. } if code == "weak_password"), "{weak:?}"); + let c = client.clone(); + assert_eq!(off(move || c.revoke_other_devices()).await.unwrap().revoked, 0); + let c = client.clone(); + let devices = off(move || c.devices()).await.unwrap(); + let this = devices.iter().find(|d| d.current).unwrap().id; + let c = client.clone(); + let renamed = off(move || c.rename_device(this, "my desktop")).await.unwrap(); + assert_eq!(renamed.name, "my desktop"); + let c = client.clone(); + let missing = off(move || c.rename_device(craft_cloud_app::DeviceId::generate(), "x")).await.unwrap_err(); + assert!(matches!(&missing, Error::Refused { code, .. } if code == "not_found"), "{missing:?}"); + + // Ten wrong codes lock it: 429 `two_factor_locked`, typed with its wait, and still no sign-out. + for _ in 0..10 { + let proof = TwoFactorProofRequest { password: PASSWORD.into(), totp_code: Some(bad_code(&secret, &server)), recovery_code: None }; + let c = client.clone(); + let invalid = off(move || c.two_factor_disable(&proof)).await.unwrap_err(); + assert!(matches!(&invalid, Error::Refused { code, .. } if code == "two_factor_invalid"), "{invalid:?}"); + } + let proof = TwoFactorProofRequest { password: PASSWORD.into(), totp_code: Some(code_for(&secret, server.now())), recovery_code: None }; + let c = client.clone(); + let locked = off(move || c.two_factor_disable(&proof)).await.unwrap_err(); + assert_eq!((locked.code(), locked.retry_after_secs()), (Some("two_factor_locked"), Some(900)), "{locked:?}"); + let status = client.status().unwrap(); + assert!(status.signed_in && !status.removed, "{status:?}"); + + // Turning it off with a recovery code works once the lock is over. + server.advance(900_001); + let proof = TwoFactorProofRequest { password: PASSWORD.into(), totp_code: None, recovery_code: Some(renewed.recovery_codes[0].clone()) }; + let c = client.clone(); + off(move || c.two_factor_disable(&proof)).await.unwrap(); + let c = client.clone(); + assert!(!off(move || c.two_factor_status()).await.unwrap().enabled); + + // A real 401 (the device was signed out on the server) is still what it was: the agent can't go on. + let ana = server.meta.user_by_name("ana").await.unwrap().unwrap(); + server.meta.revoke_all_devices(ana.id, craft_cloud_meta::RevokeReason::Admin, server.now()).await.unwrap(); + let c = client.clone(); + let revoked = off(move || c.two_factor_status()).await.unwrap_err(); + assert!(matches!(&revoked, Error::Refused { code, .. } if code == "needs_you"), "{revoked:?}"); + let _ = machine.stop().await; +} + +fn bad_code(secret: &[u8], server: &Server) -> String { + wrong_code(&code_for(secret, server.now())) +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn joining_an_account_with_two_factor_takes_the_code_in_a_second_call() { + let server = Server::start().await; + let first = Machine::start().await; + let (secret, recovery) = protect(&server, &first).await; + let invite = Code::generate(INVITE_CHARS).unwrap(); + server.meta.create_invite("ana", &invite.hash(), server.now(), INVITE_TTL_MS).await.unwrap(); + + let second = Machine::start().await; + let client = second.client(); + let join = JoinRequest { + join: format!("{}/{}", server.url, invite.display()), + folder: Some(second.folder.display().to_string()), + device_name: Some("laptop".into()), + adopt: true, + password: None, + totp_code: None, + recovery_code: None, + }; + // No second factor: `two_factor_required` (no challenge to give: the same join goes again), and the invite is not used up. + let c = client.clone(); + let j = join.clone(); + let refused = off(move || c.join(&j)).await.unwrap_err(); + assert_eq!(refused.code(), Some("two_factor_required"), "{refused:?}"); + assert!(!client.status().unwrap().signed_in); + // A wrong code is `two_factor_invalid`; the invite still works. + server.advance(30_000); + let right = code_for(&secret, server.now()); + let c = client.clone(); + let j = JoinRequest { totp_code: Some(wrong_code(&right)), ..join.clone() }; + let invalid = off(move || c.join(&j)).await.unwrap_err(); + assert!(matches!(&invalid, Error::Refused { code, .. } if code == "two_factor_invalid"), "{invalid:?}"); + let c = client.clone(); + let j = JoinRequest { totp_code: Some(right), ..join.clone() }; + assert!(off(move || c.join(&j)).await.unwrap().signed_in); + // A recovery code does it too, on a new invite. + let invite = Code::generate(INVITE_CHARS).unwrap(); + server.meta.create_invite("ana", &invite.hash(), server.now(), INVITE_TTL_MS).await.unwrap(); + let third = Machine::start().await; + let c = third.client(); + let j = JoinRequest { + join: format!("{}/{}", server.url, invite.display()), + folder: Some(third.folder.display().to_string()), + recovery_code: Some(recovery[2].clone()), + ..join + }; + assert!(off(move || c.join(&j)).await.unwrap().signed_in); + let _ = (first.stop().await, second.stop().await, third.stop().await); +} + +/// The new calls take small bodies only, and what is secret is kept nowhere: not in `account.json`, not in what `run` prints. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn nothing_secret_is_stored_or_printed_and_bodies_are_small() { + let server = Server::start().await; + let machine = Machine::start().await; + let (secret, recovery) = protect(&server, &machine).await; + sign_out(&machine).await; + let client = machine.client(); + let request = machine.login_request(&server); + let c = client.clone(); + let challenge = off(move || c.login(&request)).await.unwrap_err().challenge().cloned().unwrap(); + server.advance(30_000); + let used = code_for(&secret, server.now()); + let step = machine.second_step(&server, &challenge.challenge, Some(&used), None); + let c = client.clone(); + off(move || c.login_second_factor(&step)).await.unwrap(); + + // Big bodies are refused before they are read. + let api = machine.api(); + let big = format!(r#"{{"password":"{}"}}"#, "x".repeat(6000)); + for path in ["/v1/account/two-factor/setup", "/v1/login/second-factor", "/v1/account/password"] { + let (status, _) = raw( + api.port, + &format!("POST {path} HTTP/1.1\r\nHost: 127.0.0.1:{}\r\nAuthorization: Bearer {}\r\nContent-Type: application/json", api.port, api.token), + &big, + ); + assert_eq!(status, 413, "{path}"); + } + let account_json = std::fs::read_to_string(machine.data.path().join("account.json")).unwrap(); + let mut every_file = String::new(); + collect(machine.data.path(), &mut every_file); + let (exit, out) = machine.stop().await; + assert_eq!(exit.unwrap(), Exit::Done, "{out}"); + for secret_text in std::iter::once(used).chain(std::iter::once(challenge.challenge)).chain(recovery).chain(std::iter::once(PASSWORD.to_owned())) { + assert!(!account_json.contains(&secret_text), "{secret_text} in account.json"); + assert!(!every_file.contains(&secret_text), "{secret_text} in the data folder"); + assert!(!out.contains(&secret_text), "{secret_text} in what run printed"); + } +} + +fn collect(dir: &Path, into: &mut String) { + for entry in std::fs::read_dir(dir).unwrap().flatten() { + let path = entry.path(); + if path.is_dir() { + collect(&path, into); + } else if let Ok(text) = std::fs::read_to_string(&path) { + // The local API's own token is this folder's business, not a second factor. + if path.file_name().is_some_and(|n| n != LOCAL_API_FILE) { + into.push_str(&text); + } + } + } +} + +/// One raw HTTP/1.1 request, so a test controls every header. +fn raw(port: u16, head: &str, body: &str) -> (u16, String) { + use std::io::{Read, Write}; + let mut stream = std::net::TcpStream::connect(("127.0.0.1", port)).unwrap(); + stream.set_read_timeout(Some(PATIENCE)).unwrap(); + let request = format!("{head}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()); + stream.write_all(request.as_bytes()).unwrap(); + let mut answer = String::new(); + let _ = stream.read_to_string(&mut answer); + let status = answer.split(' ').nth(1).and_then(|s| s.parse().ok()).unwrap_or(0); + (status, answer.split_once("\r\n\r\n").map(|(_, b)| b.to_owned()).unwrap_or_default()) +} + +// ---- the command line ---- + +fn cli_login(url: &str, data: &Path, folder: &Path) -> cli::Cli { + cli::Cli::try_parse_from([ + "craft-cloud-agent", + "--data-dir", + data.to_str().unwrap(), + "login", + url, + "--user", + "ana", + "--folder", + folder.to_str().unwrap(), + "--adopt", + ]) + .unwrap() +} + +/// A signed-in, protected account from the command line's point of view: the secret and a recovery code. Another agent's data +/// folder made it, through the server's own routes. +async fn protected_account(server: &Server) -> (Vec, Vec) { + let machine = Machine::start().await; + let made = protect(server, &machine).await; + let _ = machine.stop().await; + made +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_command_line_asks_for_the_code_on_standard_input() { + let server = Server::start().await; + let (secret, recovery) = protected_account(&server).await; + let dir = tempfile::tempdir().unwrap(); + let (data, folder) = (dir.path().join("data"), dir.path().join("Craft Cloud")); + + // A wrong code, then the right one: two lines after the password, and the code is never an argument. + server.advance(30_000); + let right = code_for(&secret, server.now()); + let mut out = Vec::new(); + let stdin = format!("{PASSWORD}\n{}\n{right}\n", wrong_code(&right)); + let exit = cli::run(cli_login(&server.url, &data, &folder), &mut stdin.as_bytes(), &mut out).await.unwrap(); + let printed = String::from_utf8(out).unwrap(); + assert_eq!(exit, Exit::Done, "{printed}"); + assert!(printed.contains("two-factor sign-in") && printed.contains("signed in as ana"), "{printed}"); + assert!(!printed.contains(&right) && !printed.contains(PASSWORD), "nothing secret is echoed: {printed}"); + + // Three wrong codes: it gives up with the server's message, and nothing is signed in. + let dir = tempfile::tempdir().unwrap(); + let (data, folder) = (dir.path().join("data"), dir.path().join("Craft Cloud")); + let wrong = wrong_code(&right); + let stdin = format!("{PASSWORD}\n{wrong}\n{wrong}\n{wrong}\n"); + let err = cli::run(cli_login(&server.url, &data, &folder), &mut stdin.as_bytes(), &mut Vec::new()).await.unwrap_err(); + assert!(matches!(err, AgentError::TwoFactorInvalid(_)), "{err:?}"); + assert!(DataDir::new(data.clone()).load_account().unwrap().is_none()); + + // A recovery code instead of a code. + let stdin = format!("{PASSWORD}\n{}\n", recovery[0].to_lowercase()); + let mut out = Vec::new(); + let exit = cli::run(cli_login(&server.url, &data, &folder), &mut stdin.as_bytes(), &mut out).await.unwrap(); + assert_eq!(exit, Exit::Done, "{}", String::from_utf8_lossy(&out)); + + // Nothing on standard input where the code should be: a plain usage error, no panic. + let dir = tempfile::tempdir().unwrap(); + let (data, folder) = (dir.path().join("data"), dir.path().join("Craft Cloud")); + let stdin = format!("{PASSWORD}\n"); + let err = cli::run(cli_login(&server.url, &data, &folder), &mut stdin.as_bytes(), &mut Vec::new()).await.unwrap_err(); + assert!(matches!(err, AgentError::Usage(ref m) if m.contains("no code")), "{err:?}"); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_lock_says_how_long_to_wait() { + let server = Server::start().await; + let machine = Machine::start().await; + let (secret, _) = protect(&server, &machine).await; + let client = machine.client(); + for _ in 0..10 { + let proof = TwoFactorProofRequest { password: PASSWORD.into(), totp_code: Some(bad_code(&secret, &server)), recovery_code: None }; + let c = client.clone(); + let _ = off(move || c.two_factor_disable(&proof)).await; + } + let _ = machine.stop().await; + let dir = tempfile::tempdir().unwrap(); + let (data, folder) = (dir.path().join("data"), dir.path().join("Craft Cloud")); + let stdin = format!("{PASSWORD}\n{}\n", code_for(&secret, server.now() + 30_000)); + let err = cli::run(cli_login(&server.url, &data, &folder), &mut stdin.as_bytes(), &mut Vec::new()).await.unwrap_err(); + match err { + AgentError::TwoFactorLocked { retry_after_secs, message } => { + assert_eq!(retry_after_secs, Some(900)); + assert!(message.contains("15 minutes"), "{message}"); + } + other => panic!("{other:?}"), + } +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_command_line_joins_with_a_code_and_through_a_running_agent() { + let server = Server::start().await; + let (secret, _) = protected_account(&server).await; + let invite = Code::generate(INVITE_CHARS).unwrap(); + server.meta.create_invite("ana", &invite.hash(), server.now(), INVITE_TTL_MS).await.unwrap(); + server.advance(30_000); + let right = code_for(&secret, server.now()); + let dir = tempfile::tempdir().unwrap(); + let (data, folder) = (dir.path().join("data"), dir.path().join("Craft Cloud")); + let join = cli::Cli::try_parse_from([ + "craft-cloud-agent", + "--data-dir", + data.to_str().unwrap(), + "join", + &format!("{}/{}", server.url, invite.display()), + "--folder", + folder.to_str().unwrap(), + "--adopt", + ]) + .unwrap(); + let mut out = Vec::new(); + let stdin = format!("{right}\n"); + let exit = cli::run(join, &mut stdin.as_bytes(), &mut out).await.unwrap(); + let printed = String::from_utf8(out).unwrap(); + assert_eq!(exit, Exit::Done, "{printed}"); + assert!(printed.contains("two-factor sign-in") && printed.contains("joined as ana"), "{printed}"); + + // `login` while `run` holds the data folder goes through the running agent, second step and all. + let machine = Machine::start().await; + server.advance(30_000); + let right = code_for(&secret, server.now()); + let data = machine.data.path().to_path_buf(); + let stdin = format!("{PASSWORD}\n{right}\n"); + let mut out = Vec::new(); + let exit = cli::run(cli_login(&server.url, &data, &machine.folder), &mut stdin.as_bytes(), &mut out).await.unwrap(); + let printed = String::from_utf8(out).unwrap(); + assert_eq!(exit, Exit::Done, "{printed}"); + assert!(printed.contains("through the running agent"), "{printed}"); + assert!(machine.client().status().unwrap().signed_in); + let _ = machine.stop().await; +} diff --git a/apps/craft-cloud-desktop/src/agent.rs b/apps/craft-cloud-desktop/src/agent.rs index 2ed7185d..c1f29308 100644 --- a/apps/craft-cloud-desktop/src/agent.rs +++ b/apps/craft-cloud-desktop/src/agent.rs @@ -10,9 +10,13 @@ use std::sync::mpsc::{Receiver, Sender, channel}; use std::sync::{Arc, Mutex, PoisonError}; use std::time::Duration; -use craft_cloud_app::{Client, DeviceId, DeviceInfo, Error, JoinRequest, PairingInfo, PairingStatus, Status, Uuid}; +use craft_cloud_app::{ + Client, DeviceId, DeviceInfo, Error, JoinRequest, PairingInfo, PairingStatus, RecoveryCodes, RevokedOthers, Status, TwoFactorProofRequest, + TwoFactorSetupResponse, TwoFactorStatus, Uuid, +}; use crate::model::{Effect, Event}; +use crate::security::{SecurityAnswer, SecurityCall}; /// How often the clock ticks (and the status is asked for). pub const TICK: Duration = Duration::from_secs(2); @@ -33,6 +37,14 @@ pub trait Agent: Send + Sync { fn resume(&self) -> Result; fn quit(&self) -> Result; fn set_autostart(&self, on: bool) -> Result; + /// The Security page's calls: the account's two-factor sign-in, its password and its other computers. + fn two_factor_status(&self) -> Result; + fn two_factor_setup(&self, password: &str) -> Result; + fn two_factor_confirm(&self, totp_code: &str) -> Result; + fn two_factor_disable(&self, request: &TwoFactorProofRequest) -> Result<(), Error>; + fn two_factor_recovery_codes(&self, password: &str, totp_code: &str) -> Result; + fn change_password(&self, current: &str, new: &str) -> Result<(), Error>; + fn revoke_other_devices(&self) -> Result; } /// The agent through `craft_cloud_app::Client`. It finds the agent again whenever a call finds it gone (the agent @@ -113,6 +125,27 @@ impl Agent for ClientAgent { fn set_autostart(&self, on: bool) -> Result { self.with(|c| c.set_autostart(on)) } + fn two_factor_status(&self) -> Result { + self.with(Client::two_factor_status) + } + fn two_factor_setup(&self, password: &str) -> Result { + self.with(|c| c.two_factor_setup(password)) + } + fn two_factor_confirm(&self, totp_code: &str) -> Result { + self.with(|c| c.two_factor_confirm(totp_code)) + } + fn two_factor_disable(&self, request: &TwoFactorProofRequest) -> Result<(), Error> { + self.with(|c| c.two_factor_disable(request)) + } + fn two_factor_recovery_codes(&self, password: &str, totp_code: &str) -> Result { + self.with(|c| c.two_factor_recovery_codes(password, totp_code)) + } + fn change_password(&self, current: &str, new: &str) -> Result<(), Error> { + self.with(|c| c.change_password(current, new)) + } + fn revoke_other_devices(&self) -> Result { + self.with(Client::revoke_other_devices) + } } /// Starts the agent and waits for it ([`crate::start_agent::start_and_wait`]), for [`Effect::StartAgent`]. @@ -207,6 +240,15 @@ pub fn call(agent: &dyn Agent, start: &Starter, effect: Effect) -> Option let _ = agent.quit(); Event::AgentQuit } + Effect::Security(call) => Event::SecurityAnswer(match call { + SecurityCall::Status => SecurityAnswer::Status(agent.two_factor_status()), + SecurityCall::Setup { password } => SecurityAnswer::Setup(agent.two_factor_setup(&password)), + SecurityCall::Confirm { code } => SecurityAnswer::Confirm(agent.two_factor_confirm(&code)), + SecurityCall::Disable(request) => SecurityAnswer::Disable(agent.two_factor_disable(&request)), + SecurityCall::RecoveryCodes { password, code } => SecurityAnswer::RecoveryCodes(agent.two_factor_recovery_codes(&password, &code)), + SecurityCall::ChangePassword { current, new } => SecurityAnswer::ChangePassword(agent.change_password(¤t, &new)), + SecurityCall::SignOutOthers => SecurityAnswer::SignOutOthers(agent.revoke_other_devices()), + }), Effect::OpenFolder(_) | Effect::QuitApp | Effect::QuitTray | Effect::AppAutostart(_) | Effect::PickFolder(_) => return None, }) } @@ -218,17 +260,71 @@ pub(crate) mod fake { use super::*; use std::sync::atomic::{AtomicBool, Ordering}; + /// The password, code and recovery code the fake's account takes. + pub const PASSWORD: &str = "correct horse battery"; + pub const CODE: &str = "123456"; + pub const RECOVERY: &str = "ABCD-EFGH-JKMN-PQRS"; + + /// What the fake agent's server has of the account's two-factor sign-in. + #[derive(Debug, Clone, Default)] + pub struct FakeFactor { + pub enabled: bool, + pub pending: bool, + /// A server from before two-factor sign-in: its calls are `not_found`. + pub unsupported: bool, + /// Every check of a code is refused as locked (840 s to wait). + pub locked: bool, + pub other_computers: u32, + } + pub struct FakeAgent { pub status: Mutex, pub gone: AtomicBool, /// What `join` answers, in turn (then the status). pub joins: Mutex>>, pub asked: Mutex>, + pub factor: Mutex, + } + + fn refused(code: &str, message: &str) -> Error { + Error::Refused { code: code.into(), message: message.into() } } impl FakeAgent { pub fn new(status: Status) -> Self { - Self { status: Mutex::new(status), gone: AtomicBool::new(false), joins: Mutex::new(Vec::new()), asked: Mutex::new(Vec::new()) } + Self { + status: Mutex::new(status), + gone: AtomicBool::new(false), + joins: Mutex::new(Vec::new()), + asked: Mutex::new(Vec::new()), + factor: Mutex::new(FakeFactor::default()), + } + } + + /// A refusal or nothing: whether a call of the account's two-factor sign-in gets through at all. + fn factor_gate(&self, what: &str) -> Result<(), Error> { + self.answer(what, |_| ())?; + if self.factor.lock().unwrap().unsupported { + return Err(refused("not_found", "no such endpoint")); + } + Ok(()) + } + + /// A code is checked: locked, wrong or right. + fn check_code(&self, code: Option<&str>) -> Result<(), Error> { + if self.factor.lock().unwrap().locked { + return Err(Error::RefusedWith { + code: "two_factor_locked".into(), + message: "locked".into(), + two_factor: None, + retry_after_secs: Some(840), + }); + } + if code == Some(CODE) || code == Some(RECOVERY) { Ok(()) } else { Err(refused("two_factor_invalid", "the code is wrong")) } + } + + fn codes() -> RecoveryCodes { + RecoveryCodes { recovery_codes: (0..10).map(|n| format!("AAAA-BBBB-CCCC-{n:04}")).collect() } } fn answer(&self, what: &str, ok: impl FnOnce(&Status) -> T) -> Result { @@ -287,6 +383,78 @@ pub(crate) mod fake { fn set_autostart(&self, on: bool) -> Result { self.answer("set_autostart", |s| Status { autostart: Some(on), ..s.clone() }) } + fn two_factor_status(&self) -> Result { + self.factor_gate("two_factor_status")?; + let f = self.factor.lock().unwrap(); + Ok(TwoFactorStatus { + enabled: f.enabled, + enabled_ms: f.enabled.then_some(1_000), + recovery_codes_left: if f.enabled { 10 } else { 0 }, + pending: f.pending, + }) + } + fn two_factor_setup(&self, password: &str) -> Result { + self.factor_gate("two_factor_setup")?; + if password != PASSWORD { + return Err(refused("wrong_password", "wrong")); + } + let mut f = self.factor.lock().unwrap(); + if f.enabled { + return Err(refused("conflict", "on already")); + } + f.pending = true; + Ok(TwoFactorSetupResponse { + secret: "JBSWY3DPEHPK3PXP".into(), + otpauth_uri: "otpauth://totp/Craft%20Cloud:fabio?secret=JBSWY3DPEHPK3PXP&issuer=Craft%20Cloud".into(), + expires_ms: 1_790_000_600_000, + }) + } + fn two_factor_confirm(&self, totp_code: &str) -> Result { + self.factor_gate("two_factor_confirm")?; + if !self.factor.lock().unwrap().pending { + return Err(refused("conflict", "no set-up to confirm")); + } + if totp_code != CODE { + return Err(refused("two_factor_invalid", "the code is wrong")); + } + let mut f = self.factor.lock().unwrap(); + f.enabled = true; + f.pending = false; + Ok(Self::codes()) + } + fn two_factor_disable(&self, request: &TwoFactorProofRequest) -> Result<(), Error> { + self.factor_gate("two_factor_disable")?; + if request.password != PASSWORD { + return Err(refused("wrong_password", "wrong")); + } + self.check_code(request.totp_code.as_deref().or(request.recovery_code.as_deref()))?; + self.factor.lock().unwrap().enabled = false; + Ok(()) + } + fn two_factor_recovery_codes(&self, password: &str, totp_code: &str) -> Result { + self.factor_gate("two_factor_recovery_codes")?; + if password != PASSWORD { + return Err(refused("wrong_password", "wrong")); + } + self.check_code(Some(totp_code))?; + Ok(Self::codes()) + } + fn change_password(&self, current: &str, new: &str) -> Result<(), Error> { + self.answer("change_password", |_| ())?; + if current != PASSWORD { + return Err(refused("wrong_password", "wrong")); + } + if new.len() < 8 { + return Err(refused("weak_password", "passwords are 8 to 1024 bytes")); + } + Ok(()) + } + fn revoke_other_devices(&self) -> Result { + self.answer("revoke_other_devices", |_| ())?; + let mut f = self.factor.lock().unwrap(); + let revoked = std::mem::take(&mut f.other_computers); + Ok(RevokedOthers { revoked }) + } } } diff --git a/apps/craft-cloud-desktop/src/lib.rs b/apps/craft-cloud-desktop/src/lib.rs index 8f876736..1bcb27e3 100644 --- a/apps/craft-cloud-desktop/src/lib.rs +++ b/apps/craft-cloud-desktop/src/lib.rs @@ -3,6 +3,7 @@ //! (`craft-cloud-agent run`), which it reaches through `craft-cloud-app` and starts when none answers. //! //! - [`model`]: the state and every way between its screens, pure (unit-tested against a fake agent). +//! - [`security`]: the Security page and the code a join asks for (two-factor sign-in). //! - [`agent`]: the agent's calls behind a trait, run off the UI thread. //! - [`start_agent`]: starting the agent from this app's folder. //! - [`single_instance`]: one app per user; a second start brings the first window forward. @@ -14,6 +15,7 @@ pub mod agent; pub mod app; pub mod autostart; pub mod model; +pub mod security; pub mod single_instance; pub mod start_agent; pub mod tray; diff --git a/apps/craft-cloud-desktop/src/model.rs b/apps/craft-cloud-desktop/src/model.rs index 83c03578..61f04851 100644 --- a/apps/craft-cloud-desktop/src/model.rs +++ b/apps/craft-cloud-desktop/src/model.rs @@ -11,6 +11,8 @@ use craft_cloud_app::{DeviceId, DeviceInfo, Error, JoinRequest, PairingInfo, Pai use craft_cloud_proto::api::codes as api_codes; use craft_cloud_proto::join::JoinString; +use crate::security::{SecondFactorForm, SecurityAnswer, SecurityCall, SecurityEvent, SecurityPage}; + /// A code that is wrong, expired or used (the server never says which). pub const INVALID_CODE: &str = "This code doesn't work any more. Make a new one on a signed-in computer."; /// The agent can't be started. @@ -48,8 +50,12 @@ pub enum Screen { Joining { form: JoinForm, password: Option }, /// An invite to a new account: its password, twice. PasswordNeeded(PasswordForm), + /// The account has two-factor sign-in: the join asks for the code ([`SecondFactorForm`]). + SecondFactor(SecondFactorForm), /// Signed in: the account, the sync, the computers. SignedIn(Overview), + /// The Security page ([`SecurityPage`]): two-factor sign-in, the password, the other computers. Boxed: it is large. + Security(Box), /// Showing an add-a-computer code. AddingComputer(Adding), /// The server refused this computer: it was removed from the account. @@ -110,8 +116,15 @@ impl JoinForm { folder: Some(folder), device_name: (!name.is_empty()).then(|| name.to_owned()), password, + totp_code: None, + recovery_code: None, } } + + /// [`JoinForm::request`] with the second factor of an account that has two-factor sign-in. + fn request_with_factor(&self, password: Option, factor: (Option, Option)) -> JoinRequest { + JoinRequest { totp_code: factor.0, recovery_code: factor.1, ..self.request(password) } + } } /// The new account's password, typed twice. @@ -209,6 +222,16 @@ pub enum Event { /// [`Effect::AppAutostart`]'s outcome. AppAutostartSet(Result<(), String>), + /// The Security button of the signed-in view. + OpenSecurity, + /// A button of the Security page. + Security(SecurityEvent), + /// An answer to one of the page's calls. + SecurityAnswer(SecurityAnswer), + /// The code of a join that asked for one (the button, or Enter). + SubmitSecondFactor, + /// Back from the code field to the join form. + CancelSecondFactor, /// Join (the form's button, or Enter). SubmitJoin, /// The answer to "this folder has files in it: sync them too?". @@ -255,6 +278,8 @@ pub enum Effect { Connect, /// Start the agent hidden from this app's folder and wait up to 5 s for it ([`Event::AgentStarted`]). StartAgent, + /// A call of the Security page ([`Event::SecurityAnswer`]). + Security(SecurityCall), Status, Join(JoinRequest), SetAutostart(bool), @@ -346,6 +371,7 @@ impl Model { pub fn overview(&self) -> Option<&Overview> { match &self.screen { Screen::SignedIn(overview) => Some(overview), + Screen::Security(page) => Some(&page.overview), Screen::AddingComputer(adding) => Some(&adding.overview), _ => None, } @@ -354,6 +380,7 @@ impl Model { fn overview_mut(&mut self) -> Option<&mut Overview> { match &mut self.screen { Screen::SignedIn(overview) => Some(overview), + Screen::Security(page) => Some(&mut page.overview), Screen::AddingComputer(adding) => Some(&mut adding.overview), _ => None, } @@ -451,6 +478,28 @@ impl Model { Vec::new() } + Event::OpenSecurity => { + let Screen::SignedIn(overview) = &self.screen else { return Vec::new() }; + let (page, calls) = SecurityPage::open(overview.clone()); + self.screen = Screen::Security(Box::new(page)); + calls.into_iter().map(Effect::Security).collect() + } + Event::Security(event) => self.security(event), + Event::SecurityAnswer(answer) => { + let now_ms = self.now_ms; + let Screen::Security(page) = &mut self.screen else { return Vec::new() }; + page.answer(answer, now_ms).into_iter().map(Effect::Security).collect() + } + Event::SubmitSecondFactor => self.submit_second_factor(), + Event::CancelSecondFactor => { + if let Screen::SecondFactor(form) = &self.screen { + self.screen = match &form.password { + Some(pw) => Screen::PasswordNeeded(pw.clone()), + None => Screen::Join(form.form.clone()), + }; + } + Vec::new() + } Event::SubmitJoin => self.submit_join(), Event::SyncExistingFiles(yes) => { let Screen::Join(form) = &mut self.screen else { return Vec::new() }; @@ -602,6 +651,7 @@ impl Model { if status.signed_in { match &mut self.screen { Screen::SignedIn(overview) => overview.status = status, + Screen::Security(page) => page.overview.status = status, Screen::AddingComputer(adding) => adding.overview.status = status, // The join's own answer moves on from there. Screen::Joining { .. } => {} @@ -619,7 +669,7 @@ impl Model { self.screen = Screen::Error(problem); } } - (_, Screen::Join(_) | Screen::Joining { .. } | Screen::PasswordNeeded(_)) => {} + (_, Screen::Join(_) | Screen::Joining { .. } | Screen::PasswordNeeded(_) | Screen::SecondFactor(_)) => {} _ => self.screen = Screen::Join(JoinForm::new(&self.defaults)), } effects @@ -650,7 +700,7 @@ impl Model { self.version_problem(*v); return; } - Error::Refused { .. } => false, + Error::Refused { .. } | Error::RefusedWith { .. } => false, }; if !gone || matches!(self.screen, Screen::Starting | Screen::NotRunning(_)) || self.busy == Some(Busy::Quitting) { return; @@ -678,6 +728,34 @@ impl Model { vec![Effect::Join(request)] } + /// The code of a join that asked for one: the same join, with the code, goes to the agent again. + fn submit_second_factor(&mut self) -> Vec { + let Screen::SecondFactor(second) = &mut self.screen else { return Vec::new() }; + let factor = match second.factor() { + Ok(factor) => factor, + Err(why) => { + second.error = Some(why); + return Vec::new(); + } + }; + let request = second.form.request_with_factor(second.password.as_ref().map(|pw| pw.password.clone()), factor); + let (form, password) = (second.form.clone(), second.password.clone()); + self.screen = Screen::Joining { form, password }; + vec![Effect::Join(request)] + } + + /// A button of the Security page. + fn security(&mut self, event: SecurityEvent) -> Vec { + let Screen::Security(page) = &mut self.screen else { return Vec::new() }; + if event == SecurityEvent::Close { + // Whatever the page showed (a secret, recovery codes) goes with it. + let overview = page.overview.clone(); + self.screen = Screen::SignedIn(overview); + return vec![Effect::Devices]; + } + page.update(event).into_iter().map(Effect::Security).collect() + } + fn submit_password(&mut self) -> Vec { let Screen::PasswordNeeded(pw) = &mut self.screen else { return Vec::new() }; pw.error = if pw.password.is_empty() { @@ -709,7 +787,7 @@ impl Model { Err(e) => e, }; let (code, text) = match &e { - Error::Refused { code, message } => (code.as_str(), message.clone()), + Error::Refused { code, message } | Error::RefusedWith { code, message, .. } => (code.as_str(), message.clone()), Error::NotRunning(_) => { self.status = None; self.screen = Screen::NotRunning(self.not_running(STOPPED, &e.to_string())); @@ -724,6 +802,11 @@ impl Model { Screen::PasswordNeeded(pw) }; self.screen = match (code, password) { + // The account has two-factor sign-in: the same join goes again with its code (the invite was not used up). + (codes::TWO_FACTOR_REQUIRED, password) => Screen::SecondFactor(SecondFactorForm::new(form, password, None)), + (codes::TWO_FACTOR_INVALID | codes::TWO_FACTOR_LOCKED, password) => { + Screen::SecondFactor(SecondFactorForm::new(form, password, Some(crate::security::explain(&e)))) + } (codes::PASSWORD_REQUIRED, _) => { Screen::PasswordNeeded(PasswordForm { form, password: String::new(), again: String::new(), error: None }) } @@ -820,7 +903,7 @@ impl Model { /// An error's message, for people. fn message(e: &Error) -> String { match e { - Error::Refused { message, .. } => message.clone(), + Error::Refused { message, .. } | Error::RefusedWith { message, .. } => message.clone(), other => other.to_string(), } } @@ -1105,8 +1188,32 @@ pub(crate) mod tests { let Screen::SignedIn(o) = &m.screen else { panic!() }; assert_eq!(o.status.account.as_deref(), Some("fabio")); let devices = vec![ - DeviceInfo { id: DeviceId::generate(), name: "lptp1".into(), created_ms: 1, last_used_ms: 2, revoked: false, current: true }, - DeviceInfo { id: DeviceId::generate(), name: "BEAST".into(), created_ms: 1, last_used_ms: 3, revoked: false, current: false }, + DeviceInfo { + id: DeviceId::generate(), + name: "lptp1".into(), + created_ms: 1, + last_used_ms: 2, + revoked: false, + current: true, + client_kind: String::new(), + platform: String::new(), + app_version: String::new(), + revoked_ms: None, + revoke_reason: None, + }, + DeviceInfo { + id: DeviceId::generate(), + name: "BEAST".into(), + created_ms: 1, + last_used_ms: 3, + revoked: false, + current: false, + client_kind: String::new(), + platform: String::new(), + app_version: String::new(), + revoked_ms: None, + revoke_reason: None, + }, ]; m.update(Event::Devices(Ok(devices.clone()))); // This computer can't be removed from here; another one can, once at a time. diff --git a/apps/craft-cloud-desktop/src/security.rs b/apps/craft-cloud-desktop/src/security.rs new file mode 100644 index 00000000..044222b6 --- /dev/null +++ b/apps/craft-cloud-desktop/src/security.rs @@ -0,0 +1,532 @@ +//! The Security page and the second step of a join: the signed-in account's two-factor sign-in (status, setting it up with a +//! secret and a QR code, the recovery codes shown once, turning it off, new recovery codes), the password change and signing +//! the other computers out; and the code field a join asks for when the account has two-factor sign-in. +//! +//! Pure like the rest of the model: [`SecurityPage::update`] takes an event and returns the calls to make, answers come back +//! as [`SecurityAnswer`]s. **Nothing secret is kept longer than it is shown**: the password a form held is moved into its call +//! and cleared, the secret and the recovery codes live in the page's step only, and leaving the page (or a signed-out or +//! removed screen) drops them. Every type that holds one hides it in `Debug`. + +use craft_cloud_app::{Error, RecoveryCodes, RevokedOthers, TwoFactorProofRequest, TwoFactorSetupResponse, TwoFactorStatus, codes}; +use craft_cloud_proto::api::codes as api_codes; + +use crate::model::Overview; + +/// The server doesn't know two-factor sign-in (it is older than this app), or the agent doesn't pass it on. +pub const UNSUPPORTED: &str = "This server doesn't support two-factor sign-in yet"; + +/// What the page knows of the account's two-factor sign-in. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum TwoFactorInfo { + /// Asking the server. + Loading, + /// The server doesn't have it ([`UNSUPPORTED`]). + Unsupported, + Known(TwoFactorStatus), + /// The status couldn't be read (the message). + Failed(String), +} + +/// Why recovery codes are shown. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CodesWhy { + /// Two-factor sign-in was just turned on. + TurnedOn, + /// New codes were made; the old ones don't work. + New, +} + +/// What the page is doing. +#[derive(Clone, PartialEq, Eq)] +pub enum Step { + /// The status and the actions. + Home, + /// Set-up, first: the password. + SetupPassword { password: String }, + /// Set-up, second: the secret to put into the authenticator app, and the first code it shows. + SetupCode { secret: String, uri: String, expires_ms: i64, code: String }, + /// The recovery codes, shown once. + Codes { codes: Vec, why: CodesWhy }, + /// Turning it off: the password, and a code or a recovery code. + Disable { password: String, code: String, recovery: bool }, + /// New recovery codes: the password and a code. + Regenerate { password: String, code: String }, + /// A new password: the current one, the new one twice. + Password { current: String, new: String, again: String }, + /// Signing the other computers out: asked once more. + SignOutOthers, +} + +impl std::fmt::Debug for Step { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // Which step, never what was typed or shown in it. + f.write_str(match self { + Step::Home => "Home", + Step::SetupPassword { .. } => "SetupPassword()", + Step::SetupCode { .. } => "SetupCode()", + Step::Codes { .. } => "Codes()", + Step::Disable { .. } => "Disable()", + Step::Regenerate { .. } => "Regenerate()", + Step::Password { .. } => "Password()", + Step::SignOutOthers => "SignOutOthers", + }) + } +} + +/// The Security page. +#[derive(Debug, Clone, PartialEq)] +pub struct SecurityPage { + /// The signed-in view the page was opened from and returns to. + pub overview: Overview, + pub info: TwoFactorInfo, + pub step: Step, + /// What went wrong with the last try, for people. + pub error: Option, + /// What the last action did ("Password changed"). + pub note: Option, + /// A call is under way: nothing can be sent twice. + pub busy: bool, +} + +/// What a button of the page asks for. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SecurityEvent { + /// Back to the signed-in view; whatever the page showed is dropped. + Close, + /// Back to the page's start (the recovery codes shown are dropped). + Home, + StartSetup, + StartDisable, + StartRegenerate, + StartPassword, + StartSignOutOthers, + /// The current step's button: continue, turn on, turn off, make, change, sign out. + Submit, + /// Disable's switch between a code and a recovery code. + ToggleRecovery, + /// Ask the server for the status again. + Refresh, +} + +/// A call of the agent the page makes. +#[derive(Clone, PartialEq, Eq)] +pub enum SecurityCall { + Status, + Setup { password: String }, + Confirm { code: String }, + Disable(TwoFactorProofRequest), + RecoveryCodes { password: String, code: String }, + ChangePassword { current: String, new: String }, + SignOutOthers, +} + +impl std::fmt::Debug for SecurityCall { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + SecurityCall::Status => "Status", + SecurityCall::Setup { .. } => "Setup()", + SecurityCall::Confirm { .. } => "Confirm()", + SecurityCall::Disable(_) => "Disable()", + SecurityCall::RecoveryCodes { .. } => "RecoveryCodes()", + SecurityCall::ChangePassword { .. } => "ChangePassword()", + SecurityCall::SignOutOthers => "SignOutOthers", + }) + } +} + +/// An agent's answer to a [`SecurityCall`]. +#[derive(Clone, PartialEq)] +pub enum SecurityAnswer { + Status(Result), + Setup(Result), + Confirm(Result), + Disable(Result<(), Error>), + RecoveryCodes(Result), + ChangePassword(Result<(), Error>), + SignOutOthers(Result), +} + +impl std::fmt::Debug for SecurityAnswer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // An answer may hold a secret or recovery codes: only whether it worked. + let (name, ok) = match self { + SecurityAnswer::Status(r) => ("Status", r.is_ok()), + SecurityAnswer::Setup(r) => ("Setup", r.is_ok()), + SecurityAnswer::Confirm(r) => ("Confirm", r.is_ok()), + SecurityAnswer::Disable(r) => ("Disable", r.is_ok()), + SecurityAnswer::RecoveryCodes(r) => ("RecoveryCodes", r.is_ok()), + SecurityAnswer::ChangePassword(r) => ("ChangePassword", r.is_ok()), + SecurityAnswer::SignOutOthers(r) => ("SignOutOthers", r.is_ok()), + }; + write!(f, "{name}({})", if ok { "ok" } else { "refused" }) + } +} + +/// Six digits (spaces allowed), the shape of a code from the authenticator app. +pub fn is_code(text: &str) -> bool { + let digits: String = text.chars().filter(|c| !c.is_whitespace()).collect(); + digits.len() == 6 && digits.bytes().all(|b| b.is_ascii_digit()) +} + +fn digits(text: &str) -> String { + text.chars().filter(|c| !c.is_whitespace()).collect() +} + +/// A wait in words. +fn wait_in_words(secs: u64) -> String { + match secs { + 0..=90 => format!("{secs} seconds"), + 91..=5_400 => format!("{} minutes", secs.div_ceil(60)), + _ => format!("{} hours", secs.div_ceil(3_600)), + } +} + +/// What a refusal says, for people, and what to do about it. +pub fn explain(e: &Error) -> String { + let wait = e.retry_after_secs().map(wait_in_words); + match e.code() { + Some(codes::WRONG_PASSWORD) => "That password isn't right.".into(), + Some(codes::TWO_FACTOR_INVALID) => "That code isn't right, or it was used already. Wait for the app's next code and try again.".into(), + Some(codes::TWO_FACTOR_LOCKED) => match wait { + Some(wait) => format!("Too many wrong codes. Try again in {wait}."), + None => "Too many wrong codes. Try again later.".into(), + }, + Some(codes::TWO_FACTOR_UNAVAILABLE) => "This server can't set up two-factor sign-in yet: its admin has to give it a key first.".into(), + Some(codes::TWO_FACTOR_PENDING) => "Setting it up is open on another computer. Finish it there, or wait ten minutes.".into(), + Some(codes::OFFLINE) => "Can't reach the server. Try again when this computer is online.".into(), + Some(craft_cloud_app::codes::NEEDS_YOU) => "This computer can't use the account any more. Sign out, then join again.".into(), + Some("rate_limited") => match wait { + Some(wait) => format!("Too many tries. Try again in {wait}."), + None => "Too many tries. Try again in a few minutes.".into(), + }, + _ => match e { + Error::Refused { message, .. } | Error::RefusedWith { message, .. } => message.clone(), + other => other.to_string(), + }, + } +} + +impl SecurityPage { + /// The page, opened from `overview`; its first call asks the server for the status. + pub fn open(overview: Overview) -> (Self, Vec) { + (Self { overview, info: TwoFactorInfo::Loading, step: Step::Home, error: None, note: None, busy: false }, vec![SecurityCall::Status]) + } + + /// Whether two-factor sign-in is known to be on. + pub fn is_on(&self) -> bool { + matches!(&self.info, TwoFactorInfo::Known(s) if s.enabled) + } + + fn start(&mut self, step: Step) { + self.step = step; + self.error = None; + self.note = None; + } + + /// A button of the page. Returns the calls to make; the page is `busy` while one is under way. + pub fn update(&mut self, event: SecurityEvent) -> Vec { + match event { + SecurityEvent::Close => Vec::new(), + SecurityEvent::Home => { + if !self.busy { + self.start(Step::Home); + } + Vec::new() + } + SecurityEvent::Refresh => { + self.info = TwoFactorInfo::Loading; + vec![SecurityCall::Status] + } + SecurityEvent::StartSetup => { + if !self.busy && !self.is_on() && matches!(self.info, TwoFactorInfo::Known(_)) { + self.start(Step::SetupPassword { password: String::new() }); + } + Vec::new() + } + SecurityEvent::StartDisable => { + if !self.busy && self.is_on() { + self.start(Step::Disable { password: String::new(), code: String::new(), recovery: false }); + } + Vec::new() + } + SecurityEvent::StartRegenerate => { + if !self.busy && self.is_on() { + self.start(Step::Regenerate { password: String::new(), code: String::new() }); + } + Vec::new() + } + SecurityEvent::StartPassword => { + if !self.busy { + self.start(Step::Password { current: String::new(), new: String::new(), again: String::new() }); + } + Vec::new() + } + SecurityEvent::StartSignOutOthers => { + if !self.busy { + self.start(Step::SignOutOthers); + } + Vec::new() + } + SecurityEvent::ToggleRecovery => { + if let Step::Disable { code, recovery, .. } = &mut self.step { + *recovery = !*recovery; + code.clear(); + self.error = None; + } + Vec::new() + } + SecurityEvent::Submit => self.submit(), + } + } + + /// The current step's call. A step that isn't filled in says what is missing and calls nothing; the typed password moves into + /// the call and is cleared from the form. + fn submit(&mut self) -> Vec { + if self.busy { + return Vec::new(); + } + self.error = None; + let call = match &mut self.step { + Step::SetupPassword { password } => { + if password.is_empty() { + self.error = Some("Type your password to start.".into()); + return Vec::new(); + } + SecurityCall::Setup { password: std::mem::take(password) } + } + Step::SetupCode { code, .. } => { + if !is_code(code) { + self.error = Some("Type the six digits the app shows.".into()); + return Vec::new(); + } + SecurityCall::Confirm { code: digits(&std::mem::take(code)) } + } + Step::Disable { password, code, recovery } => { + if password.is_empty() || code.trim().is_empty() { + self.error = Some("Type your password and a code (or a recovery code).".into()); + return Vec::new(); + } + if !*recovery && !is_code(code) { + self.error = Some("A code from the app is six digits.".into()); + return Vec::new(); + } + let typed = std::mem::take(code); + let password = std::mem::take(password); + if *recovery { + SecurityCall::Disable(TwoFactorProofRequest { password, totp_code: None, recovery_code: Some(typed.trim().to_owned()) }) + } else { + SecurityCall::Disable(TwoFactorProofRequest { password, totp_code: Some(digits(&typed)), recovery_code: None }) + } + } + Step::Regenerate { password, code } => { + if password.is_empty() || !is_code(code) { + self.error = Some("Type your password and the six digits the app shows.".into()); + return Vec::new(); + } + SecurityCall::RecoveryCodes { password: std::mem::take(password), code: digits(&std::mem::take(code)) } + } + Step::Password { current, new, again } => { + if current.is_empty() || new.is_empty() { + self.error = Some("Type your current password and the new one.".into()); + return Vec::new(); + } + if new != again { + self.error = Some("The two new passwords aren't the same.".into()); + return Vec::new(); + } + SecurityCall::ChangePassword { current: std::mem::take(current), new: std::mem::take(new) } + } + Step::SignOutOthers => SecurityCall::SignOutOthers, + Step::Home | Step::Codes { .. } => return Vec::new(), + }; + // A form that goes on with a password or a code is emptied for good: the typed ones live in the call now. + if let Step::Password { again, .. } = &mut self.step { + again.clear(); + } + self.busy = true; + vec![call] + } + + /// An answer. May ask for more (the status, after a change). + pub fn answer(&mut self, answer: SecurityAnswer, now_ms: i64) -> Vec { + match answer { + SecurityAnswer::Status(result) => { + self.info = match result { + Ok(status) => TwoFactorInfo::Known(status), + Err(e) if e.code() == Some(api_codes::NOT_FOUND) || e.code() == Some(codes::AGENT_TOO_OLD) => TwoFactorInfo::Unsupported, + Err(e) => TwoFactorInfo::Failed(explain(&e)), + }; + Vec::new() + } + SecurityAnswer::Setup(result) => { + self.busy = false; + match result { + Ok(setup) => { + self.step = + Step::SetupCode { secret: setup.secret, uri: setup.otpauth_uri, expires_ms: setup.expires_ms, code: String::new() }; + } + Err(e) => self.error = Some(explain(&e)), + } + Vec::new() + } + SecurityAnswer::Confirm(result) => { + self.busy = false; + match result { + Ok(codes) => { + self.step = Step::Codes { codes: codes.recovery_codes, why: CodesWhy::TurnedOn }; + self.note = Some("Two-factor sign-in is on. Your other computers were signed out.".into()); + return vec![SecurityCall::Status]; + } + Err(e) => { + // A set-up that ran out or was used up (five wrong codes) has to start again. + if e.code() == Some(codes::CONFLICT) { + self.step = Step::Home; + } + self.error = Some(explain(&e)); + // Past its ten minutes: the secret is of no use. + if matches!(&self.step, Step::SetupCode { expires_ms, .. } if now_ms >= *expires_ms) { + self.step = Step::Home; + } + } + } + Vec::new() + } + SecurityAnswer::Disable(result) => { + self.busy = false; + match result { + Ok(()) => { + self.step = Step::Home; + self.note = Some("Two-factor sign-in is off. Your other computers were signed out.".into()); + return vec![SecurityCall::Status]; + } + Err(e) => self.error = Some(explain(&e)), + } + Vec::new() + } + SecurityAnswer::RecoveryCodes(result) => { + self.busy = false; + match result { + Ok(codes) => { + self.step = Step::Codes { codes: codes.recovery_codes, why: CodesWhy::New }; + self.note = Some("New recovery codes. The old ones don't work any more.".into()); + return vec![SecurityCall::Status]; + } + Err(e) => self.error = Some(explain(&e)), + } + Vec::new() + } + SecurityAnswer::ChangePassword(result) => { + self.busy = false; + match result { + Ok(()) => { + self.step = Step::Home; + self.note = Some("Password changed. Your other computers were signed out.".into()); + } + Err(e) => self.error = Some(explain(&e)), + } + Vec::new() + } + SecurityAnswer::SignOutOthers(result) => { + self.busy = false; + match result { + Ok(done) => { + self.step = Step::Home; + self.note = Some(match done.revoked { + 0 => "No other computer was signed in.".to_owned(), + 1 => "Signed 1 other computer out.".to_owned(), + n => format!("Signed {n} other computers out."), + }); + } + Err(e) => self.error = Some(explain(&e)), + } + Vec::new() + } + } + } +} + +/// The second step of a join: the account has two-factor sign-in, so the code comes with the same join. +#[derive(Clone, PartialEq, Eq)] +pub struct SecondFactorForm { + pub form: crate::model::JoinForm, + /// The new account's password, when the join came by an invite that needed one (an account that has two-factor sign-in has + /// one already, so this is only ever `None` for it). + pub password: Option, + /// What was typed: a code from the app, or a recovery code. + pub code: String, + /// The field takes a recovery code instead. + pub recovery: bool, + /// What went wrong with the last try, for people. + pub error: Option, +} + +impl std::fmt::Debug for SecondFactorForm { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SecondFactorForm") + .field("form", &self.form) + .field("recovery", &self.recovery) + .field("error", &self.error) + .finish_non_exhaustive() + } +} + +impl SecondFactorForm { + pub fn new(form: crate::model::JoinForm, password: Option, error: Option) -> Self { + Self { form, password, code: String::new(), recovery: false, error } + } + + /// The code as the join takes it: `(totp_code, recovery_code)`, or what is wrong with what was typed. + pub fn factor(&self) -> Result<(Option, Option), String> { + if self.recovery { + let typed = self.code.trim(); + if typed.is_empty() { + return Err("Type a recovery code.".into()); + } + return Ok((None, Some(typed.to_owned()))); + } + if !is_code(&self.code) { + return Err("Type the six digits the app shows.".into()); + } + Ok((Some(digits(&self.code)), None)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_code_is_six_digits_with_spaces_allowed() { + assert!(is_code("123456") && is_code(" 123 456 ")); + for bad in ["12345", "1234567", "12345a", "", "١٢٣٤٥٦"] { + assert!(!is_code(bad), "{bad:?}"); + } + } + + #[test] + fn refusals_are_explained_for_people() { + let refused = |code: &str| Error::Refused { code: code.into(), message: "m".into() }; + assert!(explain(&refused("wrong_password")).contains("password isn't right")); + assert!(explain(&refused("two_factor_invalid")).contains("next code")); + let locked = Error::RefusedWith { code: "two_factor_locked".into(), message: "m".into(), two_factor: None, retry_after_secs: Some(840) }; + assert_eq!(explain(&locked), "Too many wrong codes. Try again in 14 minutes."); + assert!(explain(&refused("two_factor_unavailable")).contains("admin has to give it a key")); + assert_eq!(explain(&refused("weak_password")), "m", "an unknown code says what the server said"); + } + + #[test] + fn secrets_stay_out_of_debug_output() { + let step = Step::SetupCode { secret: "JBSWY3DPEHPK3PXP".into(), uri: "otpauth://totp/x".into(), expires_ms: 1, code: "918273".into() }; + let codes = Step::Codes { codes: vec!["ABCD-EFGH-JKMN-PQRS".into()], why: CodesWhy::TurnedOn }; + let password = Step::Disable { password: "pw-secret-1".into(), code: "918273".into(), recovery: false }; + for shown in [format!("{step:?}"), format!("{codes:?}"), format!("{password:?}")] { + assert!(!shown.contains("JBSWY") && !shown.contains("ABCD") && !shown.contains("pw-secret") && !shown.contains("918273"), "{shown}"); + } + let call = SecurityCall::Setup { password: "pw-secret-1".into() }; + assert!(!format!("{call:?}").contains("pw-secret")); + let answer = SecurityAnswer::Confirm(Ok(RecoveryCodes { recovery_codes: vec!["ABCD-EFGH-JKMN-PQRS".into()] })); + assert!(!format!("{answer:?}").contains("ABCD")); + let form = SecondFactorForm { form: Default::default(), password: None, code: "918273".into(), recovery: false, error: None }; + assert!(!format!("{form:?}").contains("918273")); + } +} diff --git a/apps/craft-cloud-desktop/src/ui/add_computer.rs b/apps/craft-cloud-desktop/src/ui/add_computer.rs index 5e4a3c23..230fb672 100644 --- a/apps/craft-cloud-desktop/src/ui/add_computer.rs +++ b/apps/craft-cloud-desktop/src/ui/add_computer.rs @@ -65,7 +65,7 @@ pub(super) fn show(ui: &mut egui::Ui, adding: &Adding, now_ms: i64, events: &mut /// `text` as a QR code `side` points wide: dark modules on white with the standard quiet zone, whatever the theme /// (cameras read it that way round). -fn qr(ui: &mut egui::Ui, text: &str, side: f32) { +pub(super) fn qr(ui: &mut egui::Ui, text: &str, side: f32) { let Ok(code) = QrCode::encode_text(text, QrCodeEcc::Medium) else { return; }; diff --git a/apps/craft-cloud-desktop/src/ui/mod.rs b/apps/craft-cloud-desktop/src/ui/mod.rs index 6bd7b2cb..8b42af3c 100644 --- a/apps/craft-cloud-desktop/src/ui/mod.rs +++ b/apps/craft-cloud-desktop/src/ui/mod.rs @@ -7,6 +7,8 @@ mod computers; mod join; mod overview; mod problem; +mod second_factor; +mod security; use egui::{Color32, RichText}; @@ -20,6 +22,7 @@ pub const TITLE: &str = "Craft Cloud"; /// Draws the window for `model` at `now_ms`, and returns what its buttons asked for. pub fn show(ui: &mut egui::Ui, model: &mut Model, now_ms: i64) -> Vec { let mut events = Vec::new(); + security::forget_secrets(ui.ctx(), &model.screen); egui::CentralPanel::default().show(ui, |ui| { egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| { ui.add_space(8.0); @@ -31,6 +34,8 @@ pub fn show(ui: &mut egui::Ui, model: &mut Model, now_ms: i64) -> Vec { Screen::Join(form) => join::form(ui, form, false, &mut events), Screen::Joining { form, .. } => join::form(ui, form, true, &mut events), Screen::PasswordNeeded(pw) => join::password(ui, pw, &mut events), + Screen::SecondFactor(form) => second_factor::show(ui, form, &mut events), + Screen::Security(page) => security::show(ui, page, &mut events), Screen::SignedIn(o) => overview::show(ui, o, busy, now_ms, autostart_problem.as_deref(), &mut events), Screen::AddingComputer(adding) => add_computer::show(ui, adding, now_ms, &mut events), Screen::Removed => problem::removed(ui, busy, &mut events), @@ -99,3 +104,6 @@ pub fn ago(now_ms: i64, then_ms: i64) -> String { #[cfg(test)] mod tests; + +#[cfg(test)] +mod tests_security; diff --git a/apps/craft-cloud-desktop/src/ui/overview.rs b/apps/craft-cloud-desktop/src/ui/overview.rs index 92aa3da3..8d6038c6 100644 --- a/apps/craft-cloud-desktop/src/ui/overview.rs +++ b/apps/craft-cloud-desktop/src/ui/overview.rs @@ -69,6 +69,10 @@ pub(super) fn show(ui: &mut egui::Ui, o: &Overview, busy: Option, now_ms: if super::wide_button(ui, true, "Add a computer") { events.push(Event::AddComputer); } + ui.add_space(4.0); + if ui.button("Security…").on_hover_text("Two-factor sign-in, your password, your other computers").clicked() { + events.push(Event::OpenSecurity); + } ui.add_space(14.0); super::computers::list(ui, o, now_ms, events); diff --git a/apps/craft-cloud-desktop/src/ui/second_factor.rs b/apps/craft-cloud-desktop/src/ui/second_factor.rs new file mode 100644 index 00000000..b77d9ccc --- /dev/null +++ b/apps/craft-cloud-desktop/src/ui/second_factor.rs @@ -0,0 +1,38 @@ +//! The code a join asks for when the account has two-factor sign-in: the six digits from the authenticator app, or a recovery +//! code instead. + +use egui::RichText; + +use crate::model::Event; +use crate::security::SecondFactorForm; + +pub(super) fn show(ui: &mut egui::Ui, form: &mut SecondFactorForm, events: &mut Vec) { + super::heading(ui, "Enter your code"); + ui.label("This account has two-factor sign-in."); + ui.add_space(8.0); + ui.label(if form.recovery { "Recovery code" } else { "Code from the authenticator app" }); + let hint = if form.recovery { "ABCD-EFGH-JKMN-PQRS" } else { "123456" }; + let response = ui.add( + egui::TextEdit::singleline(&mut form.code).id(super::security::secret_id("second-factor-code")).hint_text(hint).desired_width(f32::INFINITY), + ); + let enter = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)); + ui.add_space(4.0); + let toggle = if form.recovery { "Use a code from the app" } else { "Use a recovery code" }; + if ui.checkbox(&mut form.recovery, toggle).changed() { + form.code.clear(); + form.error = None; + } + ui.label(RichText::new("A recovery code works once. Your invite or code isn't used up until this works.").small().weak()); + ui.add_space(10.0); + if let Some(error) = &form.error { + super::error_line(ui, error); + ui.add_space(8.0); + } + if super::wide_button(ui, true, "Join") || enter { + events.push(Event::SubmitSecondFactor); + } + ui.add_space(4.0); + if ui.button("Back").clicked() { + events.push(Event::CancelSecondFactor); + } +} diff --git a/apps/craft-cloud-desktop/src/ui/security.rs b/apps/craft-cloud-desktop/src/ui/security.rs new file mode 100644 index 00000000..a4f1443c --- /dev/null +++ b/apps/craft-cloud-desktop/src/ui/security.rs @@ -0,0 +1,260 @@ +//! Security: two-factor sign-in (turn it on with a secret and a QR code, the recovery codes shown once, turn it off, new +//! recovery codes), the password, and signing the other computers out. + +use egui::RichText; + +use crate::model::Event; +use crate::security::{CodesWhy, SecurityEvent, SecurityPage, Step, TwoFactorInfo}; + +fn send(events: &mut Vec, event: SecurityEvent) { + events.push(Event::Security(event)); +} + +/// The id of a secret field of the page (a password or a code), by name. +pub(super) fn secret_id(name: &str) -> egui::Id { + egui::Id::new(("security-secret", name)) +} + +/// Every secret field of the page and the second step of a join: its name, and the text the model holds for it in `screen` +/// (empty when the field isn't there). +fn secrets(screen: &crate::model::Screen) -> Vec<(&'static str, &str)> { + use crate::model::Screen; + match screen { + Screen::Security(page) => match &page.step { + Step::SetupPassword { password } => vec![("setup-password", password)], + Step::SetupCode { code, .. } => vec![("setup-code", code)], + Step::Disable { password, code, .. } => vec![("disable-password", password), ("disable-code", code)], + Step::Regenerate { password, code } => vec![("regenerate-password", password), ("regenerate-code", code)], + Step::Password { current, new, again } => vec![("password-current", current), ("password-new", new), ("password-again", again)], + Step::Home | Step::Codes { .. } | Step::SignOutOthers => Vec::new(), + }, + Screen::SecondFactor(form) => vec![("second-factor-code", &form.code)], + _ => Vec::new(), + } +} + +const SECRET_NAMES: [&str; 10] = [ + "setup-password", + "setup-code", + "disable-password", + "disable-code", + "regenerate-password", + "regenerate-code", + "password-current", + "password-new", + "password-again", + "second-factor-code", +]; + +/// Drops what egui remembers of every secret field (its cursor and, above all, its undo history, which holds the text as it +/// was typed) unless the model holds something typed in that field right now. So once a password or code was sent, a step was +/// cancelled or the page left, Undo can't bring it back; called every frame, before the fields are drawn. egui keeps this state +/// in memory only (this app doesn't enable eframe's persistence, and the undo history is not serialized either). +pub(super) fn forget_secrets(ctx: &egui::Context, screen: &crate::model::Screen) { + let typed = secrets(screen); + for name in SECRET_NAMES { + if !typed.iter().any(|(n, text)| *n == name && !text.is_empty()) { + ctx.data_mut(|d| d.remove::(secret_id(name))); + } + } +} + +/// A password field. +fn password_field(ui: &mut egui::Ui, name: &str, label: &str, text: &mut String) -> egui::Response { + ui.label(label); + let response = ui.add(egui::TextEdit::singleline(text).id(secret_id(name)).password(true).desired_width(f32::INFINITY)); + ui.add_space(6.0); + response +} + +pub(super) fn show(ui: &mut egui::Ui, page: &mut SecurityPage, events: &mut Vec) { + super::heading(ui, "Security"); + let (busy, error, note) = (page.busy, page.error.clone(), page.note.clone()); + let info = page.info.clone(); + match &mut page.step { + Step::Home => home(ui, &info, busy, note.as_deref(), error.as_deref(), events), + Step::SetupPassword { password } => { + ui.label( + "Two-factor sign-in asks for a code from an app on your phone (Aegis, 1Password, Google Authenticator…) as well as your password.", + ); + ui.add_space(8.0); + let response = password_field(ui, "setup-password", "Your password", password); + let enter = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)); + footer(ui, busy, error.as_deref(), "Continue", enter, events); + } + Step::SetupCode { secret, uri, expires_ms: _, code } => { + ui.label("1. Scan this with your authenticator app, or type the secret into it."); + ui.add_space(6.0); + super::add_computer::qr(ui, uri, 180.0); + ui.add_space(6.0); + ui.horizontal(|ui| { + ui.add(egui::Label::new(RichText::new(secret.as_str()).monospace().size(16.0).strong()).selectable(true)); + if ui.small_button("Copy").clicked() { + ui.ctx().copy_text(secret.clone()); + } + }); + ui.collapsing("The setup link", |ui| { + ui.add(egui::Label::new(RichText::new(uri.as_str()).monospace().small()).selectable(true).wrap()); + if ui.small_button("Copy link").clicked() { + ui.ctx().copy_text(uri.clone()); + } + }); + ui.add_space(10.0); + ui.label("2. Type the six digits the app shows now."); + let response = ui.add(egui::TextEdit::singleline(code).id(secret_id("setup-code")).hint_text("123456").desired_width(160.0)); + let enter = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)); + ui.add_space(6.0); + footer(ui, busy, error.as_deref(), "Turn on", enter, events); + } + Step::Codes { codes, why } => { + let (line, button) = match why { + CodesWhy::TurnedOn => ("Two-factor sign-in is on.", "I have saved them"), + CodesWhy::New => ("These are your new recovery codes.", "I have saved them"), + }; + ui.label(RichText::new(line).strong().color(super::good(ui))); + ui.add_space(6.0); + ui.label("Save these recovery codes somewhere safe now. Each works once, if you lose your phone, and they are shown only this once."); + ui.add_space(8.0); + egui::Grid::new("recovery-codes").num_columns(2).spacing([16.0, 4.0]).show(ui, |ui| { + for (n, code) in codes.iter().enumerate() { + ui.add(egui::Label::new(RichText::new(code.as_str()).monospace().size(15.0)).selectable(true)); + if n % 2 == 1 { + ui.end_row(); + } + } + }); + ui.add_space(8.0); + if ui.button("Copy all").clicked() { + ui.ctx().copy_text(codes.join("\n")); + } + ui.add_space(10.0); + if super::wide_button(ui, true, button) { + send(events, SecurityEvent::Home); + } + } + Step::Disable { password, code, recovery } => { + ui.label("Turning it off signs your other computers out. Type your password and a code from the app."); + ui.add_space(8.0); + password_field(ui, "disable-password", "Your password", password); + ui.label(if *recovery { "A recovery code" } else { "The six digits from the app" }); + let response = ui.add(egui::TextEdit::singleline(code).id(secret_id("disable-code")).desired_width(f32::INFINITY)); + let enter = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)); + if ui.small_button(if *recovery { "Use a code from the app" } else { "Use a recovery code instead" }).clicked() { + send(events, SecurityEvent::ToggleRecovery); + } + ui.add_space(6.0); + footer(ui, busy, error.as_deref(), "Turn off", enter, events); + } + Step::Regenerate { password, code } => { + ui.label("New recovery codes replace the old ones at once. Type your password and a code from the app."); + ui.add_space(8.0); + password_field(ui, "regenerate-password", "Your password", password); + ui.label("The six digits from the app"); + let response = ui.add(egui::TextEdit::singleline(code).id(secret_id("regenerate-code")).desired_width(160.0)); + let enter = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)); + ui.add_space(6.0); + footer(ui, busy, error.as_deref(), "Make new codes", enter, events); + } + Step::Password { current, new, again } => { + ui.label("Changing your password signs your other computers out."); + ui.add_space(8.0); + password_field(ui, "password-current", "Current password", current); + password_field(ui, "password-new", "New password", new); + let response = password_field(ui, "password-again", "The new password again", again); + let enter = response.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)); + footer(ui, busy, error.as_deref(), "Change password", enter, events); + } + Step::SignOutOthers => { + ui.label("Every other computer of your account is signed out. Their files stay there; they join again to sync."); + ui.add_space(8.0); + footer(ui, busy, error.as_deref(), "Sign them out", false, events); + } + } +} + +/// The step's error, its main button and the way back. +fn footer(ui: &mut egui::Ui, busy: bool, error: Option<&str>, button: &str, enter: bool, events: &mut Vec) { + if let Some(error) = error { + super::error_line(ui, error); + ui.add_space(6.0); + } + if busy { + ui.horizontal(|ui| { + ui.spinner(); + ui.label("Working…"); + }); + } else if super::wide_button(ui, true, button) || enter { + send(events, SecurityEvent::Submit); + } + ui.add_space(4.0); + if ui.add_enabled(!busy, egui::Button::new("Cancel")).clicked() { + send(events, SecurityEvent::Home); + } +} + +fn home(ui: &mut egui::Ui, info: &TwoFactorInfo, busy: bool, note: Option<&str>, error: Option<&str>, events: &mut Vec) { + if let Some(note) = note { + ui.label(RichText::new(note).color(super::good(ui))); + ui.add_space(8.0); + } + if let Some(error) = error { + super::error_line(ui, error); + ui.add_space(8.0); + } + ui.label(RichText::new("Two-factor sign-in").strong()); + match info { + TwoFactorInfo::Loading => { + ui.horizontal(|ui| { + ui.spinner(); + ui.label("Asking the server…"); + }); + } + TwoFactorInfo::Unsupported => { + ui.label(crate::security::UNSUPPORTED); + ui.label(RichText::new("Ask whoever runs your Craft Cloud server to update it.").small().weak()); + } + TwoFactorInfo::Failed(why) => { + super::error_line(ui, why); + if ui.button("Try again").clicked() { + send(events, SecurityEvent::Refresh); + } + } + TwoFactorInfo::Known(status) if status.enabled => { + ui.label(format!("On. {} recovery codes left.", status.recovery_codes_left)); + ui.add_space(4.0); + ui.horizontal(|ui| { + if ui.add_enabled(!busy, egui::Button::new("Turn off…")).clicked() { + send(events, SecurityEvent::StartDisable); + } + if ui.add_enabled(!busy, egui::Button::new("New recovery codes…")).clicked() { + send(events, SecurityEvent::StartRegenerate); + } + }); + } + TwoFactorInfo::Known(status) => { + ui.label("Off. Anyone who knows your password can sign in."); + if status.pending { + ui.label(RichText::new("A set-up was started and isn't finished.").small().weak()); + } + ui.add_space(4.0); + if ui.add_enabled(!busy, egui::Button::new("Set up two-factor sign-in…")).clicked() { + send(events, SecurityEvent::StartSetup); + } + } + } + ui.add_space(14.0); + ui.separator(); + ui.add_space(6.0); + ui.horizontal(|ui| { + if ui.button("Change password…").clicked() { + send(events, SecurityEvent::StartPassword); + } + if ui.button("Sign out other computers…").clicked() { + send(events, SecurityEvent::StartSignOutOthers); + } + }); + ui.add_space(14.0); + if super::wide_button(ui, true, "Back") { + send(events, SecurityEvent::Close); + } +} diff --git a/apps/craft-cloud-desktop/src/ui/tests.rs b/apps/craft-cloud-desktop/src/ui/tests.rs index e059f6be..0fb1bc60 100644 --- a/apps/craft-cloud-desktop/src/ui/tests.rs +++ b/apps/craft-cloud-desktop/src/ui/tests.rs @@ -77,7 +77,19 @@ fn after(agent: &FakeAgent, events: Vec) -> Model { fn devices() -> Vec { vec![ - DeviceInfo { id: DeviceId::generate(), name: "lptp1".into(), created_ms: 0, last_used_ms: NOW - 5_000, revoked: false, current: true }, + DeviceInfo { + id: DeviceId::generate(), + name: "lptp1".into(), + created_ms: 0, + last_used_ms: NOW - 5_000, + revoked: false, + current: true, + client_kind: String::new(), + platform: String::new(), + app_version: String::new(), + revoked_ms: None, + revoke_reason: None, + }, DeviceInfo { id: DeviceId::generate(), name: "BEAST".into(), @@ -85,6 +97,11 @@ fn devices() -> Vec { last_used_ms: NOW - 3 * 3_600_000, revoked: false, current: false, + client_kind: String::new(), + platform: String::new(), + app_version: String::new(), + revoked_ms: None, + revoke_reason: None, }, ] } diff --git a/apps/craft-cloud-desktop/src/ui/tests_security.rs b/apps/craft-cloud-desktop/src/ui/tests_security.rs new file mode 100644 index 00000000..96550797 --- /dev/null +++ b/apps/craft-cloud-desktop/src/ui/tests_security.rs @@ -0,0 +1,629 @@ +//! The Security page and the join's code field: every step against the fake agent (the model, effect by effect), the screens as +//! kittest draws them (snapshots `second-factor*` and `security-*` in `tests/snapshots/`, written with `UPDATE_SNAPSHOTS=1`), +//! and that nothing secret outlives the screen that showed it. + +use std::path::PathBuf; + +use craft_cloud_app::{Error, JoinRequest, RunState, Status}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; + +use super::*; +use crate::agent::fake::{CODE, FakeAgent, PASSWORD, RECOVERY}; +use crate::agent::{Agent, Starter, call}; +use crate::model::{Effect, JoinDefaults, Model, Screen}; +use crate::security::{SecurityEvent, Step, TwoFactorInfo}; + +const NOW: i64 = 1_790_000_000_000; +const TOLERANCE: u8 = 48; +const MAX_DIFFERENT: usize = 4; + +fn defaults() -> JoinDefaults { + JoinDefaults { folder: "/home/fabio/Craft Cloud".into(), device_name: "lptp1".into() } +} + +fn signed_in() -> Status { + Status { + api: 1, + signed_in: true, + account: Some("fabio".into()), + server: Some("https://cloud.shadows.ch".into()), + folder: Some("/home/fabio/Craft Cloud".into()), + machine: Some("lptp1".into()), + online: true, + run_state: RunState::Idle, + autostart: Some(true), + last_sync_ms: Some(NOW - 120_000), + removed: false, + } +} + +fn signed_out() -> Status { + Status { + signed_in: false, + account: None, + server: None, + folder: None, + machine: None, + online: false, + run_state: RunState::SignedOut, + ..signed_in() + } +} + +fn no_start() -> Starter { + std::sync::Arc::new(|_: &dyn Agent| Err("no agent in tests".to_owned())) +} + +/// Feeds `event` to the model and answers every effect with the fake, as the link does, until nothing is left. +fn feed(model: &mut Model, agent: &FakeAgent, event: Event) { + let start = no_start(); + let mut pending = model.update(event); + while let Some(effect) = pending.pop() { + if let Some(answer) = call(agent, &start, effect) { + pending.extend(model.update(answer)); + } + } +} + +fn model_after(agent: &FakeAgent, events: Vec) -> Model { + let mut model = Model::new(defaults(), true, "run.log".into()); + for event in std::iter::once(Event::Start).chain(events) { + feed(&mut model, agent, event); + } + model +} + +fn on_security(agent: &FakeAgent) -> Model { + model_after(agent, vec![Event::OpenSecurity]) +} + +/// A folder that is a full path on every platform (the join form refuses anything else). +fn absolute_folder() -> String { + std::env::temp_dir().to_string_lossy().into_owned() +} + +fn page(model: &mut Model) -> &mut crate::security::SecurityPage { + match &mut model.screen { + Screen::Security(page) => page, + other => panic!("not the Security page: {other:?}"), + } +} + +fn type_into(model: &mut Model, fill: impl FnOnce(&mut Step)) { + fill(&mut page(model).step); +} + +// ---- the model ---- + +#[test] +fn the_page_asks_the_server_and_shows_what_it_says() { + let agent = FakeAgent::new(signed_in()); + let mut model = on_security(&agent); + assert_eq!( + page(&mut model).info, + TwoFactorInfo::Known(craft_cloud_app::TwoFactorStatus { enabled: false, enabled_ms: None, recovery_codes_left: 0, pending: false }) + ); + assert!(agent.asked.lock().unwrap().iter().any(|a| a == "two_factor_status")); + // A server from before two-factor sign-in: said so, the page doesn't break. + let old = FakeAgent::new(signed_in()); + old.factor.lock().unwrap().unsupported = true; + let mut model = on_security(&old); + assert_eq!(page(&mut model).info, TwoFactorInfo::Unsupported); + feed(&mut model, &old, Event::Security(SecurityEvent::StartSetup)); + assert_eq!(page(&mut model).step, Step::Home, "nothing to set up"); + // Back to the signed-in view. + feed(&mut model, &old, Event::Security(SecurityEvent::Close)); + assert!(matches!(model.screen, Screen::SignedIn(_))); +} + +#[test] +fn setting_it_up_turns_it_on_and_shows_the_recovery_codes_once() { + let agent = FakeAgent::new(signed_in()); + let mut model = on_security(&agent); + feed(&mut model, &agent, Event::Security(SecurityEvent::StartSetup)); + // Nothing typed: nothing is asked of the server. + let asked = agent.asked.lock().unwrap().len(); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert!(page(&mut model).error.is_some()); + assert_eq!(agent.asked.lock().unwrap().len(), asked); + // A wrong password stays on the step, with the password gone from the field. + type_into(&mut model, |step| { + if let Step::SetupPassword { password } = step { + *password = "wrong password!".into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).error.as_deref(), Some("That password isn't right.")); + assert!(matches!(&page(&mut model).step, Step::SetupPassword { password } if password.is_empty())); + type_into(&mut model, |step| { + if let Step::SetupPassword { password } = step { + *password = PASSWORD.into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + let Step::SetupCode { secret, uri, .. } = &page(&mut model).step else { panic!("{:?}", page(&mut model).step) }; + assert_eq!(secret, "JBSWY3DPEHPK3PXP"); + assert!(uri.starts_with("otpauth://totp/"), "{uri}"); + // A code that isn't six digits is told so; a wrong one is refused by the server and the secret stays. + type_into(&mut model, |step| { + if let Step::SetupCode { code, .. } = step { + *code = "12345".into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).error.as_deref(), Some("Type the six digits the app shows.")); + type_into(&mut model, |step| { + if let Step::SetupCode { code, .. } = step { + *code = "654 321".into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert!(page(&mut model).error.as_deref().is_some_and(|e| e.contains("isn't right")), "{:?}", page(&mut model).error); + assert!(matches!(&page(&mut model).step, Step::SetupCode { .. })); + type_into(&mut model, |step| { + if let Step::SetupCode { code, .. } = step { + *code = " 123 456 ".into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + let Step::Codes { codes, .. } = &page(&mut model).step else { panic!("{:?}", page(&mut model).step) }; + assert_eq!(codes.len(), 10); + assert!(page(&mut model).is_on(), "the status was asked again"); + // Done: the codes are dropped, and so is everything secret of the page. + feed(&mut model, &agent, Event::Security(SecurityEvent::Home)); + assert_eq!(page(&mut model).step, Step::Home); + let shown = format!("{:?}", model.screen); + assert!(!shown.contains("AAAA-BBBB") && !shown.contains("JBSWY"), "{shown}"); +} + +#[test] +fn leaving_the_page_drops_the_recovery_codes() { + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().enabled = true; + let mut model = on_security(&agent); + feed(&mut model, &agent, Event::Security(SecurityEvent::StartRegenerate)); + type_into(&mut model, |step| { + if let Step::Regenerate { password, code } = step { + *password = PASSWORD.into(); + *code = CODE.into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert!(matches!(&page(&mut model).step, Step::Codes { .. })); + feed(&mut model, &agent, Event::Security(SecurityEvent::Close)); + assert!(matches!(model.screen, Screen::SignedIn(_))); + let shown = format!("{:?}", model.screen); + assert!(!shown.contains("AAAA"), "{shown}"); + // Opened again, the page starts at its start with nothing shown. + feed(&mut model, &agent, Event::OpenSecurity); + assert_eq!(page(&mut model).step, Step::Home); +} + +#[test] +fn turning_it_off_takes_the_password_and_a_code_or_a_recovery_code() { + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().enabled = true; + let mut model = on_security(&agent); + feed(&mut model, &agent, Event::Security(SecurityEvent::StartDisable)); + let set = |model: &mut Model, pw: &str, code: &str| { + type_into(model, |step| { + if let Step::Disable { password, code: c, .. } = step { + *password = pw.into(); + *c = code.into(); + } + }); + }; + set(&mut model, PASSWORD, "12"); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).error.as_deref(), Some("A code from the app is six digits.")); + set(&mut model, "wrong password!", CODE); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).error.as_deref(), Some("That password isn't right.")); + // A lock says how long to wait, and nothing changes. + agent.factor.lock().unwrap().locked = true; + set(&mut model, PASSWORD, CODE); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).error.as_deref(), Some("Too many wrong codes. Try again in 14 minutes.")); + assert!(agent.factor.lock().unwrap().enabled); + agent.factor.lock().unwrap().locked = false; + // A recovery code, once the field is switched. + feed(&mut model, &agent, Event::Security(SecurityEvent::ToggleRecovery)); + set(&mut model, PASSWORD, RECOVERY); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).step, Step::Home); + assert!(!page(&mut model).is_on()); + assert!(page(&mut model).note.as_deref().is_some_and(|n| n.contains("is off"))); + // Passwords never stay in the page. + assert!(!format!("{:?}", model.screen).contains(PASSWORD)); +} + +#[test] +fn the_password_and_the_other_computers() { + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().other_computers = 2; + let mut model = on_security(&agent); + feed(&mut model, &agent, Event::Security(SecurityEvent::StartPassword)); + let set = |model: &mut Model, cur: &str, new: &str, again: &str| { + type_into(model, |step| { + if let Step::Password { current, new: n, again: a } = step { + *current = cur.into(); + *n = new.into(); + *a = again.into(); + } + }); + }; + set(&mut model, PASSWORD, "a brand new password", "another one"); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).error.as_deref(), Some("The two new passwords aren't the same.")); + set(&mut model, PASSWORD, "short", "short"); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).error.as_deref(), Some("passwords are 8 to 1024 bytes")); + set(&mut model, PASSWORD, "a brand new password", "a brand new password"); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert!(page(&mut model).note.as_deref().is_some_and(|n| n.starts_with("Password changed"))); + feed(&mut model, &agent, Event::Security(SecurityEvent::StartSignOutOthers)); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert_eq!(page(&mut model).note.as_deref(), Some("Signed 2 other computers out.")); + assert!(!format!("{:?}", model.screen).contains("brand new")); +} + +#[test] +fn a_join_for_an_account_with_two_factor_asks_for_the_code_and_keeps_the_invite() { + let agent = FakeAgent::new(signed_out()); + let required = Error::RefusedWith { + code: "two_factor_required".into(), + message: "needs a code".into(), + two_factor: Some(craft_cloud_app::TwoFactorChallenge { challenge: String::new(), expires_ms: 0 }), + retry_after_secs: None, + }; + agent.joins.lock().unwrap().push(Ok(signed_in())); + agent.joins.lock().unwrap().push(Err(Error::Refused { code: "two_factor_invalid".into(), message: "wrong".into() })); + agent.joins.lock().unwrap().push(Err(required)); + let mut model = model_after(&agent, vec![]); + let Screen::Join(form) = &mut model.screen else { panic!("{:?}", model.screen) }; + form.join = "cloud.shadows.ch/K7M4-Q9TX".into(); + form.folder = absolute_folder(); + let mut sent = Vec::new(); + let start = no_start(); + let run = |model: &mut Model, event: Event, sent: &mut Vec| { + let mut pending = model.update(event); + while let Some(effect) = pending.pop() { + if let Effect::Join(request) = &effect { + sent.push(request.clone()); + } + if let Some(answer) = call(&agent, &start, effect) { + pending.extend(model.update(answer)); + } + } + }; + run(&mut model, Event::SubmitJoin, &mut sent); + let Screen::SecondFactor(second) = &mut model.screen else { panic!("{:?}", model.screen) }; + assert_eq!(second.error, None); + assert_eq!((sent[0].totp_code.clone(), sent[0].recovery_code.clone()), (None, None), "the first join carries no code"); + // Six digits are needed; the same join goes again with them. + second.code = "12".into(); + run(&mut model, Event::SubmitSecondFactor, &mut sent); + let Screen::SecondFactor(second) = &mut model.screen else { panic!("{:?}", model.screen) }; + assert_eq!(second.error.as_deref(), Some("Type the six digits the app shows.")); + assert_eq!(sent.len(), 1); + second.code = "654 321".into(); + run(&mut model, Event::SubmitSecondFactor, &mut sent); + assert_eq!(sent.len(), 2); + assert_eq!( + (sent[1].join.as_str(), sent[1].totp_code.as_deref(), sent[1].recovery_code.as_deref()), + (sent[0].join.as_str(), Some("654321"), None) + ); + // The server said it was wrong: back at the field, with the reason, the code cleared. + let Screen::SecondFactor(second) = &mut model.screen else { panic!("{:?}", model.screen) }; + assert!(second.error.as_deref().is_some_and(|e| e.contains("isn't right")), "{:?}", second.error); + assert!(second.code.is_empty()); + // A recovery code instead. + second.recovery = true; + second.code = RECOVERY.into(); + *agent.status.lock().unwrap() = signed_in(); + run(&mut model, Event::SubmitSecondFactor, &mut sent); + assert_eq!(sent[2].recovery_code.as_deref(), Some(RECOVERY)); + assert!(matches!(model.screen, Screen::SignedIn(_)), "{:?}", model.screen); + // Back from the field goes to the join form, which keeps what was typed. + *agent.status.lock().unwrap() = signed_out(); + let mut again = model_after(&agent, vec![]); + if let Screen::Join(form) = &mut again.screen { + form.join = "cloud.shadows.ch/K7M4-Q9TX".into(); + form.folder = absolute_folder(); + } + agent.joins.lock().unwrap().push(Err(Error::RefusedWith { + code: "two_factor_required".into(), + message: "m".into(), + two_factor: None, + retry_after_secs: None, + })); + run(&mut again, Event::SubmitJoin, &mut sent); + run(&mut again, Event::CancelSecondFactor, &mut sent); + assert!(matches!(&again.screen, Screen::Join(form) if form.join == "cloud.shadows.ch/K7M4-Q9TX")); +} + +// ---- the screens ---- + +fn harness(model: Model, theme: egui::Theme) -> Harness<'static, (Model, Vec)> { + Harness::builder().with_size(egui::Vec2::from(WINDOW_SIZE)).with_theme(theme).wgpu().build_ui_state( + |ui, (model, events): &mut (Model, Vec)| { + events.extend(show(ui, model, NOW)); + }, + (model, Vec::new()), + ) +} + +fn snapshot(model: Model, name: &str) { + let mut h = harness(model, egui::Theme::Light); + h.run_steps(4); + let image = h.render().unwrap(); + let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("snapshots"); + let path = dir.join(format!("{name}.png")); + if std::env::var_os("UPDATE_SNAPSHOTS").is_some() { + std::fs::create_dir_all(&dir).unwrap(); + image.save(&path).unwrap(); + return; + } + let new = dir.join(format!("{name}.new.png")); + let Ok(old) = image::open(&path) else { + image.save(&new).unwrap(); + panic!("no snapshot {} (UPDATE_SNAPSHOTS=1 writes it; this run's is {})", path.display(), new.display()); + }; + let old = old.into_rgba8(); + let same_size = old.dimensions() == image.dimensions(); + let different = old.pixels().zip(image.pixels()).filter(|(a, b)| a.0.iter().zip(b.0.iter()).any(|(x, y)| x.abs_diff(*y) > TOLERANCE)).count(); + let allowed = (image.width() * image.height()) as usize * MAX_DIFFERENT / 1000; + if !same_size || different > allowed { + image.save(&new).unwrap(); + panic!("{name}: {different} pixels differ (at most {allowed}); this run's is {}", new.display()); + } + let _ = std::fs::remove_file(new); +} + +#[test] +fn snapshot_second_factor() { + let agent = FakeAgent::new(signed_out()); + agent.joins.lock().unwrap().push(Err(Error::RefusedWith { + code: "two_factor_required".into(), + message: "m".into(), + two_factor: None, + retry_after_secs: None, + })); + let mut model = model_after(&agent, vec![]); + if let Screen::Join(form) = &mut model.screen { + form.join = "cloud.shadows.ch/K7M4-Q9TX".into(); + form.folder = absolute_folder(); + } + feed(&mut model, &agent, Event::SubmitJoin); + assert!(matches!(model.screen, Screen::SecondFactor(_))); + snapshot(model, "second-factor"); +} + +#[test] +fn snapshot_second_factor_with_a_wrong_code() { + let form = crate::security::SecondFactorForm::new( + crate::model::JoinForm { join: "cloud.shadows.ch/K7M4-Q9TX".into(), folder: "/home/fabio/Craft Cloud".into(), ..Default::default() }, + None, + Some("That code isn't right, or it was used already. Wait for the app's next code and try again.".into()), + ); + let mut model = Model::new(defaults(), true, String::new()); + model.screen = Screen::SecondFactor(form); + snapshot(model, "second-factor-wrong-code"); +} + +fn security_model(agent: &FakeAgent, events: Vec) -> Model { + let mut model = on_security(agent); + for event in events { + feed(&mut model, agent, event); + } + model +} + +#[test] +fn snapshot_security_off() { + snapshot(security_model(&FakeAgent::new(signed_in()), vec![]), "security-off"); +} + +#[test] +fn snapshot_security_on() { + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().enabled = true; + snapshot(security_model(&agent, vec![]), "security-on"); +} + +#[test] +fn snapshot_security_unsupported() { + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().unsupported = true; + snapshot(security_model(&agent, vec![]), "security-unsupported"); +} + +#[test] +fn snapshot_security_setup_with_its_qr_code() { + let agent = FakeAgent::new(signed_in()); + let mut model = security_model(&agent, vec![Event::Security(SecurityEvent::StartSetup)]); + type_into(&mut model, |step| { + if let Step::SetupPassword { password } = step { + *password = PASSWORD.into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert!(matches!(&page(&mut model).step, Step::SetupCode { .. })); + snapshot(model, "security-setup"); +} + +#[test] +fn snapshot_security_recovery_codes() { + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().enabled = true; + let mut model = security_model(&agent, vec![Event::Security(SecurityEvent::StartRegenerate)]); + type_into(&mut model, |step| { + if let Step::Regenerate { password, code } = step { + *password = PASSWORD.into(); + *code = CODE.into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert!(matches!(&page(&mut model).step, Step::Codes { .. })); + snapshot(model, "security-recovery-codes"); +} + +#[test] +fn snapshot_security_turning_it_off_when_locked() { + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().enabled = true; + let mut model = security_model(&agent, vec![Event::Security(SecurityEvent::StartDisable)]); + agent.factor.lock().unwrap().locked = true; + type_into(&mut model, |step| { + if let Step::Disable { password, code, .. } = step { + *password = PASSWORD.into(); + *code = CODE.into(); + } + }); + feed(&mut model, &agent, Event::Security(SecurityEvent::Submit)); + assert!(page(&mut model).error.is_some()); + snapshot(model, "security-disable-locked"); +} + +// ---- the buttons ---- + +#[test] +fn the_overview_has_a_security_button_and_the_page_its_buttons() { + let model = model_after(&FakeAgent::new(signed_in()), vec![]); + let mut h = harness(model, egui::Theme::Light); + h.run_steps(2); + h.get_by_label("Security…").click(); + h.run_steps(2); + assert!(h.state().1.contains(&Event::OpenSecurity), "{:?}", h.state().1); + + let agent = FakeAgent::new(signed_in()); + let mut h = harness(security_model(&agent, vec![]), egui::Theme::Light); + h.run_steps(2); + h.get_by_label("Set up two-factor sign-in…").click(); + h.run_steps(2); + h.get_by_label("Change password…").click(); + h.run_steps(2); + h.get_by_label("Back").click(); + h.run_steps(2); + let events = &h.state().1; + assert!(events.contains(&Event::Security(SecurityEvent::StartSetup)), "{events:?}"); + assert!(events.contains(&Event::Security(SecurityEvent::StartPassword)), "{events:?}"); + assert!(events.contains(&Event::Security(SecurityEvent::Close)), "{events:?}"); +} + +// ---- what egui remembers of what was typed ---- + +type H = Harness<'static, (Model, Vec)>; + +/// Whether egui still has state for the field: and whether Undo could bring typed text back into an empty field. +fn remembered(h: &H, name: &str) -> (bool, bool) { + use egui::text_edit::TextEditState; + let state = TextEditState::load(&h.ctx, super::security::secret_id(name)); + let undoable = state.as_ref().is_some_and(|s| s.undoer().undo(&(egui::text::CCursorRange::default(), String::new())).is_some()); + (state.is_some(), undoable) +} + +fn undo(h: &mut H, role: egui::accesskit::Role) { + h.get_by_role(role).focus(); + h.run_steps(2); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::Z); + h.run_steps(3); +} + +#[test] +fn a_typed_password_or_code_is_not_left_in_eguis_undo_history() { + use egui::accesskit::Role; + let agent = FakeAgent::new(signed_in()); + agent.factor.lock().unwrap().enabled = true; + let model = security_model(&agent, vec![Event::Security(SecurityEvent::StartDisable)]); + let mut h = harness(model, egui::Theme::Light); + h.run_steps(3); + let typed = |h: &H| match &h.state().0.screen { + Screen::Security(page) => match &page.step { + Step::Disable { password, code, .. } => (password.clone(), code.clone()), + other => panic!("{other:?}"), + }, + other => panic!("{other:?}"), + }; + + h.get_by_role(Role::PasswordInput).focus(); + h.get_by_role(Role::PasswordInput).type_text("hunter2-secret"); + h.run_steps(8); + assert!(remembered(&h, "disable-password").1, "while typed, egui has the field's history (the test sees what it guards against)"); + h.get_by_role(Role::TextInput).focus(); + h.get_by_role(Role::TextInput).type_text("123456"); + h.run_steps(8); + assert_eq!(typed(&h), ("hunter2-secret".into(), "123456".into())); + assert!(remembered(&h, "disable-code").1, "and the code's"); + + // Sent: the model moved the typed text into the call and cleared the fields. + let effects = h.state_mut().0.update(Event::Security(SecurityEvent::Submit)); + assert!(matches!(effects.as_slice(), [Effect::Security(_)]), "{effects:?}"); + h.run_steps(2); + assert!(!remembered(&h, "disable-password").1, "once sent: no history (a field still drawn has an empty state)"); + assert!(!remembered(&h, "disable-code").1, "once sent: no history (a field still drawn has an empty state)"); + let refused = Error::Refused { code: "wrong_password".into(), message: "no".into() }; + h.state_mut().0.update(Event::SecurityAnswer(crate::security::SecurityAnswer::Disable(Err(refused)))); + h.run_steps(3); + undo(&mut h, Role::PasswordInput); + undo(&mut h, Role::TextInput); + assert_eq!(typed(&h), (String::new(), String::new()), "Undo brings nothing back"); + + // Typed again and cancelled, then the step opened again. + h.get_by_role(Role::PasswordInput).focus(); + h.get_by_role(Role::PasswordInput).type_text("another-secret"); + h.run_steps(8); + assert!(remembered(&h, "disable-password").1); + h.state_mut().0.update(Event::Security(SecurityEvent::Home)); + h.run_steps(2); + assert_eq!(remembered(&h, "disable-password"), (false, false), "once cancelled"); + h.state_mut().0.update(Event::Security(SecurityEvent::StartDisable)); + h.run_steps(3); + undo(&mut h, Role::PasswordInput); + assert_eq!(typed(&h).0, ""); + + // Left with something typed: the same. + h.get_by_role(Role::PasswordInput).focus(); + h.get_by_role(Role::PasswordInput).type_text("third-secret"); + h.run_steps(3); + h.state_mut().0.update(Event::Security(SecurityEvent::Close)); + h.run_steps(2); + assert_eq!(remembered(&h, "disable-password"), (false, false), "once the page is left"); +} + +#[test] +fn a_typed_join_code_is_not_left_in_eguis_undo_history() { + use egui::accesskit::Role; + let form = crate::security::SecondFactorForm::new( + crate::model::JoinForm { join: "cloud.shadows.ch/K7M4-Q9TX".into(), folder: absolute_folder(), ..Default::default() }, + None, + None, + ); + let mut model = Model::new(defaults(), true, String::new()); + model.screen = Screen::SecondFactor(form); + let mut h = harness(model, egui::Theme::Light); + h.run_steps(3); + h.get_by_role(Role::TextInput).focus(); + h.get_by_role(Role::TextInput).type_text("654321"); + h.run_steps(8); + assert!(remembered(&h, "second-factor-code").1, "while typed, egui has its history"); + // Back to the join form, then the field again. + h.state_mut().0.update(Event::CancelSecondFactor); + h.run_steps(2); + assert_eq!(remembered(&h, "second-factor-code"), (false, false), "once cancelled"); + let form = crate::security::SecondFactorForm::new(crate::model::JoinForm::default(), None, None); + h.state_mut().0.screen = Screen::SecondFactor(form); + h.run_steps(3); + undo(&mut h, Role::TextInput); + match &h.state().0.screen { + Screen::SecondFactor(form) => assert_eq!(form.code, "", "Undo brings nothing back"), + other => panic!("{other:?}"), + } + // A code sent (the model clears or leaves the screen): forgotten as well. + h.get_by_role(Role::TextInput).type_text("111111"); + h.run_steps(8); + h.state_mut().0.screen = Screen::Join(Default::default()); + h.run_steps(2); + assert_eq!(remembered(&h, "second-factor-code"), (false, false), "once the screen is left"); +} diff --git a/apps/craft-cloud-desktop/tests/snapshots/second-factor-wrong-code.png b/apps/craft-cloud-desktop/tests/snapshots/second-factor-wrong-code.png new file mode 100644 index 00000000..ab5e2747 Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/second-factor-wrong-code.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/second-factor.png b/apps/craft-cloud-desktop/tests/snapshots/second-factor.png new file mode 100644 index 00000000..1857d806 Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/second-factor.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/security-disable-locked.png b/apps/craft-cloud-desktop/tests/snapshots/security-disable-locked.png new file mode 100644 index 00000000..cce9701e Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/security-disable-locked.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/security-off.png b/apps/craft-cloud-desktop/tests/snapshots/security-off.png new file mode 100644 index 00000000..4283d7dd Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/security-off.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/security-on.png b/apps/craft-cloud-desktop/tests/snapshots/security-on.png new file mode 100644 index 00000000..1399264e Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/security-on.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/security-recovery-codes.png b/apps/craft-cloud-desktop/tests/snapshots/security-recovery-codes.png new file mode 100644 index 00000000..a9215832 Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/security-recovery-codes.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/security-setup.png b/apps/craft-cloud-desktop/tests/snapshots/security-setup.png new file mode 100644 index 00000000..4e4b7082 Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/security-setup.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/security-unsupported.png b/apps/craft-cloud-desktop/tests/snapshots/security-unsupported.png new file mode 100644 index 00000000..ed75428d Binary files /dev/null and b/apps/craft-cloud-desktop/tests/snapshots/security-unsupported.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/signed-in-dark.png b/apps/craft-cloud-desktop/tests/snapshots/signed-in-dark.png index b443f0bb..d2619e8b 100644 Binary files a/apps/craft-cloud-desktop/tests/snapshots/signed-in-dark.png and b/apps/craft-cloud-desktop/tests/snapshots/signed-in-dark.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/signed-in-offline.png b/apps/craft-cloud-desktop/tests/snapshots/signed-in-offline.png index 8a80cb23..68cba73e 100644 Binary files a/apps/craft-cloud-desktop/tests/snapshots/signed-in-offline.png and b/apps/craft-cloud-desktop/tests/snapshots/signed-in-offline.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/signed-in-paused.png b/apps/craft-cloud-desktop/tests/snapshots/signed-in-paused.png index b6365788..4fc9b949 100644 Binary files a/apps/craft-cloud-desktop/tests/snapshots/signed-in-paused.png and b/apps/craft-cloud-desktop/tests/snapshots/signed-in-paused.png differ diff --git a/apps/craft-cloud-desktop/tests/snapshots/signed-in.png b/apps/craft-cloud-desktop/tests/snapshots/signed-in.png index aef3f674..d606f184 100644 Binary files a/apps/craft-cloud-desktop/tests/snapshots/signed-in.png and b/apps/craft-cloud-desktop/tests/snapshots/signed-in.png differ diff --git a/apps/craft-cloud-server/Cargo.toml b/apps/craft-cloud-server/Cargo.toml index 4c1c3ef6..d472dc3f 100644 --- a/apps/craft-cloud-server/Cargo.toml +++ b/apps/craft-cloud-server/Cargo.toml @@ -12,6 +12,7 @@ publish = false test-support = [] [dependencies] +aes-gcm.workspace = true argon2.workspace = true axum.workspace = true base64.workspace = true @@ -21,20 +22,27 @@ clap.workspace = true craft-cloud-meta.workspace = true craft-cloud-proto.workspace = true craft-cloud-store.workspace = true +# The audit export's body is a stream of pages. +futures-util.workspace = true getrandom.workspace = true +hmac.workspace = true serde.workspace = true serde_json.workspace = true +sha1.workspace = true +subtle.workspace = true thiserror.workspace = true tokio.workspace = true toml.workspace = true tracing.workspace = true tracing-subscriber.workspace = true uuid.workspace = true +zeroize.workspace = true [dev-dependencies] +# The tests move the server's clock (`AppState::with_clock`): the feature is on for them only. +craft-cloud-server = { path = ".", features = ["test-support"] } reqwest.workspace = true tokio-tungstenite.workspace = true -futures-util.workspace = true serde_json.workspace = true sqlx.workspace = true tempfile.workspace = true diff --git a/apps/craft-cloud-server/src/account.rs b/apps/craft-cloud-server/src/account.rs new file mode 100644 index 00000000..6af4d177 --- /dev/null +++ b/apps/craft-cloud-server/src/account.rs @@ -0,0 +1,170 @@ +//! The signed-in account: `GET /api/v1/account`, `POST /api/v1/account/password`, and the device routes that change +//! a device (`PATCH /api/v1/devices/{id}`, `POST /api/v1/devices/revoke-others`). +//! +//! - **A wrong current password is `403 wrong_password`, never 401**: on a signed-in route the agent reads every 401 +//! as "this device was revoked" and signs out. The same holds for every later route that asks for a password. +//! - **Changing the password** reads the body (at most [`TINY_JSON_BYTES`]), counts the try against the account +//! ([`PASSWORD_CHANGES_PER_USER`] in 15 minutes, wrong passwords included), takes one of the server-wide +//! password-check slots (every slot busy is `429 rate_limited` and the try is given back), checks the current +//! password and the new one's rules, and hashes the new one, all in that one slot. Then one transaction sets the +//! hash, revokes every other device and deletes the account's open invite and pairings +//! ([`craft_cloud_meta::Meta::change_password`]); afterwards each revoked device's live connections end at once. +//! The device that asked stays signed in. +//! - **Nothing secret is logged**: not a password, a hash or a token. The count of revoked devices is logged at +//! `info`, the username at most at `debug`. + +use std::sync::Arc; + +use axum::Json; +use axum::extract::rejection::PathRejection; +use axum::extract::{Extension, Path, Request, State}; +use axum::http::StatusCode; +use craft_cloud_meta::{DeviceAuth, RevokeReason, UserId}; +use craft_cloud_proto::DeviceId; +use craft_cloud_proto::api::{AccountInfo, ChangePasswordRequest, DeviceInfo, RenameDeviceRequest, RevokedOthers, codes}; + +use crate::api::{PASSWORD_RETRY_SECS, TINY_JSON_BYTES, device_info, read_json}; +use crate::auth::{MAX_PASSWORD_BYTES, check_password_rules, hash_password, verify_password}; +use crate::error::ApiError; +use crate::state::AppState; + +/// Password change tries one account may make in [`AppState::password_changes`]'s window (15 minutes). +pub const PASSWORD_CHANGES_PER_USER: u32 = 5; + +type Shared = Arc; + +/// `403 wrong_password`: the current password was wrong. Never 401, see the module's notes. +pub(crate) fn wrong_password() -> ApiError { + ApiError::new(StatusCode::FORBIDDEN, codes::WRONG_PASSWORD, "the current password is wrong") +} + +/// Checks the signed-in account's own password (`user`, named `username`: a device's account, or a browser session's) for a +/// route that asks for one again (a change of the two-factor settings, an admin's confirmation) and returns the password hash it was verified against, which the change then compares with the +/// stored one under the account's lock. Wrong passwords are counted per account ([`PASSWORD_CHANGES_PER_USER`] in 15 +/// minutes, the window of the password change) and a right one is given back, so only a wrong password stays counted; +/// every check holds one of the server-wide password-check slots, and all busy is `429 rate_limited` that costs no try. A +/// wrong password is `403 wrong_password`, never 401. +pub(crate) async fn confirm_password(state: &AppState, user_id: UserId, username: &str, password: String) -> Result { + if password.len() > MAX_PASSWORD_BYTES { + return Err(ApiError::invalid("passwords are at most 1024 bytes")); + } + // tokio's clock: the system's monotonic clock in the server, the simulated one in a simulation. + let now = tokio::time::Instant::now().into_std(); + let key = format!("user:{username}"); + let Some(counted) = state.password_changes.count(&key, PASSWORD_CHANGES_PER_USER, now) else { + return Err(ApiError::rate_limited("too many wrong passwords; wait a few minutes", None)); + }; + // Each check costs 19 MiB and real CPU time: all slots busy is told to come back, not queued, and costs no try. + let Ok(permit) = state.password_checks.clone().try_acquire_owned() else { + state.password_changes.refund(&key, counted); + return Err(ApiError::rate_limited("the server is busy checking other passwords; try again in a moment", Some(PASSWORD_RETRY_SECS))); + }; + let user = match state.meta.user_by_name(username).await { + Ok(Some(user)) if user.id == user_id => user, + other => { + state.password_changes.refund(&key, counted); + other?; + return Err(ApiError::unauthorized()); + } + }; + let hash = user.password_hash.clone(); + let ok = tokio::task::spawn_blocking(move || { + let _permit = permit; + verify_password(&password, &hash) + }) + .await + .map_err(|e| { + tracing::error!(error = %e, "password check task failed"); + ApiError::internal() + })?; + if !ok { + return Err(wrong_password()); + } + state.password_changes.refund(&key, counted); + Ok(user.password_hash) +} + +/// `GET /api/v1/account`. +pub async fn get_account(State(state): State, Extension(auth): Extension) -> Result, ApiError> { + let account = state.meta.account(auth.user).await?; + Ok(Json(AccountInfo { + user_id: account.id.0, + username: account.username, + role: account.role.as_str().to_owned(), + created_ms: account.created_ms, + password_changed_ms: account.password_changed_ms, + two_factor_enabled: account.two_factor_enabled, + })) +} + +/// `POST /api/v1/account/password`: `204` once the password is changed and the account's other devices are signed out. +pub async fn change_password(State(state): State, Extension(auth): Extension, request: Request) -> Result { + let req: ChangePasswordRequest = read_json(&state, request, TINY_JSON_BYTES).await?; + if req.current_password.len() > MAX_PASSWORD_BYTES || req.new_password.len() > MAX_PASSWORD_BYTES { + return Err(ApiError::invalid("passwords are at most 1024 bytes")); + } + // tokio's clock: the system's monotonic clock in the server, the simulated one in a simulation. + let now = tokio::time::Instant::now().into_std(); + let key = format!("user:{}", auth.username); + let Some(counted) = state.password_changes.count(&key, PASSWORD_CHANGES_PER_USER, now) else { + return Err(ApiError::rate_limited("too many password change attempts; wait a few minutes", None)); + }; + // Each check costs 19 MiB and real CPU time (see the sign-in): all slots busy is told to come back, not queued, + // and costs the account no try, since nothing was checked. + let Ok(permit) = state.password_checks.clone().try_acquire_owned() else { + state.password_changes.refund(&key, counted); + return Err(ApiError::rate_limited("the server is busy checking other passwords; try again in a moment", Some(PASSWORD_RETRY_SECS))); + }; + let user = state.meta.user_by_name(&auth.username).await?.ok_or_else(ApiError::unauthorized)?; + let current_hash = user.password_hash.clone(); + let ChangePasswordRequest { current_password, new_password } = req; + let new_hash = tokio::task::spawn_blocking(move || { + let _permit = permit; + if !verify_password(¤t_password, ¤t_hash) { + return Err(wrong_password()); + } + check_password_rules(&new_password).map_err(|e| ApiError::new(StatusCode::BAD_REQUEST, codes::WEAK_PASSWORD, e.message))?; + if new_password == current_password { + return Err(ApiError::new(StatusCode::BAD_REQUEST, codes::WEAK_PASSWORD, "the new password must differ from the current one")); + } + hash_password(&new_password) + }) + .await + .map_err(|e| { + tracing::error!(error = %e, "password change task failed"); + ApiError::internal() + })??; + let revoked = state.meta.change_password(auth.user, &user.password_hash, &new_hash, auth.device, state.now_ms()).await?; + for device in &revoked { + state.live.revoke(*device); + } + tracing::debug!(user = %auth.username, "password changed"); + tracing::info!(device = %auth.device, revoked = revoked.len(), "password changed"); + Ok(StatusCode::NO_CONTENT) +} + +/// `PATCH /api/v1/devices/{id}`: renames one of the account's own devices that isn't revoked. Anything else is +/// `404 not_found`, so the answer never says whether another account has the device. +pub async fn rename_device( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result, ApiError> { + let Path(id) = id?; + let req: RenameDeviceRequest = read_json(&state, request, TINY_JSON_BYTES).await?; + crate::api::check_device_name(&req.name)?; + let device = state.meta.rename_device(auth.user, id, &req.name).await?; + Ok(Json(device_info(device, auth.device))) +} + +/// `POST /api/v1/devices/revoke-others`: signs out every other device of the account. +pub async fn revoke_others(State(state): State, Extension(auth): Extension) -> Result, ApiError> { + let revoked = state.meta.revoke_other_devices(auth.user, auth.device, RevokeReason::Others, state.now_ms()).await?; + // Their live connections end now, not at their next request. + for device in &revoked { + state.live.revoke(*device); + } + tracing::info!(device = %auth.device, revoked = revoked.len(), "other devices signed out"); + Ok(Json(RevokedOthers { revoked: u32::try_from(revoked.len()).unwrap_or(u32::MAX) })) +} diff --git a/apps/craft-cloud-server/src/admin.rs b/apps/craft-cloud-server/src/admin.rs new file mode 100644 index 00000000..b61d3c32 --- /dev/null +++ b/apps/craft-cloud-server/src/admin.rs @@ -0,0 +1,408 @@ +//! The admin API, under `/api/v1/admin`: server counters, accounts (list, create, change role and quota, disable, +//! enable, mark for removal), invites, password reset codes, and a user's devices. +//! +//! - **Every route is behind [`require_admin`](crate::auth::require_admin)** (after `require_device`): a member gets `403 +//! forbidden`, a bad token `401`. The role is read from the database for each request, so a demoted admin is out with +//! the next one. +//! - **Every change needs a fresh confirmation in its body**: the acting admin's current `password`. A device token never +//! expires, so a stolen admin token alone must not be enough to make an account an admin's or to sign others out. A +//! missing or mistyped field is `400 invalid`, a wrong password `403 wrong_password` (never 401: on a signed-in route +//! the agent reads 401 as "this device was revoked"). Tries are counted per account in +//! [`AppState::password_changes`], the same window the password change uses, but only a wrong password stays counted; +//! every check holds a server-wide password-check slot. An admin whose account has two-factor sign-in adds `totp_code` +//! (or `recovery_code`): missing or wrong is `403 two_factor_invalid`, too many wrong ones `429 two_factor_locked`. The +//! code is used up by the confirmation (one time step serves one change, as at sign-in), and the check shares its +//! counters and limiter with sign-in. +//! - **Bodies are tiny** ([`TINY_JSON_BYTES`]), the `DELETE` routes carry one too, and are read under the same deadline +//! as every JSON body. +//! - **Nothing secret is logged**: not a password, a code or a join link. Events are logged at `info` with the kind and +//! the target's ID, never a username. +//! - **Live connections** of devices an admin signs out end at once in this process (after the commit); another server +//! process, or the command line, finds out at the next ping (about 30 seconds). + +use std::sync::Arc; + +use axum::Json; +use axum::body::Bytes; +use axum::extract::rejection::{PathRejection, QueryRejection}; +use axum::extract::{DefaultBodyLimit, Extension, FromRequest, Path, Query, Request, State}; +use axum::http::StatusCode; +use axum::routing::{get, post}; +use axum::{Router, middleware}; +use base64::Engine; +use base64::engine::general_purpose::URL_SAFE_NO_PAD; +use craft_cloud_meta::{AdminActor, AdminUser, AdminVia, DeviceAuth, Role, UserId, valid_username}; +use craft_cloud_proto::DeviceId; +use craft_cloud_proto::api::{ + AdminCode, AdminConfirm, AdminCreateUser, AdminCreatedUser, AdminDevices, AdminPasswordReset, AdminServerInfo, AdminUpdateUser, AdminUserInfo, + AdminUserList, +}; +use craft_cloud_proto::join::{Code, INVITE_CHARS, INVITE_TTL_MS, JoinString}; +use serde::Deserialize; +use uuid::Uuid; + +use crate::account::confirm_password; +use crate::api::{TINY_JSON_BYTES, body_too_slow, device_info}; +use crate::auth::require_admin; +use crate::error::ApiError; +use crate::state::AppState; + +type Shared = Arc; + +/// Accounts one page of `GET /admin/users` holds when `limit` is not given. +pub(crate) const DEFAULT_PAGE: u32 = 50; +/// The most accounts a page may hold. +pub(crate) const MAX_PAGE: u32 = 200; +/// The longest `query` of the user list, in bytes. +pub(crate) const MAX_QUERY_BYTES: usize = craft_cloud_meta::MAX_SEARCH_BYTES; + +/// The admin routes, all behind `require_admin`. `router` merges them into the signed-in routes, so `require_device` +/// wraps them too and runs first. +pub fn routes() -> Router { + Router::new() + .route("/admin/server", get(server)) + .route("/admin/users", get(list_users).post(create_user).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/admin/users/{id}", axum::routing::patch(update_user).delete(remove_user).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/admin/users/{id}/disable", post(disable_user).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/admin/users/{id}/enable", post(enable_user).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/admin/users/{id}/invite", post(issue_invite).delete(revoke_invite).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/admin/users/{id}/password-reset", post(issue_reset).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/admin/users/{id}/reset-two-factor", post(reset_two_factor).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/admin/users/{id}/devices", get(list_devices)) + .route("/admin/users/{id}/devices/{device_id}", axum::routing::delete(revoke_device).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route_layer(middleware::from_fn(require_admin)) + .route_layer(middleware::from_fn(crate::web::reject_mixed_credentials)) +} + +// ---- helpers ---- + +/// Reads a JSON body of at most [`TINY_JSON_BYTES`] under the usual deadline. Whatever is wrong with its shape (not JSON, +/// a missing or mistyped field, an unknown role) is `400 invalid`; too long is `413 too_large`. +async fn read_body(state: &AppState, request: Request) -> Result { + let bytes = match tokio::time::timeout(state.limits.body_time(TINY_JSON_BYTES), Bytes::from_request(request, &())).await { + Ok(bytes) => bytes?, + Err(_) => return Err(body_too_slow()), + }; + serde_json::from_slice(&bytes) + .map_err(|_| ApiError::invalid("the body must be a JSON object with your current password in `password`, and the fields of this call")) +} + +/// What an admin's body gives for a confirmation: the password, and the second factor when the account has one. +pub(crate) struct Confirmation { + password: Option, + totp_code: Option, + recovery_code: Option, +} + +impl Confirmation { + pub(crate) fn new(password: Option, totp_code: Option, recovery_code: Option) -> Self { + Self { password, totp_code, recovery_code } + } +} + +/// Checks the acting admin's confirmation and returns the actor to hand to the change: the password ([`confirm_password`]: +/// one wrong password stays counted against the account, a right one is given back), then, when the admin's account has +/// two-factor sign-in, the second factor (`totp_code`, or `recovery_code`), which is used up here: the time step or the +/// recovery code can't serve a second change, and wrong ones are counted and can lock ([`crate::two_factor`]). A password +/// that is wrong is told before anything about the second factor. +async fn confirm(state: &AppState, auth: &DeviceAuth, given: Confirmation) -> Result { + confirm_actor(state, auth.user, &auth.username, AdminVia::Device(auth.device), given).await +} + +/// [`confirm`] for any admin who acts: `via` says whether a device or a browser session does (the web pages call this with +/// the same confirmation, the same counters and the same statuses as the JSON routes above). +pub(crate) async fn confirm_actor( + state: &AppState, + user: UserId, + username: &str, + via: AdminVia, + given: Confirmation, +) -> Result { + let password = given.password.ok_or_else(|| ApiError::invalid("this change needs your current password in `password`"))?; + // The shape of the second factor is checked before anything is counted. + let proof = crate::two_factor::parse_proof(given.totp_code, given.recovery_code)?; + let password_hash = confirm_password(state, user, username, password).await?; + let two_factor = crate::two_factor::confirm_admin_second_factor(state, user, username, proof).await?; + Ok(AdminActor { user, via, password_hash, two_factor }) +} + +fn user_id(id: Result, PathRejection>) -> Result { + let Path(id) = id?; + Ok(UserId(id)) +} + +pub(crate) fn role_of(name: &str) -> Result { + match name { + "admin" => Ok(Role::Admin), + "member" => Ok(Role::Member), + _ => Err(ApiError::invalid("role is admin or member")), + } +} + +fn user_info(u: AdminUser) -> AdminUserInfo { + AdminUserInfo { + id: u.id.0, + username: u.username, + role: u.role.as_str().to_owned(), + created_ms: u.created_ms, + disabled_ms: u.disabled_ms, + two_factor_enabled: u.two_factor_enabled, + devices_active: u.devices_active, + stored_bytes: u.stored_bytes, + quota_bytes: u.quota_bytes, + invite_pending: u.invite_pending, + } +} + +/// A fresh invite or reset code, as the admin gets it: shown once, never logged. +pub(crate) fn new_code() -> Result { + Code::generate(INVITE_CHARS).map_err(|e| { + tracing::error!(error = %e, "the system random source failed"); + ApiError::internal() + }) +} + +pub(crate) fn admin_code(state: &AppState, code: Code, expires_ms: i64) -> AdminCode { + let shown = code.as_str().to_owned(); + let join_url = state.public_url.as_ref().map(|server| JoinString::new(server.clone(), code).link()); + AdminCode { code: shown, join_url, expires_ms } +} + +pub(crate) fn end_live_connections(state: &AppState, devices: &[DeviceId]) { + for device in devices { + state.live.revoke(*device); + } +} + +// ---- reads ---- + +/// `GET /admin/server`: counters and kinds only. +async fn server(State(state): State) -> Result, ApiError> { + let counts = state.meta.admin_server_counts().await?; + Ok(Json(AdminServerInfo { + version: env!("CARGO_PKG_VERSION").to_owned(), + users: counts.users, + devices_active: counts.devices_active, + stored_bytes: counts.stored_bytes, + live_bytes: counts.live_bytes, + storage_kind: state.store.kind().to_owned(), + database_kind: state.meta.database_kind().to_owned(), + })) +} + +#[derive(Debug, Deserialize)] +struct ListQuery { + cursor: Option, + limit: Option, + query: Option, +} + +/// The username a cursor stands for. A cursor is the last username of a page, in base64url: opaque to clients, and +/// checked like any username, since it is hostile input. +pub(crate) fn decode_cursor(cursor: &str) -> Result { + let bad = || ApiError::invalid("the cursor is not one this server gave"); + let bytes = URL_SAFE_NO_PAD.decode(cursor).map_err(|_| bad())?; + let name = String::from_utf8(bytes).map_err(|_| bad())?; + if valid_username(&name) { Ok(name) } else { Err(bad()) } +} + +/// `GET /admin/users?cursor&limit&query`: accounts by username, 50 a page (at most 200). `query` finds a username +/// containing it (case does not matter, at most 64 bytes of printable characters). +async fn list_users(State(state): State, q: Result, QueryRejection>) -> Result, ApiError> { + let Query(q) = q?; + let limit = q.limit.unwrap_or(DEFAULT_PAGE); + if !(1..=MAX_PAGE).contains(&limit) { + return Err(ApiError::invalid(format!("limit is 1 to {MAX_PAGE}"))); + } + let after = q.cursor.as_deref().map(decode_cursor).transpose()?; + let search = q.query.as_deref().filter(|s| !s.is_empty()); + if let Some(search) = search + && (search.len() > MAX_QUERY_BYTES || search.chars().any(char::is_control)) + { + return Err(ApiError::invalid(format!("query is at most {MAX_QUERY_BYTES} bytes of printable characters"))); + } + let page = state.meta.admin_list_users(after.as_deref(), search, limit, state.now_ms()).await?; + Ok(Json(AdminUserList { + users: page.users.into_iter().map(user_info).collect(), + next: page.next_after.map(|name| URL_SAFE_NO_PAD.encode(name.as_bytes())), + })) +} + +/// `GET /admin/users/{id}/devices`: every device of the account, revoked ones with when and why. +async fn list_devices( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, +) -> Result, ApiError> { + let devices = state.meta.admin_devices(user_id(id)?).await?; + Ok(Json(AdminDevices { devices: devices.into_iter().map(|d| device_info(d, auth.device)).collect() })) +} + +// ---- changes ---- + +/// `POST /admin/users`: a new account without a password and its invite. +async fn create_user( + State(state): State, + Extension(auth): Extension, + request: Request, +) -> Result, ApiError> { + let req: AdminCreateUser = read_body(&state, request).await?; + if !valid_username(&req.username) { + return Err(ApiError::invalid("usernames are 1-64 letters, digits, '.', '_' or '-'")); + } + let role = role_of(&req.role)?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + let code = new_code()?; + let now = state.now_ms(); + let (user, issued) = state.meta.admin_create_user(&actor, &req.username, role, &code.hash(), now, INVITE_TTL_MS).await?; + tracing::info!(actor = %auth.device, target = %user.id.0, "admin: account created"); + Ok(Json(AdminCreatedUser { user: user_info(user), invite: admin_code(&state, code, issued.expires_ms) })) +} + +/// `PATCH /admin/users/{id}`: a new role and/or quota. +async fn update_user( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result, ApiError> { + let target = user_id(id)?; + let req: AdminUpdateUser = read_body(&state, request).await?; + let role = req.role.as_deref().map(role_of).transpose()?; + if role.is_none() && req.quota_bytes.is_none() { + return Err(ApiError::invalid("give a role or a quota_bytes to change")); + } + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + let user = state.meta.admin_update_user(&actor, target, role, req.quota_bytes, state.now_ms()).await?; + tracing::info!(actor = %auth.device, target = %target.0, role_changed = role.is_some(), quota_changed = req.quota_bytes.is_some(), "admin: account changed"); + Ok(Json(user_info(user))) +} + +/// `POST /admin/users/{id}/disable`: 204. +async fn disable_user( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result { + let target = user_id(id)?; + let req: AdminConfirm = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + let revoked = state.meta.admin_disable_user(&actor, target, state.now_ms()).await?; + end_live_connections(&state, &revoked); + tracing::info!(actor = %auth.device, target = %target.0, revoked = revoked.len(), "admin: account disabled"); + Ok(StatusCode::NO_CONTENT) +} + +/// `POST /admin/users/{id}/enable`: 204. Devices that were signed out stay signed out. +async fn enable_user( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result { + let target = user_id(id)?; + let req: AdminConfirm = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + state.meta.admin_enable_user(&actor, target, state.now_ms()).await?; + tracing::info!(actor = %auth.device, target = %target.0, "admin: account enabled"); + Ok(StatusCode::NO_CONTENT) +} + +/// `DELETE /admin/users/{id}`: 202. Marks the account for removal; nothing it stored is deleted. +async fn remove_user( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result { + let target = user_id(id)?; + let req: AdminConfirm = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + let revoked = state.meta.admin_remove_user(&actor, target, state.now_ms()).await?; + end_live_connections(&state, &revoked); + tracing::info!(actor = %auth.device, target = %target.0, revoked = revoked.len(), "admin: account marked for removal"); + Ok(StatusCode::ACCEPTED) +} + +/// `POST /admin/users/{id}/invite`: a new invite for an account that has no password yet. +async fn issue_invite( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result, ApiError> { + let target = user_id(id)?; + let req: AdminConfirm = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + let code = new_code()?; + let issued = state.meta.admin_issue_invite(&actor, target, &code.hash(), state.now_ms(), INVITE_TTL_MS).await?; + tracing::info!(actor = %auth.device, target = %target.0, "admin: invite issued"); + Ok(Json(admin_code(&state, code, issued.expires_ms))) +} + +/// `DELETE /admin/users/{id}/invite`: 204. Ends the account's open invite or reset code. +async fn revoke_invite( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result { + let target = user_id(id)?; + let req: AdminConfirm = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + state.meta.admin_revoke_invite(&actor, target, state.now_ms()).await?; + tracing::info!(actor = %auth.device, target = %target.0, "admin: invite revoked"); + Ok(StatusCode::NO_CONTENT) +} + +/// `POST /admin/users/{id}/password-reset`: a code good for an hour. Redeemed at `POST /join`, it sets a new password, +/// signs every device of the account out, and signs the redeeming computer in. +async fn issue_reset( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result, ApiError> { + let target = user_id(id)?; + let req: AdminPasswordReset = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + let code = new_code()?; + let issued = + state.meta.admin_issue_reset(&actor, target, &code.hash(), req.reset_two_factor, state.now_ms(), craft_cloud_meta::RESET_TTL_MS).await?; + tracing::info!(actor = %auth.device, target = %target.0, reset_two_factor = req.reset_two_factor, "admin: password reset code issued"); + Ok(Json(admin_code(&state, code, issued.expires_ms))) +} + +/// `POST /admin/users/{id}/reset-two-factor`: 204. Takes the account's two-factor sign-in away and signs every device of +/// it out (for someone who lost their authenticator and their recovery codes). +async fn reset_two_factor( + State(state): State, + Extension(auth): Extension, + id: Result, PathRejection>, + request: Request, +) -> Result { + let target = user_id(id)?; + let req: AdminConfirm = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + let revoked = state.meta.admin_reset_two_factor(&actor, target, state.now_ms()).await?; + end_live_connections(&state, &revoked); + tracing::info!(actor = %auth.device, target = %target.0, revoked = revoked.len(), "admin: two-factor sign-in reset"); + Ok(StatusCode::NO_CONTENT) +} + +/// `DELETE /admin/users/{id}/devices/{device_id}`: 204. +async fn revoke_device( + State(state): State, + Extension(auth): Extension, + ids: Result, PathRejection>, + request: Request, +) -> Result { + let Path((target, device)) = ids?; + let req: AdminConfirm = read_body(&state, request).await?; + let actor = confirm(&state, &auth, Confirmation::new(req.password, req.totp_code, req.recovery_code)).await?; + state.meta.admin_revoke_device(&actor, UserId(target), device, state.now_ms()).await?; + state.live.revoke(device); + tracing::info!(actor = %auth.device, target = %target, %device, "admin: device revoked"); + Ok(StatusCode::NO_CONTENT) +} diff --git a/apps/craft-cloud-server/src/api.rs b/apps/craft-cloud-server/src/api.rs index efc5392f..e0a2fcf7 100644 --- a/apps/craft-cloud-server/src/api.rs +++ b/apps/craft-cloud-server/src/api.rs @@ -1,4 +1,6 @@ -//! The HTTP API, under `/api/v1`. Everything but `info`, `auth/login` and `join` needs a device token. `GET /live` +//! The HTTP API, under `/api/v1`. Everything but `info`, `auth/login`, `auth/login/second-factor` and `join` needs a device +//! token. The account and its devices' routes (`account`, `account/password`, renaming and signing out devices) are in +//! [`crate::account`], two-factor sign-in's in [`crate::two_factor`]. `GET /live` //! becomes the live service's WebSocket ([`crate::live`]); join codes (`pairings`, `join`, and the `/join/{code}` page //! outside the API) are in [`crate::join`]. @@ -13,7 +15,7 @@ use axum::http::{HeaderMap, StatusCode, header}; use axum::response::{IntoResponse, Response}; use axum::routing::{delete, get, post, put}; use axum::{Json, Router, middleware}; -use craft_cloud_meta::{BlobKind, DeviceAuth, valid_username}; +use craft_cloud_meta::{BlobKind, DeviceAuth, DeviceClient, DeviceRecord, NewChallenge, Origin, RevokeReason, SignIn, valid_username}; use craft_cloud_proto::api::{ CommitRequest, CommitResponse, DeviceInfo, InfoResponse, LoginRequest, LoginResponse, MAX_MISSING_IDS, MissingRequest, MissingResponse, PROTOCOL_MAX, PROTOCOL_MIN, SpaceResponse, codes, @@ -23,23 +25,28 @@ use craft_cloud_proto::{ChangeSet, ChunkId, DeviceId, Manifest, ManifestId, Node use craft_cloud_store::{BlobKey, StoreError}; use serde::Deserialize; +use crate::account; +use crate::audit; use crate::auth::{new_token, require_device, verify_password}; use crate::error::ApiError; use crate::join; use crate::ratelimit::{client_ip, ip_key}; use crate::state::AppState; +use crate::two_factor; /// Chunker parameter sets the server accepts. pub const CHUNKER_PARAMS: &[&str] = &["fastcdc-v1"]; /// Login attempts per account and per client address in five minutes. -const LOGINS_PER_USER: u32 = 10; -const LOGINS_PER_IP: u32 = 30; +pub(crate) const LOGINS_PER_USER: u32 = 10; +pub(crate) const LOGINS_PER_IP: u32 = 30; /// Longest change-feed long-poll. const MAX_WAIT_SECS: u64 = 60; /// `Retry-After` for a refused long-poll. const WAITERS_RETRY_SECS: u32 = 5; /// Small JSON bodies (login, join and missing-chunk queries). pub(crate) const SMALL_JSON_BYTES: usize = 1024 * 1024; +/// Tiny JSON bodies (a password change, a device's new name): a few short strings, so far less than a small body. +pub(crate) const TINY_JSON_BYTES: usize = 4 * 1024; /// `Retry-After` for a sign-in refused because every password check slot is busy. pub(crate) const PASSWORD_RETRY_SECS: u32 = 1; @@ -47,8 +54,17 @@ type Shared = Arc; pub fn router(state: Shared) -> Router { let signed_in = Router::new() + .route("/account", get(account::get_account)) + .route("/account/password", post(account::change_password).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/account/activity", get(audit::account_activity)) + .route("/account/two-factor", get(two_factor::get_status)) + .route("/account/two-factor/setup", post(two_factor::setup).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/account/two-factor/confirm", post(two_factor::confirm).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/account/two-factor/disable", post(two_factor::disable).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) + .route("/account/two-factor/recovery-codes", post(two_factor::recovery_codes).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) .route("/devices", get(list_devices)) - .route("/devices/{id}", delete(revoke_device)) + .route("/devices/revoke-others", post(account::revoke_others)) + .route("/devices/{id}", delete(revoke_device).patch(account::rename_device).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) .route("/pairings", post(join::create_pairing)) .route("/pairings/{id}", get(join::pairing_status).delete(join::delete_pairing)) .route("/chunks/missing", post(missing_chunks).layer(DefaultBodyLimit::max(SMALL_JSON_BYTES))) @@ -60,20 +76,27 @@ pub fn router(state: Shared) -> Router { .route("/tree", get(tree)) .route("/changes", get(changes)) .route("/live", get(crate::live::connect)) + // The admin routes carry `require_admin` themselves; `require_device` below wraps them too and runs first. + .merge(crate::admin::routes()) + .merge(audit::admin_routes()) .route_layer(middleware::from_fn_with_state(state.clone(), require_device)); let public = Router::new() .route("/info", get(info)) .route("/auth/login", post(login).layer(DefaultBodyLimit::max(SMALL_JSON_BYTES))) + .route("/auth/login/second-factor", post(two_factor::second_factor).layer(DefaultBodyLimit::max(TINY_JSON_BYTES))) .route("/join", post(join::join).layer(DefaultBodyLimit::max(SMALL_JSON_BYTES))); - Router::new() + let app = Router::new() .nest("/api/v1", public.merge(signed_in)) // Version 1 isn't known yet when a client first asks what the server speaks. .route("/api/info", get(info)) // A join link opened in a browser. .route("/join/{code}", get(join::join_page)) .fallback(no_such_endpoint) - .method_not_allowed_fallback(wrong_method) - .with_state(state) + .method_not_allowed_fallback(wrong_method); + // The admin pages exist only on a server that was set up for them. + let app = + if state.web.is_some() { app.nest("/web", crate::web::routes(state.clone())).merge(crate::web::trailing_slash(state.clone())) } else { app }; + app.with_state(state) } async fn no_such_endpoint() -> ApiError { @@ -109,6 +132,7 @@ async fn login( return Err(ApiError::invalid("usernames are 1-64 letters, digits, '.', '_' or '-'")); } check_device_name(&req.device_name)?; + let client = device_client(req.client_kind.as_deref(), req.platform.as_deref(), req.app_version.as_deref())?; // tokio's clock: the system's monotonic clock in the server, the simulated one in a simulation. let now = tokio::time::Instant::now().into_std(); let ip = client_ip(peer, &headers, &state.trusted_proxies); @@ -131,7 +155,10 @@ async fn login( return Err(ApiError::invalid("passwords are at most 1024 bytes")); } let user = state.meta.user_by_name(&username).await?; - let hash = user.as_ref().map_or(state.dummy_hash.as_str(), |u| u.password_hash.as_str()).to_owned(); + let known = user.as_ref().map(|u| u.id); + // An account without a password (an open invite) costs the same Argon2id check as a name nobody has, and never signs in. + let usable = user.as_ref().is_some_and(|u| crate::auth::usable_hash(&u.password_hash)); + let hash = user.as_ref().filter(|_| usable).map_or(state.dummy_hash.as_str(), |u| u.password_hash.as_str()).to_owned(); let password = req.password.clone(); let ok = tokio::task::spawn_blocking(move || { let _permit = permit; @@ -142,11 +169,29 @@ async fn login( tracing::error!(error = %e, "password check task failed"); ApiError::internal() })?; - let (Some(user), true) = (user, ok) else { + let (Some(user), true) = (user, ok && usable) else { + // Written after the answer is decided and in a transaction of its own: a log that cannot be written changes + // neither the answer nor the limits above. A name no account has is never put in the log. + audit::login_failed(&state, known).await; return Err(ApiError::new(StatusCode::UNAUTHORIZED, codes::UNAUTHORIZED, "wrong username or password")); }; let (token, token_hash) = new_token()?; - let device = state.meta.create_device(user.id, &req.device_name, &token_hash, state.now_ms()).await?; + // A challenge is made only if the account needs a second factor; the token the client gets for it is a secret of its own. + let (challenge_token, challenge_hash) = new_token()?; + let challenge = NewChallenge { id_hash: challenge_hash, client_key: ip_key(ip) }; + // Created only if the password just checked is still the account's: one changed meanwhile (and the devices + // signed out with it) must not be followed by a device that the old password let in. Answered as a wrong password. The + // account's two-factor state is read under the same lock: with it on, there is a challenge and no device. + let signed_in = + state.meta.sign_in(user.id, (&user.password_hash, user.auth_gen), &req.device_name, &client, &token_hash, &challenge, state.now_ms()).await?; + let device = match signed_in { + None => return Err(ApiError::new(StatusCode::UNAUTHORIZED, codes::UNAUTHORIZED, "wrong username or password")), + Some(SignIn::Device(device)) => device, + Some(SignIn::Challenge { expires_ms }) => { + tracing::info!("sign-in needs a second factor"); + return Err(ApiError::two_factor_required(challenge_token, expires_ms)); + } + }; // The username is the only personal identifier the server could log here: kept to debug. tracing::debug!(user = %user.username, "sign-in"); tracing::info!(%device, "device signed in"); @@ -162,6 +207,29 @@ pub(crate) fn check_device_name(name: &str) -> Result<(), ApiError> { Ok(()) } +/// The client fields of a sign-in or join, checked before anything is counted: an unknown `client_kind`, a field over +/// 32 bytes or a control character is `400 invalid`. The fields are only shown in the device list. +pub(crate) fn device_client(kind: Option<&str>, platform: Option<&str>, app_version: Option<&str>) -> Result { + Ok(DeviceClient::new(kind, platform, app_version)?) +} + +/// A device as the API shows it; `current` is the device making the request. +pub(crate) fn device_info(d: DeviceRecord, current: DeviceId) -> DeviceInfo { + DeviceInfo { + current: d.id == current, + id: d.id, + name: d.name, + created_ms: d.created_ms, + last_used_ms: d.last_used_ms, + revoked: d.revoked, + client_kind: d.client.kind, + platform: d.client.platform, + app_version: d.client.app_version, + revoked_ms: d.revoked_ms, + revoke_reason: d.revoke_reason.map(|r| r.as_str().to_owned()), + } +} + async fn space(State(state): State, Extension(auth): Extension) -> Result, ApiError> { let info = state.meta.space_info(auth.space).await?; Ok(Json(SpaceResponse { space: auth.space, root: info.root, epoch: info.epoch, seq: info.seq, username: auth.username })) @@ -169,19 +237,7 @@ async fn space(State(state): State, Extension(auth): Extension, Extension(auth): Extension) -> Result>, ApiError> { let devices = state.meta.list_devices(auth.user).await?; - Ok(Json( - devices - .into_iter() - .map(|d| DeviceInfo { - current: d.id == auth.device, - id: d.id, - name: d.name, - created_ms: d.created_ms, - last_used_ms: d.last_used_ms, - revoked: d.revoked, - }) - .collect(), - )) + Ok(Json(devices.into_iter().map(|d| device_info(d, auth.device)).collect())) } async fn revoke_device( @@ -190,7 +246,7 @@ async fn revoke_device( id: Result, PathRejection>, ) -> Result { let Path(id) = id?; - state.meta.revoke_device(auth.user, id).await?; + state.meta.revoke_device_by(auth.user, id, RevokeReason::User, Origin::Device(auth.device), state.now_ms()).await?; // Its live connections end now, not at their next request. state.live.revoke(id); Ok(StatusCode::NO_CONTENT) @@ -252,7 +308,7 @@ pub(crate) async fn read_json(state: &AppState, } } -fn body_too_slow() -> ApiError { +pub(crate) fn body_too_slow() -> ApiError { ApiError::new(StatusCode::REQUEST_TIMEOUT, codes::TIMEOUT, "the request body took too long to arrive") } @@ -409,6 +465,19 @@ async fn tree( Ok(Json(state.meta.tree(auth.space, cursor(q.after_seq, q.after_node)?, q.limit.unwrap_or(MAX_PAGE_NODES)).await?)) } +/// Resolves when `device` is revoked (or some revocations were missed, so that it must be looked up). +async fn revoked(revocations: &mut tokio::sync::broadcast::Receiver, device: DeviceId) { + use tokio::sync::broadcast::error::RecvError; + loop { + match revocations.recv().await { + Ok(revoked) if revoked == device => return, + Ok(_) => {} + Err(RecvError::Lagged(_)) => return, + Err(RecvError::Closed) => std::future::pending::<()>().await, + } + } +} + // The paging fields are written out here rather than flattened from `PageQuery`: serde's `flatten` buffers // values as strings, so numbers in a query string would never parse. #[derive(Debug, Deserialize)] @@ -441,14 +510,29 @@ async fn changes( // Subscribe before reading the sequence number, so a commit in between still wakes us. let mut updates = state.notifier.subscribe(auth.space); let mut stopping = state.notifier.shutdown_signal(); + // Listening for revocations first, then looking the device up: a revoke before the lookup is seen by it, one + // after it ends the wait, so none is missed between the sign-in check and the wait. + let mut revocations = state.live.revocations(); + if !state.meta.device_active(auth.device).await? { + return Err(ApiError::unauthorized()); + } if state.meta.space_info(auth.space).await?.seq == q.since { let deadline = Duration::from_secs(q.wait.min(MAX_WAIT_SECS)); - // Whichever comes first: a commit, the deadline, or the server shutting down. + // Whichever comes first: a commit, the deadline, the server shutting down, or this device signed out. tokio::select! { _ = tokio::time::timeout(deadline, updates.wait_for(|seq| *seq > q.since)) => {} _ = stopping.wait_for(|stop| *stop) => {} + () = revoked(&mut revocations, auth.device) => {} } } } - Ok(Json(state.meta.changes(auth.space, q.since, cursor, q.limit.unwrap_or(MAX_PAGE_NODES)).await?)) + let changes = state.meta.changes(auth.space, q.since, cursor, q.limit.unwrap_or(MAX_PAGE_NODES)).await?; + // A long-poll looks the device up once more after the fetch, the last thing before the answer: a device signed + // out while it waited or while the change set was read (by the API, or by the command line in another process, + // which wakes nothing) gets no change feed. A revoke that commits after this lookup is a sign-out that happened + // after the answer was decided. + if slot.is_some() && !state.meta.device_active(auth.device).await? { + return Err(ApiError::unauthorized()); + } + Ok(Json(changes)) } diff --git a/apps/craft-cloud-server/src/audit.rs b/apps/craft-cloud-server/src/audit.rs new file mode 100644 index 00000000..5571b9a3 --- /dev/null +++ b/apps/craft-cloud-server/src/audit.rs @@ -0,0 +1,486 @@ +//! The security audit log over HTTP: `GET /api/v1/account/activity` (the signed-in account's own events), and for +//! administrators `GET /api/v1/admin/audit` (the whole log, filtered) and `GET /api/v1/admin/audit/export` (the same as +//! NDJSON, streamed). +//! +//! - **Events are written by the changes they record**, in the transaction of the change (`craft_cloud_meta::audit`). +//! This module only reads, and writes the one event that is not part of a change: a failed sign-in +//! ([`login_failed`]), after the answer is decided, whose failure to be written never changes the answer. +//! - **Pages** are newest first in the order `(at_ms, id)` descending. `cursor` is opaque (base64url of that pair) and +//! exclusive: the next page starts strictly behind it, so events with one timestamp are neither repeated nor lost. +//! A cursor this server did not make is `400 invalid`. `cursor` wins over `before_ms`, which only limits a listing's +//! first call. `limit` over the route's most is `400 invalid` rather than quietly smaller. +//! - **The account's own activity** holds the events the account acted in or was the target of, and shows the device that +//! acted only when it is the account's own. +//! - **The export** is bounded in every way: pages of [`AuditExportLimits::page`] rows per query, a channel of +//! [`AuditExportLimits::channel`] pages between the database and the connection (so a slow client slows the reading +//! and does not fill memory), the end of the log fixed when it starts, at most [`AuditExportLimits::max_rows`] event +//! lines. The last line says how it ended: `{"complete":true,...}`, or `{"truncated":true,...}` when there were more +//! events than it holds. A database error in the middle ends the connection with an error and no last line, and a +//! client that goes away stops the reading at once. A client counts an export as whole only with the `complete` line. +//! - **Nothing is logged of the events.** A failure to write or read one is logged without its content. + +use std::future::Future; +use std::sync::Arc; + +use axum::Router; +use axum::body::{Body, Bytes}; +use axum::extract::rejection::QueryRejection; +use axum::extract::{Extension, Query, State}; +use axum::http::{StatusCode, header}; +use axum::response::Response; +use axum::routing::get; +use axum::{Json, middleware}; +use base64::Engine; +use base64::engine::general_purpose::URL_SAFE_NO_PAD; +use craft_cloud_meta::{ActionFilter, AuditCursor, AuditEvent, AuditFailure, AuditQuery, DeviceAuth, MetaError, Step, UserId}; +use craft_cloud_proto::api::{AuditDetails, AuditExportEnd, AuditPage}; +use serde::Deserialize; +use tokio::sync::mpsc; +use uuid::Uuid; + +use crate::auth::require_admin; +use crate::error::ApiError; +use crate::state::AppState; + +type Shared = Arc; + +/// Events one page of the account's activity holds when `limit` is not given, and the most it may hold. +const DEFAULT_PAGE: u32 = 50; +const ACCOUNT_MAX_PAGE: u32 = 100; +/// The same for the admin's list. +const ADMIN_MAX_PAGE: u32 = 200; +/// How an export is offered: always under this name, whatever the filters. +const EXPORT_DISPOSITION: &str = "attachment; filename=\"craft-cloud-audit.ndjson\""; + +/// How an export is cut. The defaults are the contract in `docs/design/sync.md`; tests make them smaller. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct AuditExportLimits { + /// Rows one database query reads. + pub page: u32, + /// Pages the channel to the connection holds. + pub channel: usize, + /// Event lines in an export; more events than that make it `truncated`. + pub max_rows: u64, +} + +impl Default for AuditExportLimits { + fn default() -> Self { + Self { page: 500, channel: 2, max_rows: 100_000 } + } +} + +/// The admin routes, behind `require_admin`; `crate::api::router` merges them into the signed-in routes, so +/// `require_device` runs first. +pub fn admin_routes() -> Router { + Router::new() + .route("/admin/audit", get(admin_list)) + .route("/admin/audit/export", get(admin_export)) + .route_layer(middleware::from_fn(require_admin)) + .route_layer(middleware::from_fn(crate::web::reject_mixed_credentials)) +} + +/// Writes the event of a sign-in that failed at its password, in a transaction of its own. `target` is the account the +/// guess was about, `None` for a name no account has (the log never holds a name that was typed). A failure is logged and +/// otherwise ignored: the answer to the client and the limits around it do not depend on the log. +pub(crate) async fn login_failed(state: &AppState, target: Option) { + record_login_failure(state, target, false).await; +} + +pub(crate) async fn web_login_failed(state: &AppState, target: Option) { + record_login_failure(state, target, true).await; +} + +async fn record_login_failure(state: &AppState, target: Option, web: bool) { + let failure = AuditFailure { action: craft_cloud_meta::AuditAction::AuthLoginFailed, target, step: Step::Password }; + let result = if web { + state.meta.audit_record_web_failure(failure, state.now_ms()).await + } else { + state.meta.audit_record_failure(failure, state.now_ms()).await + }; + if let Err(e) = result { + tracing::warn!(error = %e, "couldn't write an audit event"); + } +} + +/// Writes the event of a sign-in whose challenge was not good (unknown, expired, used, or out of tries), as +/// [`login_failed`] does. `target` is the account when the challenge was found, `None` when it was not (nothing a client sent +/// is stored). The wrong second factors themselves are written by the store, where the account is known. +pub(crate) async fn challenge_failed(state: &AppState, target: Option) { + record_challenge_failure(state, target, false).await; +} + +pub(crate) async fn web_challenge_failed(state: &AppState, target: Option) { + record_challenge_failure(state, target, true).await; +} + +async fn record_challenge_failure(state: &AppState, target: Option, web: bool) { + let failure = AuditFailure { action: craft_cloud_meta::AuditAction::AuthSecondFactorFailed, target, step: Step::ChallengeExpired }; + let result = if web { + state.meta.audit_record_web_failure(failure, state.now_ms()).await + } else { + state.meta.audit_record_failure(failure, state.now_ms()).await + }; + if let Err(e) = result { + tracing::warn!(error = %e, "couldn't write an audit event"); + } +} + +// ---- reading parameters ---- + +/// The query of the two lists and the export. Unknown parameters are ignored, as everywhere in the API. +#[derive(Debug, Default, Deserialize)] +pub(crate) struct Params { + pub(crate) cursor: Option, + before_ms: Option, + limit: Option, + pub(crate) user: Option, + pub(crate) action: Option, +} + +impl Params { + /// The same filters with the empty ones taken out: a form that was sent with a field left blank asks for no filter. + pub(crate) fn nonempty(&self) -> Params { + let keep = |field: &Option| field.clone().filter(|text| !text.is_empty()); + Params { cursor: keep(&self.cursor), before_ms: self.before_ms, limit: self.limit, user: keep(&self.user), action: keep(&self.action) } + } +} + +fn bad_cursor() -> ApiError { + ApiError::invalid("the cursor is not one this server gave") +} + +pub(crate) fn encode_cursor(cursor: AuditCursor) -> String { + URL_SAFE_NO_PAD.encode(cursor.to_bytes()) +} + +pub(crate) fn decode_cursor(text: &str) -> Result { + let bytes = URL_SAFE_NO_PAD.decode(text).map_err(|_| bad_cursor())?; + AuditCursor::from_bytes(&bytes).ok_or_else(bad_cursor) +} + +/// The limit of a list: 1 to `max`, `DEFAULT_PAGE` when not given. +fn page_limit(limit: Option, max: u32) -> Result { + let limit = limit.unwrap_or(DEFAULT_PAGE); + if (1..=max).contains(&limit) { Ok(limit) } else { Err(ApiError::invalid(format!("limit is 1 to {max}"))) } +} + +/// The `user` and `action` filters. A `user` is an account ID, an `action` one of the names or a prefix of them ending +/// in a dot like `auth.`; anything else is `400 invalid`, so nothing typed reaches a query. +pub(crate) fn filters(params: &Params) -> Result<(Option, Option), ApiError> { + let user = + params.user.as_deref().map(|text| Uuid::parse_str(text).map(UserId).map_err(|_| ApiError::invalid("user is an account ID"))).transpose()?; + let action = params + .action + .as_deref() + .map(|text| ActionFilter::parse(text).ok_or_else(|| ApiError::invalid("action is an event name like auth.login, or a prefix like auth."))) + .transpose()?; + Ok((user, action)) +} + +/// The event as the API shows it. `own`: an account whose own activity this is, which sees the device only of its own acts, and +/// the details of an event about another account (a former administrator's past act on someone else) not at all. +fn wire(event: AuditEvent, own: Option) -> craft_cloud_proto::api::AuditEvent { + let device_visible = own.is_none_or(|own| event.actor_user == Some(own)); + let about_another = own.is_some_and(|own| event.target_kind == "user" && event.target_id.is_some_and(|t| t != own.0)); + craft_cloud_proto::api::AuditEvent { + id: event.id, + at_ms: event.at_ms, + actor_user: event.actor_user.map(|u| u.0), + actor_device: if device_visible { event.actor_device } else { None }, + action: event.action.as_str().to_owned(), + target_kind: event.target_kind, + target_id: event.target_id, + outcome: event.outcome.as_str().to_owned(), + // Written by this code from closed lists; a row that does not read as such shows no details rather than failing the page. + details: if about_another { AuditDetails::default() } else { serde_json::from_str::(&event.details).unwrap_or_default() }, + count: event.count, + } +} + +/// One page: asks for one more event than `limit` to know whether there is a next one. +async fn page(state: &AppState, params: &Params, max: u32, restrict_to: Option) -> Result, ApiError> { + let limit = page_limit(params.limit, max)?; + let (user, action) = filters(params)?; + if params.before_ms.is_some_and(|before| before < 0) { + return Err(ApiError::invalid("before_ms is a time in milliseconds")); + } + let after = params.cursor.as_deref().map(decode_cursor).transpose()?; + let query = AuditQuery { + // An account sees nobody's events but its own, whatever the request names. + user: restrict_to.or(user), + action, + // The cursor is a position and wins over the time. + before_ms: if after.is_some() { None } else { params.before_ms }, + until_ms: None, + after, + limit: limit.saturating_add(1), + }; + let mut events = state.meta.audit_list(&query).await?; + let more = events.len() > limit as usize; + events.truncate(limit as usize); + let next = if more { events.last().map(|event| encode_cursor(event.cursor())) } else { None }; + Ok(Json(AuditPage { events: events.into_iter().map(|event| wire(event, restrict_to)).collect(), next })) +} + +/// `GET /api/v1/account/activity?cursor&before_ms&limit`: the signed-in account's own security events. +pub(crate) async fn account_activity( + State(state): State, + Extension(auth): Extension, + params: Result, QueryRejection>, +) -> Result, ApiError> { + let Query(params) = params?; + // The account sees events about itself; `user` and `action` are the admin's filters and are not read here. + let own = Params { user: None, action: None, ..params }; + page(&state, &own, ACCOUNT_MAX_PAGE, Some(auth.user)).await +} + +/// `GET /api/v1/admin/audit?cursor&before_ms&limit&user&action`. +async fn admin_list(State(state): State, params: Result, QueryRejection>) -> Result, ApiError> { + let Query(params) = params?; + page(&state, ¶ms, ADMIN_MAX_PAGE, None).await +} + +// ---- the export ---- + +/// One page of the export's reading: the events strictly behind `after`, at most `rows`. +type Fetch<'a> = + Box, u32) -> std::pin::Pin, MetaError>> + Send + 'a>> + Send + 'a>; + +/// Reads the log in pages with `fetch` and sends NDJSON to `tx`, a page at a time, ending with the line that says how it +/// ended. Returns without a last line when `fetch` fails (the error is sent, so the connection ends in error) or when the +/// receiver is gone (the reading stops there and then: it also stops a query that is under way). +async fn export_pages(mut fetch: Fetch<'_>, limits: AuditExportLimits, tx: mpsc::Sender>) { + let page = limits.page.max(1); + let mut after = None; + let mut exported = 0u64; + loop { + let rows = tokio::select! { + () = tx.closed() => return, + rows = fetch(after, page) => rows, + }; + let rows = match rows { + Ok(rows) => rows, + Err(e) => { + // The content of the error is the database's; the client only learns that the stream broke. + tracing::warn!(error = %e, "the audit export broke off"); + let _ = tx.send(Err(std::io::Error::other("the audit export broke off"))).await; + return; + } + }; + let more_in_log = rows.len() >= page as usize; + let room = limits.max_rows.saturating_sub(exported); + let take = usize::try_from(room).unwrap_or(usize::MAX).min(rows.len()); + let mut chunk = Vec::new(); + for event in rows.iter().take(take) { + // Serializing these plain structs cannot fail; if it did, the stream breaks like any other error. + let Ok(line) = serde_json::to_vec(&wire(event.clone(), None)) else { + let _ = tx.send(Err(std::io::Error::other("the audit export broke off"))).await; + return; + }; + chunk.extend_from_slice(&line); + chunk.push(b'\n'); + } + exported = exported.saturating_add(take as u64); + let cut = take < rows.len(); + if let Some(last) = take.checked_sub(1).and_then(|i| rows.get(i)) { + after = Some(last.cursor()); + } + // At the limit with a full page behind it there may be more, which one more row tells. + let at_limit = !cut && exported >= limits.max_rows && more_in_log; + let ended = if cut { + Some(false) + } else if !more_in_log { + Some(true) + } else if at_limit { + let probe = tokio::select! { + () = tx.closed() => return, + probe = fetch(after, 1) => probe, + }; + match probe { + Ok(rows) => Some(rows.is_empty()), + Err(e) => { + tracing::warn!(error = %e, "the audit export broke off"); + let _ = tx.send(Err(std::io::Error::other("the audit export broke off"))).await; + return; + } + } + } else { + None + }; + if let Some(complete) = ended { + let end = AuditExportEnd { complete: complete.then_some(true), truncated: (!complete).then_some(true), exported }; + if let Ok(line) = serde_json::to_vec(&end) { + chunk.extend_from_slice(&line); + chunk.push(b'\n'); + } + } + if !chunk.is_empty() && tx.send(Ok(Bytes::from(chunk))).await.is_err() { + return; + } + if ended.is_some() { + return; + } + } +} + +/// `GET /api/v1/admin/audit/export?user&action&before_ms`: the log as NDJSON (`before_ms` also limits it, besides the fixed end). +async fn admin_export(State(state): State, params: Result, QueryRejection>) -> Result { + let Query(params) = params?; + export_response(&state, params).await +} + +/// The export as a response, for the JSON route and the web pages' download alike: the same bounds, the same last line. +pub(crate) async fn export_response(state: &Shared, params: Params) -> Result { + let (user, action) = filters(¶ms)?; + if params.before_ms.is_some_and(|before| before < 0) { + return Err(ApiError::invalid("before_ms is a time in milliseconds")); + } + let before_ms = params.before_ms; + let limits = state.audit_export; + // The end of the log is fixed now: events that arrive while the export runs are not in it. + let until_ms = state.now_ms(); + let (tx, rx) = mpsc::channel(limits.channel.max(1)); + let reader = state.clone(); + let fetch: Fetch<'static> = Box::new(move |after, rows| { + let meta = reader.meta.clone(); + Box::pin(async move { meta.audit_list(&AuditQuery { user, action, before_ms, until_ms: Some(until_ms), after, limit: rows }).await }) + }); + tokio::spawn(export_pages(fetch, limits, tx)); + let body = Body::from_stream(futures_util::stream::unfold(rx, |mut rx| async move { rx.recv().await.map(|item| (item, rx)) })); + let mut response = Response::new(body); + *response.status_mut() = StatusCode::OK; + let headers = response.headers_mut(); + headers.insert(header::CONTENT_TYPE, header::HeaderValue::from_static("application/x-ndjson")); + headers.insert(header::CONTENT_DISPOSITION, header::HeaderValue::from_static(EXPORT_DISPOSITION)); + headers.insert(header::CACHE_CONTROL, header::HeaderValue::from_static("no-store")); + Ok(response) +} + +#[cfg(test)] +mod tests { + use std::sync::atomic::{AtomicUsize, Ordering}; + + use craft_cloud_meta::{AuditAction, AuditOutcome}; + + use super::*; + + fn event(n: i64) -> AuditEvent { + AuditEvent { + id: Uuid::from_u128(n as u128), + at_ms: 1000 - n, + actor_user: None, + actor_device: None, + action: AuditAction::AuthLogin, + target_kind: String::new(), + target_id: None, + outcome: AuditOutcome::Success, + details: "{}".to_owned(), + count: 1, + } + } + + /// A log of `total` events, newest first, read `rows` at a time; `fail_at` makes that call (counting from 0) fail. + fn fake_log(total: i64, calls: Arc, fail_at: Option) -> Fetch<'static> { + Box::new(move |after, rows| { + let call = calls.fetch_add(1, Ordering::SeqCst); + Box::pin(async move { + if fail_at == Some(call) { + return Err(MetaError::Corrupt("a test failure")); + } + let start = after.map_or(0, |c| 1000 - c.at_ms); + Ok((start + 1..=total).take(rows as usize).map(event).collect()) + }) + }) + } + + async fn run(fetch: Fetch<'static>, limits: AuditExportLimits) -> (Vec, bool) { + let (tx, mut rx) = mpsc::channel(limits.channel); + tokio::spawn(export_pages(fetch, limits, tx)); + let mut lines = Vec::new(); + let mut broke = false; + while let Some(item) = rx.recv().await { + match item { + Ok(bytes) => lines.extend(String::from_utf8(bytes.to_vec()).unwrap().lines().map(str::to_owned)), + Err(_) => broke = true, + } + } + (lines, broke) + } + + fn limits(page: u32, max_rows: u64) -> AuditExportLimits { + AuditExportLimits { page, channel: 1, max_rows } + } + + #[tokio::test] + async fn an_export_ends_with_complete_when_it_holds_every_event() { + for total in [0, 1, 4, 5, 6, 12] { + let (lines, broke) = run(fake_log(total, Arc::default(), None), limits(5, 100)).await; + assert!(!broke); + assert_eq!(lines.len() as i64, total + 1, "total {total}"); + assert_eq!(lines.last().unwrap(), &format!("{{\"complete\":true,\"exported\":{total}}}")); + for line in &lines[..lines.len() - 1] { + serde_json::from_str::(line).unwrap(); + } + } + } + + #[tokio::test] + async fn an_export_over_the_limit_ends_with_truncated_and_counts_only_events() { + // 12 events, at most 7 lines of events: truncated, in the middle of a page. + let (lines, broke) = run(fake_log(12, Arc::default(), None), limits(5, 7)).await; + assert!(!broke); + assert_eq!(lines.len(), 8); + assert_eq!(lines.last().unwrap(), r#"{"truncated":true,"exported":7}"#); + // Exactly at the limit with events behind it: truncated, found by the probe after a full page. + let (lines, _) = run(fake_log(12, Arc::default(), None), limits(5, 10)).await; + assert_eq!(lines.len(), 11); + assert_eq!(lines.last().unwrap(), r#"{"truncated":true,"exported":10}"#); + // Exactly the limit and nothing behind it: complete, not truncated (a full page, then the probe finds nothing). + let (lines, _) = run(fake_log(10, Arc::default(), None), limits(5, 10)).await; + assert_eq!(lines.last().unwrap(), r#"{"complete":true,"exported":10}"#); + // The limit is a multiple of nothing: a page ending short of it. + let (lines, _) = run(fake_log(7, Arc::default(), None), limits(5, 7)).await; + assert_eq!(lines.last().unwrap(), r#"{"complete":true,"exported":7}"#); + } + + #[tokio::test] + async fn a_database_error_in_the_middle_ends_the_stream_without_a_last_line() { + let (lines, broke) = run(fake_log(20, Arc::default(), Some(2)), limits(5, 100)).await; + assert!(broke); + assert_eq!(lines.len(), 10, "the two pages before the error arrived"); + assert!(lines.iter().all(|line| !line.contains("complete") && !line.contains("truncated"))); + } + + #[tokio::test] + async fn a_client_that_goes_away_stops_the_reading() { + let calls = Arc::new(AtomicUsize::new(0)); + let (tx, mut rx) = mpsc::channel(1); + let task = tokio::spawn(export_pages(fake_log(1_000_000, calls.clone(), None), limits(5, u64::MAX), tx)); + // The first page is read, and the client leaves. + assert!(rx.recv().await.is_some()); + drop(rx); + tokio::time::timeout(std::time::Duration::from_secs(10), task).await.unwrap().unwrap(); + let after_leaving = calls.load(Ordering::SeqCst); + // Reads in flight when it left may finish, the channel's depth at most; nothing goes on after that. + assert!(after_leaving <= 4, "{after_leaving}"); + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + assert_eq!(calls.load(Ordering::SeqCst), after_leaving); + } + + #[test] + fn cursors_are_opaque_and_checked() { + let cursor = AuditCursor { at_ms: 12, id: Uuid::now_v7() }; + assert_eq!(decode_cursor(&encode_cursor(cursor)).unwrap(), cursor); + for bad in ["", "!", "AAAA", &URL_SAFE_NO_PAD.encode([0u8; 23]), &URL_SAFE_NO_PAD.encode([0xffu8; 24]), &URL_SAFE_NO_PAD.encode([0u8; 25])] { + assert!(decode_cursor(bad).is_err(), "{bad}"); + } + } + + #[test] + fn a_limit_is_one_to_the_most_of_the_route() { + assert_eq!(page_limit(None, 100).unwrap(), 50); + assert_eq!(page_limit(Some(100), 100).unwrap(), 100); + assert!(page_limit(Some(101), 100).is_err()); + assert!(page_limit(Some(0), 100).is_err()); + } +} diff --git a/apps/craft-cloud-server/src/auth.rs b/apps/craft-cloud-server/src/auth.rs index 719e9cb3..356a8271 100644 --- a/apps/craft-cloud-server/src/auth.rs +++ b/apps/craft-cloud-server/src/auth.rs @@ -15,6 +15,7 @@ use axum::middleware::Next; use axum::response::Response; use base64::Engine; use base64::engine::general_purpose::URL_SAFE_NO_PAD; +use craft_cloud_meta::{DeviceAuth, Role}; use craft_cloud_proto::api::EPOCH_HEADER; use crate::error::ApiError; @@ -44,6 +45,13 @@ pub fn verify_password(password: &str, hash: &str) -> bool { } } +/// Whether a stored password hash can be verified at all. An account made by an invite has no password yet (an empty hash): a +/// sign-in must still spend a full Argon2id check on it (against the dummy hash, see [`AppState::dummy_hash`]) and then fail +/// whatever was typed, or the answer's timing tells a pending account from an unknown name. +pub fn usable_hash(hash: &str) -> bool { + PasswordHash::new(hash).is_ok() +} + /// A new device token and the hash to store for it. pub fn new_token() -> Result<(String, [u8; 32]), ApiError> { let mut raw = [0u8; 32]; @@ -86,6 +94,19 @@ pub async fn require_device(State(state): State>, mut request: Req Ok(response) } +/// Lets only an admin through, after [`require_device`] (it reads the device that layer put in the request extensions). +/// The role is the one `device_by_token` read from the database for this request, never cached, so a demoted admin +/// loses access with the next request. It is only the first check: every admin change checks the role again inside +/// its transaction. +pub async fn require_admin(request: Request, next: Next) -> Result { + match request.extensions().get::() { + Some(device) if device.role == Role::Admin => Ok(next.run(request).await), + Some(_) => Err(ApiError::forbidden("this needs an administrator account")), + // Not reachable behind `require_device`; a request without a device is not signed in. + None => Err(ApiError::unauthorized()), + } +} + impl AppState { /// Updates the device's `last used` time, at most once a minute (every write takes the SQLite write lock). async fn touch(&self, device: craft_cloud_proto::DeviceId, now: i64) { @@ -118,6 +139,13 @@ mod tests { assert!(!verify_password("correct horse", "not a hash")); } + #[test] + fn only_a_parseable_hash_is_usable() { + assert!(usable_hash(&hash_password("correct horse").unwrap())); + assert!(!usable_hash(""), "an invited account has no password yet"); + assert!(!usable_hash("not a hash")); + } + #[test] fn password_rules() { assert!(check_password_rules("short").is_err()); diff --git a/apps/craft-cloud-server/src/cli.rs b/apps/craft-cloud-server/src/cli.rs index a9b4908e..b9f35f7b 100644 --- a/apps/craft-cloud-server/src/cli.rs +++ b/apps/craft-cloud-server/src/cli.rs @@ -6,7 +6,7 @@ use std::net::{IpAddr, SocketAddr}; use std::path::{Path, PathBuf}; use clap::{Parser, Subcommand}; -use craft_cloud_meta::{Meta, Role, valid_username}; +use craft_cloud_meta::{ActionFilter, AuditQuery, Meta, Origin, RevokeReason, Role, valid_username}; use craft_cloud_proto::DeviceId; use craft_cloud_proto::join::{Code, INVITE_CHARS, INVITE_TTL_MS, JoinString}; use craft_cloud_store::Store; @@ -15,6 +15,7 @@ use craft_cloud_store::s3::S3Backend; use crate::auth::{check_password_rules, hash_password}; use crate::config::{Config, StorageConfig}; +use crate::secretbox::TwoFactorKey; use crate::state::now_ms; #[derive(Debug, Parser)] @@ -77,6 +78,30 @@ pub enum Command { #[arg(long)] all: bool, }, + /// Read and prune the security audit log. + Audit { + /// Path to config.toml. May come before or after the action. + #[arg(long, global = true)] + config: Option, + #[command(subcommand)] + action: AuditCommand, + }, + /// Clean up the admin pages' browser sessions. + WebSessions { + /// Path to config.toml. May come before or after the action. + #[arg(long, global = true)] + config: Option, + #[command(subcommand)] + action: WebSessionsCommand, + }, + /// Manage the key for two-factor secrets. + TwoFactor { + /// Path to config.toml. May come before or after the action. + #[arg(long, global = true)] + config: Option, + #[command(subcommand)] + action: TwoFactorAction, + }, /// Manage live documents. Live { /// Path to config.toml. May come before or after the action. @@ -87,6 +112,52 @@ pub enum Command { }, } +#[derive(Debug, Subcommand)] +pub enum AuditCommand { + /// Print the newest security events, newest first, as text. An event names accounts by ID, never by name. + List { + /// Only events this account acted in or was the target of. + #[arg(long)] + user: Option, + /// Only this event (like auth.login) or a group of them (like auth.). + #[arg(long)] + action: Option, + /// How many events at most (1 to 200). + #[arg(long, default_value_t = 50)] + limit: u32, + }, + /// Delete the events older than `[audit] retention_days` and say how many. Run it now and then (daily, from cron or + /// the task scheduler): the server also prunes when it writes an event, but not while no one signs in or changes anything. + Prune { + /// Say how many events would go and delete none. + #[arg(long)] + dry_run: bool, + }, +} + +#[derive(Debug, Subcommand)] +pub enum WebSessionsCommand { + /// Delete the browser sessions that are over (logged out, past `[web_admin] absolute_hours`, or unused for longer than + /// `idle_minutes`) and say how many. The server also deletes a few at each sign-in, but not while no one signs in; run this + /// now and then (daily, from cron or the task scheduler) to empty the table. A session that is still valid is never touched. + Prune { + /// Say how many sessions would go and delete none. + #[arg(long)] + dry_run: bool, + }, +} + +#[derive(Debug, Subcommand)] +pub enum TwoFactorAction { + /// Rewrite every two-factor secret that is sealed with the older key (`key_file_old`) under the current one + /// (`key_file`). To rotate the key: put the new key in `key_file` and the old one in `key_file_old`, run this, then + /// remove `key_file_old`. It can be run again, changes nothing when it is done, and refuses (changing nothing) when a + /// secret is sealed with a key that is neither. Prints counts, never a secret or a key. Restart every server instance on + /// the database with the new `key_file` and `key_file_old` first (a running server holds the keys it started with), and + /// remove `key_file_old` only after this reports that every secret is under the current key. + Rekey, +} + #[derive(Debug, Subcommand)] pub enum LiveAction { /// Empty a user's recents document of an app (one that is full: `quota_exceeded`), or, given a document's ID @@ -103,11 +174,26 @@ pub enum UserAction { #[arg(long)] admin: bool, }, - /// Set a new password, read from standard input. - ResetPassword { username: String }, + /// Set a new password, read from standard input. Every device of the account is signed out and its open invite + /// and add-a-computer codes are deleted, so a stolen device or code does not outlive the old password; with + /// `--keep-devices` the devices stay signed in. A device's live connection ends at its next ping (about 30 + /// seconds) and a change-feed long-poll within 60 seconds, since the server runs in another process. + ResetPassword { + username: String, + /// Leave the account's devices signed in. + #[arg(long)] + keep_devices: bool, + }, + /// Take away an account's two-factor sign-in, for someone who lost their authenticator and their recovery codes: its + /// secret, recovery codes and open sign-in challenges are deleted, the lock after wrong codes goes, **every device of the + /// account is signed out** and its open invite and add-a-computer codes are deleted. Prints how many devices were signed + /// out. The person signs in with their password again and can set it up anew. A device's live connection ends at its next + /// ping (about 30 seconds), since the server runs in another process. + #[command(name = "reset-2fa")] + Reset2fa { username: String }, /// Print an invite code for an account, good once for 7 days, to join a computer with in Craft Cloud. Creates the /// account (without a password, chosen when the invite is redeemed) if there is none, and replaces its earlier - /// invite. + /// invite or password reset code. Invite { username: String, /// Revoke the account's invite instead. @@ -118,8 +204,18 @@ pub enum UserAction { #[derive(Debug, Subcommand)] pub enum DeviceAction { - List { username: String }, - Revoke { username: String, device: DeviceId }, + List { + username: String, + }, + Revoke { + username: String, + device: DeviceId, + }, + /// Sign out every device of an account. Live connections end at their next ping (about 30 seconds), change-feed + /// long-polls within 60 seconds. + RevokeAll { + username: String, + }, } #[derive(Debug, thiserror::Error)] @@ -138,8 +234,11 @@ pub enum CliError { /// Opens the database and blob storage a config names. pub async fn open(config: &Config) -> Result<(Meta, Store), CliError> { - let meta = - Meta::open(&config.database.url).await?.with_stored_bytes_cap(config.stored_bytes_per_user).with_live_bytes_cap(config.live_bytes_per_user); + let meta = Meta::open(&config.database.url) + .await? + .with_stored_bytes_cap(config.stored_bytes_per_user) + .with_live_bytes_cap(config.live_bytes_per_user) + .with_audit_retention_days(config.audit.retention_days); let store = match (&config.storage, config.storage.s3()?) { (_, Some(s3)) => Store::new(S3Backend::new(&s3)?), (StorageConfig::Disk { path }, None) => Store::new(DiskBackend::new(path)?), @@ -190,6 +289,12 @@ pub async fn run(command: Command, input: &mut impl BufRead, out: &mut impl Writ writeln!(out, "revoked the invite for {}", username.to_ascii_lowercase())?; } UserAction::Invite { username, revoke: false } => invite(&meta, &config, &username, out).await?, + UserAction::Reset2fa { username } => { + let user = user_id(&meta, &username).await?; + let revoked = meta.reset_two_factor(user.id, now_ms()).await?; + tracing::info!(revoked = revoked.len(), "two-factor sign-in reset"); + writeln!(out, "two-factor sign-in of {} is off; signed out {} device(s)", user.username, revoked.len())?; + } UserAction::Add { username, admin } => { let password = read_password(input)?; let role = if admin { Role::Admin } else { Role::Member }; @@ -197,12 +302,17 @@ pub async fn run(command: Command, input: &mut impl BufRead, out: &mut impl Writ let user = meta.create_user(&username, &hash, role, now_ms()).await?; writeln!(out, "added {} ({})", user.username, if admin { "admin" } else { "member" })?; } - UserAction::ResetPassword { username } => { + UserAction::ResetPassword { username, keep_devices } => { let user = user_id(&meta, &username).await?; let password = read_password(input)?; let hash = hash_password(&password).map_err(|e| CliError::Usage(e.message))?; - meta.set_password_hash(user.id, &hash).await?; - writeln!(out, "password changed for {}", user.username)?; + let revoked = meta.reset_password(user.id, &hash, keep_devices, now_ms()).await?; + tracing::info!(revoked = revoked.len(), "password reset"); + if keep_devices { + writeln!(out, "password changed for {}; the devices stay signed in", user.username)?; + } else { + writeln!(out, "password changed for {}; signed out {} device(s)", user.username, revoked.len())?; + } } } Ok(()) @@ -218,12 +328,28 @@ pub async fn run(command: Command, input: &mut impl BufRead, out: &mut impl Writ } DeviceAction::Revoke { username, device } => { let user = user_id(&meta, &username).await?; - meta.revoke_device(user.id, device).await?; + meta.revoke_device_by(user.id, device, RevokeReason::Admin, Origin::Cli, now_ms()).await?; writeln!(out, "revoked {device}")?; } + DeviceAction::RevokeAll { username } => { + let user = user_id(&meta, &username).await?; + let revoked = meta.revoke_all_devices(user.id, RevokeReason::Admin, now_ms()).await?; + tracing::info!(revoked = revoked.len(), "all devices revoked"); + writeln!(out, "revoked {} device(s) of {}", revoked.len(), user.username)?; + } } Ok(()) } + Command::Audit { config, action } => { + let config = Config::load(&config_path(config)?)?; + let (meta, _) = open(&config).await?; + audit(&meta, &config, action, out).await + } + Command::WebSessions { config, action } => { + let config = Config::load(&config_path(config)?)?; + let (meta, _) = open(&config).await?; + web_sessions(&meta, &config, action, out).await + } Command::ResetEpoch { config, username, all } => { let (meta, _) = open(&Config::load(&config_path(config)?)?).await?; if all { @@ -241,6 +367,27 @@ pub async fn run(command: Command, input: &mut impl BufRead, out: &mut impl Writ } Ok(()) } + Command::TwoFactor { config, action: TwoFactorAction::Rekey } => { + let config = Config::load(&config_path(config)?)?; + let (meta, _) = open(&config).await?; + let keys = crate::two_factor::load_keys(&config).map_err(|e| CliError::Usage(e.to_string()))?; + let crate::two_factor::KeyState::Loaded(secret_box) = keys else { + return Err(CliError::Usage( + "there is no key: set [two_factor] key_file (and key_file_old for the key to rotate away from) in the config".into(), + )); + }; + let done = crate::two_factor::rekey(&meta, &secret_box).await.map_err(CliError::Usage)?; + writeln!(out, "rewrote {} secret(s) under the current key; {} were under it already", done.rewritten, done.already_current)?; + if secret_box.old_id().is_some() { + writeln!( + out, + "every secret is under the current key now. key_file_old can be removed once every server instance on this database has been \ + restarted with key_file (the new key) and key_file_old, as it must have been before this command: an instance still running with \ + only the old key would seal new secrets with it" + )?; + } + Ok(()) + } Command::Live { config, action: LiveAction::ResetDoc { username, app } } => { let (meta, _) = open(&Config::load(&config_path(config)?)?).await?; let user = user_id(&meta, &username).await?; @@ -259,6 +406,85 @@ pub async fn run(command: Command, input: &mut impl BufRead, out: &mut impl Writ } } +/// Sessions one batch of `web-sessions prune` deletes. +const WEB_SESSION_PRUNE_BATCH: u32 = 1000; + +/// `web-sessions prune`: the batches go on until none is left, each in a transaction of its own. +async fn web_sessions(meta: &Meta, config: &Config, action: WebSessionsCommand, out: &mut impl Write) -> Result<(), CliError> { + let idle_ms = config.web_admin.idle_ms(); + let WebSessionsCommand::Prune { dry_run } = action; + if dry_run { + let due = meta.web_sessions_count_over(now_ms(), idle_ms).await?; + writeln!(out, "{due} web session(s) are over; dry run, nothing deleted")?; + return Ok(()); + } + let mut deleted = 0u64; + loop { + let batch = meta.web_sessions_prune(now_ms(), idle_ms, WEB_SESSION_PRUNE_BATCH).await?; + deleted = deleted.saturating_add(batch); + if batch < u64::from(WEB_SESSION_PRUNE_BATCH) { + break; + } + } + tracing::info!(deleted, "web sessions pruned"); + writeln!(out, "deleted {deleted} web session(s) that were over")?; + Ok(()) +} + +/// `audit list` and `audit prune`. +async fn audit(meta: &Meta, config: &Config, action: AuditCommand, out: &mut impl Write) -> Result<(), CliError> { + match action { + AuditCommand::List { user, action, limit } => { + if !(1..=200).contains(&limit) { + return Err(CliError::Usage("--limit is 1 to 200".into())); + } + let user = match user { + Some(name) => Some(user_id(meta, &name).await?.id), + None => None, + }; + let action = match action { + Some(text) => Some( + ActionFilter::parse(&text) + .ok_or_else(|| CliError::Usage("--action is an event name like auth.login, or a group like auth.".into()))?, + ), + None => None, + }; + let events = meta.audit_list(&AuditQuery { user, action, limit, ..AuditQuery::default() }).await?; + let id = |id: Option| id.map_or_else(|| "-".to_owned(), |id| id.to_string()); + for e in &events { + // Only closed words, IDs and numbers: nothing here is a secret or a name anyone typed. + writeln!( + out, + "{} {} {} actor={} device={} target={}:{} count={} {}", + e.at_ms, + e.action.as_str(), + e.outcome.as_str(), + id(e.actor_user.map(|u| u.0)), + id(e.actor_device.map(|d| *d.as_uuid())), + if e.target_kind.is_empty() { "-" } else { e.target_kind.as_str() }, + id(e.target_id), + e.count, + e.details, + )?; + } + writeln!(out, "{} event(s)", events.len())?; + } + AuditCommand::Prune { dry_run } => { + let days = config.audit.retention_days; + let before = now_ms().saturating_sub(i64::from(days).saturating_mul(24 * 60 * 60 * 1000)); + if dry_run { + let due = meta.audit_count_before(before).await?; + writeln!(out, "{due} audit event(s) are older than {days} days; dry run, nothing deleted")?; + } else { + let deleted = meta.audit_prune(before).await?; + tracing::info!(deleted, "audit events pruned"); + writeln!(out, "deleted {deleted} audit event(s) older than {days} days")?; + } + } + } + Ok(()) +} + /// `user invite`: makes the invite and prints how to use it. The code is printed only here, never logged. async fn invite(meta: &Meta, config: &Config, username: &str, out: &mut impl Write) -> Result<(), CliError> { let code = Code::generate(INVITE_CHARS).map_err(|e| CliError::Usage(format!("the system random source failed: {e}")))?; @@ -320,7 +546,8 @@ async fn init( let blobs = without_verbatim_prefix(&dir.join("blobs").display().to_string()).to_owned(); let trusted: Vec = trusted_proxies.iter().map(|ip| format!("\"{ip}\"")).collect(); let config_text = format!( - "listen = \"{listen}\"\nbehind_proxy = {behind_proxy}\ntrusted_proxies = [{}]\n\n[database]\nurl = {}\n\n[storage]\nkind = \"disk\"\npath = {}\n", + "listen = \"{listen}\"\nbehind_proxy = {behind_proxy}\ntrusted_proxies = [{}]\n\n[database]\nurl = {}\n\n[storage]\nkind = \"disk\"\npath = {}\n\n\ + [two_factor]\nkey_file = \"{KEY_FILE_NAME}\"\n", trusted.join(", "), toml_string(&format!("sqlite:{db}")), toml_string(&blobs), @@ -335,12 +562,18 @@ async fn init( return Err(e); } writeln!(out, "created {}\nrun: craft-cloud-server serve --config {}", dir.display(), dir.join("config.toml").display())?; + writeln!( + out, + "two-factor key: {}. Keep a copy apart from your database backups: without it the two-factor secrets in a backup can't be opened.", + dir.join(KEY_FILE_NAME).display() + )?; Ok(()) } async fn create_files(dir: &Path, config_text: &str, config: &Config, admin: &str, hash: &str) -> Result<(), CliError> { create_data_dir(dir)?; write_config(&dir.join("config.toml"), config_text)?; + write_key_file(&dir.join(KEY_FILE_NAME))?; let (meta, _) = open(config).await?; meta.create_user(admin, hash, Role::Admin, now_ms()).await?; for name in ["meta.db", "meta.db-wal", "meta.db-shm"] { @@ -406,10 +639,28 @@ fn write_config(path: &Path, text: &str) -> Result<(), CliError> { Ok(()) } +/// The key file `init` makes, next to `config.toml`. +const KEY_FILE_NAME: &str = "two-factor.key"; + +/// Writes a new random key for two-factor secrets: 32 bytes, owner-only on Unix, and never over a file that is there. +fn write_key_file(path: &Path) -> Result<(), CliError> { + let key = TwoFactorKey::generate().map_err(|e| CliError::Usage(format!("the system random source failed: {e}")))?; + let mut options = std::fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let mut file = options.open(path)?; + file.write_all(key.to_file_bytes().as_slice())?; + Ok(()) +} + /// Removes what a failed `init` made. The directory was empty (or absent) when `init` started, so /// everything in it is `init`'s. Best effort: the original error is what the admin needs to see. fn remove_made(dir: &Path, created_dir: bool) { - for name in ["config.toml", "meta.db", "meta.db-wal", "meta.db-shm"] { + for name in ["config.toml", KEY_FILE_NAME, "meta.db", "meta.db-wal", "meta.db-shm"] { let _ = std::fs::remove_file(dir.join(name)); } let _ = std::fs::remove_dir_all(dir.join("blobs")); @@ -464,7 +715,7 @@ mod tests { let parent = tempfile::tempdir().unwrap(); let dir = parent.path().join("data"); std::fs::create_dir_all(dir.join("blobs")).unwrap(); - for name in ["config.toml", "meta.db", "meta.db-wal", "blobs/chunk"] { + for name in ["config.toml", "two-factor.key", "meta.db", "meta.db-wal", "blobs/chunk"] { std::fs::write(dir.join(name), b"x").unwrap(); } remove_made(&dir, true); diff --git a/apps/craft-cloud-server/src/config.rs b/apps/craft-cloud-server/src/config.rs index 725e9a48..88e8e1d3 100644 --- a/apps/craft-cloud-server/src/config.rs +++ b/apps/craft-cloud-server/src/config.rs @@ -9,6 +9,14 @@ //! # live_bytes_per_user = 1_073_741_824 # cap on what one account's live documents hold, in bytes (1 GiB) //! # public_url = "https://cloud.example.com" # where computers reach the server; `user invite` prints it //! +//! # [audit] +//! # retention_days = 365 # how long security events are kept (1 to 3650); `audit prune` deletes older ones +//! +//! # [web_admin] # the admin pages under /web, off unless enabled; needs behind_proxy and an https public_url +//! # enabled = true +//! # idle_minutes = 480 # a browser session ends after this long unused (5 to 1440) +//! # absolute_hours = 168 # and at the latest after this long (1 to 720) +//! //! [database] //! url = "sqlite:/var/lib/craft-cloud/meta.db" # or postgres://user:pass@host/db //! @@ -16,6 +24,10 @@ //! kind = "disk" //! path = "/var/lib/craft-cloud/blobs" //! # kind = "s3", bucket, prefix, region, endpoint, access_key_id, secret_access_key, allow_http +//! +//! [two_factor] # optional: without it, two-factor sign-in can't be set up +//! key_file = "two-factor.key" # 32 random bytes, made by `init`; relative to this file +//! # key_file_old = "old.key" # while rotating the key, see `two-factor rekey` //! ``` //! //! The server speaks plain HTTP. It refuses to listen anywhere but loopback unless `behind_proxy = true`, @@ -59,6 +71,20 @@ pub enum ConfigError { PublicUrl(String), #[error("storage.{0} is missing (set it in the config or the {1} environment variable)")] MissingS3Credential(&'static str, &'static str), + #[error("audit.retention_days = {0} is out of range: keep security events for {1} to {2} days")] + AuditRetention(u32, u32, u32), + #[error( + "web_admin.enabled = true needs public_url = \"https://host[:port]\" with no path: the browser's cookie is only sent over \ + HTTPS, and the pages check the Origin of every form against that address" + )] + WebAdminPublicUrl, + #[error( + "web_admin.enabled = true needs behind_proxy = true: the server speaks plain HTTP, and the session cookie is marked Secure, \ + so a TLS reverse proxy must stand in front of it" + )] + WebAdminNeedsProxy, + #[error("web_admin.{0} = {1} is out of range: {2} to {3}")] + WebAdminRange(&'static str, u32, u32, u32), } #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] @@ -90,14 +116,105 @@ pub struct Config { /// trailing slash. Unset: `user invite` prints the bare code and says to put the address in front. #[serde(default)] pub public_url: Option, + /// The security audit log. The section is optional and its defaults are safe. + #[serde(default)] + pub audit: AuditConfig, + /// The admin pages in the browser (`/web`). Off unless the section says `enabled = true`. + #[serde(default)] + pub web_admin: WebAdminConfig, pub database: DatabaseConfig, pub storage: StorageConfig, + /// The key for the secrets of two-factor sign-in. Without it two-factor sign-in can't be set up (`503 + /// two_factor_unavailable`), and a server whose database already holds secrets refuses to start. `init` makes the + /// key file and writes this section. The server never makes the key itself: a second server on the same database, + /// or a restore without the key, would otherwise seal secrets nobody can open. + #[serde(default)] + pub two_factor: Option, + /// The directory the config file is in, which `key_file` paths are relative to. Set by [`Config::load`], never read + /// from the file. + #[serde(skip)] + pub dir: Option, +} + +/// `[two_factor]`: where the key for two-factor secrets is. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct TwoFactorConfig { + /// A file of 32 random bytes (mode 0600; Windows has no equivalent here, keep it out of everyone's reach). A relative + /// path is relative to the directory of `config.toml`. + pub key_file: PathBuf, + /// The key the secrets were sealed with before a rotation: new secrets are sealed with `key_file`, old ones still + /// open with this one until `two-factor rekey` has rewritten them. Remove it after that. + #[serde(default)] + pub key_file_old: Option, } fn default_live_bytes() -> u64 { craft_cloud_meta::DEFAULT_LIVE_BYTES_PER_SPACE } +/// `[audit]`: how the security audit log is kept. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AuditConfig { + /// Days a security event is kept: `audit prune` deletes older ones, and a write does so now and then (at most once an + /// hour). 1 to 3650, default 365. A server that no one writes to keeps expired events until `audit prune` runs. + #[serde(default = "default_retention_days")] + pub retention_days: u32, +} + +impl Default for AuditConfig { + fn default() -> Self { + Self { retention_days: default_retention_days() } + } +} + +fn default_retention_days() -> u32 { + 365 +} + +/// `[web_admin]`: the admin pages in the browser and the lifetimes of their sessions. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct WebAdminConfig { + /// Serve the admin pages under `/web`. Default off: a server that was not told to speak to browsers does not. Needs + /// `behind_proxy = true` and an `https://` `public_url` without a path. + #[serde(default)] + pub enabled: bool, + /// A session ends when it was not used for this many minutes: 5 to 1440, default 480 (8 hours). + #[serde(default = "default_idle_minutes")] + pub idle_minutes: u32, + /// A session ends this many hours after sign-in, however busy it is: 1 to 720, default 168 (7 days). + #[serde(default = "default_absolute_hours")] + pub absolute_hours: u32, +} + +impl Default for WebAdminConfig { + fn default() -> Self { + Self { enabled: false, idle_minutes: default_idle_minutes(), absolute_hours: default_absolute_hours() } + } +} + +fn default_idle_minutes() -> u32 { + 8 * 60 +} + +fn default_absolute_hours() -> u32 { + 7 * 24 +} + +impl WebAdminConfig { + /// The idle lifetime of a session in milliseconds. + pub fn idle_ms(&self) -> i64 { + i64::from(self.idle_minutes) * 60_000 + } + + /// The absolute lifetime of a session in milliseconds. + pub fn absolute_ms(&self) -> i64 { + i64::from(self.absolute_hours) * 3_600_000 + } +} + #[derive(Clone, PartialEq, Eq, Deserialize)] #[serde(deny_unknown_fields)] pub struct DatabaseConfig { @@ -164,7 +281,19 @@ impl std::fmt::Debug for Secret { impl Config { pub fn load(path: &Path) -> Result { let text = std::fs::read_to_string(path).map_err(|source| ConfigError::Read { path: path.to_owned(), source })?; - Self::parse(&text) + let mut config = Self::parse(&text)?; + // A relative path to the config file has a parent too, possibly empty: that is the working directory. + config.dir = Some(path.parent().map(Path::to_owned).unwrap_or_default()); + Ok(config) + } + + /// `path` of a `[two_factor]` key file as the server opens it: relative paths are relative to the config file's + /// directory (when the config was loaded from a file). + pub fn key_path(&self, path: &Path) -> PathBuf { + match (&self.dir, path.is_absolute()) { + (Some(dir), false) => dir.join(path), + _ => path.to_owned(), + } } pub fn parse(text: &str) -> Result { @@ -186,10 +315,33 @@ impl Config { { return Err(ConfigError::StoredBytesCapTooSmall(cap, MAX_CHUNK_BYTES)); } + if !(craft_cloud_meta::MIN_RETENTION_DAYS..=craft_cloud_meta::MAX_RETENTION_DAYS).contains(&config.audit.retention_days) { + return Err(ConfigError::AuditRetention( + config.audit.retention_days, + craft_cloud_meta::MIN_RETENTION_DAYS, + craft_cloud_meta::MAX_RETENTION_DAYS, + )); + } + let web = &config.web_admin; + for (name, value, low, high) in [("idle_minutes", web.idle_minutes, 5, 1440), ("absolute_hours", web.absolute_hours, 1, 720)] { + if !(low..=high).contains(&value) { + return Err(ConfigError::WebAdminRange(name, value, low, high)); + } + } let mut config = config; if let Some(url) = &config.public_url { config.public_url = Some(public_url(url)?); } + if config.web_admin.enabled { + // The pages' address is an origin: https, a host and maybe a port, and no path to put in front of `/web`. + let origin_only = config.public_url.as_deref().and_then(|url| url.strip_prefix("https://")).is_some_and(|rest| !rest.contains('/')); + if !origin_only { + return Err(ConfigError::WebAdminPublicUrl); + } + if !config.behind_proxy { + return Err(ConfigError::WebAdminNeedsProxy); + } + } Ok(config) } } @@ -323,11 +475,79 @@ path = "/tmp/blobs" assert!(matches!(Config::parse(&no_proxy), Err(ConfigError::IgnoreForwardedForMisplaced))); } + #[test] + fn the_audit_section_is_optional_and_checked() { + assert_eq!(Config::parse(DISK).unwrap().audit.retention_days, 365, "a year by default"); + let with = |section: &str| Config::parse(&format!("{DISK}\n{section}\n")); + assert_eq!(with("[audit]").unwrap().audit.retention_days, 365); + assert_eq!(with("[audit]\nretention_days = 30").unwrap().audit.retention_days, 30); + for days in [1, 3650] { + assert_eq!(with(&format!("[audit]\nretention_days = {days}")).unwrap().audit.retention_days, days); + } + for days in ["0", "3651", "-1", "\"30\"", "1.5"] { + assert!(with(&format!("[audit]\nretention_days = {days}")).is_err(), "{days}"); + } + assert!(matches!(with("[audit]\nretention_days = 0"), Err(ConfigError::AuditRetention(0, 1, 3650)))); + assert!(matches!(with("[audit]\nkeep = 5"), Err(ConfigError::Parse(_))), "unknown keys are refused"); + } + + #[test] + fn the_web_admin_is_off_by_default_and_checked_when_on() { + let c = Config::parse(DISK).unwrap(); + assert_eq!(c.web_admin, WebAdminConfig { enabled: false, idle_minutes: 480, absolute_hours: 168 }, "off, 8 hours idle, 7 days at most"); + assert_eq!((c.web_admin.idle_ms(), c.web_admin.absolute_ms()), (8 * 3_600_000, 7 * 24 * 3_600_000)); + let proxied = DISK.replace("127.0.0.1:8080", "0.0.0.0:8080"); + let with = |top: &str, section: &str| Config::parse(&format!("{top}\n{proxied}\n[web_admin]\n{section}\n")); + let proxy = "behind_proxy = true\ntrusted_proxies = [\"10.0.0.1\"]"; + let good = format!("{proxy}\npublic_url = \"https://cloud.example.com\""); + let c = with(&good, "enabled = true\nidle_minutes = 30\nabsolute_hours = 12").unwrap(); + assert_eq!((c.web_admin.enabled, c.web_admin.idle_minutes, c.web_admin.absolute_hours), (true, 30, 12)); + // Each missing piece of the setup is refused with its own message. + assert!(matches!(with(proxy, "enabled = true"), Err(ConfigError::WebAdminPublicUrl)), "no public_url"); + for url in ["http://cloud.example.com", "https://cloud.example.com/cloud"] { + let bad = format!("{proxy}\npublic_url = \"{url}\""); + assert!(matches!(with(&bad, "enabled = true"), Err(ConfigError::WebAdminPublicUrl)), "{url}"); + } + let no_proxy = format!("public_url = \"https://cloud.example.com\"\n{DISK}\n[web_admin]\nenabled = true\n"); + assert!(matches!(Config::parse(&no_proxy), Err(ConfigError::WebAdminNeedsProxy))); + // Off, none of it matters; the lifetimes are always in range; unknown keys are errors. + assert!(!Config::parse(&format!("{DISK}\n[web_admin]\nenabled = false\n")).unwrap().web_admin.enabled); + for section in ["idle_minutes = 4", "idle_minutes = 1441", "absolute_hours = 0", "absolute_hours = 721"] { + assert!(matches!(Config::parse(&format!("{DISK}\n[web_admin]\n{section}\n")), Err(ConfigError::WebAdminRange(..))), "{section}"); + } + assert!(matches!(Config::parse(&format!("{DISK}\n[web_admin]\ncookie = 1\n")), Err(ConfigError::Parse(_)))); + } + #[test] fn unknown_keys_are_errors() { assert!(matches!(Config::parse(&format!("lisen = 1\n{DISK}")), Err(ConfigError::Parse(_)))); } + #[test] + fn the_two_factor_section_is_optional_and_strict() { + assert_eq!(Config::parse(DISK).unwrap().two_factor, None, "a config from before two-factor sign-in still loads"); + let with = |section: &str| Config::parse(&format!("{DISK}\n[two_factor]\n{section}\n")); + let c = with("key_file = \"two-factor.key\"").unwrap(); + assert_eq!(c.two_factor, Some(TwoFactorConfig { key_file: "two-factor.key".into(), key_file_old: None })); + let c = with("key_file = \"new.key\"\nkey_file_old = \"old.key\"").unwrap(); + assert_eq!(c.two_factor.unwrap().key_file_old, Some("old.key".into())); + assert!(matches!(with("key_file = \"k\"\nkey = \"x\""), Err(ConfigError::Parse(_))), "an unknown key in the section"); + assert!(matches!(with("key_file_old = \"old.key\""), Err(ConfigError::Parse(_))), "key_file is required"); + } + + #[test] + fn key_files_are_relative_to_the_config_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.toml"); + std::fs::write(&path, format!("{DISK}\n[two_factor]\nkey_file = \"two-factor.key\"\n")).unwrap(); + let c = Config::load(&path).unwrap(); + assert_eq!(c.key_path(Path::new("two-factor.key")), dir.path().join("two-factor.key")); + let absolute = dir.path().join("elsewhere.key"); + assert_eq!(c.key_path(&absolute), absolute); + // A config that was parsed from text has no directory: the path stays as it is. + assert_eq!(Config::parse(DISK).unwrap().key_path(Path::new("k")), PathBuf::from("k")); + } + #[test] fn s3_config_hides_its_secret() { let text = r#" diff --git a/apps/craft-cloud-server/src/error.rs b/apps/craft-cloud-server/src/error.rs index 73d5a18f..3cd71c8a 100644 --- a/apps/craft-cloud-server/src/error.rs +++ b/apps/craft-cloud-server/src/error.rs @@ -6,7 +6,7 @@ use axum::extract::rejection::{BytesRejection, JsonRejection, PathRejection, Que use axum::http::{HeaderValue, StatusCode, header}; use axum::response::{IntoResponse, Response}; use craft_cloud_meta::MetaError; -use craft_cloud_proto::api::{ErrorBody, QuotaUse, codes}; +use craft_cloud_proto::api::{ErrorBody, QuotaUse, TwoFactorChallenge, codes}; use craft_cloud_proto::{ChunkId, Conflict}; use craft_cloud_store::StoreError; @@ -21,11 +21,13 @@ pub struct ApiError { pub quota: Option>, /// Seconds for the `Retry-After` header. pub retry_after: Option, + /// The challenge of `two_factor_required`. Boxed for the same reason as `quota`. + pub two_factor: Option>, } impl ApiError { pub fn new(status: StatusCode, code: &'static str, message: impl Into) -> Self { - Self { status, code, message: message.into(), conflicts: None, missing: None, quota: None, retry_after: None } + Self { status, code, message: message.into(), conflicts: None, missing: None, quota: None, retry_after: None, two_factor: None } } /// `507 quota_exceeded`: the account would store more than the server lets it. @@ -36,6 +38,11 @@ impl ApiError { e } + /// `403 forbidden`: signed in, but not allowed to do this. + pub fn forbidden(message: impl Into) -> Self { + Self::new(StatusCode::FORBIDDEN, codes::FORBIDDEN, message) + } + pub fn invalid(message: impl Into) -> Self { Self::new(StatusCode::BAD_REQUEST, codes::INVALID, message) } @@ -69,6 +76,49 @@ impl ApiError { Self::new(StatusCode::BAD_REQUEST, codes::PASSWORD_REQUIRED, "this invite is for a new account: choose a password for it") } + /// `401 two_factor_required`: the password (or code) was right and the account needs its second factor. No token. + /// `challenge` is the secret to send back to `POST /auth/login/second-factor` (empty for `/join`, which has none). + pub fn two_factor_required(challenge: String, expires_ms: i64) -> Self { + let mut e = Self::new(StatusCode::UNAUTHORIZED, codes::TWO_FACTOR_REQUIRED, "this account needs a code from its authenticator app"); + e.two_factor = Some(Box::new(TwoFactorChallenge { challenge, expires_ms })); + e + } + + /// `403 two_factor_invalid`: a wrong code or recovery code on a signed-in route (never 401: on a signed-in route the + /// agent reads 401 as "this device was revoked"). [`ApiError::on_public_route`] makes it 401 for sign-in and join. + pub fn two_factor_invalid() -> Self { + Self::new(StatusCode::FORBIDDEN, codes::TWO_FACTOR_INVALID, "the code is wrong, or was used already") + } + + /// `429 two_factor_locked` with `Retry-After`. + pub fn two_factor_locked(retry_after_secs: u32) -> Self { + let mut e = Self::new( + StatusCode::TOO_MANY_REQUESTS, + codes::TWO_FACTOR_LOCKED, + "too many wrong codes: this account's second factor is locked for a while; try again later, or ask the server's admin", + ); + e.retry_after = Some(retry_after_secs); + e + } + + /// `503 two_factor_unavailable`: the server has no key for two-factor secrets. + pub fn two_factor_unavailable() -> Self { + Self::new( + StatusCode::SERVICE_UNAVAILABLE, + codes::TWO_FACTOR_UNAVAILABLE, + "this server has no key for two-factor secrets; the admin sets [two_factor] key_file in its config", + ) + } + + /// The answer on a route that is not signed in (sign-in's second step, join): a wrong second factor is `401` there, + /// where it is `403` on every signed-in route. + pub fn on_public_route(mut self) -> Self { + if self.code == codes::TWO_FACTOR_INVALID { + self.status = StatusCode::UNAUTHORIZED; + } + self + } + pub fn internal() -> Self { Self::new(StatusCode::INTERNAL_SERVER_ERROR, codes::INTERNAL, "the server hit an internal error; the admin can find details in its log") } @@ -95,6 +145,23 @@ impl From for ApiError { MetaError::NotFound(what) => Self::not_found(what), MetaError::InvalidCode => Self::invalid_code(), MetaError::PasswordRequired => Self::password_required(), + MetaError::DeviceRevoked => Self::unauthorized(), + MetaError::Forbidden(message) => Self::forbidden(message), + MetaError::LastAdmin => Self::new(StatusCode::CONFLICT, codes::LAST_ADMIN, e.to_string()), + MetaError::AccountDisabled => Self::new(StatusCode::FORBIDDEN, codes::ACCOUNT_DISABLED, "this account is disabled; ask an administrator"), + // The status of a wrong second factor depends on the route: 403 here (every signed-in route), made 401 by + // `on_public_route` for sign-in and join. + MetaError::TwoFactorInvalid => Self::two_factor_invalid(), + MetaError::TwoFactorLocked { retry_after_ms } => Self::two_factor_locked(craft_cloud_meta::retry_after_secs(retry_after_ms)), + MetaError::TwoFactorPending => { + Self::new(StatusCode::CONFLICT, codes::TWO_FACTOR_PENDING, "a two-factor set-up is open on another device: finish it there, or wait") + } + // Only the join route can see this and it builds its own answer; a signed-in route never asks a second factor. + MetaError::TwoFactorRequired => Self::new(StatusCode::UNAUTHORIZED, codes::TWO_FACTOR_REQUIRED, "this account needs its second factor"), + MetaError::ChallengeGone => { + Self::new(StatusCode::UNAUTHORIZED, codes::UNAUTHORIZED, "this sign-in step doesn't work any more: sign in again") + } + MetaError::Conflict(message) => Self::new(StatusCode::CONFLICT, codes::CONFLICT, message), MetaError::UsernameTaken => Self::new(StatusCode::CONFLICT, codes::USERNAME_TAKEN, "the username is taken"), MetaError::QuotaExceeded { used, limit } => Self::quota_exceeded(used, limit), MetaError::DocFull { .. } | MetaError::LiveFull { .. } | MetaError::TooManyDocs { .. } | MetaError::TooManyDocRows { .. } => { @@ -160,6 +227,7 @@ impl IntoResponse for ApiError { conflicts: self.conflicts, missing: self.missing, quota: self.quota.map(|q| *q), + two_factor: self.two_factor.map(|t| *t), }; let mut response = (self.status, Json(body)).into_response(); if let Some(secs) = self.retry_after { @@ -168,3 +236,42 @@ impl IntoResponse for ApiError { response } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_change_that_lost_a_race_is_a_conflict_and_a_signed_out_caller_is_unauthorized() { + let conflict = ApiError::from(MetaError::Conflict("changed meanwhile")); + assert_eq!((conflict.status, conflict.code), (StatusCode::CONFLICT, codes::CONFLICT)); + let revoked = ApiError::from(MetaError::DeviceRevoked); + assert_eq!((revoked.status, revoked.code), (StatusCode::UNAUTHORIZED, codes::UNAUTHORIZED)); + let forbidden = ApiError::from(MetaError::Forbidden("no")); + assert_eq!((forbidden.status, forbidden.code), (StatusCode::FORBIDDEN, codes::FORBIDDEN)); + let last = ApiError::from(MetaError::LastAdmin); + assert_eq!((last.status, last.code), (StatusCode::CONFLICT, codes::LAST_ADMIN)); + let disabled = ApiError::from(MetaError::AccountDisabled); + assert_eq!((disabled.status, disabled.code), (StatusCode::FORBIDDEN, codes::ACCOUNT_DISABLED)); + } + + #[test] + fn second_factor_errors_are_403_on_signed_in_routes_and_401_on_public_ones() { + let invalid = ApiError::from(MetaError::TwoFactorInvalid); + assert_eq!((invalid.status, invalid.code), (StatusCode::FORBIDDEN, codes::TWO_FACTOR_INVALID)); + let public = invalid.on_public_route(); + assert_eq!((public.status, public.code), (StatusCode::UNAUTHORIZED, codes::TWO_FACTOR_INVALID)); + let locked = ApiError::from(MetaError::TwoFactorLocked { retry_after_ms: 1500 }); + assert_eq!((locked.status, locked.code, locked.retry_after), (StatusCode::TOO_MANY_REQUESTS, codes::TWO_FACTOR_LOCKED, Some(2))); + let locked = locked.on_public_route(); + assert_eq!(locked.status, StatusCode::TOO_MANY_REQUESTS); + let pending = ApiError::from(MetaError::TwoFactorPending); + assert_eq!((pending.status, pending.code), (StatusCode::CONFLICT, codes::TWO_FACTOR_PENDING)); + assert_eq!(ApiError::two_factor_unavailable().status, StatusCode::SERVICE_UNAVAILABLE); + let gone = ApiError::from(MetaError::ChallengeGone); + assert_eq!((gone.status, gone.code), (StatusCode::UNAUTHORIZED, codes::UNAUTHORIZED)); + let required = ApiError::two_factor_required("challenge-secret".into(), 5); + assert_eq!((required.status, required.code), (StatusCode::UNAUTHORIZED, codes::TWO_FACTOR_REQUIRED)); + assert!(!format!("{required:?}").contains("challenge-secret"), "the challenge is hidden in Debug"); + } +} diff --git a/apps/craft-cloud-server/src/join.rs b/apps/craft-cloud-server/src/join.rs index 41d57bdc..0bf3266b 100644 --- a/apps/craft-cloud-server/src/join.rs +++ b/apps/craft-cloud-server/src/join.rs @@ -4,11 +4,18 @@ //! - **One answer for every code that doesn't work**: wrong, expired, used, malformed, or a pairing whose maker has //! signed out are all `404 invalid_code` with the same message, so an answer never says which. //! - **Failed redeems are limited per client address** (behind trusted proxies, as sign-ins are): [`JOINS_PER_IP`] -//! in a minute, then `429 rate_limited`, even for a code that works. Only `invalid_code` counts. A successful -//! redeem doesn't, and neither does `password_required` or `weak_password`: they are only answered for a real, -//! unused invite, so they tell a guesser nothing a success wouldn't. Every try is counted before the code is -//! looked at, and given back once it turns out not to be `invalid_code`, so tries sent at the same moment are held -//! to the limit as well. +//! in a minute, then `429 rate_limited`, even for a code that works. `invalid_code` and `two_factor_invalid` count. A +//! successful redeem doesn't, and neither does `password_required`, `weak_password` or `two_factor_required`: they are +//! only answered for a real, unused invite, so they tell a guesser nothing a success wouldn't. Every try is counted +//! before the code is looked at, and given back once it turns out not to be one of those two, so tries sent at the same +//! moment are held to the limit as well. +//! - **An account with two-factor sign-in** needs its second factor (`totp_code`, or `recovery_code`) to redeem an invite or +//! a password reset code, unless the reset code was made to turn it off. Without it the answer is `401 +//! two_factor_required` and the code is **not** used up: the same body is sent again with the factor (there is no +//! challenge for `/join`, so the answer's `challenge` is empty). A wrong factor is `401 two_factor_invalid`, leaves the +//! code as it was and counts against the account (and its lock) and, with the client address, against the +//! second-factor limiter. A pairing code needs no second factor: only a signed-in device can make one, and turning +//! two-factor sign-in on signs the account's other devices out. //! - **An invite to a new account** must carry a password (the server's password rules), hashed with Argon2id under //! the same server-wide slots as sign-ins; it becomes the account's in the transaction that creates the device. //! - **Codes are never logged**, and neither are tokens or passwords; the database keeps only codes' hashes. @@ -21,16 +28,17 @@ use axum::extract::rejection::PathRejection; use axum::extract::{ConnectInfo, Extension, Path, Request, State}; use axum::http::{HeaderMap, StatusCode, header}; use axum::response::IntoResponse; -use craft_cloud_meta::DeviceAuth; +use craft_cloud_meta::{CHALLENGE_TTL_MS, DeviceAuth, DeviceClient, MetaError}; use craft_cloud_proto::api::{JoinRequest, LoginResponse, PairingCreated, PairingStatus, codes}; use craft_cloud_proto::join::{Code, PAIRING_CHARS, PAIRING_TTL_MS}; use uuid::Uuid; -use crate::api::{PASSWORD_RETRY_SECS, SMALL_JSON_BYTES, check_device_name, read_json}; +use crate::api::{PASSWORD_RETRY_SECS, SMALL_JSON_BYTES, check_device_name, device_client, read_json}; use crate::auth::{check_password_rules, hash_password, new_token}; use crate::error::ApiError; use crate::ratelimit::{client_ip, ip_key}; use crate::state::AppState; +use crate::two_factor::{Prepared, ProofInput, SECOND_FACTORS_PER_IP, parse_proof}; /// Failed redeems one client address may make in a minute. pub const JOINS_PER_IP: u32 = 10; @@ -82,22 +90,50 @@ pub async fn join( ) -> Result, ApiError> { let req: JoinRequest = read_json(&state, request, SMALL_JSON_BYTES).await?; check_device_name(&req.device_name)?; + // Checked before the try is counted, as the device name is: a body that is refused for it costs nothing. + let client = device_client(req.client_kind.as_deref(), req.platform.as_deref(), req.app_version.as_deref())?; + let proof = parse_proof(req.totp_code.clone(), req.recovery_code.clone())?; // tokio's clock: the system's monotonic clock in the server, the simulated one in a simulation. let now = tokio::time::Instant::now().into_std(); let key = ip_key(client_ip(peer, &headers, &state.trusted_proxies)); // Counted before the redeem, so tries made at the same moment are held to the limit too; given back unless the // code didn't work. let Some(counted) = state.joins.count(&key, JOINS_PER_IP, now) else { - return Err(ApiError::rate_limited("too many tries with codes that don't work; wait a minute", None)); + return Err(limited("too many tries with codes that don't work; wait a minute", state.joins.retry_after(&key, now))); }; - let result = redeem(&state, &req).await; - if !result.as_ref().is_err_and(|e| e.code == codes::INVALID_CODE) { + // A second factor is also counted against the address's second-factor limiter: wrong ones stay counted. + let second = match proof { + Some(_) => match state.second_factors.count(&key, SECOND_FACTORS_PER_IP, now) { + Some(counted) => Some(counted), + None => { + state.joins.refund(&key, counted); + return Err(limited("too many wrong codes from this address; wait a few minutes", state.second_factors.retry_after(&key, now))); + } + }, + None => None, + }; + let result = redeem(&state, &req, &client, proof).await; + // Wrong codes (of the invite or of the second factor) stay counted; everything else is given back. + if !result.as_ref().is_err_and(|e| e.code == codes::INVALID_CODE || e.code == codes::TWO_FACTOR_INVALID) { state.joins.refund(&key, counted); } + if let Some(second) = second + && !result.as_ref().is_err_and(|e| e.code == codes::TWO_FACTOR_INVALID) + { + state.second_factors.refund(&key, second); + } result.map(Json) } -async fn redeem(state: &AppState, req: &JoinRequest) -> Result { +/// The answer for a join a limiter refuses: the same for every code (a real invite, a locked account's, a made-up one), with the +/// time until the limiter's window ends in `Retry-After`, so that the refusal tells a guesser nothing about codes or locks. +/// A lock is only told to a request the limiters let through (and counted), as at sign-in. +fn limited(message: &'static str, window: std::time::Duration) -> ApiError { + let secs = u32::try_from(window.as_secs().saturating_add(1)).unwrap_or(u32::MAX).max(1); + ApiError::rate_limited(message, Some(secs)) +} + +async fn redeem(state: &AppState, req: &JoinRequest, client: &DeviceClient, proof: Option) -> Result { let code = Code::parse_any(&req.code).ok_or_else(ApiError::invalid_code)?; let code_hash = code.hash(); let now_ms = state.now_ms(); @@ -110,8 +146,22 @@ async fn redeem(state: &AppState, req: &JoinRequest) -> Result None, }; let (token, token_hash) = new_token()?; - let redeemed = state.meta.redeem(&code_hash, &req.device_name, &token_hash, new_password_hash.as_deref(), now_ms).await?; - tracing::info!(device = %redeemed.device, "device joined with a code"); + let prepared = proof.map(|proof| proof.prepare(state, now_ms)); + let second_factor = prepared.as_ref().map(Prepared::proof); + let redeemed = state + .meta + .redeem_with_second_factor(&code_hash, &req.device_name, client, &token_hash, new_password_hash.as_deref(), second_factor.as_ref(), now_ms) + .await + .map_err(|e| match e { + // No challenge row for a join: the answer only says that the same code goes again with the second factor. + MetaError::TwoFactorRequired => ApiError::two_factor_required(String::new(), now_ms.saturating_add(CHALLENGE_TTL_MS)), + e => ApiError::from(e).on_public_route(), + })?; + // A reset code signed the account's other devices out: their live connections end now, not at their next request. + for device in &redeemed.revoked { + state.live.revoke(*device); + } + tracing::info!(device = %redeemed.device, revoked = redeemed.revoked.len(), "device joined with a code"); Ok(LoginResponse { device: redeemed.device, token }) } diff --git a/apps/craft-cloud-server/src/lib.rs b/apps/craft-cloud-server/src/lib.rs index 97ffd436..3a84fabc 100644 --- a/apps/craft-cloud-server/src/lib.rs +++ b/apps/craft-cloud-server/src/lib.rs @@ -2,7 +2,10 @@ //! (`craft-cloud-store`), the live service on a WebSocket ([`live`]), plus the admin command line. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod account; +pub mod admin; pub mod api; +pub mod audit; pub mod auth; pub mod cli; pub mod config; @@ -11,7 +14,11 @@ pub mod join; pub mod live; pub mod notify; pub mod ratelimit; +pub mod secretbox; pub mod state; +pub mod totp; +pub mod two_factor; +pub mod web; use std::net::SocketAddr; use std::sync::Arc; diff --git a/apps/craft-cloud-server/src/live.rs b/apps/craft-cloud-server/src/live.rs index 859d5c88..e01e966f 100644 --- a/apps/craft-cloud-server/src/live.rs +++ b/apps/craft-cloud-server/src/live.rs @@ -768,13 +768,19 @@ pub async fn connect( return Err(ApiError::rate_limited(format!("a device may hold at most {MAX_CONNECTIONS_PER_DEVICE} live connections"), Some(5))); } let slot = ConnectionSlot { hub: state.clone(), device: auth.device }; + // Listening for revocations before the device is looked up once more, and before the upgrade: a revoke between + // the sign-in check and here is seen by the lookup, one after it by the receiver, so none is missed. + let revocations = state.live.revocations(); + if !state.meta.device_active(auth.device).await? { + return Err(ApiError::unauthorized()); + } // No frame is over an update and its envelope (snapshots travel through the chunk API). Ok(upgrade.max_message_size(MAX_FRAME_BYTES).max_frame_size(MAX_FRAME_BYTES).on_upgrade(move |socket| async move { let _slot = slot; // Listening before the session reads anything, so no update falls between a read and the notification. let notes = state.live.listen(auth.space); let presence = state.live.listen_presence(auth.space); - serve(socket, Session::new(state, &auth), notes, presence).await; + serve(socket, Session::new(state, &auth), notes, presence, revocations).await; })) } @@ -801,9 +807,14 @@ async fn send_all(socket: &mut WebSocket, frames: &[ServerFrame]) -> bool { /// The connection loop: frames in, frames out, the log pumped while a document has news, pings, and an end on /// shutdown, silence or a broken connection. -async fn serve(mut socket: WebSocket, mut session: Session, mut notes: broadcast::Receiver, mut presence: broadcast::Receiver) { +async fn serve( + mut socket: WebSocket, + mut session: Session, + mut notes: broadcast::Receiver, + mut presence: broadcast::Receiver, + mut revocations: broadcast::Receiver, +) { let mut stopping = session.state.notifier.shutdown_signal(); - let mut revocations = session.state.live.revocations(); let mut last_heard = Instant::now(); let mut ping = tokio::time::interval_at(Instant::now() + PING_EVERY, PING_EVERY); let hello_by = Instant::now() + HELLO_TIMEOUT; diff --git a/apps/craft-cloud-server/src/main.rs b/apps/craft-cloud-server/src/main.rs index 99384030..00f15ecd 100644 --- a/apps/craft-cloud-server/src/main.rs +++ b/apps/craft-cloud-server/src/main.rs @@ -5,6 +5,7 @@ use std::process::ExitCode; use clap::Parser; use craft_cloud_server::cli::{Cli, Command, open}; use craft_cloud_server::config::Config; +use craft_cloud_server::web::WebSettings; use craft_cloud_server::{AppState, serve}; #[tokio::main] @@ -35,9 +36,21 @@ async fn run_server(config_path: &std::path::Path) -> Result<(), String> { tracing::warn!("ignore_forwarded_for is set: every client shares one address for sign-in rate limits"); } let (meta, store) = open(&config).await.map_err(|e| e.to_string())?; - let state = AppState::new(meta, store, config.trusted_proxies.clone()).map_err(|e| e.message)?; + // Fail closed: a database that holds two-factor secrets the key can't open is not served. The server never makes the key. + let keys = craft_cloud_server::two_factor::load_keys(&config).map_err(|e| e.to_string())?; + let secrets = craft_cloud_server::two_factor::check_startup(&meta, keys, &config, craft_cloud_server::state::now_ms()).await?; + // `Config::load` has checked the setup the pages need (an https public_url, a proxy in front). + let web = match (config.web_admin.enabled, &config.public_url) { + (true, Some(public_url)) => Some(WebSettings::new(public_url, config.web_admin.idle_ms(), config.web_admin.absolute_ms())?), + _ => None, + }; + let state = AppState::new(meta, store, config.trusted_proxies.clone()) + .map_err(|e| e.message)? + .with_public_url(config.public_url.clone()) + .with_web_admin(web) + .with_two_factor(secrets); let listener = tokio::net::TcpListener::bind(config.listen).await.map_err(|e| format!("can't listen on {}: {e}", config.listen))?; - tracing::info!(listen = %config.listen, behind_proxy = config.behind_proxy, "Craft Cloud server running"); + tracing::info!(listen = %config.listen, behind_proxy = config.behind_proxy, web_admin = config.web_admin.enabled, "Craft Cloud server running"); let shutdown = async { wait_for_stop_signal().await; tracing::info!("shutting down"); diff --git a/apps/craft-cloud-server/src/ratelimit.rs b/apps/craft-cloud-server/src/ratelimit.rs index 27e5b4ad..1a9f33c9 100644 --- a/apps/craft-cloud-server/src/ratelimit.rs +++ b/apps/craft-cloud-server/src/ratelimit.rs @@ -32,14 +32,16 @@ impl Limiter { Self { windows: Mutex::new(Windows { entries: HashMap::new(), last_sweep: None }), period, capacity } } - /// Counts one attempt for `key`; false once `max` attempts were made in the current window, or when + /// Counts one attempt for `key`; false once `max` attempts were counted in the current window, or when /// the key is new and the limiter is full. pub fn allow(&self, key: &str, max: u32, now: Instant) -> bool { self.count(key, max, now).is_some() } /// [`Limiter::allow`] for a caller that may give the attempt back: the window it was counted in, for - /// [`Limiter::refund`], or `None` when it is refused. + /// [`Limiter::refund`], or `None` when it is refused. A refused attempt is not counted: the count never goes past + /// `max`, so refused tries made at the same moment as a counted one cannot make that one's refund (or the window's + /// end) leave the key with more counted attempts than were accepted. pub fn count(&self, key: &str, max: u32, now: Instant) -> Option { let mut windows = self.windows.lock().unwrap_or_else(std::sync::PoisonError::into_inner); let sweep_due = windows.last_sweep.is_none_or(|last| now.saturating_duration_since(last) >= self.period); @@ -55,8 +57,18 @@ impl Limiter { if now.saturating_duration_since(entry.0) >= self.period { *entry = (now, 0); } - entry.1 = entry.1.saturating_add(1); - (entry.1 <= max).then_some(Counted(entry.0)) + if entry.1 >= max { + return None; + } + entry.1 += 1; + Some(Counted(entry.0)) + } + + /// How long until `key`'s current window ends (when its count starts again from nothing): what a refused caller is + /// told in `Retry-After`. A key that has no window running is `Duration::ZERO`. + pub fn retry_after(&self, key: &str, now: Instant) -> Duration { + let windows = self.windows.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + windows.entries.get(key).map_or(Duration::ZERO, |(start, _)| (*start + self.period).saturating_duration_since(now)) } /// Gives back one attempt [`Limiter::count`] counted for `key`, if the window it was counted in is still the @@ -145,6 +157,21 @@ mod tests { /// An attempt counted near the end of a window whose redeem finishes in the next one: its refund would give the /// new window one try more than the limit. + #[test] + fn a_refused_attempt_is_not_counted_so_a_refund_frees_exactly_one_place() { + let limiter = Limiter::new(Duration::from_secs(60)); + let t0 = Instant::now(); + let first = limiter.count("user:a", 2, t0).unwrap(); + let _second = limiter.count("user:a", 2, t0).unwrap(); + // Tries made while the key is full are refused, however many there are. + for _ in 0..10 { + assert!(limiter.count("user:a", 2, t0).is_none()); + } + limiter.refund("user:a", first); + assert!(limiter.count("user:a", 2, t0).is_some(), "the refund freed one place"); + assert!(limiter.count("user:a", 2, t0).is_none(), "and only one"); + } + #[test] fn a_refund_never_crosses_into_a_later_window() { let limiter = Limiter::new(Duration::from_secs(60)); diff --git a/apps/craft-cloud-server/src/secretbox.rs b/apps/craft-cloud-server/src/secretbox.rs new file mode 100644 index 00000000..e2685f1b --- /dev/null +++ b/apps/craft-cloud-server/src/secretbox.rs @@ -0,0 +1,279 @@ +//! Sealing the TOTP secrets that the database keeps. +//! +//! - **The key** is 32 random bytes in a file the server's config names (`[two_factor] key_file`, see +//! [`crate::two_factor`]). It is never used as it is: the AES-256 key is [`blake3::derive_key`] of it under a fixed +//! context. A key shows in no log, no `Debug` output and no error message, and its bytes are wiped when it is dropped. +//! - **A sealed secret** is one byte naming the key (see below), a 12-byte random nonce and the AES-256-GCM ciphertext +//! with its tag. The associated data is `craft-cloud:totp:v1:` and the 16 bytes of the account's ID, so a ciphertext +//! copied to another account does not open. +//! - **The key's name** is the first byte of the BLAKE3 hash of the key file's bytes: one of 256 values, only there to tell +//! the current key from the older one, so two keys that share one are refused when the server starts. It does not +//! identify a key, and the authentication tag refuses a wrong key whatever the name says. With a second, older key (`key_file_old`) configured, the +//! name picks the key to open a secret with; sealing always uses the current key. The `two-factor rekey` command +//! rewrites the secrets under the current key, after which the old key can go. +//! - **Errors say nothing about their input**: not the key, not the plaintext, not the ciphertext. + +use std::io::Read; +use std::path::{Path, PathBuf}; + +use aes_gcm::aead::{Aead, KeyInit, Payload}; +use aes_gcm::{Aes256Gcm, Nonce}; +use craft_cloud_meta::UserId; +use zeroize::Zeroizing; + +/// The bytes a key file holds. +pub const KEY_BYTES: usize = 32; +const NONCE_BYTES: usize = 12; +const TAG_BYTES: usize = 16; +const KDF_CONTEXT: &str = "craft-cloud 2026 totp secret v1"; +const AAD_PREFIX: &[u8] = b"craft-cloud:totp:v1:"; + +/// A secret could not be sealed or opened. Carries nothing of what was sealed or opened. +#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] +#[error("a two-factor secret could not be sealed or opened")] +pub struct SecretBoxError; + +/// A key file that cannot be used. The message names the file and what is wrong with it, never its content. +#[derive(Debug, thiserror::Error)] +pub enum KeyError { + #[error("the two-factor key file {path} does not exist")] + Missing { path: PathBuf }, + #[error("can't read the two-factor key file {path}: {source}")] + Read { path: PathBuf, source: std::io::Error }, + #[error("the two-factor key file {path} must hold exactly 32 bytes")] + Length { path: PathBuf }, + #[error( + "key_file and key_file_old are the same key, or two keys that share a name: make a new key for key_file (the first byte of the keys' BLAKE3 \ + hashes must differ)" + )] + SameName, +} + +/// A key as read from a key file. `Debug` shows nothing of it. +pub struct TwoFactorKey { + bytes: Zeroizing<[u8; KEY_BYTES]>, + id: u8, +} + +impl std::fmt::Debug for TwoFactorKey { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("TwoFactorKey()") + } +} + +impl TwoFactorKey { + /// A key from its 32 bytes. + pub fn from_bytes(bytes: [u8; KEY_BYTES]) -> Self { + let id = blake3::hash(&bytes).as_bytes().first().copied().unwrap_or(0); + Self { bytes: Zeroizing::new(bytes), id } + } + + /// A new random key (for `init`). + pub fn generate() -> Result { + let mut bytes = [0u8; KEY_BYTES]; + getrandom::fill(&mut bytes)?; + Ok(Self::from_bytes(bytes)) + } + + /// The key file's bytes: only for writing a new key file. + pub fn to_file_bytes(&self) -> Zeroizing<[u8; KEY_BYTES]> { + self.bytes.clone() + } + + /// Reads a key file: exactly [`KEY_BYTES`] bytes (a longer file is not read further). + pub fn from_file(path: &Path) -> Result { + let mut file = std::fs::File::open(path).map_err(|source| match source.kind() { + std::io::ErrorKind::NotFound => KeyError::Missing { path: path.to_owned() }, + _ => KeyError::Read { path: path.to_owned(), source }, + })?; + let mut bytes = Zeroizing::new(Vec::with_capacity(KEY_BYTES + 1)); + (&mut file).take(KEY_BYTES as u64 + 1).read_to_end(&mut bytes).map_err(|source| KeyError::Read { path: path.to_owned(), source })?; + let bytes: [u8; KEY_BYTES] = bytes.as_slice().try_into().map_err(|_| KeyError::Length { path: path.to_owned() })?; + Ok(Self::from_bytes(bytes)) + } + + /// The name sealed secrets carry for this key. + pub fn id(&self) -> u8 { + self.id + } +} + +struct KeyEntry { + id: u8, + cipher: Aes256Gcm, +} + +impl KeyEntry { + fn new(key: &TwoFactorKey) -> Result { + let derived = Zeroizing::new(blake3::derive_key(KDF_CONTEXT, key.bytes.as_slice())); + let cipher = Aes256Gcm::new_from_slice(derived.as_slice()).map_err(|_| SecretBoxError)?; + Ok(Self { id: key.id, cipher }) + } +} + +/// Seals and opens TOTP secrets with the current key and, for reading, an older one. +pub struct SecretBox { + current: KeyEntry, + old: Option, +} + +impl std::fmt::Debug for SecretBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("SecretBox()") + } +} + +fn aad(user: UserId) -> Vec { + let mut aad = Vec::with_capacity(AAD_PREFIX.len() + 16); + aad.extend_from_slice(AAD_PREFIX); + aad.extend_from_slice(user.0.as_bytes()); + aad +} + +impl SecretBox { + /// A box for `current` and, optionally, the `old` key secrets are still sealed with. Two keys with one name are + /// refused. + pub fn new(current: &TwoFactorKey, old: Option<&TwoFactorKey>) -> Result { + if old.is_some_and(|old| old.id == current.id) { + return Err(KeyError::SameName); + } + let entry = |key: &TwoFactorKey| KeyEntry::new(key).map_err(|_| KeyError::SameName); + Ok(Self { current: entry(current)?, old: old.map(entry).transpose()? }) + } + + /// The name of the current key. + pub fn current_id(&self) -> u8 { + self.current.id + } + + /// The name of the older key, if one is configured. + pub fn old_id(&self) -> Option { + self.old.as_ref().map(|old| old.id) + } + + /// The name of the key a sealed secret was sealed with (`None` for something too short to be one). + pub fn key_id(sealed: &[u8]) -> Option { + (sealed.len() >= 1 + NONCE_BYTES + TAG_BYTES).then(|| sealed.first().copied()).flatten() + } + + /// Seals `plaintext` for `user` with the current key and a fresh random nonce. + pub fn seal(&self, user: UserId, plaintext: &[u8]) -> Result, SecretBoxError> { + let mut nonce = [0u8; NONCE_BYTES]; + getrandom::fill(&mut nonce).map_err(|_| SecretBoxError)?; + let ciphertext = + self.current.cipher.encrypt(Nonce::from_slice(&nonce), Payload { msg: plaintext, aad: &aad(user) }).map_err(|_| SecretBoxError)?; + let mut sealed = Vec::with_capacity(1 + NONCE_BYTES + ciphertext.len()); + sealed.push(self.current.id); + sealed.extend_from_slice(&nonce); + sealed.extend_from_slice(&ciphertext); + Ok(sealed) + } + + /// Opens a secret sealed for `user`. A secret of another account, a damaged or truncated one, one sealed with a key this + /// box doesn't hold: all the same error. + pub fn open(&self, user: UserId, sealed: &[u8]) -> Result>, SecretBoxError> { + let id = Self::key_id(sealed).ok_or(SecretBoxError)?; + let entry = if id == self.current.id { &self.current } else { self.old.as_ref().filter(|old| old.id == id).ok_or(SecretBoxError)? }; + let (nonce, ciphertext) = sealed.get(1..).and_then(|rest| rest.split_at_checked(NONCE_BYTES)).ok_or(SecretBoxError)?; + let plaintext = entry.cipher.decrypt(Nonce::from_slice(nonce), Payload { msg: ciphertext, aad: &aad(user) }).map_err(|_| SecretBoxError)?; + Ok(Zeroizing::new(plaintext)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn user() -> UserId { + UserId(uuid::Uuid::now_v7()) + } + + fn key(byte: u8) -> TwoFactorKey { + TwoFactorKey::from_bytes([byte; KEY_BYTES]) + } + + #[test] + fn a_sealed_secret_opens_again() { + let sb = SecretBox::new(&key(1), None).unwrap(); + let ana = user(); + let sealed = sb.seal(ana, b"12345678901234567890").unwrap(); + assert_eq!(sb.open(ana, &sealed).unwrap().as_slice(), b"12345678901234567890"); + assert_eq!(sealed.first().copied(), Some(key(1).id())); + assert_eq!(sealed.len(), 1 + NONCE_BYTES + 20 + TAG_BYTES); + assert_ne!(sealed, sb.seal(ana, b"12345678901234567890").unwrap(), "a fresh nonce each time"); + assert!(!sealed.windows(20).any(|w| w == b"12345678901234567890"), "no plaintext in the ciphertext"); + } + + #[test] + fn a_wrong_key_another_account_or_a_damaged_ciphertext_fails() { + let sb = SecretBox::new(&key(1), None).unwrap(); + let (ana, bob) = (user(), user()); + let sealed = sb.seal(ana, b"secret").unwrap(); + assert_eq!(sb.open(bob, &sealed), Err(SecretBoxError), "the account is bound in"); + let other = SecretBox::new(&key(2), None).unwrap(); + assert_eq!(other.open(ana, &sealed), Err(SecretBoxError), "wrong key"); + // A key with the same name but other bytes fails on the tag. + let mut bytes = [1u8; KEY_BYTES]; + let mut same_name = None; + for i in 0..=255u8 { + bytes[0] = i; + if TwoFactorKey::from_bytes(bytes).id() == key(1).id() && bytes != [1u8; KEY_BYTES] { + same_name = Some(TwoFactorKey::from_bytes(bytes)); + break; + } + } + if let Some(same_name) = same_name { + assert_eq!(SecretBox::new(&same_name, None).unwrap().open(ana, &sealed), Err(SecretBoxError)); + } + for cut in [0, 1, 12, 13, sealed.len() - 1] { + assert_eq!(sb.open(ana, &sealed[..cut]), Err(SecretBoxError), "cut at {cut}"); + } + let mut flipped = sealed.clone(); + if let Some(last) = flipped.last_mut() { + *last ^= 1; + } + assert_eq!(sb.open(ana, &flipped), Err(SecretBoxError)); + } + + #[test] + fn the_older_key_opens_what_it_sealed_and_the_new_one_seals() { + let ana = user(); + let old_only = SecretBox::new(&key(1), None).unwrap(); + let sealed_old = old_only.seal(ana, b"secret").unwrap(); + let rotated = SecretBox::new(&key(2), Some(&key(1))).unwrap(); + assert_eq!(rotated.open(ana, &sealed_old).unwrap().as_slice(), b"secret"); + assert_eq!(rotated.old_id(), Some(key(1).id())); + let sealed_new = rotated.seal(ana, b"secret").unwrap(); + assert_eq!(SecretBox::key_id(&sealed_new), Some(key(2).id())); + assert_eq!(old_only.open(ana, &sealed_new), Err(SecretBoxError), "the old key alone does not open the new"); + // Without the old key configured, what it sealed is a stranger's. + assert_eq!(SecretBox::new(&key(2), None).unwrap().open(ana, &sealed_old), Err(SecretBoxError)); + } + + #[test] + fn two_keys_with_one_name_are_refused() { + assert!(matches!(SecretBox::new(&key(1), Some(&key(1))), Err(KeyError::SameName))); + } + + #[test] + fn a_key_is_read_from_a_file_of_exactly_32_bytes() { + let dir = tempfile::tempdir().unwrap(); + let good = dir.path().join("good.key"); + std::fs::write(&good, [7u8; KEY_BYTES]).unwrap(); + assert_eq!(TwoFactorKey::from_file(&good).unwrap().id(), key(7).id()); + for (name, len) in [("short.key", 31), ("long.key", 33), ("empty.key", 0), ("huge.key", 100_000)] { + let path = dir.path().join(name); + std::fs::write(&path, vec![7u8; len]).unwrap(); + assert!(matches!(TwoFactorKey::from_file(&path), Err(KeyError::Length { .. })), "{name}"); + } + assert!(matches!(TwoFactorKey::from_file(&dir.path().join("nope.key")), Err(KeyError::Missing { .. }))); + } + + #[test] + fn keys_and_boxes_show_nothing_in_debug_and_errors_say_nothing() { + let k = key(9); + assert_eq!(format!("{k:?}"), "TwoFactorKey()"); + assert_eq!(format!("{:?}", SecretBox::new(&k, None).unwrap()), "SecretBox()"); + assert_eq!(SecretBoxError.to_string(), "a two-factor secret could not be sealed or opened"); + } +} diff --git a/apps/craft-cloud-server/src/state.rs b/apps/craft-cloud-server/src/state.rs index 10d7edc8..090c9f8e 100644 --- a/apps/craft-cloud-server/src/state.rs +++ b/apps/craft-cloud-server/src/state.rs @@ -10,11 +10,13 @@ use craft_cloud_proto::DeviceId; use craft_cloud_store::Store; use tokio::sync::{OwnedSemaphorePermit, Semaphore}; +use crate::audit::AuditExportLimits; use crate::auth::hash_password; use crate::error::ApiError; use crate::live::LiveHub; use crate::notify::Notifier; use crate::ratelimit::Limiter; +use crate::secretbox::SecretBox; /// How much one user, or one request, may hold on the server at once. Each user's limits are their own, so a /// stalled or greedy client only ever slows its own account. @@ -109,10 +111,24 @@ pub struct AppState { pub logins: Limiter, /// Failed join-code redeems per client address, in one-minute windows ([`crate::join`]). pub joins: Limiter, + /// Password change tries per account, in 15-minute windows ([`crate::account`]). + pub password_changes: Limiter, + /// Wrong second factors per account and per client address, in five-minute windows ([`crate::two_factor`]). + pub second_factors: Limiter, + /// Sign-in attempts to the admin pages per client address and per account, in five-minute windows ([`crate::web`]). + pub web_logins: Limiter, + /// The admin pages' settings; `None`: they are off and `/web` does not exist. + pub web: Option, + /// Seals and opens the secrets of two-factor sign-in; `None`: no key is configured, so it can't be set up. + pub two_factor: Option>, pub notifier: Notifier, /// The live service's connections and subscriptions ([`crate::live`]). pub live: LiveHub, pub limits: Limits, + /// How the audit export is cut into pages and bounded ([`crate::audit`]). + pub audit_export: AuditExportLimits, + /// The address computers reach the server at (`public_url` in the config), for the join links of the admin API. + pub public_url: Option, /// Chunk and manifest bodies being received, per user ([`Limits::bodies_per_user`]). pub bodies: PerUser, /// Manifests being decoded and checked, per user ([`Limits::manifests_per_user`]). @@ -135,12 +151,19 @@ impl AppState { trusted_proxies, logins: Limiter::new(Duration::from_secs(300)), joins: Limiter::new(Duration::from_secs(60)), + password_changes: Limiter::new(Duration::from_secs(900)), + second_factors: Limiter::new(Duration::from_secs(300)), + web_logins: Limiter::new(Duration::from_secs(300)), + web: None, + two_factor: None, notifier: Notifier::default(), live: LiveHub::default(), bodies: PerUser::new(limits.bodies_per_user), manifests: PerUser::new(limits.manifests_per_user), password_checks: Arc::new(Semaphore::new(limits.password_checks)), limits, + audit_export: AuditExportLimits::default(), + public_url: None, touches: Mutex::new(HashMap::new()), dummy_hash: hash_password("craft-cloud dummy password")?, clock: Arc::new(now_ms), @@ -156,10 +179,36 @@ impl AppState { self } + /// The state with other bounds for the audit export (tests use small ones). + pub fn with_audit_export(mut self, limits: AuditExportLimits) -> Self { + self.audit_export = limits; + self + } + + /// The state with the key for two-factor secrets (`None`: none is configured and two-factor sign-in can't be set up). + pub fn with_two_factor(mut self, secrets: Option) -> Self { + self.two_factor = secrets.map(Arc::new); + self + } + + /// The state with the admin pages on (`None`: off, the default): their address, session lifetimes and the like. + pub fn with_web_admin(mut self, web: Option) -> Self { + self.web = web; + self + } + + /// The state with the server's public address (`public_url`), which the admin API puts in the join links it makes. + pub fn with_public_url(mut self, public_url: Option) -> Self { + self.public_url = public_url; + self + } + /// The state with another clock: a simulation's, so that what requests record and when a device's last use is /// written again follow simulated time, never the real one. A test seam (the `test-support` feature). #[cfg(feature = "test-support")] pub fn with_clock(mut self, clock: Clock) -> Self { + // The store judges a browser session's expiry inside an admin change by the same simulated time. + self.meta = self.meta.clone().with_clock(clock.clone()); self.clock = clock; self } diff --git a/apps/craft-cloud-server/src/totp.rs b/apps/craft-cloud-server/src/totp.rs new file mode 100644 index 00000000..aa7dd107 --- /dev/null +++ b/apps/craft-cloud-server/src/totp.rs @@ -0,0 +1,221 @@ +//! TOTP (RFC 6238) as authenticator apps speak it: HMAC-SHA-1, six digits, 30-second steps. +//! +//! - A code is accepted for the step the clock is in and the one before and after it (a clock a little off); the step +//! it matched is handed back, and the database lets an account accept a step only once, and only a later one than the +//! last (see `craft_cloud_meta`'s two-factor module), so a code can't be used twice. +//! - A code is exactly six ASCII digits. Anything else is refused before it is compared. +//! - Codes are compared in constant time. The secret is 20 random bytes, shown to the person as Base32 without padding. + +use hmac::{Hmac, KeyInit, Mac}; +use sha1::Sha1; +use subtle::ConstantTimeEq; +use zeroize::Zeroizing; + +/// Bytes of a new secret: 160 bits. +pub const SECRET_BYTES: usize = 20; +/// Digits of a code. +pub const DIGITS: usize = 6; +/// Seconds a code is good for. +pub const STEP_SECS: i64 = 30; +/// What the authenticator app shows as the service's name. +pub const ISSUER: &str = "Craft Cloud"; + +/// A new random secret. +pub fn generate_secret() -> Result, getrandom::Error> { + let mut secret = Zeroizing::new([0u8; SECRET_BYTES]); + getrandom::fill(secret.as_mut_slice())?; + Ok(secret) +} + +/// The time step of `now_ms` (milliseconds since the Unix epoch). +pub fn step_at(now_ms: i64) -> i64 { + now_ms.div_euclid(1000).div_euclid(STEP_SECS) +} + +/// Whether `code` has the shape of a code: exactly six ASCII digits. +pub fn is_code(code: &str) -> bool { + code.len() == DIGITS && code.bytes().all(|b| b.is_ascii_digit()) +} + +/// The code for time step `step` (HOTP's counter), with `secret`. `None` for a step before the epoch. +pub fn code_for(secret: &[u8], step: i64) -> Option { + let counter = u64::try_from(step).ok()?; + let mut mac = as KeyInit>::new_from_slice(secret).ok()?; + mac.update(&counter.to_be_bytes()); + let digest = mac.finalize().into_bytes(); + let offset = usize::from(digest.last().copied()? & 0x0f); + let word = digest.get(offset..offset.checked_add(4)?)?; + let value = u32::from_be_bytes([*word.first()?, *word.get(1)?, *word.get(2)?, *word.get(3)?]) & 0x7fff_ffff; + Some(format!("{:0width$}", value % 10u32.pow(DIGITS as u32), width = DIGITS)) +} + +/// The time step `code` is right for at `now_ms`, if it is right for the step of `now_ms` or the one before or after it +/// (the latest if several match, which is all but impossible); `None` for a code that is wrong or is not six digits. All +/// three steps are always computed and compared in constant time. +pub fn verify(secret: &[u8], code: &str, now_ms: i64) -> Option { + if !is_code(code) { + return None; + } + let now = step_at(now_ms); + let mut matched = None; + for step in [now.saturating_sub(1), now, now.saturating_add(1)] { + let expected = code_for(secret, step).unwrap_or_default(); + // Equal lengths (six) for a real code; `ct_eq` on slices of different lengths is false. + if bool::from(expected.as_bytes().ct_eq(code.as_bytes())) && !expected.is_empty() { + matched = Some(step); + } + } + matched +} + +const BASE32: &[u8; 32] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + +/// RFC 4648 Base32 without padding. +pub fn base32_encode(bytes: &[u8]) -> String { + let mut out = String::with_capacity((bytes.len() * 8).div_ceil(5)); + let (mut buffer, mut bits) = (0u32, 0u32); + for &byte in bytes { + buffer = (buffer << 8) | u32::from(byte); + bits += 8; + while bits >= 5 { + bits -= 5; + if let Some(&c) = BASE32.get(((buffer >> bits) & 31) as usize) { + out.push(char::from(c)); + } + } + buffer &= (1 << bits) - 1; + } + if bits > 0 + && let Some(&c) = BASE32.get(((buffer << (5 - bits)) & 31) as usize) + { + out.push(char::from(c)); + } + out +} + +/// The inverse of [`base32_encode`] (upper or lower case, no padding, no spaces); `None` for anything else. +pub fn base32_decode(text: &str) -> Option> { + let mut out = Vec::with_capacity(text.len() * 5 / 8); + let (mut buffer, mut bits) = (0u32, 0u32); + for c in text.bytes() { + let value = BASE32.iter().position(|&b| b == c.to_ascii_uppercase())?; + buffer = (buffer << 5) | u32::try_from(value).ok()?; + bits += 5; + if bits >= 8 { + bits -= 8; + out.push(u8::try_from((buffer >> bits) & 0xff).ok()?); + buffer &= (1 << bits) - 1; + } + } + // Leftover bits must be the zeros the encoder pads with. + (buffer == 0).then_some(out) +} + +/// `text` with everything but `A-Z a-z 0-9 - _ . ~` percent-encoded (UTF-8 bytes), for a URI's path and query. +pub fn percent_encode(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + for byte in text.bytes() { + if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'~') { + out.push(char::from(byte)); + } else { + out.push_str(&format!("%{byte:02X}")); + } + } + out +} + +/// The `otpauth://` URI an authenticator app reads (from a QR code or typed in): the issuer and the account's username +/// as the label, both percent-encoded. +pub fn otpauth_uri(username: &str, secret_base32: &str) -> String { + let issuer = percent_encode(ISSUER); + format!( + "otpauth://totp/{issuer}:{}?secret={secret_base32}&issuer={issuer}&algorithm=SHA1&digits={DIGITS}&period={STEP_SECS}", + percent_encode(username) + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// RFC 6238's SHA-1 key; its eight-digit test codes, cut to the last six digits. + const RFC_KEY: &[u8] = b"12345678901234567890"; + + #[test] + fn the_rfc_6238_vectors_hold_for_six_digits() { + for (time, code) in [ + (59i64, "287082"), + (1_111_111_109, "081804"), + (1_111_111_111, "050471"), + (1_234_567_890, "005924"), + (2_000_000_000, "279037"), + (20_000_000_000, "353130"), + ] { + assert_eq!(code_for(RFC_KEY, step_at(time * 1000)).as_deref(), Some(code), "t = {time}"); + } + // RFC 4226's own HOTP vectors, counter 0 and 1. + assert_eq!(code_for(RFC_KEY, 0).as_deref(), Some("755224")); + assert_eq!(code_for(RFC_KEY, 1).as_deref(), Some("287082")); + assert_eq!(code_for(RFC_KEY, -1), None); + } + + #[test] + fn the_window_is_one_step_either_way_and_reports_the_matched_step() { + let now_ms = 1_111_111_109_000; + let step = step_at(now_ms); + for (offset, accepted) in [(-2, false), (-1, true), (0, true), (1, true), (2, false)] { + let code = code_for(RFC_KEY, step + offset).unwrap(); + assert_eq!(verify(RFC_KEY, &code, now_ms), accepted.then_some(step + offset), "offset {offset}"); + } + // Just inside and just outside the step's edge. + let edge = step_at(1_111_111_109_000) * STEP_SECS * 1000; + assert_eq!(step_at(edge), step); + assert_eq!(step_at(edge - 1), step - 1); + } + + #[test] + fn only_six_ascii_digits_are_codes() { + for bad in ["", "12345", "1234567", "12 456", "١٢٣٤٥٦", "12345a", " 123456", "123456 ", "+12345"] { + assert!(!is_code(bad), "{bad:?}"); + assert_eq!(verify(RFC_KEY, bad, 59_000), None, "{bad:?}"); + } + assert!(is_code("000000")); + assert_eq!(verify(RFC_KEY, "287082", 59_000), Some(1)); + assert_eq!(verify(RFC_KEY, "287083", 59_000), None); + } + + #[test] + fn base32_round_trips_and_matches_the_rfc() { + assert_eq!(base32_encode(b""), ""); + assert_eq!(base32_encode(b"f"), "MY"); + assert_eq!(base32_encode(b"fo"), "MZXQ"); + assert_eq!(base32_encode(b"foo"), "MZXW6"); + assert_eq!(base32_encode(b"foob"), "MZXW6YQ"); + assert_eq!(base32_encode(b"fooba"), "MZXW6YTB"); + assert_eq!(base32_encode(b"foobar"), "MZXW6YTBOI"); + assert_eq!(base32_encode(RFC_KEY), "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"); + for text in ["", "MY", "MZXQ", "MZXW6", "MZXW6YQ", "MZXW6YTB", "MZXW6YTBOI"] { + assert_eq!(base32_encode(&base32_decode(text).unwrap()), text); + } + assert_eq!(base32_decode("mzxw6ytboi").as_deref(), Some(b"foobar".as_slice())); + assert_eq!(base32_decode("MZXW6YTB1"), None, "1 is not in the alphabet"); + assert_eq!(base32_decode("MZXW6Y=="), None, "no padding"); + assert_eq!(base32_decode("MZ"), None, "the leftover bits must be zero"); + let secret = generate_secret().unwrap(); + assert_eq!(base32_decode(&base32_encode(secret.as_slice())).unwrap(), secret.to_vec()); + assert_eq!(base32_encode(secret.as_slice()).len(), 32); + } + + #[test] + fn the_otpauth_uri_encodes_the_label() { + let uri = otpauth_uri("ana", "GEZDGNBVGY3TQOJQ"); + assert_eq!(uri, "otpauth://totp/Craft%20Cloud:ana?secret=GEZDGNBVGY3TQOJQ&issuer=Craft%20Cloud&algorithm=SHA1&digits=6&period=30"); + let odd = otpauth_uri("a.b_c-d~e", "AAAA"); + assert!(odd.contains(":a.b_c-d~e?"), "{odd}"); + // Not a real username today, but the encoder must hold for whatever one comes to be. + let wild = otpauth_uri("a b&c=d/é?#:", "AAAA"); + assert!(wild.contains(":a%20b%26c%3Dd%2F%C3%A9%3F%23%3A?"), "{wild}"); + assert_eq!(wild.matches('?').count(), 1); + assert_eq!(wild.matches('#').count(), 0); + } +} diff --git a/apps/craft-cloud-server/src/two_factor.rs b/apps/craft-cloud-server/src/two_factor.rs new file mode 100644 index 00000000..98149aad --- /dev/null +++ b/apps/craft-cloud-server/src/two_factor.rs @@ -0,0 +1,617 @@ +//! Two-factor sign-in over HTTP: `GET /api/v1/account/two-factor`, `POST …/setup`, `…/confirm`, `…/disable`, +//! `…/recovery-codes`, and the second step of a sign-in, `POST /api/v1/auth/login/second-factor`. The admin API's +//! confirmation uses [`confirm_admin_second_factor`], `POST /join` the same parsing and proofs. The key for the secrets, +//! its checks at start and `two-factor rekey` are here too. +//! +//! - **TOTP and recovery codes** (see [`crate::totp`]): the secret is 160 random bits, shown once as Base32 and as an +//! `otpauth://` URI, and kept only sealed with the key from `[two_factor] key_file` ([`crate::secretbox`]). Ten recovery +//! codes of 16 characters (80 bits, the alphabet of the join codes) are shown once; the database keeps their BLAKE3 +//! hashes, so they work whatever happens to the key, and 80 bits can't be searched even from a leaked database. +//! - **Statuses**: a wrong password is `403 wrong_password`; a wrong code `403 two_factor_invalid` on a signed-in route and +//! `401` at sign-in and join; a body that is not what it should be, a code that is not six digits and a recovery code +//! that is not 16 letters and digits `400 invalid` (nothing is counted); a lock `429 two_factor_locked` with +//! `Retry-After`. 401 is otherwise the answer for a token that is revoked, and for a challenge that doesn't work. +//! - **Every change takes the account's lock in `craft_cloud_meta`** and checks again there what the handler checked +//! before (the device still signed in, the password still the verified one, the account's current state). Handlers end +//! the live connections of the devices a change signed out only after it committed. +//! - **Counting**: a wrong password counts against `password_changes` (as a password change does), a wrong second factor +//! against the account (the lock) and, at sign-in's second step, join and the admin's confirmation, against +//! [`AppState::second_factors`] ([`SECOND_FACTORS_PER_USER`] per account and [`SECOND_FACTORS_PER_IP`] per client +//! address in five minutes; a right proof is given back). Wrong codes at the second step also count against the +//! sign-in limiter. +//! - **Nothing secret is logged**: not a secret, a code, a recovery code, a challenge, the key or a password. +//! - **The key**: [`load_keys`] reads the key files, [`check_startup`] refuses to start a server that has secrets it can't +//! open (all of them are opened, not a sample), and [`rekey`] rewrites the secrets under the current key. + +use std::net::SocketAddr; +use std::path::PathBuf; +use std::sync::Arc; + +use axum::Json; +use axum::body::Bytes; +use axum::extract::{ConnectInfo, Extension, FromRequest, Request, State}; +use axum::http::{HeaderMap, StatusCode}; +use base64::Engine; +use base64::engine::general_purpose::URL_SAFE_NO_PAD; +use craft_cloud_meta::{DeviceActor, DeviceAuth, Meta, MetaError, Proof, RECOVERY_CODES, TotpVerifier, UnreadableSecret, UserId}; +use craft_cloud_proto::api::{ + LoginResponse, RecoveryCodes, SecondFactorRequest, TwoFactorConfirmRequest, TwoFactorProofRequest, TwoFactorSetupRequest, TwoFactorSetupResponse, + TwoFactorStatus, +}; +use craft_cloud_proto::join::{Code, INVITE_CHARS}; + +use crate::account::confirm_password; +use crate::api::{LOGINS_PER_IP, LOGINS_PER_USER, TINY_JSON_BYTES, body_too_slow}; +use crate::auth::{new_token, token_hash}; +use crate::config::Config; +use crate::error::ApiError; +use crate::ratelimit::{client_ip, ip_key}; +use crate::secretbox::{KeyError, SecretBox, TwoFactorKey}; +use crate::state::AppState; +use crate::totp; + +/// Wrong second factors one account may give in [`AppState::second_factors`]'s window (five minutes). +pub const SECOND_FACTORS_PER_USER: u32 = 10; +/// Wrong second factors one client address may give in the same window. +pub const SECOND_FACTORS_PER_IP: u32 = 30; + +type Shared = Arc; + +// ---- reading and checking what a client sends ---- + +/// Reads a tiny JSON body (at most [`TINY_JSON_BYTES`]) under the usual deadline. Whatever is wrong with its shape (not +/// JSON, a missing or mistyped field) is `400 invalid`, one too long `413 too_large`, one too slow `408`. +pub(crate) async fn read_tiny(state: &AppState, request: Request) -> Result { + let bytes = match tokio::time::timeout(state.limits.body_time(TINY_JSON_BYTES), Bytes::from_request(request, &())).await { + Ok(bytes) => bytes?, + Err(_) => return Err(body_too_slow()), + }; + serde_json::from_slice(&bytes).map_err(|_| ApiError::invalid("the body must be a JSON object with the fields of this call")) +} + +/// A second factor as a client sent it, checked for its shape only. +pub(crate) enum ProofInput { + /// Six digits. + Totp(String), + /// The BLAKE3 hash of the normalised recovery code. + Recovery([u8; 32]), +} + +/// The second factor of a body: `totp_code` (six ASCII digits) or `recovery_code` (16 letters and digits, case, dashes and +/// spaces forgiven, `O` for `0` and `I`, `L` for `1`), never both. `None` when the body has neither. Anything else is `400 +/// invalid`, before anything is counted. +pub(crate) fn parse_proof(totp_code: Option, recovery_code: Option) -> Result, ApiError> { + match (totp_code, recovery_code) { + (None, None) => Ok(None), + (Some(_), Some(_)) => Err(ApiError::invalid("send either totp_code or recovery_code, not both")), + (Some(code), None) => { + if !totp::is_code(&code) { + return Err(ApiError::invalid("totp_code is the six digits from the authenticator app")); + } + Ok(Some(ProofInput::Totp(code))) + } + (None, Some(code)) => match Code::parse(&code, INVITE_CHARS) { + Some(code) => Ok(Some(ProofInput::Recovery(code.hash()))), + None => Err(ApiError::invalid("recovery_code is 16 letters and digits, like ABCD-EFGH-JKMN-PQRS")), + }, + } +} + +/// A second factor ready to hand to `craft_cloud_meta`: a recovery code as its hash, a TOTP code as the check that opens +/// the account's stored secret (under the account's lock, in the transaction) and compares. +pub(crate) enum Prepared { + Totp(Box>), + Recovery([u8; 32]), +} + +impl Prepared { + pub(crate) fn proof(&self) -> Proof<'_> { + match self { + Prepared::Totp(verify) => Proof::Totp(&**verify), + Prepared::Recovery(hash) => Proof::Recovery(*hash), + } + } +} + +impl ProofInput { + pub(crate) fn prepare(self, state: &AppState, now_ms: i64) -> Prepared { + match self { + ProofInput::Recovery(hash) => Prepared::Recovery(hash), + ProofInput::Totp(code) => Prepared::Totp(totp_check(state.two_factor.clone(), code, now_ms)), + } + } +} + +/// The TOTP check for `code` at `now_ms`: opens the stored secret (sealed for the account in question), compares, and +/// hands back the time step. No key, or a secret the key doesn't open, is [`UnreadableSecret`]. +fn totp_check(secrets: Option>, code: String, now_ms: i64) -> Box> { + Box::new(move |user: UserId, sealed: &[u8]| { + let secrets = secrets.as_ref().ok_or(UnreadableSecret)?; + let secret = secrets.open(user, sealed).map_err(|_| UnreadableSecret)?; + Ok(totp::verify(&secret, &code, now_ms)) + }) +} + +fn random_failed(e: getrandom::Error) -> ApiError { + tracing::error!(error = %e, "the system random source failed"); + ApiError::internal() +} + +fn sealing_failed(_: crate::secretbox::SecretBoxError) -> ApiError { + tracing::error!("a two-factor secret could not be sealed"); + ApiError::internal() +} + +/// Ten new, distinct recovery codes. +fn new_recovery_codes() -> Result, ApiError> { + let mut codes: Vec = Vec::with_capacity(RECOVERY_CODES); + while codes.len() < RECOVERY_CODES { + let code = Code::generate(INVITE_CHARS).map_err(random_failed)?; + // A repeated code is all but impossible (80 bits), and is drawn again if it comes. + if !codes.iter().any(|known| known.hash() == code.hash()) { + codes.push(code); + } + } + Ok(codes) +} + +/// The codes as shown once, and their hashes as stored. +fn shown(codes: &[Code]) -> (RecoveryCodes, Vec<[u8; 32]>) { + (RecoveryCodes { recovery_codes: codes.iter().map(Code::display).collect() }, codes.iter().map(Code::hash).collect()) +} + +fn no_second_factor() -> ApiError { + ApiError::invalid("send a code in totp_code or a recovery code in recovery_code") +} + +// ---- the account's own routes ---- + +/// `GET /api/v1/account/two-factor`. +pub async fn get_status(State(state): State, Extension(auth): Extension) -> Result, ApiError> { + let info = state.meta.two_factor_info(auth.user, state.now_ms()).await?; + Ok(Json(TwoFactorStatus { + enabled: info.enabled_ms.is_some(), + enabled_ms: info.enabled_ms, + recovery_codes_left: info.recovery_codes_left, + pending: info.pending, + })) +} + +/// `POST /api/v1/account/two-factor/setup`: the password starts a set-up and gets the new secret, shown once. Nothing is on +/// until `confirm`. +pub async fn setup( + State(state): State, + Extension(auth): Extension, + request: Request, +) -> Result, ApiError> { + let req: TwoFactorSetupRequest = read_tiny(&state, request).await?; + let secrets = state.two_factor.clone().ok_or_else(ApiError::two_factor_unavailable)?; + let password_hash = confirm_password(&state, auth.user, &auth.username, req.password).await?; + let secret = totp::generate_secret().map_err(random_failed)?; + let sealed = secrets.seal(auth.user, secret.as_slice()).map_err(sealing_failed)?; + let actor = DeviceActor { user: auth.user, device: auth.device, password_hash }; + let expires_ms = state.meta.two_factor_begin_setup(&actor, &sealed, state.now_ms()).await?; + let encoded = totp::base32_encode(secret.as_slice()); + tracing::info!(device = %auth.device, "two-factor set-up started"); + Ok(Json(TwoFactorSetupResponse { otpauth_uri: totp::otpauth_uri(&auth.username, &encoded), secret: encoded, expires_ms })) +} + +/// `POST /api/v1/account/two-factor/confirm`: the first code turns two-factor sign-in on and shows the recovery codes, +/// once. Every other device is signed out. +pub async fn confirm(State(state): State, Extension(auth): Extension, request: Request) -> Result, ApiError> { + let req: TwoFactorConfirmRequest = read_tiny(&state, request).await?; + let Some(ProofInput::Totp(code)) = parse_proof(Some(req.totp_code), None)? else { return Err(no_second_factor()) }; + let now_ms = state.now_ms(); + let verify = totp_check(state.two_factor.clone(), code, now_ms); + let (answer, hashes) = shown(&new_recovery_codes()?); + let revoked = state.meta.two_factor_confirm(auth.user, auth.device, &*verify, &hashes, now_ms).await?; + // Only after the commit: their live connections end now, not at their next request. + end_live_connections(&state, &revoked); + tracing::info!(device = %auth.device, revoked = revoked.len(), "two-factor sign-in turned on"); + Ok(Json(answer)) +} + +/// `POST /api/v1/account/two-factor/disable`: the password and a code or recovery code turn it off. Every other device is +/// signed out. +pub async fn disable(State(state): State, Extension(auth): Extension, request: Request) -> Result { + let req: TwoFactorProofRequest = read_tiny(&state, request).await?; + let proof = parse_proof(req.totp_code, req.recovery_code)?.ok_or_else(no_second_factor)?; + let password_hash = confirm_password(&state, auth.user, &auth.username, req.password).await?; + let now_ms = state.now_ms(); + let prepared = proof.prepare(&state, now_ms); + let actor = DeviceActor { user: auth.user, device: auth.device, password_hash }; + let revoked = state.meta.two_factor_disable(&actor, &prepared.proof(), now_ms).await?; + end_live_connections(&state, &revoked); + tracing::info!(device = %auth.device, revoked = revoked.len(), "two-factor sign-in turned off"); + Ok(StatusCode::NO_CONTENT) +} + +/// `POST /api/v1/account/two-factor/recovery-codes`: the password and a code make ten new recovery codes, shown once; the +/// old ones stop working. +pub async fn recovery_codes( + State(state): State, + Extension(auth): Extension, + request: Request, +) -> Result, ApiError> { + let req: TwoFactorProofRequest = read_tiny(&state, request).await?; + if req.recovery_code.is_some() { + return Err(ApiError::invalid("new recovery codes are made with a code from the authenticator app (totp_code)")); + } + let proof = parse_proof(req.totp_code, None)?.ok_or_else(no_second_factor)?; + let password_hash = confirm_password(&state, auth.user, &auth.username, req.password).await?; + let now_ms = state.now_ms(); + let prepared = proof.prepare(&state, now_ms); + let (answer, hashes) = shown(&new_recovery_codes()?); + let actor = DeviceActor { user: auth.user, device: auth.device, password_hash }; + state.meta.two_factor_new_recovery_codes(&actor, &prepared.proof(), &hashes, now_ms).await?; + tracing::info!(device = %auth.device, "new recovery codes made"); + Ok(Json(answer)) +} + +fn end_live_connections(state: &AppState, devices: &[craft_cloud_proto::DeviceId]) { + for device in devices { + state.live.revoke(*device); + } +} + +// ---- the second step of a sign-in ---- + +/// The hash of a challenge token as the client sent it; `None` for text that is no token (it is answered like an unknown +/// challenge). +pub(crate) fn challenge_hash(challenge: &str) -> Option<[u8; 32]> { + let is_token = challenge.len() == 43 && URL_SAFE_NO_PAD.decode(challenge).is_ok(); + is_token.then(|| token_hash(challenge)) +} + +/// `POST /api/v1/auth/login/second-factor` (no sign-in): the challenge `POST /auth/login` gave and a code or recovery +/// code make the device, answered as a sign-in is. A challenge that is unknown, expired, used, from before a password +/// change or a reset, or fit no more is `401 unauthorized`, the same for each. A wrong code is `401 two_factor_invalid` +/// (five of them delete the challenge), a lock `429 two_factor_locked`. +pub async fn second_factor( + State(state): State, + ConnectInfo(peer): ConnectInfo, + headers: HeaderMap, + request: Request, +) -> Result, ApiError> { + let req: SecondFactorRequest = read_tiny(&state, request).await?; + let proof = parse_proof(req.totp_code, req.recovery_code)?.ok_or_else(no_second_factor)?; + // tokio's clock: the system's monotonic clock in the server, the simulated one in a simulation. + let now = tokio::time::Instant::now().into_std(); + let now_ms = state.now_ms(); + let ip = ip_key(client_ip(peer, &headers, &state.trusted_proxies)); + // Counted before anything is looked at, so tries at the same moment are held to the limits too, and given back for a + // right proof or a lock (which checked nothing). + let Some(ip_counted) = state.second_factors.count(&ip, SECOND_FACTORS_PER_IP, now) else { + return Err(ApiError::rate_limited("too many wrong codes from this address; wait a few minutes", None)); + }; + let gone = || ApiError::from(MetaError::ChallengeGone); + let Some(hash) = challenge_hash(&req.challenge) else { + crate::audit::challenge_failed(&state, None).await; + return Err(gone()); + }; + let Some((user, username)) = state.meta.challenge_owner(&hash, now_ms).await? else { + crate::audit::challenge_failed(&state, None).await; + return Err(gone()); + }; + let user_key = format!("user:{username}"); + let Some(user_counted) = state.second_factors.count(&user_key, SECOND_FACTORS_PER_USER, now) else { + state.second_factors.refund(&ip, ip_counted); + // Ten wrong codes both fill this window and lock the account: say the lock, which tells how long. + if let Some(locked_ms) = state.meta.two_factor_locked_for(user, now_ms).await? { + return Err(ApiError::two_factor_locked(craft_cloud_meta::retry_after_secs(locked_ms))); + } + return Err(ApiError::rate_limited("too many wrong codes for this account; wait a few minutes", None)); + }; + let (token, new_token_hash) = new_token()?; + let prepared = proof.prepare(&state, now_ms); + let result = state.meta.complete_second_factor(&hash, &prepared.proof(), &new_token_hash, now_ms).await; + match result { + Ok(done) => { + state.second_factors.refund(&ip, ip_counted); + state.second_factors.refund(&user_key, user_counted); + // The username is the only personal identifier the server could log here: kept to debug. + tracing::debug!(%username, "sign-in with a second factor"); + tracing::info!(device = %done.device, "device signed in with a second factor"); + Ok(Json(LoginResponse { device: done.device, token })) + } + Err(MetaError::TwoFactorInvalid) => { + // A wrong second factor counts against sign-in too. + let _ = state.logins.allow(&ip, LOGINS_PER_IP, now); + let _ = state.logins.allow(&user_key, LOGINS_PER_USER, now); + Err(ApiError::from(MetaError::TwoFactorInvalid).on_public_route()) + } + Err(MetaError::TwoFactorLocked { retry_after_ms }) => { + state.second_factors.refund(&ip, ip_counted); + state.second_factors.refund(&user_key, user_counted); + Err(ApiError::from(MetaError::TwoFactorLocked { retry_after_ms })) + } + Err(MetaError::ChallengeGone) => { + // The challenge was found a moment ago and has gone since (used, out of tries, expired, or the account changed). + crate::audit::challenge_failed(&state, Some(user)).await; + Err(gone()) + } + Err(e) => Err(e.into()), + } +} + +// ---- the admin's confirmation ---- + +/// The second factor of an admin's confirmation (see [`crate::admin`], the account is a device's or a browser session's): when the admin's account has two-factor sign-in, +/// `proof` is checked and used up (`403 two_factor_invalid` when it is missing, wrong or a time step that was used, `429 +/// two_factor_locked` while locked), counted against [`AppState::second_factors`] for the account. Whether the account +/// has it is read again under its lock when the proof is checked. The answer says whether it had: the change compares +/// that with the account's state in its own transaction. +pub(crate) async fn confirm_admin_second_factor(state: &AppState, user: UserId, username: &str, proof: Option) -> Result { + let enabled = state.meta.two_factor_enabled(user).await?; + if !enabled { + // A code given to an account that has none is not looked at: the change compares the account's state again anyway. + return Ok(false); + } + let Some(proof) = proof else { + let mut e = ApiError::two_factor_invalid(); + e.message = "this account has two-factor sign-in: add a code in totp_code (or a recovery code in recovery_code)".to_owned(); + return Err(e); + }; + let now = tokio::time::Instant::now().into_std(); + let key = format!("user:{username}"); + let now_ms = state.now_ms(); + let Some(counted) = state.second_factors.count(&key, SECOND_FACTORS_PER_USER, now) else { + if let Some(locked_ms) = state.meta.two_factor_locked_for(user, now_ms).await? { + return Err(ApiError::two_factor_locked(craft_cloud_meta::retry_after_secs(locked_ms))); + } + return Err(ApiError::rate_limited("too many wrong codes; wait a few minutes", None)); + }; + let prepared = proof.prepare(state, now_ms); + match state.meta.confirm_second_factor(user, Some(&prepared.proof()), now_ms).await { + Ok(had) => { + state.second_factors.refund(&key, counted); + Ok(had) + } + Err(MetaError::TwoFactorInvalid) => Err(MetaError::TwoFactorInvalid.into()), + Err(e) => { + state.second_factors.refund(&key, counted); + Err(e.into()) + } + } +} + +// ---- the key ---- + +/// Where the server's key for two-factor secrets stands, from its config. +#[derive(Debug)] +pub enum KeyState { + /// The config has no `[two_factor]` section. + NotConfigured, + /// The config names a key file that is not there. + Missing(PathBuf), + /// The key (and the older one, if named) is read. + Loaded(Box), +} + +/// Reads the key files the config names. A key file that is not there is [`KeyState::Missing`] (whether that is fatal +/// depends on whether the database holds secrets, see [`check_startup`]); one that can't be read, has the wrong length, or +/// shares its name with the older key is an error, naming the file. +pub fn load_keys(config: &Config) -> Result { + let Some(section) = &config.two_factor else { return Ok(KeyState::NotConfigured) }; + let path = config.key_path(§ion.key_file); + let current = match TwoFactorKey::from_file(&path) { + Ok(key) => key, + Err(KeyError::Missing { path }) => return Ok(KeyState::Missing(path)), + Err(e) => return Err(e), + }; + warn_if_readable_by_others(&path); + let old = match §ion.key_file_old { + Some(old) => { + let old = config.key_path(old); + warn_if_readable_by_others(&old); + Some(TwoFactorKey::from_file(&old)?) + } + None => None, + }; + Ok(KeyState::Loaded(Box::new(SecretBox::new(¤t, old.as_ref())?))) +} + +#[cfg(unix)] +fn warn_if_readable_by_others(path: &std::path::Path) { + use std::os::unix::fs::PermissionsExt; + if let Ok(meta) = std::fs::metadata(path) + && meta.permissions().mode() & 0o077 != 0 + { + tracing::warn!(path = %path.display(), "the two-factor key file is readable by other users: make it owner-only (chmod 600)"); + } +} + +#[cfg(not(unix))] +fn warn_if_readable_by_others(_path: &std::path::Path) {} + +/// Refuses to start a server that holds two-factor secrets it can't open: with secrets in the database and no key (not +/// configured, or the file is missing), or a key that doesn't open **every** one of them. The message names the key file +/// the config expects. Without secrets there is nothing to open, and a server without a key runs, with set-up answering `503 +/// two_factor_unavailable`. Returns the box to give [`AppState::with_two_factor`]. +pub async fn check_startup(meta: &Meta, keys: KeyState, config: &Config, now_ms: i64) -> Result, String> { + let secrets = meta.two_factor_secrets(now_ms).await.map_err(|e| e.to_string())?; + let active: Vec<_> = secrets.iter().filter_map(|row| row.active.as_ref().map(|sealed| (row.user, sealed))).collect(); + let expected = config.two_factor.as_ref().map(|section| config.key_path(§ion.key_file)); + match keys { + KeyState::Loaded(secret_box) => { + let unreadable = active.iter().filter(|(user, sealed)| secret_box.open(*user, sealed).is_err()).count(); + if unreadable > 0 { + let path = expected.map(|p| p.display().to_string()).unwrap_or_default(); + return Err(format!( + "{unreadable} of {} two-factor secrets in the database can't be opened with the key in {path}{}: this is not the key they were sealed \ + with. Put the right key there (and a previous one in key_file_old while rotating); the server won't start without it", + active.len(), + if config.two_factor.as_ref().is_some_and(|section| section.key_file_old.is_some()) { " or key_file_old" } else { "" }, + )); + } + Ok(Some(*secret_box)) + } + KeyState::Missing(path) => { + if active.is_empty() { + tracing::warn!(path = %path.display(), "the two-factor key file is missing: two-factor sign-in can't be set up"); + Ok(None) + } else { + Err(format!( + "{} accounts use two-factor sign-in but the key file {} does not exist: restore it (it is not part of the database), or the server won't start", + active.len(), + path.display() + )) + } + } + KeyState::NotConfigured => { + if active.is_empty() { + Ok(None) + } else { + Err(format!( + "{} accounts use two-factor sign-in but the config has no [two_factor] key_file: set it to the key file the server had (it is not part of the \ + database), or the server won't start", + active.len() + )) + } + } + } +} + +/// What [`rekey`] did. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Rekeyed { + /// Secrets rewritten under the current key. + pub rewritten: usize, + /// Secrets that were under it already. + pub already_current: usize, +} + +/// Rewrites every secret (the active ones and the set-ups', expired ones too) that is sealed with the older key under the +/// current one, one account at a time, each in a transaction that reads the account's ciphertexts fresh under its lock and +/// rewrites both, so a confirmation that races it can't leave one behind. It refuses, before changing anything, when a secret +/// is sealed with a key that is neither (a key that was lost) or can't be opened. It then scans again and goes round again +/// (at most five times) until no ciphertext is under any key but the current one, and fails if some still is: only a +/// successful return means `key_file_old` can be removed **and** that no server still running holds only the old key, which +/// is the operator's to see to (every instance must run with both keys before this, see `docs/server.md`). Neither the +/// secrets nor the keys are printed. It can be run again. +pub async fn rekey(meta: &Meta, secret_box: &SecretBox) -> Result { + rekey_hooked(meta, secret_box, &|| {}).await +} + +/// [`rekey`] with a test seam: `after_scan` is called once, after the first scan of what is stored and before anything is +/// rewritten, so a test can let other changes (a confirmation moving a set-up's secret) land exactly there. It blocks the +/// thread it runs on, so a test runs the rest on others. +pub async fn rekey_hooked(meta: &Meta, secret_box: &SecretBox, after_scan: &(dyn Fn() + Sync)) -> Result { + let neither = || { + "some secrets are sealed with a key that is neither key_file nor key_file_old: nothing was changed; put that key in key_file_old first" + .to_owned() + }; + let mut already_current = 0usize; + for row in meta.two_factor_all_secrets().await.map_err(|e| e.to_string())? { + for sealed in [row.active, row.pending].into_iter().flatten() { + let id = SecretBox::key_id(&sealed); + if id == Some(secret_box.current_id()) { + // Under the current key: it must open, or the rest of the work would hide a lost key. + secret_box.open(row.user, &sealed).map_err(|_| "a secret that carries the current key's name does not open with it".to_owned())?; + already_current += 1; + } else if id.is_some() && id == secret_box.old_id() { + secret_box.open(row.user, &sealed).map_err(|_| "a secret does not open with key_file_old: nothing was changed".to_owned())?; + } else { + return Err(neither()); + } + } + } + after_scan(); + let current = secret_box.current_id(); + let rewrite = |user: UserId, sealed: &[u8]| -> Result>, UnreadableSecret> { + if SecretBox::key_id(sealed) == Some(current) { + return Ok(None); + } + let secret = secret_box.open(user, sealed).map_err(|_| UnreadableSecret)?; + secret_box.seal(user, &secret).map(Some).map_err(|_| UnreadableSecret) + }; + let mut rewritten = 0usize; + for _round in 0..5 { + let behind: Vec = meta + .two_factor_all_secrets() + .await + .map_err(|e| e.to_string())? + .into_iter() + .filter(|row| [&row.active, &row.pending].into_iter().flatten().any(|sealed| SecretBox::key_id(sealed) != Some(current))) + .map(|row| row.user) + .collect(); + if behind.is_empty() { + return Ok(Rekeyed { rewritten, already_current }); + } + for user in behind { + let (done, _) = meta.two_factor_rewrite(user, &rewrite).await.map_err(|e| e.to_string())?; + rewritten += done; + } + } + // One last look: a secret still under another key means the migration is not complete. + let left = meta + .two_factor_all_secrets() + .await + .map_err(|e| e.to_string())? + .iter() + .flat_map(|row| [&row.active, &row.pending]) + .flatten() + .filter(|sealed| SecretBox::key_id(sealed) != Some(current)) + .count(); + if left == 0 { + Ok(Rekeyed { rewritten, already_current }) + } else { + Err(format!( + "{left} secret(s) are still sealed with another key after rewriting: a server may still be sealing new ones with the old key. \ + Restart every instance with key_file and key_file_old, and run this again; do not remove key_file_old yet" + )) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_proof_has_the_shape_of_one_code_only() { + assert!(matches!(parse_proof(None, None), Ok(None))); + assert!(matches!(parse_proof(Some("123456".into()), None), Ok(Some(ProofInput::Totp(c))) if c == "123456")); + for bad in ["12345", "1234567", "abcdef", " 12345", "١٢٣٤٥٦", ""] { + assert!(parse_proof(Some(bad.into()), None).is_err(), "{bad:?}"); + } + assert!(parse_proof(Some("123456".into()), Some("ABCD-EFGH-JKMN-PQRS".into())).is_err(), "both"); + let a = parse_proof(None, Some("abcd-efgh-jkmn-pqrs".into())).unwrap(); + let b = parse_proof(None, Some("ABCDEFGHJKMNPQRS".into())).unwrap(); + let (Some(ProofInput::Recovery(a)), Some(ProofInput::Recovery(b))) = (a, b) else { panic!("recovery codes") }; + assert_eq!(a, b, "case and dashes don't matter"); + // O is 0, and I and L are 1, as in the join codes. + let zero = parse_proof(None, Some("0000-0000-0000-0000".into())).unwrap(); + let letter = parse_proof(None, Some("OOOO-OOOO-OOOO-OOOO".into())).unwrap(); + let (Some(ProofInput::Recovery(zero)), Some(ProofInput::Recovery(letter))) = (zero, letter) else { panic!("recovery codes") }; + assert_eq!(zero, letter); + for bad in ["ABCD-EFGH-JKMN-PQR", "ABCD-EFGH-JKMN-PQRSA", "ABCD-EFGH-JKMN-PQRU", ""] { + assert!(parse_proof(None, Some(bad.into())).is_err(), "{bad:?}"); + } + } + + #[test] + fn ten_distinct_recovery_codes_of_16_characters_are_shown_in_groups() { + let codes = new_recovery_codes().unwrap(); + assert_eq!(codes.len(), 10); + let (answer, hashes) = shown(&codes); + assert_eq!(answer.recovery_codes.len(), 10); + let unique: std::collections::BTreeSet<_> = hashes.iter().collect(); + assert_eq!(unique.len(), 10); + for (shown, code) in answer.recovery_codes.iter().zip(&codes) { + assert_eq!(shown.len(), 19, "16 characters in four groups"); + assert_eq!(shown.replace('-', ""), code.as_str()); + assert_eq!(Code::parse(shown, INVITE_CHARS).map(|c| c.hash()), Some(code.hash())); + } + // The hash is BLAKE3 of the normalised code, as the join codes' are, and reproducible. + assert_eq!(hashes.first().copied(), codes.first().map(|c| *blake3::hash(c.as_str().as_bytes()).as_bytes())); + } + + #[test] + fn a_challenge_is_a_43_character_token_or_nothing() { + let (token, hash) = new_token().unwrap(); + assert_eq!(challenge_hash(&token), Some(hash)); + for bad in ["", "short", &"x".repeat(44), &format!("{}=", &token[..42]), &format!("{}!", &token[..42])] { + assert_eq!(challenge_hash(bad), None, "{bad:?}"); + } + } +} diff --git a/apps/craft-cloud-server/src/web/html.rs b/apps/craft-cloud-server/src/web/html.rs new file mode 100644 index 00000000..eb05fcff --- /dev/null +++ b/apps/craft-cloud-server/src/web/html.rs @@ -0,0 +1,255 @@ +//! The pages' building blocks: escaping, the layout, and the form fields. +//! +//! Pages are plain `format!`ed HTML. The rule that keeps them safe is small: **every value that is not a literal in this +//! code goes through [`esc`]** before it is put into markup, and the helpers here escape their own arguments, so a page +//! built from them has no place to forget it. There is no script, no inline style and no external file anywhere (the +//! CSP, see [`super::secure_headers`], forbids all three), so a value that did slip through could still not run code. + +use std::fmt::Write; + +/// `text` escaped for HTML text and for attribute values in double or single quotes. +pub fn esc(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + for c in text.chars() { + match c { + '&' => out.push_str("&"), + '<' => out.push_str("<"), + '>' => out.push_str(">"), + '"' => out.push_str("""), + '\'' => out.push_str("'"), + // Control characters other than the usual whitespace have no place in a page. + c if c.is_control() && !matches!(c, '\n' | '\t') => out.push('\u{fffd}'), + c => out.push(c), + } + } + out +} + +/// A time in milliseconds since the Unix epoch as `2026-11-04 13:05:09 UTC`, or `-` for one that is out of range. +pub fn fmt_time(ms: i64) -> String { + let secs = ms.div_euclid(1000); + let (days, rest) = (secs.div_euclid(86_400), secs.rem_euclid(86_400)); + // Civil date from days since 1970-01-01 (Howard Hinnant's algorithm), in checked arithmetic. + let date = || -> Option<(i64, i64, i64)> { + let z = days.checked_add(719_468)?; + let era = z.div_euclid(146_097); + let doe = z.rem_euclid(146_097); + let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; + let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); + let mp = (5 * doy + 2) / 153; + let day = doy - (153 * mp + 2) / 5 + 1; + let month = if mp < 10 { mp + 3 } else { mp - 9 }; + let year = yoe.checked_add(era.checked_mul(400)?)?.checked_add(i64::from(month <= 2))?; + Some((year, month, day)) + }; + match date() { + Some((y, m, d)) if (0..=9999).contains(&y) => format!("{y:04}-{m:02}-{d:02} {:02}:{:02}:{:02} UTC", rest / 3600, rest % 3600 / 60, rest % 60), + _ => "-".to_owned(), + } +} + +/// A size in bytes as `1.5 GiB`, exact below one KiB. +pub fn fmt_bytes(bytes: u64) -> String { + const UNITS: [&str; 5] = ["KiB", "MiB", "GiB", "TiB", "PiB"]; + if bytes < 1024 { + return format!("{bytes} B"); + } + // Tenths of the unit, in 128 bits so that nothing overflows. + let mut tenths = u128::from(bytes) * 10 / 1024; + let mut unit = 0usize; + while tenths >= 10_240 && unit + 1 < UNITS.len() { + tenths /= 1024; + unit += 1; + } + format!("{}.{} {}", tenths / 10, tenths % 10, UNITS.get(unit).copied().unwrap_or("PiB")) +} + +/// What a page shows about the signed-in admin. +pub struct Ctx { + pub username: String, + /// The CSRF token the forms of this session carry. + pub csrf: String, +} + +/// The whole page: `body` is markup built with this module's helpers, `title` plain text. +pub fn page(ctx: Option<&Ctx>, title: &str, body: &str) -> String { + let nav = match ctx { + Some(ctx) => format!( + "", + esc(&ctx.csrf), + esc(&ctx.username) + ), + None => String::new(), + }; + format!( + "\n\ + {} - Craft Cloud admin\ + Skip to the content\ +

Craft Cloud admin

{nav}
{body}
\n", + esc(title) + ) +} + +/// A page heading and its first paragraph. +pub fn heading(text: &str) -> String { + format!("

{}

", esc(text)) +} + +/// A notice that something worked (`role=status`) or failed (`role=alert`). +pub fn notice(text: &str, error: bool) -> String { + let (class, role) = if error { ("notice error", "alert") } else { ("notice", "status") }; + format!("

{}

", esc(text)) +} + +/// A link to a path made by this code (never to a value from the outside as it is). +pub fn link(href: &str, text: &str) -> String { + format!("{}", esc(href), esc(text)) +} + +/// Percent-encoding for a value that goes into a query string. +pub fn url_encode(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + for byte in text.bytes() { + if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'.' | b'_' | b'~') { + out.push(char::from(byte)); + } else { + let _ = write!(out, "%{byte:02X}"); + } + } + out +} + +/// A labelled text field. `kind` is an input type from this code (`text`, `password`, `search`, `number`). +pub fn field(form: &str, name: &str, label: &str, kind: &str, value: &str, extra: &str) -> String { + let id = esc(&format!("{form}-{name}")); + format!( + "

", + esc(label), + esc(name), + esc(value) + ) +} + +/// A labelled select; `options` are `(value, text)` pairs and `selected` the value that is chosen. +pub fn select(form: &str, name: &str, label: &str, options: &[(&str, &str)], selected: &str) -> String { + let id = esc(&format!("{form}-{name}")); + let mut out = format!("

"); + out +} + +/// A labelled checkbox. +pub fn checkbox(form: &str, name: &str, label: &str) -> String { + let id = esc(&format!("{form}-{name}")); + format!( + "

", + esc(name), + esc(label) + ) +} + +/// The confirmation every change asks for: the admin's password and, when their account has two-factor sign-in, a code or a +/// recovery code. The server asks again whatever the page showed. +pub fn confirm_fields(form: &str, two_factor: bool) -> String { + let mut out = + field(form, "password", "Your password, to confirm", "password", "", "required autocomplete=\"current-password\" maxlength=\"1024\""); + if two_factor { + out.push_str(&field( + form, + "totp_code", + "Code from your authenticator app", + "text", + "", + "inputmode=\"numeric\" autocomplete=\"one-time-code\" pattern=\"[0-9]{6}\" maxlength=\"6\"", + )); + out.push_str(&field(form, "recovery_code", "Or a recovery code", "text", "", "autocomplete=\"off\" maxlength=\"32\" spellcheck=\"false\"")); + } + out +} + +/// A form that posts to `action` with the session's CSRF token, `inner` being markup built with the helpers above. +pub fn post_form(ctx: &Ctx, action: &str, class: &str, inner: &str) -> String { + format!( + "
{inner}
", + esc(action), + esc(class), + esc(&ctx.csrf) + ) +} + +/// A table with a caption and column headers. Every cell is markup already built with [`esc`]. +pub fn table(caption: &str, headers: &[&str], rows: &[Vec]) -> String { + let mut out = format!("", esc(caption)); + for header in headers { + let _ = write!(out, "", esc(header)); + } + out.push_str(""); + for row in rows { + out.push_str(""); + for (n, cell) in row.iter().enumerate() { + if n == 0 { + let _ = write!(out, ""); + } else { + let _ = write!(out, ""); + } + } + out.push_str(""); + } + out.push_str("
{}
{}
{cell}{cell}
"); + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn markup_characters_are_escaped_everywhere_a_value_goes() { + let nasty = "'&"; + assert_eq!(esc(nasty), "<script>alert("x")</script>'&"); + assert_eq!(esc("a\u{0}b\u{7}c\nd"), "a\u{fffd}b\u{fffd}c\nd"); + for html in [ + heading(nasty), + notice(nasty, true), + link(nasty, nasty), + field("f", nasty, nasty, "text", nasty, ""), + select("f", nasty, nasty, &[(nasty, nasty)], nasty), + checkbox("f", nasty, nasty), + page(Some(&Ctx { username: nasty.to_owned(), csrf: nasty.to_owned() }), nasty, ""), + post_form(&Ctx { username: String::new(), csrf: nasty.to_owned() }, nasty, nasty, ""), + table(nasty, &[nasty], &[]), + ] { + assert!(!html.contains(""; + let platform = "&\""; + let res = s + .api( + None, + Method::POST, + "/auth/login", + Some(json!({ "username": "ana", "password": PASSWORD, "device_name": name, "platform": platform, "app_version": "" })), + ) + .await; + assert_eq!(res.status(), StatusCode::OK); + // A username can't hold markup, but a row in a damaged or hand-edited database could: the pages must hold anyway. + use sqlx::Connection; + sqlx::any::install_default_drivers(); + let mut conn = sqlx::AnyConnection::connect(&s.url).await.unwrap(); + sqlx::query("UPDATE users SET username = $1 WHERE username = 'ana'").bind("").execute(&mut conn).await.unwrap(); + drop(conn); + let pages = [ + s.get("/web/users", Some(&sess.token)).await, + s.get(&format!("/web/users/{}", ana.id.0), Some(&sess.token)).await, + s.get("/web/audit", Some(&sess.token)).await, + s.get("/web/users?query=%3Cscript%3Ealert(2)%3C%2Fscript%3E", Some(&sess.token)).await, + s.get("/web/audit?action=%22%3E%3Cscript%3Ealert(3)%3C%2Fscript%3E", Some(&sess.token)).await, + s.get("/web/audit?user=%3Cscript%3Ealert(4)%3C%2Fscript%3E", Some(&sess.token)).await, + s.get("/web/%3Cscript%3Ealert(5)%3C%2Fscript%3E", Some(&sess.token)).await, + s.get(&format!("/web/users/{}?notice=%3Cscript%3E", ana.id.0), Some(&sess.token)).await, + ]; + let mut shown = String::new(); + for res in pages { + let body = res.text().await.unwrap(); + assert!(!body.contains(""), "{body}"); + shown.push_str(&body); + } + assert!(shown.contains("<script>alert(1)</script>"), "the user name is shown, escaped"); + assert!(shown.contains("<script>alert('d')</script>"), "the device name is shown, escaped"); + assert!(shown.contains("<b onmouseover=x>&""), "the platform is shown, escaped"); + assert!(shown.contains("<script>alert(2)</script>"), "the search is shown back, escaped"); +} + +async fn old_sessions_are_cleaned_up_at_sign_in(s: &Web) { + let root = s.add_admin("root").await; + s.clock.store(craft_cloud_server::state::now_ms(), Ordering::SeqCst); + for _ in 0..5 { + s.sign_in("root").await; + } + let out = s.sign_in("root").await; + s.post("/web/logout", Some(&out.token), &[("csrf", &out.csrf)]).await; + assert_eq!(s.meta.web_sessions_count_over(s.at(), IDLE_MS).await.unwrap(), 1, "the logged-out one"); + s.advance(IDLE_MS + 1); + assert_eq!(s.meta.web_sessions_count_over(s.at(), IDLE_MS).await.unwrap(), 6, "five idle ones and the logged-out one"); + // The next sign-in deletes them on its way and is itself unharmed. + let fresh = s.sign_in("root").await; + assert_eq!(s.meta.web_sessions_count_over(s.at(), IDLE_MS).await.unwrap(), 0); + assert!(signed_in(s, &fresh).await); + assert_eq!(s.meta.web_sessions_open(root.id, s.at(), IDLE_MS).await.unwrap(), 1); +} + +async fn form_bodies_are_small_and_odd_ones_refused(s: &Web) { + s.add_admin("root").await; + let sess = s.sign_in("root").await; + // More than 4 KiB is 413, and no form at all is not a sign-in. + let big = "x".repeat(5000); + let res = s.post("/web/login", None, &[("username", "root"), ("password", &big)]).await; + assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE); + let res = s + .client + .post(s.url("/web/login")) + .header("origin", ORIGIN) + .header("content-type", "application/json") + .body("{\"username\":\"root\",\"password\":\"x\"}") + .send() + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::BAD_REQUEST, "a body that is not a form is a malformed request"); + let res = s + .client + .post(s.url("/web/logout")) + .header("origin", ORIGIN) + .header("cookie", format!("{COOKIE_NAME}={}", sess.token)) + .header("content-type", "application/x-www-form-urlencoded") + .body(vec![0xff, 0xfe, 0x25, 0x00]) + .send() + .await + .unwrap(); + assert!(res.status().is_client_error()); + assert!(signed_in(s, &sess).await); + // Parameters of the wrong kind in a query are a 400 page. + assert_eq!(s.get("/web/users?query=a&query=b&cursor=%FF", Some(&sess.token)).await.status().as_u16() / 100, 4); +} + +// ---- off by default, and its own tests ---- + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_pages_do_not_exist_unless_the_server_was_set_up_for_them() { + let s = sqlite_with(vec![], false).await; + for path in ["/web", "/web/", "/web/login", "/web/style.css", "/web/users"] { + let res = s.get(path, None).await; + assert_eq!(res.status(), StatusCode::NOT_FOUND, "{path}"); + assert_eq!(header(&res, "content-type"), "application/json", "{path}: the API's own not-found answer"); + } + let res = s.post("/web/login", None, &[("username", "root"), ("password", PASSWORD)]).await; + assert_eq!(res.status(), StatusCode::NOT_FOUND); + // The API is unharmed. + s.add_admin("root").await; + assert!(!s.device("root").await.is_empty()); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_client_address_behind_a_trusted_proxy_is_what_the_limits_count() { + let s = sqlite_with(vec!["127.0.0.1".parse().unwrap()], true).await; + s.add_admin("root").await; + // Thirty wrong tries from one forwarded address use it up, and another forwarded address still signs in. + for n in 0..30 { + let name = format!("guess{n}"); + let res = s + .client + .post(s.url("/web/login")) + .header("origin", ORIGIN) + .header("x-forwarded-for", "203.0.113.7") + .header("content-type", "application/x-www-form-urlencoded") + .body(encode(&[("username", &name), ("password", "x")])) + .send() + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::UNAUTHORIZED); + } + let post = |from: &'static str| { + let (client, url) = (s.client.clone(), s.url("/web/login")); + async move { + client + .post(url) + .header("origin", ORIGIN) + .header("x-forwarded-for", from) + .header("content-type", "application/x-www-form-urlencoded") + .body(encode(&[("username", "root"), ("password", PASSWORD)])) + .send() + .await + .unwrap() + } + }; + assert_eq!(post("203.0.113.7").await.status(), StatusCode::TOO_MANY_REQUESTS); + assert_eq!(post("203.0.113.8").await.status(), StatusCode::SEE_OTHER); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn wrong_confirmation_passwords_are_limited_per_account_on_sqlite() { + wrong_confirmation_passwords_are_limited_per_account(&sqlite_web().await).await; +} + +// ---- review fixes ---- + +async fn an_account_without_a_password_never_signs_in(s: &Web) { + // What an invite makes: an account whose password hash is empty. It is answered like a name nobody has, whatever is typed, + // the server's own dummy password included (which is what an unknown name is checked against). + s.meta.create_user("pending", "", Role::Admin, 0).await.unwrap(); + let unknown = refused(s.post("/web/login", None, &[("username", "nobody"), ("password", PASSWORD)]).await).await; + for password in [PASSWORD, "", "craft-cloud dummy password", "$argon2id$"] { + let answer = refused(s.post("/web/login", None, &[("username", "pending"), ("password", password)]).await).await; + assert_eq!(answer, unknown, "{password:?}"); + } + for password in [PASSWORD, "craft-cloud dummy password"] { + let res = s.api(None, Method::POST, "/auth/login", Some(json!({ "username": "pending", "password": password, "device_name": "pc" }))).await; + assert_eq!(res.status(), StatusCode::UNAUTHORIZED, "{password:?}: the JSON sign-in too"); + } +} + +async fn the_slash_path_has_the_same_headers_and_policy(s: &Web) { + let get = s.get("/web/", None).await; + assert_eq!((get.status(), location(&get).as_str()), (StatusCode::SEE_OTHER, "/web")); + let head = s.client.head(s.url("/web/")).send().await.unwrap(); + assert_eq!((head.status(), location(&head).as_str()), (StatusCode::SEE_OTHER, "/web")); + let wrong_method = s.post("/web/", None, &[]).await; + assert_eq!(wrong_method.status(), StatusCode::METHOD_NOT_ALLOWED); + let no_origin = s.post_from("/web/", None, None, None, &[]).await; + assert_eq!(no_origin.status(), StatusCode::FORBIDDEN, "a post from nowhere is refused here too"); + for res in [get, head, wrong_method, no_origin] { + let csp = header(&res, "content-security-policy"); + assert!(csp.contains("script-src 'none'") && csp.contains("frame-ancestors 'none'"), "{}: {csp}", res.status()); + assert_eq!(header(&res, "x-frame-options"), "DENY"); + assert_eq!(header(&res, "x-content-type-options"), "nosniff"); + assert_eq!(header(&res, "referrer-policy"), "no-referrer"); + assert_eq!(header(&res, "cache-control"), "no-store"); + } +} + +/// A change whose body is still arriving when its session ends is refused, though the session was valid when the request began. +async fn a_change_that_outlives_its_session_is_refused(s: &Web) { + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + s.add_admin("root").await; + let vic = s.add_user("vic", Role::Member).await; + s.clock.store(craft_cloud_server::state::now_ms(), Ordering::SeqCst); + let sess = s.sign_in("root").await; + let body = encode(&[("csrf", &sess.csrf), ("password", PASSWORD)]); + let (first, rest) = body.split_at(8); + let host = s.base.trim_start_matches("http://").to_owned(); + let mut stream = tokio::net::TcpStream::connect(&host).await.unwrap(); + let head = format!( + "POST /web/users/{}/disable HTTP/1.1\r\nHost: {host}\r\nOrigin: {ORIGIN}\r\nCookie: {COOKIE_NAME}={}\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{first}", + vic.id.0, + sess.token, + body.len() + ); + stream.write_all(head.as_bytes()).await.unwrap(); + // The server has looked at the session by now (it does so before it reads the form); the session ends; the body goes on. + tokio::time::sleep(std::time::Duration::from_millis(400)).await; + s.advance(IDLE_MS + 1); + stream.write_all(rest.as_bytes()).await.unwrap(); + let mut answer = String::new(); + stream.read_to_string(&mut answer).await.unwrap(); + assert!(answer.starts_with("HTTP/1.1 401"), "{answer}"); + assert!(s.meta.admin_user(vic.id, s.at()).await.unwrap().unwrap().disabled_ms.is_none(), "nothing changed"); +} diff --git a/apps/craft-cloud-server/tests/web_log.rs b/apps/craft-cloud-server/tests/web_log.rs new file mode 100644 index 00000000..216a80b4 --- /dev/null +++ b/apps/craft-cloud-server/tests/web_log.rs @@ -0,0 +1,248 @@ +//! What the admin pages write to the log: events with the kind and the target's ID, and no password, session cookie, CSRF +//! token, challenge, code, recovery code, invite or reset code, or join link. The subscriber is global, so this is the only +//! test in its binary. + +use std::sync::{Arc, Mutex}; + +use craft_cloud_meta::{Meta, Role}; +use craft_cloud_proto::api::{LoginResponse, RecoveryCodes, TwoFactorSetupResponse}; +use craft_cloud_server::AppState; +use craft_cloud_server::auth::hash_password; +use craft_cloud_server::secretbox::{SecretBox, TwoFactorKey}; +use craft_cloud_server::totp; +use craft_cloud_server::web::{COOKIE_NAME, WebSettings}; +use craft_cloud_store::Store; +use craft_cloud_store::disk::DiskBackend; +use reqwest::{Client, StatusCode}; +use serde_json::json; + +#[derive(Clone, Default)] +struct Capture(Arc>>); + +impl std::io::Write for Capture { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.lock().unwrap().extend_from_slice(buf); + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + +impl<'a> tracing_subscriber::fmt::MakeWriter<'a> for Capture { + type Writer = Capture; + + fn make_writer(&'a self) -> Capture { + self.clone() + } +} + +const PASSWORD: &str = "correct horse battery"; +const ORIGIN: &str = "https://cloud.example.org"; + +fn form(fields: &[(&str, &str)]) -> String { + fields + .iter() + .map(|(k, v)| { + let esc: String = v + .bytes() + .map(|b| { + if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') { char::from(b).to_string() } else { format!("%{b:02X}") } + }) + .collect(); + format!("{k}={esc}") + }) + .collect::>() + .join("&") +} + +async fn post_form(client: &Client, base: &str, path: &str, token: Option<&str>, fields: Vec<(&'static str, String)>) -> reqwest::Response { + let borrowed: Vec<(&str, &str)> = fields.iter().map(|(k, v)| (*k, v.as_str())).collect(); + let mut req = client + .post(format!("{base}{path}")) + .header("origin", ORIGIN) + .header("content-type", "application/x-www-form-urlencoded") + .body(form(&borrowed)); + if let Some(token) = token { + req = req.header("cookie", format!("{COOKIE_NAME}={token}")); + } + req.send().await.unwrap() +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn the_admin_pages_log_no_secret() { + let log = Capture::default(); + tracing::subscriber::set_global_default( + tracing_subscriber::fmt().with_writer(log.clone()).with_max_level(tracing::Level::DEBUG).with_ansi(false).finish(), + ) + .unwrap(); + + let db = tempfile::tempdir().unwrap(); + let blobs = tempfile::tempdir().unwrap(); + let meta = Meta::open(&format!("sqlite:{}", db.path().join("meta.db").display())).await.unwrap(); + let store = Store::new(DiskBackend::new(blobs.path()).unwrap()); + let state = AppState::new(meta.clone(), store, vec![]) + .unwrap() + .with_public_url(Some(ORIGIN.to_owned())) + .with_web_admin(Some(WebSettings::new(ORIGIN, 30 * 60_000, 4 * 3_600_000).unwrap())) + .with_two_factor(Some(SecretBox::new(&TwoFactorKey::from_bytes([42; 32]), None).unwrap())); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let base = format!("http://{}", listener.local_addr().unwrap()); + tokio::spawn(craft_cloud_server::serve(state, listener, std::future::pending::<()>())); + let client = Client::builder().redirect(reqwest::redirect::Policy::none()).build().unwrap(); + meta.create_user("root", &hash_password(PASSWORD).unwrap(), Role::Admin, 0).await.unwrap(); + meta.create_user("ana", &hash_password(PASSWORD).unwrap(), Role::Member, 0).await.unwrap(); + let ana_id = meta.user_by_name("ana").await.unwrap().unwrap().id.0; + + // Two-factor sign-in for root, from a device; its token and codes are secrets too. + let device = client + .post(format!("{base}/api/v1/auth/login")) + .json(&json!({ "username": "root", "password": PASSWORD, "device_name": "pc" })) + .send() + .await + .unwrap() + .json::() + .await + .unwrap() + .token; + let setup: TwoFactorSetupResponse = client + .post(format!("{base}/api/v1/account/two-factor/setup")) + .bearer_auth(&device) + .json(&json!({ "password": PASSWORD })) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + let secret = totp::base32_decode(&setup.secret).unwrap(); + let first_code = totp::code_for(&secret, totp::step_at(craft_cloud_server::state::now_ms())).unwrap(); + let recovery: RecoveryCodes = client + .post(format!("{base}/api/v1/account/two-factor/confirm")) + .bearer_auth(&device) + .json(&json!({ "totp_code": first_code })) + .send() + .await + .unwrap() + .json() + .await + .unwrap(); + let recovery_code = recovery.recovery_codes.first().unwrap().clone(); + + // A wrong password, the password step with a challenge, a wrong code, a recovery code that signs in. + let wrong = post_form(&client, &base, "/web/login", None, vec![("username", "root".into()), ("password", "a wrong password".into())]).await; + assert_eq!(wrong.status(), StatusCode::UNAUTHORIZED); + let step = + post_form(&client, &base, "/web/login", None, vec![("username", "root".into()), ("password", PASSWORD.into())]).await.text().await.unwrap(); + let marker = "name=\"challenge\" value=\""; + let challenge = step[step.find(marker).unwrap() + marker.len()..].split('"').next().unwrap().to_owned(); + let bad = + post_form(&client, &base, "/web/login/second-factor", None, vec![("challenge", challenge.clone()), ("totp_code", "000000".into())]).await; + assert_eq!(bad.status(), StatusCode::FORBIDDEN); + let signed = + post_form(&client, &base, "/web/login/second-factor", None, vec![("challenge", challenge.clone()), ("recovery_code", recovery_code.clone())]) + .await; + assert_eq!(signed.status(), StatusCode::SEE_OTHER); + let line = signed.headers().get("set-cookie").unwrap().to_str().unwrap().to_owned(); + let token = line.strip_prefix(&format!("{COOKIE_NAME}=")).unwrap().split(';').next().unwrap().to_owned(); + let activity: serde_json::Value = + client.get(format!("{base}/api/v1/account/activity?limit=100")).bearer_auth(&device).send().await.unwrap().json().await.unwrap(); + assert!(activity["events"].as_array().unwrap().iter().any(|event| event["action"] == "auth.login" && event["details"]["client_kind"] == "web")); + let audit: serde_json::Value = + client.get(format!("{base}/api/v1/admin/audit?limit=200")).bearer_auth(&device).send().await.unwrap().json().await.unwrap(); + let events = audit["events"].as_array().unwrap(); + assert!(events.iter().any(|event| event["action"] == "auth.login" && event["details"]["client_kind"] == "web")); + assert!(events.iter().any(|event| event["action"] == "auth.login_failed" && event["details"]["client_kind"] == "web")); + assert!(events.iter().any(|event| event["action"] == "auth.second_factor_failed" && event["details"]["client_kind"] == "web")); + assert!(events.iter().any(|event| event["action"] == "auth.recovery_code_used" && event["details"]["client_kind"] == "web")); + let export = client.get(format!("{base}/api/v1/admin/audit/export")).bearer_auth(&device).send().await.unwrap().text().await.unwrap(); + assert!(export.lines().any(|line| line.contains("\"action\":\"auth.login\"") && line.contains("\"client_kind\":\"web\"")), "{export}"); + assert!(export.lines().any(|line| line.contains("\"action\":\"auth.login_failed\"") && line.contains("\"client_kind\":\"web\"")), "{export}"); + assert!( + export.lines().any(|line| line.contains("\"action\":\"auth.second_factor_failed\"") && line.contains("\"client_kind\":\"web\"")), + "{export}" + ); + assert!( + export.lines().any(|line| line.contains("\"action\":\"auth.recovery_code_used\"") && line.contains("\"client_kind\":\"web\"")), + "{export}" + ); + let audit_page = + client.get(format!("{base}/web/audit")).header("cookie", format!("{COOKIE_NAME}={token}")).send().await.unwrap().text().await.unwrap(); + assert!(audit_page.contains(""client_kind":"web""), "{audit_page}"); + let page = client.get(format!("{base}/web")).header("cookie", format!("{COOKIE_NAME}={token}")).send().await.unwrap().text().await.unwrap(); + let marker = "name=\"csrf\" value=\""; + let csrf = page[page.find(marker).unwrap() + marker.len()..].split('"').next().unwrap().to_owned(); + + // Changes: a new account with its invite, a reset code, a wrong confirmation, a disable, and the sign-out. + let code_of = |html: &str| html.split("").nth(1).unwrap().split("").next().unwrap().to_owned(); + // Every change is confirmed with a recovery code of its own (each works once). + let mut spare = recovery.recovery_codes.iter().skip(1).cloned(); + let mut fields = |extra: Vec<(&'static str, String)>| { + let mut all = extra; + all.push(("csrf", csrf.clone())); + all.push(("password", PASSWORD.to_owned())); + all.push(("recovery_code", spare.next().unwrap())); + all + }; + let created = post_form(&client, &base, "/web/users", Some(&token), fields(vec![("username", "newbie".into()), ("role", "member".into())])).await; + assert_eq!(created.status(), StatusCode::OK); + let invite = code_of(&created.text().await.unwrap()); + let reset = post_form(&client, &base, &format!("/web/users/{ana_id}/password-reset"), Some(&token), fields(vec![])).await; + assert_eq!(reset.status(), StatusCode::OK); + let reset_code = code_of(&reset.text().await.unwrap()); + let refused = post_form( + &client, + &base, + &format!("/web/users/{ana_id}/disable"), + Some(&token), + vec![("csrf", csrf.clone()), ("password", "another wrong password".into()), ("totp_code", "000000".into())], + ) + .await; + assert_eq!(refused.status(), StatusCode::FORBIDDEN); + let disabled = post_form(&client, &base, &format!("/web/users/{ana_id}/disable"), Some(&token), fields(vec![])).await; + assert_eq!(disabled.status(), StatusCode::SEE_OTHER); + let out = post_form(&client, &base, "/web/logout", Some(&token), vec![("csrf", csrf.clone())]).await; + assert_eq!(out.status(), StatusCode::SEE_OTHER); + + let text = String::from_utf8(log.0.lock().unwrap().clone()).unwrap(); + // The events are there, with the target's ID. + assert!( + text.contains("web admin signed in") && text.contains("admin (web): account created") && text.contains("admin (web): account disabled"), + "{text}" + ); + assert!(text.contains(&ana_id.to_string()), "the target's ID is logged"); + // Nothing secret is. + let join_url = format!("{ORIGIN}/join/{invite}"); + let all_codes = recovery.recovery_codes.clone(); + let secrets = [ + PASSWORD, + "a wrong password", + "another wrong password", + token.as_str(), + csrf.as_str(), + challenge.as_str(), + invite.as_str(), + reset_code.as_str(), + recovery_code.as_str(), + device.as_str(), + setup.secret.as_str(), + first_code.as_str(), + ]; + for secret in secrets { + assert!(!text.contains(secret), "{secret} in the log"); + } + for code in &all_codes { + assert!( + !text.contains(code.as_str()) && !text.to_ascii_lowercase().contains(&code.replace('-', "").to_ascii_lowercase()), + "a recovery code in the log" + ); + } + let lower = text.to_ascii_lowercase(); + for code in [&invite, &reset_code, &recovery_code.replace('-', "")] { + assert!(!lower.contains(&code.to_ascii_lowercase()), "a code in the log"); + } + assert!(!text.contains(&join_url) && !text.contains("/join/")); + // The usernames are not logged at info or above. + assert!(!text.lines().any(|l| l.contains(" INFO ") && (l.contains("newbie") || l.contains("root") || l.contains("ana"))), "{text}"); +} diff --git a/crates/api/src/api.rs b/crates/api/src/api.rs index 699dfae0..e493452a 100644 --- a/crates/api/src/api.rs +++ b/crates/api/src/api.rs @@ -4,6 +4,7 @@ //! re-exports these. use serde::{Deserialize, Serialize}; +use uuid::Uuid; use crate::ids::DeviceId; @@ -32,6 +33,31 @@ pub mod codes { pub const PASSWORD_REQUIRED: &str = "password_required"; /// `join`: the new password breaks the server's rules (the message says which). pub const WEAK_PASSWORD: &str = "weak_password"; + /// A signed-in route that asks for the account's password (changing it) got a wrong one (HTTP 403, never 401: a + /// 401 on a signed-in route means the device was revoked, and the agent signs out on it). The local API's + /// `login` uses the same code when the server refuses the username or password. + pub const WRONG_PASSWORD: &str = "wrong_password"; + /// The device is signed in but may not do this (HTTP 403): a member calling an `/admin` route, or an admin doing + /// what their account may not. The local API refuses with it a request that didn't come from a local program. + pub const FORBIDDEN: &str = "forbidden"; + /// A sign-in or a join with the right password or code, for an account an admin disabled or marked for removal + /// (HTTP 403). Said only after the password or code was right, never to a guesser. + pub const ACCOUNT_DISABLED: &str = "account_disabled"; + /// An admin change would leave the server without an enabled administrator (HTTP 409). + pub const LAST_ADMIN: &str = "last_admin"; + /// A password sign-in (or the redeeming of an invite or reset code for an account with two-factor sign-in) was + /// right, but needs a second factor (HTTP 401, no token). The answer's `two_factor` carries the challenge. + pub const TWO_FACTOR_REQUIRED: &str = "two_factor_required"; + /// A code or recovery code was wrong, or a code was used before (HTTP 401 at sign-in and join, which are not + /// signed in, and 403 on a signed-in route, where a 401 would read as "this device was revoked"). + pub const TWO_FACTOR_INVALID: &str = "two_factor_invalid"; + /// The server has no key for two-factor secrets (`[two_factor] key_file`), so it can't set it up (HTTP 503). + pub const TWO_FACTOR_UNAVAILABLE: &str = "two_factor_unavailable"; + /// A set-up is running on another device (HTTP 409). + pub const TWO_FACTOR_PENDING: &str = "two_factor_pending"; + /// Too many wrong codes: every check of this account's second factor is refused until `Retry-After` is over + /// (HTTP 429). A lock never lasts for good. + pub const TWO_FACTOR_LOCKED: &str = "two_factor_locked"; pub const INTERNAL: &str = "internal"; } @@ -43,6 +69,73 @@ pub struct ErrorAnswer { pub message: String, } +/// The second step of a sign-in: what `two_factor_required` carries. `challenge` is a secret, good once and until +/// `expires_ms` (milliseconds since the Unix epoch, by the server's clock); the code or recovery code goes with it to +/// `POST /auth/login/second-factor`. Its `Debug` output hides the challenge. For `POST /join` there is no challenge +/// (the code is simply sent again with the second factor): `challenge` is empty there. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct TwoFactorChallenge { + pub challenge: String, + pub expires_ms: i64, +} + +impl std::fmt::Debug for TwoFactorChallenge { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TwoFactorChallenge").field("challenge", &"").field("expires_ms", &self.expires_ms).finish() + } +} + +/// The answer to a set-up: the secret as Base32 without padding and as an `otpauth://` URI (for a QR code), good until +/// `expires_ms`. Shown once. Its `Debug` output hides both. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct TwoFactorSetupResponse { + pub secret: String, + pub otpauth_uri: String, + pub expires_ms: i64, +} + +impl std::fmt::Debug for TwoFactorSetupResponse { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TwoFactorSetupResponse") + .field("secret", &"") + .field("otpauth_uri", &"") + .field("expires_ms", &self.expires_ms) + .finish() + } +} + +/// Ten recovery codes, shown once (in groups of four, like `ABCD-EFGH-JKMN-PQRS`; any case, with or without dashes is +/// accepted back). Each works once. Its `Debug` output hides them. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct RecoveryCodes { + pub recovery_codes: Vec, +} + +impl std::fmt::Debug for RecoveryCodes { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("RecoveryCodes").field("recovery_codes", &format_args!("<{} redacted>", self.recovery_codes.len())).finish() + } +} + +/// `GET /account/two-factor`. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub struct TwoFactorStatus { + pub enabled: bool, + /// When it was turned on. + #[serde(default)] + pub enabled_ms: Option, + /// Recovery codes not used yet (0 while it is off). + pub recovery_codes_left: u32, + /// A set-up is running (it ends after ten minutes or five wrong codes). + pub pending: bool, +} + +/// `POST /api/v1/devices/revoke-others` (device auth): how many other devices were signed out. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +pub struct RevokedOthers { + pub revoked: u32, +} + /// Where an add-a-computer code stands, in [`PairingStatus`]. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] @@ -64,7 +157,8 @@ pub struct PairingStatus { pub device_name: Option, } -/// One of the signed-in user's devices. +/// One of the signed-in user's devices. The fields after `current` are newer than the first release of the API: an +/// older server leaves them out, and a client that doesn't know them ignores them. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct DeviceInfo { pub id: DeviceId, @@ -74,12 +168,73 @@ pub struct DeviceInfo { pub revoked: bool, /// The device making this request. pub current: bool, + /// What the device said about its client when it signed in: `agent`, `desktop` or `cli`, or empty. Display only. + #[serde(default)] + pub client_kind: String, + /// The platform the device said it runs on, free text, empty when it didn't say. + #[serde(default)] + pub platform: String, + /// The version of the client the device said it runs, free text, empty when it didn't say. + #[serde(default)] + pub app_version: String, + /// When the device was revoked, milliseconds since the Unix epoch, by the server's clock. + #[serde(default)] + pub revoked_ms: Option, + /// Why it was revoked: `user`, `others`, `password`, `admin`, `disabled`, `two_factor`, `idle` or `reset`. A client + /// treats a reason it doesn't know as "revoked". + #[serde(default)] + pub revoke_reason: Option, +} + +/// `GET /api/v1/account` (device auth): the signed-in account. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AccountInfo { + pub user_id: Uuid, + /// The username, in lowercase. + pub username: String, + /// `admin` or `member`. + pub role: String, + pub created_ms: i64, + /// When the password last changed through the server; `None` while it is still the first one. + #[serde(default)] + pub password_changed_ms: Option, + /// Whether signing in asks for a second factor. + #[serde(default)] + pub two_factor_enabled: bool, } #[cfg(test)] mod tests { use super::*; + #[test] + fn a_device_from_an_older_server_reads_with_empty_new_fields() { + let id = DeviceId::generate(); + let json = serde_json::json!({ "id": id, "name": "pc", "created_ms": 1, "last_used_ms": 2, "revoked": false, "current": true }); + let device: DeviceInfo = serde_json::from_value(json).unwrap(); + assert_eq!((device.client_kind.as_str(), device.platform.as_str(), device.app_version.as_str()), ("", "", "")); + assert_eq!((device.revoked_ms, device.revoke_reason), (None, None)); + } + + #[test] + fn the_new_error_codes_are_stable() { + assert_eq!(codes::WRONG_PASSWORD, "wrong_password"); + assert_eq!(codes::FORBIDDEN, "forbidden"); + assert_eq!(codes::ACCOUNT_DISABLED, "account_disabled"); + assert_eq!(codes::LAST_ADMIN, "last_admin"); + } + + #[test] + fn the_two_factor_codes_are_stable_and_a_challenge_hides_itself() { + assert_eq!(codes::TWO_FACTOR_REQUIRED, "two_factor_required"); + assert_eq!(codes::TWO_FACTOR_INVALID, "two_factor_invalid"); + assert_eq!(codes::TWO_FACTOR_UNAVAILABLE, "two_factor_unavailable"); + assert_eq!(codes::TWO_FACTOR_PENDING, "two_factor_pending"); + assert_eq!(codes::TWO_FACTOR_LOCKED, "two_factor_locked"); + let shown = format!("{:?}", TwoFactorChallenge { challenge: "challenge-secret".into(), expires_ms: 5 }); + assert!(!shown.contains("challenge-secret") && shown.contains(""), "{shown}"); + } + #[test] fn an_error_answer_reads_any_error_body() { let json = serde_json::json!({ "error": "conflict", "message": "m", "conflicts": [{ "op": 0, "kind": "root" }], "quota": { "used": 1, "limit": 2 } }); diff --git a/crates/api/src/local.rs b/crates/api/src/local.rs index c2b98742..2f61920c 100644 --- a/crates/api/src/local.rs +++ b/crates/api/src/local.rs @@ -57,9 +57,9 @@ pub const MAX_LOCAL_JSON_BYTES: usize = 1024 * 1024; /// `invalid`, `unauthorized` (the token), `not_found`, `too_large` and `internal`. pub mod codes { /// A `Host` that isn't `127.0.0.1:`, or an `Origin` header: the request didn't come from a local program. - pub const FORBIDDEN: &str = "forbidden"; + pub const FORBIDDEN: &str = crate::api::codes::FORBIDDEN; /// `login`: the server refused the username or password. - pub const WRONG_PASSWORD: &str = "wrong_password"; + pub const WRONG_PASSWORD: &str = crate::api::codes::WRONG_PASSWORD; /// `login` while signed in: sign out first. pub const ALREADY_SIGNED_IN: &str = "already_signed_in"; /// `logout`, or recents, while signed out. @@ -86,6 +86,23 @@ pub mod codes { pub const FOLDER_HAS_FILES: &str = "folder_has_files"; /// `GET /v1/blobs/{hash}`: neither this machine nor the account's server has that blob. pub const BLOB_NOT_FOUND: &str = "blob_not_found"; + /// `login` or `join`: the account has two-factor sign-in. The answer's `two_factor` (see [`ErrorExtras`](super::ErrorExtras)) + /// holds the challenge for `POST /v1/login/second-factor` (empty for `join`: send the same join again with the code). + pub const TWO_FACTOR_REQUIRED: &str = crate::api::codes::TWO_FACTOR_REQUIRED; + /// A code or recovery code was wrong (HTTP 401 for `login`, `login/second-factor` and `join`, 403 on the account calls). + /// It never means the device was revoked. + pub const TWO_FACTOR_INVALID: &str = crate::api::codes::TWO_FACTOR_INVALID; + /// Too many wrong codes: try again after the answer's `retry_after_secs` (HTTP 429, also the `Retry-After` header). + pub const TWO_FACTOR_LOCKED: &str = crate::api::codes::TWO_FACTOR_LOCKED; + /// The server has no key for two-factor secrets, so it can't be set up (HTTP 503). + pub const TWO_FACTOR_UNAVAILABLE: &str = crate::api::codes::TWO_FACTOR_UNAVAILABLE; + /// A set-up is open on another computer (HTTP 409). + pub const TWO_FACTOR_PENDING: &str = crate::api::codes::TWO_FACTOR_PENDING; + /// `login/second-factor`: the challenge doesn't work any more (it lasts five minutes and takes five wrong codes, and a + /// password change ends it): sign in again from the first step (HTTP 409; a 401 would read as a bad local token). + pub const TWO_FACTOR_EXPIRED: &str = "two_factor_expired"; + /// A state clash on the server (two-factor already on, no set-up to confirm, a password that changed meanwhile), HTTP 409. + pub const CONFLICT: &str = crate::api::codes::CONFLICT; /// Never sent by an agent: what a client reports when the agent answers a call it doesn't know (`not_found` for /// the call itself), as one from before that call answers it. Update Craft Cloud. pub const AGENT_TOO_OLD: &str = "agent_too_old"; @@ -101,6 +118,21 @@ pub mod paths { pub const DOCS: &str = "/v1/docs"; /// `POST`: join with a join string ([`super::JoinRequest`]) → [`super::Status`]. pub const JOIN: &str = "/v1/join"; + /// `POST` ([`super::SecondFactorLoginRequest`]): the second step of a sign-in that answered `two_factor_required` → + /// [`super::Status`]. + pub const LOGIN_SECOND_FACTOR: &str = "/v1/login/second-factor"; + /// `GET` → [`crate::api::TwoFactorStatus`]; with `/setup`, `/confirm`, `/disable` and `/recovery-codes` below it: the + /// signed-in account's two-factor sign-in, passed on to the server ([`super::PasswordRequest`], [`super::TotpRequest`], + /// [`super::TwoFactorProofRequest`]). Nothing is stored or logged on the way. + pub const TWO_FACTOR: &str = "/v1/account/two-factor"; + pub const TWO_FACTOR_SETUP: &str = "/v1/account/two-factor/setup"; + pub const TWO_FACTOR_CONFIRM: &str = "/v1/account/two-factor/confirm"; + pub const TWO_FACTOR_DISABLE: &str = "/v1/account/two-factor/disable"; + pub const TWO_FACTOR_RECOVERY_CODES: &str = "/v1/account/two-factor/recovery-codes"; + /// `POST` ([`super::ChangePasswordRequest`]) → 204. + pub const ACCOUNT_PASSWORD: &str = "/v1/account/password"; + /// `POST` → [`crate::api::RevokedOthers`]: sign every other computer of the account out. + pub const DEVICES_REVOKE_OTHERS: &str = "/v1/devices/revoke-others"; /// `POST`: a new add-a-computer code → [`super::PairingInfo`]; `GET`/`DELETE` `/v1/pairings/{id}`: its state, or /// cancel it. pub const PAIRINGS: &str = "/v1/pairings"; @@ -324,6 +356,12 @@ pub struct JoinRequest { /// The new account's password, for an invite that needs one (answered `password_required` without it). #[serde(default)] pub password: Option, + /// The second factor of an account that has two-factor sign-in (answered `two_factor_required` without it, the code not + /// used up): a code from the authenticator app, or a recovery code instead. Send the same join again with one. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, } impl std::fmt::Debug for JoinRequest { @@ -334,10 +372,128 @@ impl std::fmt::Debug for JoinRequest { .field("device_name", &self.device_name) .field("adopt", &self.adopt) .field("password", &self.password.as_ref().map(|_| "")) + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) .finish() } } +/// What an error answer carries beyond `error` and `message` ([`ErrorAnswer`](crate::api::ErrorAnswer) reads those two): the +/// challenge of `two_factor_required` and the wait of `two_factor_locked` (also in the `Retry-After` header). A client reads +/// it from the same body; every field is missing from answers that have no use for it. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct ErrorExtras { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub two_factor: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub retry_after_secs: Option, +} + +/// `POST /v1/login/second-factor`: the second step of a sign-in the agent answered `two_factor_required`. The agent keeps +/// nothing between the two steps, so the first step's `server`, `folder`, `device_name` and `adopt` come again (`user` is +/// the first step's and is only checked to be sane), with the `challenge` from the answer and a code (`totp_code`) or a recovery +/// code. Answered with [`Status`]. Its `Debug` output hides the challenge and the codes. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct SecondFactorLoginRequest { + pub server: String, + pub user: String, + #[serde(default)] + pub folder: Option, + #[serde(default)] + pub device_name: Option, + #[serde(default)] + pub adopt: bool, + pub challenge: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, +} + +impl std::fmt::Debug for SecondFactorLoginRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SecondFactorLoginRequest") + .field("server", &self.server) + .field("user", &self.user) + .field("folder", &self.folder) + .field("device_name", &self.device_name) + .field("adopt", &self.adopt) + .field("challenge", &"") + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +/// `POST /v1/account/two-factor/setup`: the account's password, to start setting two-factor sign-in up. Answered with +/// [`TwoFactorSetupResponse`](crate::api::TwoFactorSetupResponse). Its `Debug` output hides the password. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct PasswordRequest { + pub password: String, +} + +impl std::fmt::Debug for PasswordRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PasswordRequest").field("password", &"").finish() + } +} + +/// `POST /v1/account/two-factor/confirm`: the first code of the set-up; answered with +/// [`RecoveryCodes`](crate::api::RecoveryCodes), shown once. Its `Debug` output hides the code. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct TotpRequest { + pub totp_code: String, +} + +impl std::fmt::Debug for TotpRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TotpRequest").field("totp_code", &"").finish() + } +} + +/// `POST /v1/account/two-factor/disable` (answered 204) and `/recovery-codes` (answered with +/// [`RecoveryCodes`](crate::api::RecoveryCodes), with `totp_code` only): the password and one second factor. Its `Debug` output +/// hides all three. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct TwoFactorProofRequest { + pub password: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, +} + +impl std::fmt::Debug for TwoFactorProofRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TwoFactorProofRequest") + .field("password", &"") + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +/// `POST /v1/account/password`: the account's password change; answered 204, and every other computer is signed out. Its +/// `Debug` output hides both passwords. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct ChangePasswordRequest { + pub current_password: String, + pub new_password: String, +} + +impl std::fmt::Debug for ChangePasswordRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ChangePasswordRequest").field("current_password", &"").field("new_password", &"").finish() + } +} + +/// `PATCH /v1/devices/{id}`: a new name for one of the account's computers; answered with its +/// [`DeviceInfo`](crate::api::DeviceInfo). +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct RenameDeviceRequest { + pub name: String, +} + /// `POST /v1/pairings`: a new add-a-computer code of this account, to enter on the computer being added. It /// replaces this computer's earlier one. Its `Debug` output hides the code. #[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -736,9 +892,29 @@ mod tests { device_name: None, adopt: false, password: Some("hunter2-secret".into()), + totp_code: Some("918273".into()), + recovery_code: Some("RECOVERY-SECRET".into()), }; let shown = format!("{join:?}"); - assert!(!shown.contains("K7M4") && !shown.contains("hunter2-secret"), "{shown}"); + assert!(!shown.contains("K7M4") && !shown.contains("hunter2-secret") && !shown.contains("918273") && !shown.contains("RECOVERY"), "{shown}"); + let second = SecondFactorLoginRequest { + server: "https://c.example".into(), + user: "ana".into(), + folder: None, + device_name: None, + adopt: false, + challenge: "challenge-secret".into(), + totp_code: Some("918273".into()), + recovery_code: Some("RECOVERY-SECRET".into()), + }; + let shown = format!("{second:?}"); + assert!(!shown.contains("challenge-secret") && !shown.contains("918273") && !shown.contains("RECOVERY"), "{shown}"); + let proof = + TwoFactorProofRequest { password: "pw-secret-1".into(), totp_code: Some("918273".into()), recovery_code: Some("RECOVERY-SECRET".into()) }; + let shown = format!("{proof:?} {:?} {:?}", PasswordRequest { password: "pw-secret-1".into() }, TotpRequest { totp_code: "918273".into() }); + assert!(!shown.contains("pw-secret-1") && !shown.contains("918273") && !shown.contains("RECOVERY"), "{shown}"); + let change = format!("{:?}", ChangePasswordRequest { current_password: "old-secret-pw".into(), new_password: "new-secret-pw".into() }); + assert!(!change.contains("secret-pw"), "{change}"); let pairing = PairingInfo { id: uuid::Uuid::nil(), join: "cloud.example.com/K7M4-Q9TX".into(), @@ -747,7 +923,20 @@ mod tests { }; assert!(!format!("{pairing:?}").contains("K7M4")); let minimal: JoinRequest = serde_json::from_value(serde_json::json!({ "join": "x/y" })).unwrap(); - assert!(!minimal.adopt && minimal.password.is_none()); + assert!(!minimal.adopt && minimal.password.is_none() && minimal.totp_code.is_none() && minimal.recovery_code.is_none()); + // An older app's body reads, and a new body without a second factor is the old one. + assert_eq!( + serde_json::to_value(&minimal).unwrap(), + serde_json::json!({ "join": "x/y", "folder": null, "device_name": null, "adopt": false, "password": null }) + ); + // The extras of an error answer are optional and ignored by a reader that doesn't know them. + let extras: ErrorExtras = serde_json::from_value(serde_json::json!({ "error": "x", "message": "m" })).unwrap(); + assert_eq!(extras, ErrorExtras::default()); + let extras: ErrorExtras = serde_json::from_value( + serde_json::json!({ "error": "two_factor_required", "two_factor": { "challenge": "c", "expires_ms": 5 }, "retry_after_secs": 7 }), + ) + .unwrap(); + assert_eq!((extras.two_factor.map(|t| t.expires_ms), extras.retry_after_secs), (Some(5), Some(7))); } #[test] diff --git a/crates/app/src/http.rs b/crates/app/src/http.rs index 48aad8d5..a5ea37da 100644 --- a/crates/app/src/http.rs +++ b/crates/app/src/http.rs @@ -20,11 +20,12 @@ pub(crate) const SLOW_TIMEOUT: Duration = Duration::from_secs(90); /// Most bytes of an answer's head. const MAX_HEAD_BYTES: usize = 16 * 1024; -/// An answer: status and body. +/// An answer: status and body, and the wait in seconds its `Retry-After` header names (a number of seconds; a date isn't read). #[derive(Debug)] pub(crate) struct Answer { pub status: u16, pub body: Vec, + pub retry_after_secs: Option, } /// Sends `method path` with the token and an optional JSON body to the agent at `addr`; the answer must come within @@ -74,7 +75,7 @@ pub(crate) fn read_answer(stream: &mut impl Read) -> Result { pub(crate) fn read_answer_max(stream: &mut impl Read, max: usize) -> Result { let mut buf = Vec::with_capacity(4096); let mut chunk = [0u8; 4096]; - let (status, length, head_len) = loop { + let (status, length, head_len, retry_after_secs) = loop { let n = stream.read(&mut chunk).map_err(io)?; if n == 0 { return Err(Error::Protocol("the agent closed the connection before answering".into())); @@ -86,15 +87,18 @@ pub(crate) fn read_answer_max(stream: &mut impl Read, max: usize) -> Result { let status = response.code.ok_or_else(|| Error::Protocol("an answer without a status".into()))?; let mut length = None; + let mut retry_after_secs = None; for header in response.headers.iter() { if header.name.eq_ignore_ascii_case("content-length") { let text = std::str::from_utf8(header.value).map_err(|_| Error::Protocol("a bad Content-Length".into()))?; length = Some(text.trim().parse::().map_err(|_| Error::Protocol("a bad Content-Length".into()))?); + } else if header.name.eq_ignore_ascii_case("retry-after") { + retry_after_secs = std::str::from_utf8(header.value).ok().and_then(|text| text.trim().parse::().ok()); } else if header.name.eq_ignore_ascii_case("transfer-encoding") { return Err(Error::Protocol("the agent's answer is chunked, which this client doesn't read".into())); } } - break (status, length, head_len); + break (status, length, head_len, retry_after_secs); } Ok(httparse::Status::Partial) if buf.len() <= MAX_HEAD_BYTES => continue, Ok(httparse::Status::Partial) => return Err(Error::Protocol("the answer's head is too long".into())), @@ -124,7 +128,7 @@ pub(crate) fn read_answer_max(stream: &mut impl Read, max: usize) -> Result, retry_after_secs: Option }, /// The connection broke, or the agent didn't answer in time. #[error("the connection to the Craft Cloud agent broke: {0}")] Io(String), @@ -89,6 +101,32 @@ pub enum Error { Protocol(String), } +impl Error { + /// The code of a refusal ([`Error::Refused`] or [`Error::RefusedWith`]); `None` for any other error. + pub fn code(&self) -> Option<&str> { + match self { + Error::Refused { code, .. } | Error::RefusedWith { code, .. } => Some(code), + _ => None, + } + } + + /// The challenge of a `two_factor_required` refusal. + pub fn challenge(&self) -> Option<&TwoFactorChallenge> { + match self { + Error::RefusedWith { two_factor, .. } => two_factor.as_ref(), + _ => None, + } + } + + /// How many seconds a `two_factor_locked` (or other) refusal says to wait. + pub fn retry_after_secs(&self) -> Option { + match self { + Error::RefusedWith { retry_after_secs, .. } => *retry_after_secs, + _ => None, + } + } +} + /// The running agent, as `local-api.json` names it. #[derive(Debug, Clone)] pub struct Client { @@ -147,6 +185,11 @@ impl Client { serde_json::from_slice(&answer.body).map_err(|e| Error::Protocol(e.to_string())) } + /// A call with a body that is answered with no body (`204`). + fn call_unit(&self, method: &str, path: &str, body: Option>, timeout: Duration) -> Result<(), Error> { + self.answer(method, path, body, timeout).map(|_| ()) + } + /// A call answered without a body (`204`). fn call_empty(&self, method: &str, path: &str, timeout: Duration) -> Result<(), Error> { self.answer(method, path, None, timeout).map(|_| ()) @@ -183,6 +226,67 @@ impl Client { self.call("POST", paths::LOGIN, Some(body), http::SLOW_TIMEOUT) } + /// The second step of a sign-in that answered `Refused`/`RefusedWith { code: "two_factor_required" }`: the first step's + /// server, folder, computer name and `adopt` again, with the answer's challenge ([`Error::challenge`]) and a code from + /// the authenticator app or a recovery code. A wrong code is `two_factor_invalid` (the challenge takes five), too many + /// is `two_factor_locked` with [`Error::retry_after_secs`]; neither signs anything out. Waits for the server: call it off + /// the UI thread. + pub fn login_second_factor(&self, request: &SecondFactorLoginRequest) -> Result { + let body = serde_json::to_vec(request).map_err(|e| Error::Protocol(e.to_string()))?; + self.call("POST", paths::LOGIN_SECOND_FACTOR, Some(body), http::SLOW_TIMEOUT) + } + + /// Whether the signed-in account has two-factor sign-in on, how many recovery codes are left and whether a set-up is + /// open. Asks the server: call it off the UI thread. A server from before two-factor sign-in answers `not_found`. + pub fn two_factor_status(&self) -> Result { + self.call("GET", paths::TWO_FACTOR, None, http::SLOW_TIMEOUT) + } + + /// Starts setting two-factor sign-in up with the account's password: the secret (Base32 and as an `otpauth://` URI, for + /// a QR code), good for 10 minutes. `wrong_password` (403) is no sign-out. The secret is shown once and never stored. + pub fn two_factor_setup(&self, password: &str) -> Result { + let body = serde_json::to_vec(&PasswordRequest { password: password.to_owned() }).map_err(|e| Error::Protocol(e.to_string()))?; + self.call("POST", paths::TWO_FACTOR_SETUP, Some(body), http::SLOW_TIMEOUT) + } + + /// Turns two-factor sign-in on with the first code; the ten recovery codes, shown once (the account's other computers + /// are signed out). + pub fn two_factor_confirm(&self, totp_code: &str) -> Result { + let body = serde_json::to_vec(&TotpRequest { totp_code: totp_code.to_owned() }).map_err(|e| Error::Protocol(e.to_string()))?; + self.call("POST", paths::TWO_FACTOR_CONFIRM, Some(body), http::SLOW_TIMEOUT) + } + + /// Turns two-factor sign-in off: the password and a code or a recovery code. + pub fn two_factor_disable(&self, request: &TwoFactorProofRequest) -> Result<(), Error> { + let body = serde_json::to_vec(request).map_err(|e| Error::Protocol(e.to_string()))?; + self.call_unit("POST", paths::TWO_FACTOR_DISABLE, Some(body), http::SLOW_TIMEOUT) + } + + /// Ten new recovery codes (the old ones stop working): the password and a code from the authenticator app. + pub fn two_factor_recovery_codes(&self, password: &str, totp_code: &str) -> Result { + let request = TwoFactorProofRequest { password: password.to_owned(), totp_code: Some(totp_code.to_owned()), recovery_code: None }; + let body = serde_json::to_vec(&request).map_err(|e| Error::Protocol(e.to_string()))?; + self.call("POST", paths::TWO_FACTOR_RECOVERY_CODES, Some(body), http::SLOW_TIMEOUT) + } + + /// Changes the account's password; the account's other computers are signed out. + pub fn change_password(&self, current_password: &str, new_password: &str) -> Result<(), Error> { + let request = ChangePasswordRequest { current_password: current_password.to_owned(), new_password: new_password.to_owned() }; + let body = serde_json::to_vec(&request).map_err(|e| Error::Protocol(e.to_string()))?; + self.call_unit("POST", paths::ACCOUNT_PASSWORD, Some(body), http::SLOW_TIMEOUT) + } + + /// Signs every other computer of the account out; how many were. + pub fn revoke_other_devices(&self) -> Result { + self.call("POST", paths::DEVICES_REVOKE_OTHERS, None, http::SLOW_TIMEOUT) + } + + /// Renames one of the account's computers that isn't signed out. + pub fn rename_device(&self, id: DeviceId, name: &str) -> Result { + let body = serde_json::to_vec(&RenameDeviceRequest { name: name.to_owned() }).map_err(|e| Error::Protocol(e.to_string()))?; + self.call("PATCH", &format!("{}/{id}", paths::DEVICES), Some(body), http::SLOW_TIMEOUT) + } + /// Signs the agent out (the files stay). `local`: without reaching the server (the device stays valid there). /// Waits for the server (up to 90 s): call it off the UI thread. pub fn logout(&self, local: bool) -> Result { @@ -289,7 +393,16 @@ fn refusal(answer: http::Answer) -> Result { Ok(answer) } else { match serde_json::from_slice::(&answer.body) { - Ok(body) => Err(Error::Refused { code: body.error, message: body.message }), + Ok(body) => { + let extras = serde_json::from_slice::(&answer.body).unwrap_or_default(); + // The wait is in the answer's JSON; the `Retry-After` header is the same wait, used when the JSON lacks it. + let retry_after_secs = extras.retry_after_secs.or(answer.retry_after_secs); + if extras.two_factor.is_none() && retry_after_secs.is_none() { + Err(Error::Refused { code: body.error, message: body.message }) + } else { + Err(Error::RefusedWith { code: body.error, message: body.message, two_factor: extras.two_factor, retry_after_secs }) + } + } Err(_) => Err(Error::Protocol(format!("HTTP {}", answer.status))), } } @@ -300,6 +413,23 @@ mod tests { use super::*; use std::io::{Read, Write}; + #[test] + fn a_refusal_takes_its_wait_from_the_retry_after_header_when_the_json_has_none() { + let body = |text: &str| text.as_bytes().to_vec(); + let only_header = http::Answer { status: 429, body: body(r#"{"error":"two_factor_locked","message":"wait"}"#), retry_after_secs: Some(900) }; + assert_eq!(refusal(only_header).unwrap_err().retry_after_secs(), Some(900)); + // The JSON wins when both are there. + let both = http::Answer { + status: 429, + body: body(r#"{"error":"two_factor_locked","message":"wait","retry_after_secs":840}"#), + retry_after_secs: Some(900), + }; + assert_eq!(refusal(both).unwrap_err().retry_after_secs(), Some(840)); + // Neither: a plain refusal. + let neither = http::Answer { status: 403, body: body(r#"{"error":"two_factor_invalid","message":"no"}"#), retry_after_secs: None }; + assert!(matches!(refusal(neither).unwrap_err(), Error::Refused { .. })); + } + /// What the fake agent was asked: method, path and body. type Asked = (String, String, String); @@ -379,7 +509,19 @@ mod tests { let device = craft_cloud_api::DeviceId::generate(); let info = PairingInfo { id, join: "c.example/K7M4-Q9TX".into(), link: "https://c.example/join/K7M4-Q9TX".into(), expires_ms: 9 }; let used = PairingStatus { state: PairingState::Used, device_name: Some("lptp1".into()) }; - let list = vec![DeviceInfo { id: device, name: "lptp1".into(), created_ms: 1, last_used_ms: 2, revoked: false, current: false }]; + let list = vec![DeviceInfo { + id: device, + name: "lptp1".into(), + created_ms: 1, + last_used_ms: 2, + revoked: false, + current: false, + client_kind: String::new(), + platform: String::new(), + app_version: String::new(), + revoked_ms: None, + revoke_reason: None, + }]; let refused = json(&ErrorAnswer { error: "password_required".into(), message: "choose a password".into() }); let script = vec![ (400, refused), @@ -397,8 +539,15 @@ mod tests { let (_dir, agent, asked) = fake_agent(script); let next = || asked.recv_timeout(Duration::from_secs(5)).unwrap(); - let request = - JoinRequest { join: "c.example/K7M4-Q9TX".into(), folder: None, device_name: Some("lptp1".into()), adopt: true, password: None }; + let request = JoinRequest { + join: "c.example/K7M4-Q9TX".into(), + folder: None, + device_name: Some("lptp1".into()), + adopt: true, + password: None, + totp_code: None, + recovery_code: None, + }; let refused = agent.join(&request); assert!(matches!(refused, Err(Error::Refused { ref code, .. }) if code == "password_required"), "{refused:?}"); let (method, path, body) = next(); @@ -430,6 +579,114 @@ mod tests { assert_eq!(serde_json::from_str::(&body).unwrap(), AutostartRequest { on: true }); } + /// The two-factor calls send what the local API wants and read what it answers; a refusal keeps its challenge or its wait. + #[test] + fn the_two_factor_calls_send_their_requests_and_keep_what_a_refusal_carries() { + let recovery = RecoveryCodes { recovery_codes: vec!["ABCD-EFGH-JKMN-PQRS".into(); 10] }; + let setup = TwoFactorSetupResponse { secret: "JBSWY3DPEHPK3PXP".into(), otpauth_uri: "otpauth://totp/x".into(), expires_ms: 5 }; + let state = TwoFactorStatus { enabled: true, enabled_ms: Some(3), recovery_codes_left: 9, pending: false }; + let device = craft_cloud_api::DeviceId::generate(); + let renamed = DeviceInfo { + id: device, + name: "new name".into(), + created_ms: 1, + last_used_ms: 2, + revoked: false, + current: false, + client_kind: String::new(), + platform: String::new(), + app_version: String::new(), + revoked_ms: None, + revoke_reason: None, + }; + let required = + json(&serde_json::json!({ "error": "two_factor_required", "message": "m", "two_factor": { "challenge": "chal", "expires_ms": 9 } })); + let locked = json(&serde_json::json!({ "error": "two_factor_locked", "message": "wait", "retry_after_secs": 840 })); + let plain = json(&ErrorAnswer { error: "two_factor_invalid".into(), message: "wrong".into() }); + let script = vec![ + (401, required), + (200, json(&status(true))), + (429, locked), + (403, plain), + (200, json(&state)), + (200, json(&setup)), + (200, json(&recovery)), + (204, String::new()), + (200, json(&recovery)), + (204, String::new()), + (200, json(&RevokedOthers { revoked: 2 })), + (200, json(&renamed)), + ]; + let (_dir, agent, asked) = fake_agent(script); + let next = || asked.recv_timeout(Duration::from_secs(5)).unwrap(); + + let login = LoginRequest { + server: "https://c.example".into(), + user: "ana".into(), + password: "pw-secret-1".into(), + folder: None, + device_name: None, + adopt: false, + }; + let refused = agent.login(&login).unwrap_err(); + assert_eq!(refused.code(), Some("two_factor_required")); + assert_eq!(refused.challenge().map(|c| (c.challenge.as_str(), c.expires_ms)), Some(("chal", 9))); + assert!(!format!("{refused:?}").contains("\"chal\""), "the challenge is hidden in Debug"); + let _ = next(); + let second = SecondFactorLoginRequest { + server: "https://c.example".into(), + user: "ana".into(), + folder: None, + device_name: None, + adopt: false, + challenge: "chal".into(), + totp_code: Some("123456".into()), + recovery_code: None, + }; + assert!(agent.login_second_factor(&second).unwrap().signed_in); + let (method, path, body) = next(); + assert_eq!((method.as_str(), path.as_str()), ("POST", paths::LOGIN_SECOND_FACTOR)); + assert_eq!(serde_json::from_str::(&body).unwrap(), second); + + // A lock keeps its wait; a plain refusal stays a plain `Refused` (code and message only). + let err = agent.login_second_factor(&second).unwrap_err(); + assert_eq!((err.code(), err.retry_after_secs(), err.challenge()), (Some("two_factor_locked"), Some(840), None)); + let _ = next(); + let err = agent.login_second_factor(&second).unwrap_err(); + assert!(matches!(&err, Error::Refused { code, .. } if code == "two_factor_invalid"), "{err:?}"); + let _ = next(); + + assert_eq!(agent.two_factor_status().unwrap(), state); + assert_eq!(next(), ("GET".into(), paths::TWO_FACTOR.to_owned(), String::new())); + assert_eq!(agent.two_factor_setup("pw-secret-1").unwrap(), setup); + let (_, path, body) = next(); + assert_eq!( + (path.as_str(), serde_json::from_str::(&body).unwrap().password.as_str()), + (paths::TWO_FACTOR_SETUP, "pw-secret-1") + ); + assert_eq!(agent.two_factor_confirm("123456").unwrap(), recovery); + let (_, path, body) = next(); + assert_eq!((path.as_str(), serde_json::from_str::(&body).unwrap().totp_code.as_str()), (paths::TWO_FACTOR_CONFIRM, "123456")); + let off = TwoFactorProofRequest { password: "pw-secret-1".into(), totp_code: None, recovery_code: Some("ABCD-EFGH-JKMN-PQRS".into()) }; + agent.two_factor_disable(&off).unwrap(); + let (_, path, body) = next(); + assert_eq!((path.as_str(), serde_json::from_str::(&body).unwrap()), (paths::TWO_FACTOR_DISABLE, off)); + assert_eq!(agent.two_factor_recovery_codes("pw-secret-1", "654321").unwrap(), recovery); + let (_, path, body) = next(); + let sent = serde_json::from_str::(&body).unwrap(); + assert_eq!((path.as_str(), sent.totp_code.as_deref(), sent.recovery_code), (paths::TWO_FACTOR_RECOVERY_CODES, Some("654321"), None)); + agent.change_password("old-secret-pw", "new-secret-pw").unwrap(); + let (method, path, body) = next(); + assert_eq!((method.as_str(), path.as_str()), ("POST", paths::ACCOUNT_PASSWORD)); + assert_eq!(serde_json::from_str::(&body).unwrap().new_password, "new-secret-pw"); + assert_eq!(agent.revoke_other_devices().unwrap(), RevokedOthers { revoked: 2 }); + assert_eq!(next(), ("POST".into(), paths::DEVICES_REVOKE_OTHERS.to_owned(), String::new())); + assert_eq!(agent.rename_device(device, "new name").unwrap(), renamed); + let (method, path, body) = next(); + assert_eq!((method.as_str(), path), ("PATCH", format!("{}/{device}", paths::DEVICES))); + assert_eq!(serde_json::from_str::(&body).unwrap().name, "new name"); + } + /// An agent that takes the connection but never answers (hung, or something else on its port) costs a /// UI thread at most a couple of seconds; it used to wait 90 s, the sign-in's time. #[test] diff --git a/crates/app/src/recents.rs b/crates/app/src/recents.rs index 30552fef..e2dca8d5 100644 --- a/crates/app/src/recents.rs +++ b/crates/app/src/recents.rs @@ -142,7 +142,7 @@ fn open(client: &Client, app: &str, follow: bool) -> Result<(Socket, (Vec bool { match e { Error::NotRunning(_) | Error::Io(_) => true, - Error::Refused { code, .. } => code == codes::NOT_SIGNED_IN, + Error::Refused { code, .. } | Error::RefusedWith { code, .. } => code == codes::NOT_SIGNED_IN, Error::Version(_) | Error::Protocol(_) => false, } } diff --git a/crates/meta/Cargo.toml b/crates/meta/Cargo.toml index 318a1996..72c435c3 100644 --- a/crates/meta/Cargo.toml +++ b/crates/meta/Cargo.toml @@ -14,6 +14,7 @@ serde_json.workspace = true sqlx.workspace = true thiserror.workspace = true tokio.workspace = true +tracing.workspace = true uuid.workspace = true [features] diff --git a/crates/meta/migrations/postgres/0007_devices_v2.sql b/crates/meta/migrations/postgres/0007_devices_v2.sql new file mode 100644 index 00000000..21abd244 --- /dev/null +++ b/crates/meta/migrations/postgres/0007_devices_v2.sql @@ -0,0 +1,14 @@ +-- Device sessions v2. A device now says what kind of client it is (`agent`, `desktop` or `cli`, empty when the +-- client didn't say), on what platform and in what version: shown in the device list and never a basis for any +-- permission. A revoked device keeps when and why it was revoked (`user`, `others`, `password`, `admin`, +-- `disabled`, `two_factor`, `idle` or `reset`), set together with `revoked` and only by the first revoke. The +-- `revoked` flag stays the one the server checks. A user keeps when the password last changed through the +-- server (`NULL` when never) and, for a later release, when the account was disabled (`NULL` while it is not). +-- Existing rows stay valid: the new text columns read as empty and the new times as `NULL`. +ALTER TABLE devices ADD COLUMN client_kind TEXT NOT NULL DEFAULT ''; +ALTER TABLE devices ADD COLUMN platform TEXT NOT NULL DEFAULT ''; +ALTER TABLE devices ADD COLUMN app_version TEXT NOT NULL DEFAULT ''; +ALTER TABLE devices ADD COLUMN revoked_ms BIGINT; +ALTER TABLE devices ADD COLUMN revoke_reason TEXT; +ALTER TABLE users ADD COLUMN password_changed_ms BIGINT; +ALTER TABLE users ADD COLUMN disabled_ms BIGINT; diff --git a/crates/meta/migrations/postgres/0008_admin_resets.sql b/crates/meta/migrations/postgres/0008_admin_resets.sql new file mode 100644 index 00000000..fec1d19f --- /dev/null +++ b/crates/meta/migrations/postgres/0008_admin_resets.sql @@ -0,0 +1,12 @@ +-- Admin API. A row of `invites` now says what it is for: `invite` (a new computer for an account, as before) or +-- `reset` (a password reset code the admin made, good for an hour and redeemed through the same join route). A +-- space may carry a quota of its own that replaces the server-wide cap for that account (`NULL`: the cap applies), +-- and a user may be marked for removal (`removed_ms`, `NULL` while it is not): the account is disabled and left +-- out of the lists, and its data stays until a later release frees it. Existing rows stay valid: every old +-- invite reads as `invite`, and no space has a quota or user a removal mark. `users.auth_gen` counts the changes +-- that end what a sign-in checked before them (a disable, an enable, a removal, a password change or reset): a +-- sign-in that read it before verifying the password creates its device only if it is still the same afterwards. +ALTER TABLE invites ADD COLUMN purpose TEXT NOT NULL DEFAULT 'invite'; +ALTER TABLE spaces ADD COLUMN quota_bytes BIGINT; +ALTER TABLE users ADD COLUMN removed_ms BIGINT; +ALTER TABLE users ADD COLUMN auth_gen BIGINT NOT NULL DEFAULT 0; diff --git a/crates/meta/migrations/postgres/0009_audit_events.sql b/crates/meta/migrations/postgres/0009_audit_events.sql new file mode 100644 index 00000000..62e3a055 --- /dev/null +++ b/crates/meta/migrations/postgres/0009_audit_events.sql @@ -0,0 +1,26 @@ +-- The security audit log. One row per event: when, who did it (`actor_user`, `actor_device`, `NULL` for the command +-- line or a guess at a name nobody has), what (`action`, a name from a closed list in the code), what it was done to +-- (`target_kind`, `target_id`), whether it worked, and a small JSON text of typed values (`details`) that never holds +-- an address, a name of a file or device, a secret or anything a client typed. There are no foreign keys, so an +-- event outlives the account it is about. A failed sign-in is one row per account and minute: `agg_key` names +-- that row and `agg_count` counts the tries in it (rows of other events have a `NULL` key, which a unique index +-- allows many times). Nothing updates or deletes a row except the pruning of old events and that counter. +CREATE TABLE audit_events ( + id BYTEA NOT NULL PRIMARY KEY, + at_ms BIGINT NOT NULL, + actor_user BYTEA, + actor_device BYTEA, + action TEXT NOT NULL, + target_kind TEXT NOT NULL DEFAULT '', + target_id BYTEA, + outcome TEXT NOT NULL, + details TEXT NOT NULL DEFAULT '{}', + agg_key TEXT, + agg_count BIGINT NOT NULL DEFAULT 1 +); + +CREATE INDEX audit_events_at ON audit_events (at_ms); + +CREATE INDEX audit_events_actor ON audit_events (actor_user, at_ms); + +CREATE UNIQUE INDEX audit_events_agg ON audit_events (agg_key); diff --git a/crates/meta/migrations/postgres/0010_two_factor.sql b/crates/meta/migrations/postgres/0010_two_factor.sql new file mode 100644 index 00000000..961a0d28 --- /dev/null +++ b/crates/meta/migrations/postgres/0010_two_factor.sql @@ -0,0 +1,32 @@ +CREATE TABLE user_two_factor ( + user_id BYTEA NOT NULL PRIMARY KEY REFERENCES users(id), + secret_ciphertext BYTEA, + enabled_ms BIGINT, + pending_ciphertext BYTEA, + pending_device BYTEA, + pending_expires_ms BIGINT, + pending_attempts BIGINT NOT NULL DEFAULT 0, + last_accepted_step BIGINT NOT NULL DEFAULT 0, + failed_attempts BIGINT NOT NULL DEFAULT 0, + locked_until_ms BIGINT +); +CREATE TABLE user_recovery_codes ( + user_id BYTEA NOT NULL REFERENCES users(id), + code_hash BYTEA NOT NULL, + used_ms BIGINT, + PRIMARY KEY (user_id, code_hash) +); +CREATE TABLE login_challenges ( + id_hash BYTEA NOT NULL PRIMARY KEY, + user_id BYTEA NOT NULL REFERENCES users(id), + kind TEXT NOT NULL, + client_key TEXT NOT NULL, + device_name TEXT NOT NULL, + client_kind TEXT NOT NULL DEFAULT '', + platform TEXT NOT NULL DEFAULT '', + app_version TEXT NOT NULL DEFAULT '', + attempts BIGINT NOT NULL DEFAULT 0, + expires_ms BIGINT NOT NULL +); +CREATE INDEX login_challenges_expiry ON login_challenges (expires_ms); +ALTER TABLE invites ADD COLUMN reset_two_factor BIGINT NOT NULL DEFAULT 0; \ No newline at end of file diff --git a/crates/meta/migrations/postgres/0011_web_sessions.sql b/crates/meta/migrations/postgres/0011_web_sessions.sql new file mode 100644 index 00000000..999659b1 --- /dev/null +++ b/crates/meta/migrations/postgres/0011_web_sessions.sql @@ -0,0 +1,18 @@ +-- Browser sessions of the web admin. A session is a random token in a cookie, of which only the BLAKE3 hash is kept +-- here (`id_hash`). `auth_gen` is the account's generation when the session was made, so every change that raises +-- `users.auth_gen` (a password change or reset, a disable, an enable, a removal, turning two-factor sign-in on or off) +-- ends the session with it. `last_seen_ms` is moved at most once a minute and carries the idle expiry, +-- `absolute_expires_ms` the hard one, `revoked_ms` is a logout. `mfa_verified_ms` is when a second factor was given +-- at sign-in (`NULL` for an account without one). There is no password, no token and no address in a row. +CREATE TABLE web_sessions ( + id_hash BYTEA NOT NULL PRIMARY KEY, + user_id BYTEA NOT NULL REFERENCES users(id), + auth_gen BIGINT NOT NULL, + created_ms BIGINT NOT NULL, + last_seen_ms BIGINT NOT NULL, + absolute_expires_ms BIGINT NOT NULL, + revoked_ms BIGINT, + mfa_verified_ms BIGINT +); +CREATE INDEX web_sessions_user ON web_sessions (user_id); +CREATE INDEX web_sessions_expiry ON web_sessions (absolute_expires_ms) diff --git a/crates/meta/migrations/sqlite/0007_devices_v2.sql b/crates/meta/migrations/sqlite/0007_devices_v2.sql new file mode 100644 index 00000000..21abd244 --- /dev/null +++ b/crates/meta/migrations/sqlite/0007_devices_v2.sql @@ -0,0 +1,14 @@ +-- Device sessions v2. A device now says what kind of client it is (`agent`, `desktop` or `cli`, empty when the +-- client didn't say), on what platform and in what version: shown in the device list and never a basis for any +-- permission. A revoked device keeps when and why it was revoked (`user`, `others`, `password`, `admin`, +-- `disabled`, `two_factor`, `idle` or `reset`), set together with `revoked` and only by the first revoke. The +-- `revoked` flag stays the one the server checks. A user keeps when the password last changed through the +-- server (`NULL` when never) and, for a later release, when the account was disabled (`NULL` while it is not). +-- Existing rows stay valid: the new text columns read as empty and the new times as `NULL`. +ALTER TABLE devices ADD COLUMN client_kind TEXT NOT NULL DEFAULT ''; +ALTER TABLE devices ADD COLUMN platform TEXT NOT NULL DEFAULT ''; +ALTER TABLE devices ADD COLUMN app_version TEXT NOT NULL DEFAULT ''; +ALTER TABLE devices ADD COLUMN revoked_ms BIGINT; +ALTER TABLE devices ADD COLUMN revoke_reason TEXT; +ALTER TABLE users ADD COLUMN password_changed_ms BIGINT; +ALTER TABLE users ADD COLUMN disabled_ms BIGINT; diff --git a/crates/meta/migrations/sqlite/0008_admin_resets.sql b/crates/meta/migrations/sqlite/0008_admin_resets.sql new file mode 100644 index 00000000..fec1d19f --- /dev/null +++ b/crates/meta/migrations/sqlite/0008_admin_resets.sql @@ -0,0 +1,12 @@ +-- Admin API. A row of `invites` now says what it is for: `invite` (a new computer for an account, as before) or +-- `reset` (a password reset code the admin made, good for an hour and redeemed through the same join route). A +-- space may carry a quota of its own that replaces the server-wide cap for that account (`NULL`: the cap applies), +-- and a user may be marked for removal (`removed_ms`, `NULL` while it is not): the account is disabled and left +-- out of the lists, and its data stays until a later release frees it. Existing rows stay valid: every old +-- invite reads as `invite`, and no space has a quota or user a removal mark. `users.auth_gen` counts the changes +-- that end what a sign-in checked before them (a disable, an enable, a removal, a password change or reset): a +-- sign-in that read it before verifying the password creates its device only if it is still the same afterwards. +ALTER TABLE invites ADD COLUMN purpose TEXT NOT NULL DEFAULT 'invite'; +ALTER TABLE spaces ADD COLUMN quota_bytes BIGINT; +ALTER TABLE users ADD COLUMN removed_ms BIGINT; +ALTER TABLE users ADD COLUMN auth_gen BIGINT NOT NULL DEFAULT 0; diff --git a/crates/meta/migrations/sqlite/0009_audit_events.sql b/crates/meta/migrations/sqlite/0009_audit_events.sql new file mode 100644 index 00000000..7b0d86e0 --- /dev/null +++ b/crates/meta/migrations/sqlite/0009_audit_events.sql @@ -0,0 +1,26 @@ +-- The security audit log. One row per event: when, who did it (`actor_user`, `actor_device`, `NULL` for the command +-- line or a guess at a name nobody has), what (`action`, a name from a closed list in the code), what it was done to +-- (`target_kind`, `target_id`), whether it worked, and a small JSON text of typed values (`details`) that never holds +-- an address, a name of a file or device, a secret or anything a client typed. There are no foreign keys, so an +-- event outlives the account it is about. A failed sign-in is one row per account and minute: `agg_key` names +-- that row and `agg_count` counts the tries in it (rows of other events have a `NULL` key, which a unique index +-- allows many times). Nothing updates or deletes a row except the pruning of old events and that counter. +CREATE TABLE audit_events ( + id BLOB NOT NULL PRIMARY KEY, + at_ms BIGINT NOT NULL, + actor_user BLOB, + actor_device BLOB, + action TEXT NOT NULL, + target_kind TEXT NOT NULL DEFAULT '', + target_id BLOB, + outcome TEXT NOT NULL, + details TEXT NOT NULL DEFAULT '{}', + agg_key TEXT, + agg_count BIGINT NOT NULL DEFAULT 1 +); + +CREATE INDEX audit_events_at ON audit_events (at_ms); + +CREATE INDEX audit_events_actor ON audit_events (actor_user, at_ms); + +CREATE UNIQUE INDEX audit_events_agg ON audit_events (agg_key); diff --git a/crates/meta/migrations/sqlite/0010_two_factor.sql b/crates/meta/migrations/sqlite/0010_two_factor.sql new file mode 100644 index 00000000..308e19d9 --- /dev/null +++ b/crates/meta/migrations/sqlite/0010_two_factor.sql @@ -0,0 +1,32 @@ +CREATE TABLE user_two_factor ( + user_id BLOB NOT NULL PRIMARY KEY REFERENCES users(id), + secret_ciphertext BLOB, + enabled_ms BIGINT, + pending_ciphertext BLOB, + pending_device BLOB, + pending_expires_ms BIGINT, + pending_attempts INTEGER NOT NULL DEFAULT 0, + last_accepted_step BIGINT NOT NULL DEFAULT 0, + failed_attempts INTEGER NOT NULL DEFAULT 0, + locked_until_ms BIGINT +); +CREATE TABLE user_recovery_codes ( + user_id BLOB NOT NULL REFERENCES users(id), + code_hash BLOB NOT NULL, + used_ms BIGINT, + PRIMARY KEY (user_id, code_hash) +); +CREATE TABLE login_challenges ( + id_hash BLOB NOT NULL PRIMARY KEY, + user_id BLOB NOT NULL REFERENCES users(id), + kind TEXT NOT NULL, + client_key TEXT NOT NULL, + device_name TEXT NOT NULL, + client_kind TEXT NOT NULL DEFAULT '', + platform TEXT NOT NULL DEFAULT '', + app_version TEXT NOT NULL DEFAULT '', + attempts INTEGER NOT NULL DEFAULT 0, + expires_ms BIGINT NOT NULL +); +CREATE INDEX login_challenges_expiry ON login_challenges (expires_ms); +ALTER TABLE invites ADD COLUMN reset_two_factor INTEGER NOT NULL DEFAULT 0; \ No newline at end of file diff --git a/crates/meta/migrations/sqlite/0011_web_sessions.sql b/crates/meta/migrations/sqlite/0011_web_sessions.sql new file mode 100644 index 00000000..a2ee36a0 --- /dev/null +++ b/crates/meta/migrations/sqlite/0011_web_sessions.sql @@ -0,0 +1,18 @@ +-- Browser sessions of the web admin. A session is a random token in a cookie, of which only the BLAKE3 hash is kept +-- here (`id_hash`). `auth_gen` is the account's generation when the session was made, so every change that raises +-- `users.auth_gen` (a password change or reset, a disable, an enable, a removal, turning two-factor sign-in on or off) +-- ends the session with it. `last_seen_ms` is moved at most once a minute and carries the idle expiry, +-- `absolute_expires_ms` the hard one, `revoked_ms` is a logout. `mfa_verified_ms` is when a second factor was given +-- at sign-in (`NULL` for an account without one). There is no password, no token and no address in a row. +CREATE TABLE web_sessions ( + id_hash BLOB NOT NULL PRIMARY KEY, + user_id BLOB NOT NULL REFERENCES users(id), + auth_gen BIGINT NOT NULL, + created_ms BIGINT NOT NULL, + last_seen_ms BIGINT NOT NULL, + absolute_expires_ms BIGINT NOT NULL, + revoked_ms BIGINT, + mfa_verified_ms BIGINT +); +CREATE INDEX web_sessions_user ON web_sessions (user_id); +CREATE INDEX web_sessions_expiry ON web_sessions (absolute_expires_ms) diff --git a/crates/meta/src/admin.rs b/crates/meta/src/admin.rs new file mode 100644 index 00000000..b08fe343 --- /dev/null +++ b/crates/meta/src/admin.rs @@ -0,0 +1,623 @@ +//! What an administrator does through the admin API: server counters, listing and creating accounts, changing a role or +//! a quota, disabling, enabling and removing an account, invites and password reset codes, and a user's devices. +//! +//! - **One `Meta` method per change, with a typed result**, and each writes its audit event in its own transaction, before +//! the commit, so a change whose event cannot be written is not made. A change that changes nothing (disabling an account +//! that is disabled, signing out a device that is out already, giving the role an account has) writes no event. +//! - **The acting admin is checked again inside every change** ([`AdminActor`], `begin_admin`): the role the middleware +//! read and the password the handler verified (a slow Argon2id check) can be stale by the time the transaction runs. +//! The device (or the browser session) must still be signed in, the account neither disabled nor marked for removal, the role still `admin` +//! and the password hash still the one that was verified. +//! - **Lock order** in every change: (1) the `last_admin` transaction lock, if the change can take away an +//! administrator; (2) the rows of the acting admin and of the target, in ascending ID; (3) rows derived from them +//! (devices, invites, pairings). Sign-ins, redeems and pairings take the user's row first as well (see +//! `users::lock_user`), so none of these can deadlock with another, and a disable can never be followed by a device +//! that an earlier check let in. +//! - **The last administrator** cannot be demoted, disabled or removed. A count cannot be locked with `FOR UPDATE`, and +//! locking the rows of two different targets does not serialise two changes that each look at the other's row. So +//! every change that can take an administrator away takes one transaction-wide lock first and counts after it: on +//! Postgres `pg_advisory_xact_lock`, on SQLite the write transaction (`BEGIN IMMEDIATE`, held by one writer at a +//! time) already is that lock. + +use craft_cloud_proto::limits::MAX_CHUNK_BYTES; +use craft_cloud_proto::{DeviceId, NodeId, SpaceId}; +use sqlx::Row; +use uuid::Uuid; + +use crate::audit::{AuditAction, AuditDetails, NewEvent, record_in}; +use crate::codec::{to_i64, to_u64, uuid_bytes, uuid_from}; +use crate::join::{Purpose, put_code}; +use crate::two_factor; +use crate::users::{delete_join_codes, insert_user, lock_user, revoke_devices}; +use crate::web::session_is_live; +use crate::{DeviceRecord, Dialect, Meta, MetaError, RevokeReason, Role, UserId, WriteTx, valid_username}; + +/// The key of the Postgres advisory lock that serialises changes which can take an administrator away. Arbitrary and +/// fixed, and different from the migration lock's. +const LAST_ADMIN_LOCK_KEY: i64 = 7240318150662; + +/// The smallest quota an admin may give an account: one maximal chunk, like the server-wide cap. +pub const MIN_QUOTA_BYTES: u64 = MAX_CHUNK_BYTES as u64; + +/// The longest username search an admin may make, in bytes. +pub const MAX_SEARCH_BYTES: usize = 64; + +/// What an admin's request came with: the device token of the JSON API, or the browser session of the web admin. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum AdminVia { + /// The device the request came with; it must still be signed in. + Device(DeviceId), + /// The hash of the session token the request came with and the idle lifetime of sessions; the session must still be valid + /// when the change runs (not logged out, under the account's current `auth_gen`, inside its absolute lifetime and not idle + /// for `idle_ms`, all judged at the time the change is made, so a request that began before the session ended cannot + /// change anything after). No device acts, so the events it writes name none. + Web { id_hash: [u8; 32], idle_ms: i64 }, +} + +impl std::fmt::Debug for AdminVia { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AdminVia::Device(device) => f.debug_tuple("Device").field(device).finish(), + AdminVia::Web { idle_ms, .. } => f.debug_struct("Web").field("id_hash", &"").field("idle_ms", idle_ms).finish(), + } + } +} + +/// The admin who asks, as the handler knew them when it verified their password. +#[derive(Clone, PartialEq, Eq)] +pub struct AdminActor { + pub user: UserId, + /// How the request signed in; it must still be so. + pub via: AdminVia, + /// The stored password hash the confirmation was verified against; it must still be the account's. + pub password_hash: String, + /// Whether the acting admin's account had two-factor sign-in on when their confirmation was checked + /// ([`Meta::confirm_second_factor`]); it must still be so (turning it on or off meanwhile is `Conflict`). + pub two_factor: bool, +} + +impl std::fmt::Debug for AdminActor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AdminActor") + .field("user", &self.user) + .field("via", &self.via) + .field("password_hash", &"") + .field("two_factor", &self.two_factor) + .finish() + } +} + +/// An account as the admin lists it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct AdminUser { + pub id: UserId, + pub username: String, + pub role: Role, + pub created_ms: i64, + pub disabled_ms: Option, + /// When the account was marked for removal (it is disabled too); the lists leave such accounts out, a single lookup does not. + pub removed_ms: Option, + /// Two-factor sign-in is on. + pub two_factor_enabled: bool, + /// Devices that are not revoked. + pub devices_active: u64, + pub stored_bytes: u64, + /// The quota an admin set for the account (`None`: the server-wide cap, if any, applies). + pub quota_bytes: Option, + /// An unexpired invite is open (a reset code is not an invite). + pub invite_pending: bool, +} + +/// The counters `GET /admin/server` shows. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ServerCounts { + pub users: u64, + pub devices_active: u64, + pub stored_bytes: u64, + pub live_bytes: u64, +} + +/// One page of [`Meta::admin_list_users`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct UserPage { + pub users: Vec, + /// The username to continue after, when there are more. + pub next_after: Option, +} + +/// A code an admin made: when it stops working. The code itself is never stored, only its hash. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Issued { + pub expires_ms: i64, +} + +/// How long a password reset code works: one hour. +pub const RESET_TTL_MS: i64 = 60 * 60 * 1000; + +struct Target { + role: Role, + disabled: bool, + removed: bool, + has_password: bool, +} + +const USER_SELECT: &str = "SELECT u.id, u.username, u.role, u.created_ms, u.disabled_ms, u.removed_ms, s.stored_bytes, s.quota_bytes, \ + (SELECT COUNT(*) FROM user_two_factor t WHERE t.user_id = u.id AND t.enabled_ms IS NOT NULL) AS two_factor, \ + (SELECT COUNT(*) FROM devices d WHERE d.user_id = u.id AND d.revoked = 0) AS devices_active, \ + (SELECT COUNT(*) FROM invites i WHERE i.user_id = u.id AND i.purpose = 'invite' AND i.expires_ms > $1) AS invites \ + FROM users u JOIN spaces s ON s.owner = u.id"; + +fn admin_user_from(row: &sqlx::any::AnyRow) -> Result { + let quota: Option = row.try_get("quota_bytes")?; + Ok(AdminUser { + id: UserId(uuid_from(&row.try_get::, _>("id")?)?), + username: row.try_get("username")?, + role: Role::parse(&row.try_get::("role")?)?, + created_ms: row.try_get("created_ms")?, + disabled_ms: row.try_get("disabled_ms")?, + removed_ms: row.try_get("removed_ms")?, + two_factor_enabled: row.try_get::("two_factor")? > 0, + devices_active: to_u64(row.try_get("devices_active")?)?, + stored_bytes: to_u64(row.try_get("stored_bytes")?)?, + quota_bytes: quota.map(to_u64).transpose()?, + invite_pending: row.try_get::("invites")? > 0, + }) +} + +/// `text` as a `LIKE` pattern that matches it literally (`\` is the escape character, see the queries). +fn escape_like(text: &str) -> String { + let mut out = String::with_capacity(text.len()); + for c in text.chars() { + if matches!(c, '\\' | '%' | '_') { + out.push('\\'); + } + out.push(c); + } + out +} + +/// An event the acting admin makes to `target`. +fn by_admin(actor: &AdminActor, action: AuditAction, target: UserId) -> NewEvent { + let device = match actor.via { + AdminVia::Device(device) => Some(device), + AdminVia::Web { .. } => None, + }; + NewEvent::new(action).by(actor.user, device).on_user(target) +} + +fn same_user(a: UserId, b: UserId) -> bool { + a.0 == b.0 +} + +impl Meta { + /// Starts the write transaction of a change the admin `actor` makes (to `target`, if it has one): takes the + /// locks in the order the module describes and checks the actor again. `reduces_admins`: the change can take an + /// administrator away, so it takes the `last_admin` lock first. + async fn begin_admin(&self, actor: &AdminActor, target: Option, reduces_admins: bool) -> Result { + let mut w = self.begin_write().await?; + // SQLite: the write transaction already serialises every change, so nothing more is needed there. + if reduces_admins && self.dialect == Dialect::Postgres { + sqlx::query("SELECT pg_advisory_xact_lock($1)").bind(LAST_ADMIN_LOCK_KEY).execute(&mut *w.tx).await?; + } + let mut ids = vec![actor.user]; + if let Some(target) = target + && !same_user(target, actor.user) + { + ids.push(target); + } + ids.sort_by_key(|id| *id.0.as_bytes()); + for id in ids { + // A target that isn't there is the caller's `NotFound`; the actor's own row is checked just below. + lock_user(&mut w.tx, id).await?; + } + let row = sqlx::query("SELECT role, password_hash, disabled_ms, removed_ms FROM users WHERE id = $1") + .bind(uuid_bytes(&actor.user.0)) + .fetch_optional(&mut *w.tx) + .await?; + let Some(row) = row else { return Err(MetaError::DeviceRevoked) }; + let still_signed_in = match actor.via { + AdminVia::Device(device) => sqlx::query("SELECT 1 FROM devices WHERE id = $1 AND user_id = $2 AND revoked = 0") + .bind(uuid_bytes(device.as_uuid())) + .bind(uuid_bytes(&actor.user.0)) + .fetch_optional(&mut *w.tx) + .await? + .is_some(), + // The time is read here, after every lock wait above and in the order the module describes, so a session that ended + // while this change waited does not authorise it. + AdminVia::Web { id_hash, idle_ms } => session_is_live(&mut w, actor.user, &id_hash, self.clock_ms(), idle_ms).await?, + }; + if !still_signed_in || row.try_get::, _>("disabled_ms")?.is_some() || row.try_get::, _>("removed_ms")?.is_some() { + return Err(MetaError::DeviceRevoked); + } + if Role::parse(&row.try_get::("role")?)? != Role::Admin { + return Err(MetaError::Forbidden("you are no longer an administrator")); + } + if row.try_get::("password_hash")? != actor.password_hash { + return Err(MetaError::Conflict("your password changed while this request was running; try again")); + } + // The confirmation asked for a second factor, or didn't, by what the account had then. + if two_factor::is_enabled(&mut w.tx, actor.user).await? != actor.two_factor { + return Err(MetaError::Conflict("your two-factor sign-in changed while this request was running; try again")); + } + Ok(w) + } + + async fn target(w: &mut WriteTx, user: UserId) -> Result { + let row = sqlx::query("SELECT role, password_hash, disabled_ms, removed_ms FROM users WHERE id = $1") + .bind(uuid_bytes(&user.0)) + .fetch_optional(&mut *w.tx) + .await? + .ok_or(MetaError::NotFound("user"))?; + Ok(Target { + role: Role::parse(&row.try_get::("role")?)?, + disabled: row.try_get::, _>("disabled_ms")?.is_some(), + removed: row.try_get::, _>("removed_ms")?.is_some(), + has_password: !row.try_get::("password_hash")?.is_empty(), + }) + } + + /// `LastAdmin` unless an administrator who is not disabled and not marked for removal is left, counted in `w` + /// after the `last_admin` lock was taken and the change was made. + async fn keep_an_admin(w: &mut WriteTx) -> Result<(), MetaError> { + let row = sqlx::query("SELECT COUNT(*) AS n FROM users WHERE role = 'admin' AND disabled_ms IS NULL AND removed_ms IS NULL") + .fetch_one(&mut *w.tx) + .await?; + if row.try_get::("n")? < 1 { Err(MetaError::LastAdmin) } else { Ok(()) } + } + + /// The counters of `GET /admin/server`: accounts that are not marked for removal, devices that are not revoked, and + /// the bytes accounts store and their live documents hold. + pub async fn admin_server_counts(&self) -> Result { + let count = |row: sqlx::any::AnyRow, column: &str| -> Result { to_u64(row.try_get(column)?) }; + // Postgres sums BIGINT as NUMERIC, which the `Any` driver can't read: cast back. + let users = sqlx::query("SELECT COUNT(*) AS n FROM users WHERE removed_ms IS NULL").fetch_one(&self.pool).await?; + let devices = sqlx::query("SELECT COUNT(*) AS n FROM devices WHERE revoked = 0").fetch_one(&self.pool).await?; + let bytes = sqlx::query( + "SELECT CAST(COALESCE(SUM(stored_bytes), 0) AS BIGINT) AS stored, CAST(COALESCE(SUM(live_bytes), 0) AS BIGINT) AS live FROM spaces", + ) + .fetch_one(&self.pool) + .await?; + Ok(ServerCounts { + users: count(users, "n")?, + devices_active: count(devices, "n")?, + stored_bytes: to_u64(bytes.try_get("stored")?)?, + live_bytes: to_u64(bytes.try_get("live")?)?, + }) + } + + /// One page of accounts that are not marked for removal, by username ascending: those after `after` (exclusive) + /// whose username contains `search` (case does not matter, `%` and `_` are ordinary characters), at most `limit`. + /// `next_after` is set when there are more. A `search` over [`MAX_SEARCH_BYTES`] is `Invalid`. + pub async fn admin_list_users(&self, after: Option<&str>, search: Option<&str>, limit: u32, now_ms: i64) -> Result { + let search = search.unwrap_or_default(); + if search.len() > MAX_SEARCH_BYTES { + return Err(MetaError::Invalid("the search is at most 64 bytes")); + } + let pattern = format!("%{}%", escape_like(&search.to_ascii_lowercase())); + let sql = + format!("{USER_SELECT} WHERE u.removed_ms IS NULL AND u.username > $2 AND u.username LIKE $3 ESCAPE '\\' ORDER BY u.username LIMIT $4"); + let fetch = i64::from(limit).saturating_add(1); + let rows = sqlx::query(sqlx::AssertSqlSafe(sql)) + .bind(now_ms) + .bind(after.unwrap_or_default()) + .bind(pattern) + .bind(fetch) + .fetch_all(&self.pool) + .await?; + let mut users = rows.iter().map(admin_user_from).collect::, _>>()?; + let more = users.len() > limit as usize; + users.truncate(limit as usize); + let next_after = if more { users.last().map(|u| u.username.clone()) } else { None }; + Ok(UserPage { users, next_after }) + } + + /// One account, marked for removal or not; `None` if there is no such user. + pub async fn admin_user(&self, user: UserId, now_ms: i64) -> Result, MetaError> { + let row = sqlx::query(sqlx::AssertSqlSafe(format!("{USER_SELECT} WHERE u.id = $2"))) + .bind(now_ms) + .bind(uuid_bytes(&user.0)) + .fetch_optional(&self.pool) + .await?; + row.as_ref().map(admin_user_from).transpose() + } + + /// Makes an account for `username` with `role`, no password and its own space, and an invite under `code_hash` + /// (good for `ttl_ms`) the person redeems to choose a password. A name that is taken, by any account (one marked for + /// removal too), is `UsernameTaken`. + pub async fn admin_create_user( + &self, + actor: &AdminActor, + username: &str, + role: Role, + code_hash: &[u8; 32], + now_ms: i64, + ttl_ms: i64, + ) -> Result<(AdminUser, Issued), MetaError> { + if !valid_username(username) { + return Err(MetaError::Invalid("usernames are 1-64 letters, digits, '.', '_' or '-'")); + } + let username = username.to_ascii_lowercase(); + let expires_ms = now_ms.checked_add(ttl_ms).ok_or(MetaError::Invalid("the invite's lifetime is too long"))?; + let mut w = self.begin_admin(actor, None, false).await?; + let taken = sqlx::query("SELECT 1 FROM users WHERE username = $1").bind(username.as_str()).fetch_optional(&mut *w.tx).await?; + if taken.is_some() { + return Err(MetaError::UsernameTaken); + } + let ids = (UserId(Uuid::now_v7()), SpaceId::generate(), NodeId::generate()); + let user = insert_user(&mut w.tx, &username, "", role, now_ms, ids).await?.id; + put_code(&mut w.tx, user, code_hash, Purpose::Invite, false, now_ms, expires_ms).await?; + let created = by_admin(actor, AuditAction::AdminUserCreated, user).with(AuditDetails { role_to: Some(role), ..AuditDetails::default() }); + record_in(&mut w.tx, now_ms, created).await?; + self.commit_audited(w, now_ms).await?; + let made = self.admin_user(user, now_ms).await?.ok_or(MetaError::Corrupt("a new account is missing"))?; + Ok((made, Issued { expires_ms })) + } + + /// Changes an account's role and/or its quota: `role` `None` leaves the role, `quota` `None` leaves the quota, + /// `Some(None)` removes it, `Some(Some(n))` sets it (at least [`MIN_QUOTA_BYTES`], at most `i64::MAX`). Demoting the + /// last administrator is `LastAdmin`; an account marked for removal is `Conflict`. + pub async fn admin_update_user( + &self, + actor: &AdminActor, + target: UserId, + role: Option, + quota: Option>, + now_ms: i64, + ) -> Result { + if role.is_none() && quota.is_none() { + return Err(MetaError::Invalid("give a role or a quota_bytes to change")); + } + if let Some(Some(bytes)) = quota + && !(MIN_QUOTA_BYTES..=i64::MAX as u64).contains(&bytes) + { + return Err(MetaError::Invalid("quota_bytes is at least one chunk (4194304) and at most 9223372036854775807")); + } + let mut w = self.begin_admin(actor, Some(target), role == Some(Role::Member)).await?; + let found = Self::target(&mut w, target).await?; + if found.removed { + return Err(MetaError::Conflict("this account is marked for removal")); + } + if let Some(role) = role { + sqlx::query("UPDATE users SET role = $1 WHERE id = $2").bind(role.as_str()).bind(uuid_bytes(&target.0)).execute(&mut *w.tx).await?; + if found.role == Role::Admin && role == Role::Member { + Self::keep_an_admin(&mut w).await?; + } + // One event for the role and one for the quota when both change, in this transaction. A role the account has + // already is no change and has none. + if role != found.role { + let details = AuditDetails { role_from: Some(found.role), role_to: Some(role), ..AuditDetails::default() }; + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminRoleChanged, target).with(details)).await?; + } + } + if let Some(quota) = quota { + let quota = quota.map(to_i64).transpose()?; + let before: Option = sqlx::query("SELECT quota_bytes FROM spaces WHERE owner = $1") + .bind(uuid_bytes(&target.0)) + .fetch_optional(&mut *w.tx) + .await? + .map(|row| row.try_get("quota_bytes")) + .transpose()? + .flatten(); + sqlx::query("UPDATE spaces SET quota_bytes = $1 WHERE owner = $2").bind(quota).bind(uuid_bytes(&target.0)).execute(&mut *w.tx).await?; + if quota != before { + let details = AuditDetails { quota_bytes: Some(quota.map(to_u64).transpose()?), ..AuditDetails::default() }; + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminQuotaChanged, target).with(details)).await?; + } + } + self.commit_audited(w, now_ms).await?; + self.admin_user(target, now_ms).await?.ok_or(MetaError::NotFound("user")) + } + + /// Disables an account: sets `disabled_ms`, signs every device out ([`RevokeReason::Disabled`]) and deletes the + /// account's invite, reset code and pairings. Disabling one's own account is `Invalid`, the last enabled + /// administrator `LastAdmin`; an account that is disabled already is left as it is. Returns the devices signed out. + pub async fn admin_disable_user(&self, actor: &AdminActor, target: UserId, now_ms: i64) -> Result, MetaError> { + if same_user(actor.user, target) { + return Err(MetaError::Invalid("you can't disable your own account")); + } + let mut w = self.begin_admin(actor, Some(target), true).await?; + let found = Self::target(&mut w, target).await?; + sqlx::query("UPDATE users SET disabled_ms = COALESCE(disabled_ms, $1), auth_gen = auth_gen + 1 WHERE id = $2") + .bind(now_ms) + .bind(uuid_bytes(&target.0)) + .execute(&mut *w.tx) + .await?; + Self::keep_an_admin(&mut w).await?; + let revoked = revoke_devices(&mut w.tx, target, None, RevokeReason::Disabled, now_ms).await?; + delete_join_codes(&mut w.tx, target).await?; + // The devices signed out with it are counted in the event. An account that was disabled already is no change. + if !found.disabled { + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminUserDisabled, target).with(AuditDetails::revoked(revoked.len()))).await?; + } + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// Enables a disabled account. Devices that were signed out stay signed out. One marked for removal is `Conflict`. + /// An account that is not disabled is left as it is and writes no event. + pub async fn admin_enable_user(&self, actor: &AdminActor, target: UserId, now_ms: i64) -> Result<(), MetaError> { + let mut w = self.begin_admin(actor, Some(target), false).await?; + let found = Self::target(&mut w, target).await?; + if found.removed { + return Err(MetaError::Conflict("this account is marked for removal")); + } + sqlx::query("UPDATE users SET disabled_ms = NULL, auth_gen = auth_gen + 1 WHERE id = $1") + .bind(uuid_bytes(&target.0)) + .execute(&mut *w.tx) + .await?; + if found.disabled { + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminUserEnabled, target)).await?; + } + self.commit_audited(w, now_ms).await + } + + /// Marks an account for removal: as [`Meta::admin_disable_user`], and also sets `removed_ms`, which leaves it out of + /// the lists and cannot be undone here. Nothing it stored is deleted; its username stays taken. Removing one's + /// own account is `Invalid`, the last enabled administrator `LastAdmin`. Returns the devices signed out. + pub async fn admin_remove_user(&self, actor: &AdminActor, target: UserId, now_ms: i64) -> Result, MetaError> { + if same_user(actor.user, target) { + return Err(MetaError::Invalid("you can't remove your own account")); + } + let mut w = self.begin_admin(actor, Some(target), true).await?; + let found = Self::target(&mut w, target).await?; + sqlx::query( + "UPDATE users SET removed_ms = COALESCE(removed_ms, $1), disabled_ms = COALESCE(disabled_ms, $1), auth_gen = auth_gen + 1 WHERE id = $2", + ) + .bind(now_ms) + .bind(uuid_bytes(&target.0)) + .execute(&mut *w.tx) + .await?; + Self::keep_an_admin(&mut w).await?; + let revoked = revoke_devices(&mut w.tx, target, None, RevokeReason::Disabled, now_ms).await?; + delete_join_codes(&mut w.tx, target).await?; + // The event outlives the account, which stays only as a mark. Marked already: no change, no event. + if !found.removed { + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminUserRemoved, target).with(AuditDetails::revoked(revoked.len()))).await?; + } + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// An invite under `code_hash` for an account that has no password yet (one nobody redeemed an invite for), replacing + /// its open invite or reset code. An account with a password is `Conflict`: an invite adds a computer without asking + /// for the password, which would give whoever holds it the account's files; a reset code is the way in there. A + /// disabled account, or one marked for removal, is `Conflict` too. + pub async fn admin_issue_invite( + &self, + actor: &AdminActor, + target: UserId, + code_hash: &[u8; 32], + now_ms: i64, + ttl_ms: i64, + ) -> Result { + let expires_ms = now_ms.checked_add(ttl_ms).ok_or(MetaError::Invalid("the invite's lifetime is too long"))?; + let mut w = self.begin_admin(actor, Some(target), false).await?; + let found = Self::target(&mut w, target).await?; + if found.removed || found.disabled { + return Err(MetaError::Conflict("this account is disabled or marked for removal")); + } + if found.has_password { + return Err(MetaError::Conflict("this account has a password: make a password reset code for it instead")); + } + put_code(&mut w.tx, target, code_hash, Purpose::Invite, false, now_ms, expires_ms).await?; + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminInviteIssued, target)).await?; + self.commit_audited(w, now_ms).await?; + Ok(Issued { expires_ms }) + } + + /// Deletes the account's open invite or reset code; `NotFound` when there is none (so nothing changed and nothing is + /// written). + pub async fn admin_revoke_invite(&self, actor: &AdminActor, target: UserId, now_ms: i64) -> Result<(), MetaError> { + let mut w = self.begin_admin(actor, Some(target), false).await?; + Self::target(&mut w, target).await?; + let done = sqlx::query("DELETE FROM invites WHERE user_id = $1").bind(uuid_bytes(&target.0)).execute(&mut *w.tx).await?; + if done.rows_affected() == 0 { + return Err(MetaError::NotFound("invite")); + } + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminInviteRevoked, target)).await?; + self.commit_audited(w, now_ms).await + } + + /// A password reset code under `code_hash`, good for `ttl_ms`, replacing the account's open invite or reset code. + /// Redeeming it sets a new password and signs every device of the account out ([`Meta::redeem`]). Nothing changes + /// until then. With `reset_two_factor`, redeeming it also turns the account's two-factor sign-in off; without, an + /// account that has it needs its second factor to redeem the code. Resetting one's own password is `Invalid` (change + /// it), another administrator's `Forbidden` unless the acting admin has two-factor sign-in on themselves. A disabled + /// account, or one marked for removal, is `Conflict`. + pub async fn admin_issue_reset( + &self, + actor: &AdminActor, + target: UserId, + code_hash: &[u8; 32], + reset_two_factor: bool, + now_ms: i64, + ttl_ms: i64, + ) -> Result { + if same_user(actor.user, target) { + return Err(MetaError::Invalid("change your own password in your account instead")); + } + let expires_ms = now_ms.checked_add(ttl_ms).ok_or(MetaError::Invalid("the code's lifetime is too long"))?; + let mut w = self.begin_admin(actor, Some(target), false).await?; + let found = Self::target(&mut w, target).await?; + if found.removed || found.disabled { + return Err(MetaError::Conflict("this account is disabled or marked for removal")); + } + Self::admin_target_needs_two_factor(&found, actor)?; + put_code(&mut w.tx, target, code_hash, Purpose::Reset, reset_two_factor, now_ms, expires_ms).await?; + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminPasswordResetIssued, target)).await?; + self.commit_audited(w, now_ms).await?; + Ok(Issued { expires_ms }) + } + + /// Takes away another account's two-factor sign-in: the secret, the codes and the challenges go, every device is signed + /// out ([`RevokeReason::TwoFactor`]) and the invite and pairings are deleted, in one transaction (the target's lock + /// and the acting admin's checks are as for every admin change). Returns the devices signed out, for the caller to end + /// their live connections. Your own account is `Invalid` (turn it off in your account), an account without two-factor + /// sign-in `Conflict` (nothing is signed out), another administrator's `Forbidden` unless the acting admin has + /// two-factor sign-in on themselves. + pub async fn admin_reset_two_factor(&self, actor: &AdminActor, target: UserId, now_ms: i64) -> Result, MetaError> { + if same_user(actor.user, target) { + return Err(MetaError::Invalid("turn your own two-factor sign-in off in your account instead")); + } + let mut w = self.begin_admin(actor, Some(target), false).await?; + let found = Self::target(&mut w, target).await?; + Self::admin_target_needs_two_factor(&found, actor)?; + let revoked = two_factor::reset_in(&mut w.tx, target, now_ms).await?.ok_or(MetaError::Conflict("this account has no two-factor sign-in"))?; + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminTwoFactorReset, target)).await?; + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// Resetting another **administrator's** password or two-factor sign-in takes the acting admin's own two-factor + /// sign-in: a stolen admin token and password alone can't take over the other admins' accounts. (`begin_admin` has + /// compared `actor.two_factor` with the account's state under the lock, so it is the truth here.) + fn admin_target_needs_two_factor(target: &Target, actor: &AdminActor) -> Result<(), MetaError> { + if target.role == Role::Admin && !actor.two_factor { + return Err(MetaError::Forbidden( + "resetting another administrator needs two-factor sign-in on your own account: turn it on first, or use the command line on the server", + )); + } + Ok(()) + } + + /// All of an account's devices, revoked ones too; `NotFound` for a user that isn't there. + pub async fn admin_devices(&self, target: UserId) -> Result, MetaError> { + let found = sqlx::query("SELECT 1 FROM users WHERE id = $1").bind(uuid_bytes(&target.0)).fetch_optional(&self.pool).await?; + if found.is_none() { + return Err(MetaError::NotFound("user")); + } + self.list_devices(target).await + } + + /// Signs one of `target`'s devices out ([`RevokeReason::Admin`]) and deletes the pairing it made. A device that is + /// revoked already stays as it is. A device of another account, or an unknown user, is `NotFound`. + pub async fn admin_revoke_device(&self, actor: &AdminActor, target: UserId, device: DeviceId, now_ms: i64) -> Result<(), MetaError> { + let mut w = self.begin_admin(actor, Some(target), false).await?; + Self::target(&mut w, target).await?; + let done = sqlx::query("UPDATE devices SET revoked = 1, revoked_ms = $1, revoke_reason = $2 WHERE id = $3 AND user_id = $4 AND revoked = 0") + .bind(now_ms) + .bind(RevokeReason::Admin.as_str()) + .bind(uuid_bytes(device.as_uuid())) + .bind(uuid_bytes(&target.0)) + .execute(&mut *w.tx) + .await?; + if done.rows_affected() == 0 { + let owned = sqlx::query("SELECT 1 FROM devices WHERE id = $1 AND user_id = $2") + .bind(uuid_bytes(device.as_uuid())) + .bind(uuid_bytes(&target.0)) + .fetch_optional(&mut *w.tx) + .await?; + if owned.is_none() { + return Err(MetaError::NotFound("device")); + } + } + sqlx::query("DELETE FROM pairings WHERE device_id = $1").bind(uuid_bytes(device.as_uuid())).execute(&mut *w.tx).await?; + // The event is about the account, whose own activity then shows it, and names the device. A device signed out + // already is no change. + if done.rows_affected() > 0 { + let details = AuditDetails { device: Some(device), reason: Some(RevokeReason::Admin), ..AuditDetails::default() }; + record_in(&mut w.tx, now_ms, by_admin(actor, AuditAction::AdminDeviceRevoked, target).with(details)).await?; + } + self.commit_audited(w, now_ms).await + } +} diff --git a/crates/meta/src/audit.rs b/crates/meta/src/audit.rs new file mode 100644 index 00000000..cfd7452d --- /dev/null +++ b/crates/meta/src/audit.rs @@ -0,0 +1,990 @@ +//! The security audit log: events in the `audit_events` table, written in the transaction of the change they record, +//! and read back by the account's own activity, the admin's list and export, and the command line. +//! +//! - **Names are a closed list** ([`AuditAction`]); no event has a name a client typed. +//! - **`details` holds typed values only** ([`AuditDetails`]): every field is a closed enumeration or a number, each +//! event allows only its own fields ([`AuditAction::fields`]), and nothing a client sent as text ever reaches it. In +//! particular `platform` is reduced to [`Platform`] (anything unknown is `other`), because a device may call its +//! platform any printable text of 32 bytes, an address or a file name included. What is written is at most +//! [`MAX_DETAILS_BYTES`] of JSON: when it would be more, whole fields go (the least important first) and +//! `"truncated": true` says so; bytes are never cut off, which would leave invalid JSON. +//! - **No addresses, file or device names, user names of unknown accounts, secrets** are stored anywhere in a row. +//! An account is named by its ID, which outlives the account (there are no foreign keys). +//! - **Changes write their event in their own transaction, before the commit, and fail with it** (fail closed): an event +//! that cannot be written means the change is not made. A failed sign-in changes nothing, so its event is written +//! after the fact in a transaction of its own ([`Meta::audit_record_failure`]), and a failure to write it is for the +//! caller to log: it must never change the answer or undo the abuse protection around it. +//! - **Append-only, in the code.** Nothing here updates or deletes a row, with two named exceptions: the counter of an +//! aggregated failure (one row per account, action, client kind and minute, `agg_count + 1` on a collision) and +//! [`Meta::audit_prune`], which deletes events older than the retention in batches of their own, never inside the +//! transaction of a security change. A test reads this crate's sources and allows exactly those two statements. + +use craft_cloud_proto::DeviceId; +use serde_json::{Map, Value}; +use sqlx::Row; +use uuid::Uuid; + +use crate::codec::{to_u64, uuid_bytes, uuid_from}; +use crate::users::{DeviceClient, RevokeReason, Role, UserId}; +use crate::{Meta, MetaError}; + +/// The most serialized UTF-8 bytes of an event's `details`. +pub const MAX_DETAILS_BYTES: usize = 2048; + +/// Rows one pruning statement deletes. +pub const PRUNE_BATCH: i64 = 1000; +/// Batches one opportunistic pruning (after a write) runs before it leaves the rest to the next one. +pub const OPPORTUNISTIC_PRUNE_BATCHES: usize = 20; +/// How often at most a write starts a pruning. +pub const PRUNE_EVERY_MS: i64 = 60 * 60 * 1000; +/// The shortest and longest retention `[audit] retention_days` may name. +pub const MIN_RETENTION_DAYS: u32 = 1; +pub const MAX_RETENTION_DAYS: u32 = 3650; + +const MINUTE_MS: i64 = 60_000; + +/// What an event is. The names are the ones in `docs/design/sync.md`; a name is never reused for something else. +/// +/// Not every name has a writer yet: the two-factor events (`auth.second_factor_failed`, `auth.recovery_code_used`, +/// `auth.two_factor_locked`, `account.two_factor_*`, `account.recovery_codes_regenerated`, `admin.two_factor_reset`, +/// `cli.two_factor_reset`) and `cli.user_removed` are written by the changes that add those operations. The names are +/// listed here so that the list a client may filter by does not change then. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum AuditAction { + AuthLogin, + AuthLoginFailed, + AuthSecondFactorFailed, + AuthRecoveryCodeUsed, + AuthTwoFactorLocked, + DeviceRevoked, + DeviceRevokedOthers, + AccountPasswordChanged, + AccountTwoFactorEnabled, + AccountTwoFactorDisabled, + AccountRecoveryCodesRegenerated, + AdminUserCreated, + AdminUserDisabled, + AdminUserEnabled, + AdminUserRemoved, + AdminRoleChanged, + AdminQuotaChanged, + AdminInviteIssued, + AdminInviteRevoked, + AdminPasswordResetIssued, + AdminTwoFactorReset, + AdminDeviceRevoked, + JoinInviteRedeemed, + JoinResetRedeemed, + JoinPairingRedeemed, + CliPasswordReset, + CliTwoFactorReset, + CliDeviceRevoked, + CliDevicesRevokedAll, + CliUserRemoved, +} + +impl AuditAction { + /// Every action, in the order the documentation lists them. + pub const ALL: [AuditAction; 30] = [ + AuditAction::AuthLogin, + AuditAction::AuthLoginFailed, + AuditAction::AuthSecondFactorFailed, + AuditAction::AuthRecoveryCodeUsed, + AuditAction::AuthTwoFactorLocked, + AuditAction::DeviceRevoked, + AuditAction::DeviceRevokedOthers, + AuditAction::AccountPasswordChanged, + AuditAction::AccountTwoFactorEnabled, + AuditAction::AccountTwoFactorDisabled, + AuditAction::AccountRecoveryCodesRegenerated, + AuditAction::AdminUserCreated, + AuditAction::AdminUserDisabled, + AuditAction::AdminUserEnabled, + AuditAction::AdminUserRemoved, + AuditAction::AdminRoleChanged, + AuditAction::AdminQuotaChanged, + AuditAction::AdminInviteIssued, + AuditAction::AdminInviteRevoked, + AuditAction::AdminPasswordResetIssued, + AuditAction::AdminTwoFactorReset, + AuditAction::AdminDeviceRevoked, + AuditAction::JoinInviteRedeemed, + AuditAction::JoinResetRedeemed, + AuditAction::JoinPairingRedeemed, + AuditAction::CliPasswordReset, + AuditAction::CliTwoFactorReset, + AuditAction::CliDeviceRevoked, + AuditAction::CliDevicesRevokedAll, + AuditAction::CliUserRemoved, + ]; + + pub fn as_str(self) -> &'static str { + match self { + AuditAction::AuthLogin => "auth.login", + AuditAction::AuthLoginFailed => "auth.login_failed", + AuditAction::AuthSecondFactorFailed => "auth.second_factor_failed", + AuditAction::AuthRecoveryCodeUsed => "auth.recovery_code_used", + AuditAction::AuthTwoFactorLocked => "auth.two_factor_locked", + AuditAction::DeviceRevoked => "device.revoked", + AuditAction::DeviceRevokedOthers => "device.revoked_others", + AuditAction::AccountPasswordChanged => "account.password_changed", + AuditAction::AccountTwoFactorEnabled => "account.two_factor_enabled", + AuditAction::AccountTwoFactorDisabled => "account.two_factor_disabled", + AuditAction::AccountRecoveryCodesRegenerated => "account.recovery_codes_regenerated", + AuditAction::AdminUserCreated => "admin.user_created", + AuditAction::AdminUserDisabled => "admin.user_disabled", + AuditAction::AdminUserEnabled => "admin.user_enabled", + AuditAction::AdminUserRemoved => "admin.user_removed", + AuditAction::AdminRoleChanged => "admin.role_changed", + AuditAction::AdminQuotaChanged => "admin.quota_changed", + AuditAction::AdminInviteIssued => "admin.invite_issued", + AuditAction::AdminInviteRevoked => "admin.invite_revoked", + AuditAction::AdminPasswordResetIssued => "admin.password_reset_issued", + AuditAction::AdminTwoFactorReset => "admin.two_factor_reset", + AuditAction::AdminDeviceRevoked => "admin.device_revoked", + AuditAction::JoinInviteRedeemed => "join.invite_redeemed", + AuditAction::JoinResetRedeemed => "join.reset_redeemed", + AuditAction::JoinPairingRedeemed => "join.pairing_redeemed", + AuditAction::CliPasswordReset => "cli.password_reset", + AuditAction::CliTwoFactorReset => "cli.two_factor_reset", + AuditAction::CliDeviceRevoked => "cli.device_revoked", + AuditAction::CliDevicesRevokedAll => "cli.devices_revoked_all", + AuditAction::CliUserRemoved => "cli.user_removed", + } + } + + /// The action with this exact name. + pub fn parse(name: &str) -> Option { + Self::ALL.into_iter().find(|action| action.as_str() == name) + } + + /// The `details` fields this event may have; the writer drops any other. Listed in the order of importance, which is + /// the order they are written in and the reverse of the order they are dropped in when the text is too long. + pub fn fields(self) -> &'static [Field] { + use Field::*; + match self { + AuditAction::AuthLogin | AuditAction::JoinInviteRedeemed | AuditAction::JoinPairingRedeemed => &[ClientKind, Platform], + AuditAction::JoinResetRedeemed => &[Revoked, TwoFactorRemoved, ClientKind, Platform], + AuditAction::AuthLoginFailed | AuditAction::AuthSecondFactorFailed => &[ClientKind, Step], + AuditAction::AuthRecoveryCodeUsed => &[ClientKind], + AuditAction::DeviceRevoked => &[Reason], + AuditAction::DeviceRevokedOthers => &[Revoked, Reason], + AuditAction::AdminUserCreated => &[RoleTo], + AuditAction::AdminRoleChanged => &[RoleFrom, RoleTo], + AuditAction::AdminQuotaChanged => &[QuotaBytes], + AuditAction::AdminDeviceRevoked => &[Device, Reason], + AuditAction::CliDeviceRevoked => &[Device, Reason], + AuditAction::AccountPasswordChanged + | AuditAction::AdminUserDisabled + | AuditAction::AdminUserRemoved + | AuditAction::CliPasswordReset + | AuditAction::CliTwoFactorReset + | AuditAction::CliDevicesRevokedAll + | AuditAction::CliUserRemoved => &[Revoked], + AuditAction::AccountTwoFactorEnabled + | AuditAction::AccountTwoFactorDisabled + | AuditAction::AccountRecoveryCodesRegenerated + | AuditAction::AdminUserEnabled + | AuditAction::AdminInviteIssued + | AuditAction::AdminInviteRevoked + | AuditAction::AdminPasswordResetIssued + | AuditAction::AdminTwoFactorReset => &[], + AuditAction::AuthTwoFactorLocked => &[ClientKind], + } + } +} + +/// What a `details` field is. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Field { + ClientKind, + Platform, + Reason, + Step, + RoleFrom, + RoleTo, + QuotaBytes, + Revoked, + Device, + /// A reset code redeemed for an account that had two-factor sign-in turned it off. + TwoFactorRemoved, +} + +/// The platforms an event names. A device may call its platform any printable text; only these reach the log. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Platform { + Windows, + Macos, + Linux, + Android, + Ios, + Other, +} + +impl Platform { + pub fn as_str(self) -> &'static str { + match self { + Platform::Windows => "windows", + Platform::Macos => "macos", + Platform::Linux => "linux", + Platform::Android => "android", + Platform::Ios => "ios", + Platform::Other => "other", + } + } + + /// The platform a client named: `None` for none at all, one of the list when it is that (case does not matter), + /// `Other` for anything else, whatever it looks like. + pub fn normalize(platform: &str) -> Option { + if platform.is_empty() { + return None; + } + Some( + [Platform::Windows, Platform::Macos, Platform::Linux, Platform::Android, Platform::Ios] + .into_iter() + .find(|known| known.as_str().eq_ignore_ascii_case(platform)) + .unwrap_or(Platform::Other), + ) + } +} + +/// The client kinds an event names. Device kinds are [`crate::CLIENT_KINDS`]; `Web` is assigned only by web sign-in code. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ClientKind { + Agent, + Desktop, + Cli, + Web, +} + +impl ClientKind { + pub fn as_str(self) -> &'static str { + match self { + ClientKind::Agent => "agent", + ClientKind::Desktop => "desktop", + ClientKind::Cli => "cli", + ClientKind::Web => "web", + } + } + + /// Parses device-sent kinds only. Web is assigned by the server, never accepted from a device. + fn parse(kind: &str) -> Option { + [ClientKind::Agent, ClientKind::Desktop, ClientKind::Cli].into_iter().find(|known| known.as_str() == kind) + } +} + +/// How far a failed sign-in got. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Step { + Password, + Totp, + Recovery, + ChallengeExpired, +} + +impl Step { + pub fn as_str(self) -> &'static str { + match self { + Step::Password => "password", + Step::Totp => "totp", + Step::Recovery => "recovery", + Step::ChallengeExpired => "challenge_expired", + } + } +} + +/// The typed values of an event. Which of them an event keeps is decided by [`AuditAction::fields`]. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct AuditDetails { + pub client_kind: Option, + pub platform: Option, + pub reason: Option, + pub step: Option, + pub role_from: Option, + pub role_to: Option, + /// `Some(None)`: the quota was removed. + pub quota_bytes: Option>, + pub revoked: Option, + pub device: Option, + /// Redeeming a reset code also took the account's two-factor sign-in away (only ever set to `true`). + pub two_factor_removed: bool, +} + +impl AuditDetails { + /// What a device says about its client, reduced to the closed lists. + pub fn client(client: &DeviceClient) -> Self { + Self { client_kind: ClientKind::parse(&client.kind), platform: Platform::normalize(&client.platform), ..Self::default() } + } + + pub fn revoked(count: usize) -> Self { + Self { revoked: Some(u32::try_from(count).unwrap_or(u32::MAX)), ..Self::default() } + } + + pub fn reason(reason: RevokeReason) -> Self { + Self { reason: Some(reason), ..Self::default() } + } + + pub fn step(step: Step) -> Self { + Self { step: Some(step), ..Self::default() } + } + + pub fn web() -> Self { + Self { client_kind: Some(ClientKind::Web), ..Self::default() } + } + + /// The value of `field`, if it is set. + fn value(&self, field: Field) -> Option { + match field { + Field::ClientKind => self.client_kind.map(|v| Value::from(v.as_str())), + Field::Platform => self.platform.map(|v| Value::from(v.as_str())), + Field::Reason => self.reason.map(|v| Value::from(v.as_str())), + Field::Step => self.step.map(|v| Value::from(v.as_str())), + Field::RoleFrom => self.role_from.map(|v| Value::from(v.as_str())), + Field::RoleTo => self.role_to.map(|v| Value::from(v.as_str())), + Field::QuotaBytes => self.quota_bytes.map(|v| v.map_or(Value::Null, Value::from)), + Field::Revoked => self.revoked.map(Value::from), + Field::Device => self.device.map(|v| Value::from(v.to_string())), + Field::TwoFactorRemoved => self.two_factor_removed.then_some(Value::Bool(true)), + } + } + + fn name(field: Field) -> &'static str { + match field { + Field::ClientKind => "client_kind", + Field::Platform => "platform", + Field::Reason => "reason", + Field::Step => "step", + Field::RoleFrom => "role_from", + Field::RoleTo => "role_to", + Field::QuotaBytes => "quota_bytes", + Field::Revoked => "revoked", + Field::Device => "device", + Field::TwoFactorRemoved => "two_factor_removed", + } + } + + /// The JSON text stored for `action`: the fields it allows that are set, within [`MAX_DETAILS_BYTES`]. + pub fn to_json(&self, action: AuditAction) -> String { + let fields = action.fields().iter().filter_map(|field| self.value(*field).map(|value| (Self::name(*field), value))).collect(); + serialize_fields(fields, MAX_DETAILS_BYTES) + } +} + +/// `fields` (most important first) as a JSON object of at most `max` bytes. When it would be longer, fields are dropped +/// from the end, one at a time, and `"truncated": true` is added; no text is ever cut short. +pub(crate) fn serialize_fields(mut fields: Vec<(&'static str, Value)>, max: usize) -> String { + let mut truncated = false; + loop { + let mut object = Map::new(); + for (name, value) in &fields { + object.insert((*name).to_owned(), value.clone()); + } + if truncated { + object.insert("truncated".to_owned(), Value::Bool(true)); + } + let text = Value::Object(object).to_string(); + if text.len() <= max { + return text; + } + if fields.pop().is_none() { + // Not reachable for any `max` this crate uses (the flag alone is 18 bytes), but never invalid JSON. + return "{\"truncated\":true}".to_owned(); + } + truncated = true; + } +} + +/// Whether the event is about something that worked. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AuditOutcome { + Success, + Failure, +} + +impl AuditOutcome { + pub fn as_str(self) -> &'static str { + match self { + AuditOutcome::Success => "success", + AuditOutcome::Failure => "failure", + } + } + + fn parse(s: &str) -> Result { + match s { + "success" => Ok(AuditOutcome::Success), + "failure" => Ok(AuditOutcome::Failure), + _ => Err(MetaError::Corrupt("unknown audit outcome")), + } + } +} + +/// What an event was done to. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TargetKind { + User, + Device, +} + +impl TargetKind { + pub fn as_str(self) -> &'static str { + match self { + TargetKind::User => "user", + TargetKind::Device => "device", + } + } +} + +/// Who made a change that a signed-in account or the command line can make, for its event. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Origin { + /// The account's own act, from a device the caller does not say. + Account, + /// The account's own act, from this device. + Device(DeviceId), + /// The admin's command line on the server's host: no account acts. + Cli, +} + +/// An event on its way into the log. +#[derive(Debug, Clone, Copy)] +pub(crate) struct NewEvent { + pub action: AuditAction, + pub actor_user: Option, + pub actor_device: Option, + pub target: Option<(TargetKind, Uuid)>, + pub details: AuditDetails, +} + +impl NewEvent { + pub fn new(action: AuditAction) -> Self { + Self { action, actor_user: None, actor_device: None, target: None, details: AuditDetails::default() } + } + + pub fn by(mut self, user: UserId, device: Option) -> Self { + self.actor_user = Some(user); + self.actor_device = device; + self + } + + pub fn on_user(mut self, user: UserId) -> Self { + self.target = Some((TargetKind::User, user.0)); + self + } + + pub fn on_device(mut self, device: DeviceId) -> Self { + self.target = Some((TargetKind::Device, *device.as_uuid())); + self + } + + pub fn with(mut self, details: AuditDetails) -> Self { + self.details = details; + self + } +} + +const INSERT: &str = concat!( + "INSERT INTO audit_events (id, at_ms, actor_user, actor_device, action, target_kind, target_id, outcome, details, agg_key, agg_count) ", + "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, 1)" +); + +/// Inserts `event` on `conn` (a transaction's connection). `aggregate`: the key of an aggregated failure, whose row +/// only counts up when the key is there already; `None` for every other event, whose key is `NULL`. +async fn insert( + conn: &mut sqlx::AnyConnection, + event: &NewEvent, + outcome: AuditOutcome, + now_ms: i64, + aggregate: Option, +) -> Result<(), MetaError> { + // Qualified: on Postgres a bare `agg_count` is ambiguous between the table's column and `excluded`'s. Only the + // counter changes, so the row keeps the identity, time and details of its first try. + let sql = if aggregate.is_some() { + format!("{INSERT} ON CONFLICT (agg_key) DO UPDATE SET agg_count = audit_events.agg_count + 1") + } else { + INSERT.to_owned() + }; + sqlx::query(sqlx::AssertSqlSafe(sql)) + .bind(uuid_bytes(&Uuid::now_v7())) + .bind(now_ms) + .bind(event.actor_user.map(|u| uuid_bytes(&u.0))) + .bind(event.actor_device.map(|d| uuid_bytes(d.as_uuid()))) + .bind(event.action.as_str()) + .bind(event.target.map_or("", |(kind, _)| kind.as_str())) + .bind(event.target.map(|(_, id)| uuid_bytes(&id))) + .bind(outcome.as_str()) + .bind(event.details.to_json(event.action)) + .bind(aggregate) + .execute(conn) + .await?; + Ok(()) +} + +/// Writes `event` as a success in `tx`, the transaction of the change it records. Its error fails the change. +pub(crate) async fn record_in(tx: &mut sqlx::Transaction<'static, sqlx::Any>, now_ms: i64, event: NewEvent) -> Result<(), MetaError> { + insert(tx, &event, AuditOutcome::Success, now_ms, None).await +} + +/// A failed sign-in step, counted by action, client kind and minute. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct AuditFailure { + /// `AuthLoginFailed` or `AuthSecondFactorFailed`; any other action is `Invalid`. + pub action: AuditAction, + /// The account the guess was about; `None` for a name no account has, which is all the log may say of it. + pub target: Option, + pub step: Step, +} + +/// Where a page of the log starts: strictly after (older than) this event, in the order `(at_ms, id)` descending. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct AuditCursor { + pub at_ms: i64, + pub id: Uuid, +} + +impl AuditCursor { + pub const BYTES: usize = 24; + + pub fn to_bytes(self) -> [u8; Self::BYTES] { + let mut out = [0u8; Self::BYTES]; + out[..8].copy_from_slice(&self.at_ms.to_be_bytes()); + out[8..].copy_from_slice(self.id.as_bytes()); + out + } + + /// `None` unless `bytes` is exactly what [`AuditCursor::to_bytes`] makes (and a time that can be one). + pub fn from_bytes(bytes: &[u8]) -> Option { + let bytes: &[u8; Self::BYTES] = bytes.try_into().ok()?; + let at_ms = i64::from_be_bytes(bytes.get(..8)?.try_into().ok()?); + let id = Uuid::from_slice(bytes.get(8..)?).ok()?; + (at_ms >= 0).then_some(Self { at_ms, id }) + } +} + +/// Which actions a list is limited to. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ActionFilter { + /// This action only. + Exact(AuditAction), + /// The actions whose name starts with this (it ends in a dot, like `auth.`). + Prefix(&'static str), +} + +impl ActionFilter { + /// An exact name, or a prefix ending in a dot that at least one name starts with. Anything else is `None`, so + /// nothing a client typed ever reaches a query. + pub fn parse(text: &str) -> Option { + if let Some(action) = AuditAction::parse(text) { + return Some(ActionFilter::Exact(action)); + } + if !text.ends_with('.') || !text.bytes().all(|b| b.is_ascii_lowercase() || b == b'_' || b == b'.') { + return None; + } + // The prefix is kept as the `'static` start of the first name it matches: the same characters, from the closed list. + AuditAction::ALL + .iter() + .find(|action| action.as_str().starts_with(text)) + .and_then(|action| action.as_str().get(..text.len())) + .map(ActionFilter::Prefix) + } + + /// The names the filter lets through. + pub fn actions(self) -> Vec { + match self { + ActionFilter::Exact(action) => vec![action], + ActionFilter::Prefix(prefix) => AuditAction::ALL.into_iter().filter(|action| action.as_str().starts_with(prefix)).collect(), + } + } +} + +/// One page of the log, asked for. +#[derive(Debug, Clone, Copy, Default)] +pub struct AuditQuery { + /// Events the account acted in or was the target of. + pub user: Option, + pub action: Option, + /// Only events before this time (the first call of a listing). + pub before_ms: Option, + /// Only events up to and including this time (an export's fixed end). + pub until_ms: Option, + /// Only events strictly after this one in the listing's order. + pub after: Option, + pub limit: u32, +} + +/// An event as read back. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct AuditEvent { + pub id: Uuid, + pub at_ms: i64, + pub actor_user: Option, + pub actor_device: Option, + pub action: AuditAction, + /// `user`, `device`, or empty. + pub target_kind: String, + pub target_id: Option, + pub outcome: AuditOutcome, + /// A JSON object text, as written. + pub details: String, + /// The tries one aggregated row counts (1 for every other event). + pub count: u64, +} + +impl AuditEvent { + /// Where a page continues after this event. + pub fn cursor(&self) -> AuditCursor { + AuditCursor { at_ms: self.at_ms, id: self.id } + } +} + +/// What a pruning did. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Pruned { + pub deleted: u64, + /// There was more to delete than the batches allowed. + pub more: bool, +} + +const SELECT: &str = "SELECT id, at_ms, actor_user, actor_device, action, target_kind, target_id, outcome, details, agg_count FROM audit_events"; + +fn event_from(row: &sqlx::any::AnyRow) -> Result { + let actor_user: Option> = row.try_get("actor_user")?; + let actor_device: Option> = row.try_get("actor_device")?; + let target_id: Option> = row.try_get("target_id")?; + Ok(AuditEvent { + id: uuid_from(&row.try_get::, _>("id")?)?, + at_ms: row.try_get("at_ms")?, + actor_user: actor_user.map(|b| uuid_from(&b).map(UserId)).transpose()?, + actor_device: actor_device.map(|b| uuid_from(&b).map(DeviceId::from_uuid)).transpose()?, + action: AuditAction::parse(&row.try_get::("action")?).ok_or(MetaError::Corrupt("unknown audit action"))?, + target_kind: row.try_get("target_kind")?, + target_id: target_id.map(|b| uuid_from(&b)).transpose()?, + outcome: AuditOutcome::parse(&row.try_get::("outcome")?)?, + details: row.try_get("details")?, + count: to_u64(row.try_get("agg_count")?)?, + }) +} + +enum Arg { + Int(i64), + Bytes(Vec), +} + +/// How long a pruning that is running holds the right to run, and what the last complete one's time was. +#[derive(Debug)] +pub(crate) struct PruneState { + last_done_ms: std::sync::atomic::AtomicI64, + running: std::sync::atomic::AtomicBool, +} + +impl PruneState { + const NEVER: i64 = i64::MIN; + + pub fn new() -> Self { + Self { last_done_ms: std::sync::atomic::AtomicI64::new(Self::NEVER), running: std::sync::atomic::AtomicBool::new(false) } + } +} + +/// Gives the right to run back when dropped, whether the pruning finished, failed or was cancelled. +struct Running<'a>(&'a PruneState); + +impl Drop for Running<'_> { + fn drop(&mut self) { + self.0.running.store(false, std::sync::atomic::Ordering::Release); + } +} + +impl Meta { + /// The store that also prunes events older than `days` days after a write, at most once an hour and in bounded + /// batches ([`Meta::audit_prune_if_due`]). Without it nothing is pruned but by [`Meta::audit_prune`]. + pub fn with_audit_retention_days(mut self, days: u32) -> Self { + self.audit_retention_ms = Some(i64::from(days).saturating_mul(24 * 60 * 60 * 1000)); + self + } + + /// Writes a failed sign-in step in a transaction of its own, after the fact: one row per account (or per minute, for + /// names no account has), action, client kind and minute, whose `count` goes up by one with every further try. The caller logs a + /// failure and goes on: the answer to the client and the limits around the sign-in do not depend on this. + pub async fn audit_record_failure(&self, failure: AuditFailure, now_ms: i64) -> Result<(), MetaError> { + self.audit_record_failure_kind(failure, None, now_ms).await + } + + pub async fn audit_record_web_failure(&self, failure: AuditFailure, now_ms: i64) -> Result<(), MetaError> { + self.audit_record_failure_kind(failure, Some(ClientKind::Web), now_ms).await + } + + async fn audit_record_failure_kind(&self, failure: AuditFailure, kind: Option, now_ms: i64) -> Result<(), MetaError> { + if !matches!(failure.action, AuditAction::AuthLoginFailed | AuditAction::AuthSecondFactorFailed) { + return Err(MetaError::Invalid("only failed sign-in steps are aggregated")); + } + let mut details = AuditDetails::step(failure.step); + details.client_kind = kind; + let mut event = NewEvent::new(failure.action).with(details); + if let Some(user) = failure.target { + event = event.on_user(user); + } + let who = failure.target.map_or_else(|| "-".to_owned(), |u| u.0.simple().to_string()); + let kind = kind.map_or("device", ClientKind::as_str); + let key = format!("{}:{kind}:{who}:{}", failure.action.as_str(), now_ms.div_euclid(MINUTE_MS)); + let mut w = self.begin_write().await?; + insert(&mut w.tx, &event, AuditOutcome::Failure, now_ms, Some(key)).await?; + w.commit().await?; + self.prune_soon(now_ms); + Ok(()) + } + + /// One page of events, newest first, in the order `(at_ms, id)` descending: at most `query.limit` (and never more + /// than 1000). + pub async fn audit_list(&self, query: &AuditQuery) -> Result, MetaError> { + let mut sql = format!("{SELECT} WHERE 1 = 1"); + let mut args = Vec::new(); + let mut next = |arg: Arg| -> usize { + args.push(arg); + args.len() + }; + if let Some(user) = query.user { + let n = next(Arg::Bytes(uuid_bytes(&user.0))); + sql.push_str(&format!(" AND (actor_user = ${n} OR target_id = ${n})")); + } + if let Some(filter) = query.action { + // The names come from the closed list, never from a request, so they are safe to write into the text. + let names: Vec = filter.actions().into_iter().map(|a| format!("'{}'", a.as_str())).collect(); + sql.push_str(&format!(" AND action IN ({})", names.join(", "))); + } + if let Some(before) = query.before_ms { + let n = next(Arg::Int(before)); + sql.push_str(&format!(" AND at_ms < ${n}")); + } + if let Some(until) = query.until_ms { + let n = next(Arg::Int(until)); + sql.push_str(&format!(" AND at_ms <= ${n}")); + } + if let Some(after) = query.after { + let at = next(Arg::Int(after.at_ms)); + let id = next(Arg::Bytes(uuid_bytes(&after.id))); + sql.push_str(&format!(" AND (at_ms < ${at} OR (at_ms = ${at} AND id < ${id}))")); + } + let limit = next(Arg::Int(i64::from(query.limit.min(1000)))); + sql.push_str(&format!(" ORDER BY at_ms DESC, id DESC LIMIT ${limit}")); + let mut q = sqlx::query(sqlx::AssertSqlSafe(sql)); + for arg in args { + q = match arg { + Arg::Int(i) => q.bind(i), + Arg::Bytes(b) => q.bind(b), + }; + } + q.fetch_all(&self.pool).await?.iter().map(event_from).collect() + } + + /// How many events are older than `before_ms` (what [`Meta::audit_prune`] would delete). + pub async fn audit_count_before(&self, before_ms: i64) -> Result { + let row = sqlx::query("SELECT COUNT(*) AS n FROM audit_events WHERE at_ms < $1").bind(before_ms).fetch_one(&self.pool).await?; + to_u64(row.try_get("n")?) + } + + /// Deletes every event older than `before_ms`, in batches of [`PRUNE_BATCH`] rows, each in a transaction of its own. + /// Returns how many went. It is the one way a row is deleted (a test checks the sources). + pub async fn audit_prune(&self, before_ms: i64) -> Result { + let mut total = 0u64; + loop { + let done = self.audit_prune_batches(before_ms, 64).await?; + total = total.saturating_add(done.deleted); + if !done.more { + return Ok(total); + } + } + } + + /// [`Meta::audit_prune`] for at most `max_batches` batches: `more` says whether events older than `before_ms` are + /// left. A batch holds the write lock only for its own statement, so a sign-in never waits for a long pruning. + pub async fn audit_prune_batches(&self, before_ms: i64, max_batches: usize) -> Result { + let mut deleted = 0u64; + for _ in 0..max_batches { + let mut w = self.begin_write().await?; + let done = sqlx::query("DELETE FROM audit_events WHERE id IN (SELECT id FROM audit_events WHERE at_ms < $1 ORDER BY at_ms LIMIT $2)") + .bind(before_ms) + .bind(PRUNE_BATCH) + .execute(&mut *w.tx) + .await?; + w.commit().await?; + deleted = deleted.saturating_add(done.rows_affected()); + if done.rows_affected() < PRUNE_BATCH as u64 { + return Ok(Pruned { deleted, more: false }); + } + } + Ok(Pruned { deleted, more: true }) + } + + /// Prunes events past the retention ([`Meta::with_audit_retention_days`]) if the last complete pruning was an hour or + /// more before `now_ms` (or there was none), in at most [`OPPORTUNISTIC_PRUNE_BATCHES`] batches. The time of the last + /// one is set only when it deleted everything that was due, so a backlog or an error lets the next write go on. + /// Returns whether a pruning ran. Called after the commit of a write, never inside a transaction. + pub async fn audit_prune_if_due(&self, now_ms: i64) -> Result { + self.audit_prune_if_due_in(now_ms, OPPORTUNISTIC_PRUNE_BATCHES).await + } + + /// [`Meta::audit_prune_if_due`] with its own limit of batches, so that a test can make a backlog without thousands of rows. + pub async fn audit_prune_if_due_in(&self, now_ms: i64, max_batches: usize) -> Result { + use std::sync::atomic::Ordering; + let Some(keep_ms) = self.audit_retention_ms else { return Ok(false) }; + let state = &self.prune_state; + let last = state.last_done_ms.load(Ordering::Acquire); + if last != PruneState::NEVER && now_ms.saturating_sub(last) < PRUNE_EVERY_MS { + return Ok(false); + } + if state.running.swap(true, Ordering::AcqRel) { + return Ok(false); + } + let _running = Running(state); + let done = self.audit_prune_batches(now_ms.saturating_sub(keep_ms), max_batches).await?; + if !done.more { + state.last_done_ms.store(now_ms, Ordering::Release); + } + Ok(true) + } + + /// Commits a change that wrote an audit event, then lets the write start a pruning ([`Meta::prune_soon`]). + pub(crate) async fn commit_audited(&self, w: crate::WriteTx, now_ms: i64) -> Result<(), MetaError> { + w.commit().await?; + self.prune_soon(now_ms); + Ok(()) + } + + /// Starts [`Meta::audit_prune_if_due`] in the background, so the write that asked does not wait for it. + pub(crate) fn prune_soon(&self, now_ms: i64) { + if self.audit_retention_ms.is_none() { + return; + } + let Ok(runtime) = tokio::runtime::Handle::try_current() else { return }; + let meta = self.clone(); + runtime.spawn(async move { + if let Err(e) = meta.audit_prune_if_due(now_ms).await { + tracing::warn!(error = %e, "couldn't prune old audit events"); + } + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_action_has_a_unique_name_that_parses_back() { + let mut seen = std::collections::HashSet::new(); + for action in AuditAction::ALL { + assert!(seen.insert(action.as_str()), "{} twice", action.as_str()); + assert_eq!(AuditAction::parse(action.as_str()), Some(action)); + assert!(action.as_str().bytes().all(|b| b.is_ascii_lowercase() || b == b'_' || b == b'.')); + } + assert_eq!(seen.len(), 30); + assert_eq!(AuditAction::parse("auth.login "), None); + assert_eq!(AuditAction::parse("AUTH.LOGIN"), None); + } + + #[test] + fn a_filter_is_a_name_or_a_prefix_of_names() { + assert_eq!(ActionFilter::parse("auth.login"), Some(ActionFilter::Exact(AuditAction::AuthLogin))); + let auth = ActionFilter::parse("auth.").unwrap(); + assert_eq!(auth.actions().len(), 5); + assert!(auth.actions().iter().all(|a| a.as_str().starts_with("auth."))); + assert_eq!(ActionFilter::parse("admin.").unwrap().actions().len(), 11); + for bad in ["", "auth", "au.", "auth.%", "auth._", "nope.", "AUTH.", "auth.login'", "auth. ", "auth.logi", "'; DROP TABLE x; --"] { + assert_eq!(ActionFilter::parse(bad), None, "{bad:?}"); + } + } + + #[test] + fn a_platform_is_reduced_to_the_closed_list() { + assert_eq!(Platform::normalize(""), None); + assert_eq!(Platform::normalize("Windows"), Some(Platform::Windows)); + assert_eq!(Platform::normalize("macos"), Some(Platform::Macos)); + // Hostile text a device may legally send: 32 bytes of printable characters or less. + for hostile in [ + "10.0.0.1", + "secret.psd", + "fe80::1", + "\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}\u{e9}", + "windows 11", + "linux\u{7f}", + ] { + assert_eq!(Platform::normalize(hostile), Some(Platform::Other), "{hostile:?}"); + } + } + + #[test] + fn details_keep_only_the_fields_their_event_allows() { + let all = AuditDetails { + client_kind: Some(ClientKind::Agent), + platform: Some(Platform::Linux), + reason: Some(RevokeReason::Admin), + step: Some(Step::Password), + role_from: Some(Role::Member), + role_to: Some(Role::Admin), + quota_bytes: Some(None), + revoked: Some(3), + device: Some(DeviceId::from_uuid(Uuid::nil())), + two_factor_removed: true, + }; + let keys = |action: AuditAction| -> Vec { + let value: Value = serde_json::from_str(&all.to_json(action)).unwrap(); + let mut keys: Vec = value.as_object().unwrap().keys().cloned().collect(); + keys.sort(); + keys + }; + assert_eq!(keys(AuditAction::AuthLogin), ["client_kind", "platform"]); + assert_eq!(keys(AuditAction::AuthLoginFailed), ["client_kind", "step"]); + assert_eq!(keys(AuditAction::AuthRecoveryCodeUsed), ["client_kind"]); + assert_eq!(keys(AuditAction::AdminRoleChanged), ["role_from", "role_to"]); + assert_eq!(keys(AuditAction::AdminQuotaChanged), ["quota_bytes"]); + assert_eq!(keys(AuditAction::AdminUserEnabled), Vec::::new()); + assert_eq!(keys(AuditAction::CliDeviceRevoked), ["device", "reason"]); + assert_eq!(keys(AuditAction::JoinResetRedeemed), ["client_kind", "platform", "revoked", "two_factor_removed"]); + assert_eq!(keys(AuditAction::AccountTwoFactorEnabled), Vec::::new()); + assert_eq!(all.to_json(AuditAction::AdminQuotaChanged), r#"{"quota_bytes":null}"#); + assert_eq!(AuditDetails::revoked(2).to_json(AuditAction::AdminUserDisabled), r#"{"revoked":2}"#); + assert_eq!(AuditDetails::default().to_json(AuditAction::AdminInviteIssued), "{}"); + } + + #[test] + fn details_over_the_limit_lose_whole_fields_and_stay_valid_json() { + // Multi-byte characters, so that cutting bytes anywhere would break the text. + let long = "\u{20ac}".repeat(300); + let fields = vec![ + ("first", Value::from("kept")), + ("second", Value::from(long.clone())), + ("third", Value::from(long.clone())), + ("last", Value::from(long)), + ]; + let text = serialize_fields(fields.clone(), MAX_DETAILS_BYTES); + assert!(text.len() <= MAX_DETAILS_BYTES, "{}", text.len()); + let value: Value = serde_json::from_str(&text).unwrap(); + let object = value.as_object().unwrap(); + assert_eq!(object.get("truncated"), Some(&Value::Bool(true))); + assert_eq!(object.get("first"), Some(&Value::from("kept"))); + // The least important go first, and a field that stays is whole. + assert!(object.get("last").is_none()); + for (name, value) in &fields { + if let Some(kept) = object.get(*name) { + assert_eq!(kept, value); + } + } + // Under the limit nothing is dropped and nothing is flagged. + let small = serialize_fields(vec![("a", Value::from(1))], MAX_DETAILS_BYTES); + assert_eq!(small, r#"{"a":1}"#); + // Even one field over the limit leaves a valid object. + let one = serialize_fields(vec![("only", Value::from("\u{20ac}".repeat(1000)))], MAX_DETAILS_BYTES); + assert_eq!(one, r#"{"truncated":true}"#); + } + + #[test] + fn a_cursor_round_trips_and_refuses_other_bytes() { + let cursor = AuditCursor { at_ms: 1_700_000_000_123, id: Uuid::now_v7() }; + assert_eq!(AuditCursor::from_bytes(&cursor.to_bytes()), Some(cursor)); + assert_eq!(AuditCursor::from_bytes(&[]), None); + assert_eq!(AuditCursor::from_bytes(&cursor.to_bytes()[..23]), None); + let mut longer = cursor.to_bytes().to_vec(); + longer.push(0); + assert_eq!(AuditCursor::from_bytes(&longer), None); + assert_eq!(AuditCursor::from_bytes(&AuditCursor { at_ms: -1, id: Uuid::nil() }.to_bytes()), None); + } +} diff --git a/crates/meta/src/blobs.rs b/crates/meta/src/blobs.rs index 45fbd781..2a795c72 100644 --- a/crates/meta/src/blobs.rs +++ b/crates/meta/src/blobs.rs @@ -104,17 +104,18 @@ pub(crate) async fn record(tx: &mut sqlx::AnyConnection, space: SpaceId, blob: B charge(tx, space, size, cap).await } -/// Adds `size` to the space's stored bytes, refusing (`QuotaExceeded`) when that goes over `cap`. On Postgres the -/// update locks the space's row until the transaction ends, so two uploads to one space can't both slip under. +/// Adds `size` to the space's stored bytes, refusing (`QuotaExceeded`) when that goes over the space's limit: its own +/// quota (set by an admin), else `cap`. On Postgres the update locks the space's row until the transaction ends, so two +/// uploads to one space can't both slip under. async fn charge(tx: &mut sqlx::AnyConnection, space: SpaceId, size: i64, cap: Option) -> Result<(), MetaError> { - let total: i64 = sqlx::query("UPDATE spaces SET stored_bytes = stored_bytes + $1 WHERE id = $2 RETURNING stored_bytes") + let row = sqlx::query("UPDATE spaces SET stored_bytes = stored_bytes + $1 WHERE id = $2 RETURNING stored_bytes, quota_bytes") .bind(size) .bind(uuid_bytes(space.as_uuid())) .fetch_one(&mut *tx) - .await? - .try_get("stored_bytes")?; - let total = to_u64(total)?; - match cap { + .await?; + let total = to_u64(row.try_get("stored_bytes")?)?; + let quota: Option = row.try_get("quota_bytes")?; + match quota.map(to_u64).transpose()?.or(cap) { Some(limit) if total > limit => Err(MetaError::QuotaExceeded { used: total.saturating_sub(to_u64(size)?), limit }), _ => Ok(()), } @@ -122,9 +123,18 @@ async fn charge(tx: &mut sqlx::AnyConnection, space: SpaceId, size: i64, cap: Op /// The bytes `space` stores. async fn stored_bytes(conn: &mut sqlx::AnyConnection, space: SpaceId) -> Result { - let row = sqlx::query("SELECT stored_bytes FROM spaces WHERE id = $1").bind(uuid_bytes(space.as_uuid())).fetch_optional(&mut *conn).await?; + Ok(stored_and_quota(conn, space).await?.0) +} + +/// The bytes `space` stores, and the quota an admin set for it (`None`: the server-wide cap, if any, applies). +async fn stored_and_quota(conn: &mut sqlx::AnyConnection, space: SpaceId) -> Result<(u64, Option), MetaError> { + let row = sqlx::query("SELECT stored_bytes, quota_bytes FROM spaces WHERE id = $1") + .bind(uuid_bytes(space.as_uuid())) + .fetch_optional(&mut *conn) + .await?; let row = row.ok_or(MetaError::NotFound("space"))?; - to_u64(row.try_get("stored_bytes")?) + let quota: Option = row.try_get("quota_bytes")?; + Ok((to_u64(row.try_get("stored_bytes")?)?, quota.map(to_u64).transpose()?)) } /// The file size recorded for a manifest this space has, if it has it. @@ -157,19 +167,20 @@ impl Meta { stored_bytes(&mut conn, space).await } - /// Whether `space` has room for a blob of `stored_size` bytes under the cap ([`Meta::with_stored_bytes_cap`]): + /// Whether `space` has room for a blob of `stored_size` bytes under its limit: the quota an admin set for the + /// space, else the cap ([`Meta::with_stored_bytes_cap`]), else none (so a quota holds without a cap): /// `QuotaExceeded` if it doesn't have the blob yet and taking it would go over. A cheap look before the bytes are /// stored, so a space over its cap can't fill the disk with blobs that are then refused; recording the blob /// checks again, under the write lock. Two uploads under the cap can both pass this look and one then be refused /// after its bytes are stored: at most a few unrecorded blobs per account (bounded by its body slots), which GC /// (M4) sweeps. pub async fn check_room(&self, space: SpaceId, kind: BlobKind, id: &[u8; 32], stored_size: u64) -> Result<(), MetaError> { - let Some(limit) = self.stored_cap else { return Ok(()) }; let mut conn = self.pool.acquire().await?; + let (used, quota) = stored_and_quota(&mut conn, space).await?; + let Some(limit) = quota.or(self.stored_cap) else { return Ok(()) }; if space_has(&mut conn, space, kind, id).await? { return Ok(()); } - let used = stored_bytes(&mut conn, space).await?; if used.saturating_add(stored_size) > limit { Err(MetaError::QuotaExceeded { used, limit }) } else { Ok(()) } } diff --git a/crates/meta/src/join.rs b/crates/meta/src/join.rs index 4a42a837..e5b6ca6a 100644 --- a/crates/meta/src/join.rs +++ b/crates/meta/src/join.rs @@ -7,8 +7,19 @@ //! Of two redeems of one code at the same moment, the second finds nothing ([`MetaError::InvalidCode`]): SQLite runs //! them one after the other, and on Postgres the second statement waits for the first's row lock and then sees the //! row gone or used. +//! - An invite row has a purpose: `invite` (a new computer for the account: the command line's `user invite` and the +//! admin API's invites) or `reset` (a password reset code made through the admin API, good for an hour). Redeeming +//! a reset code always sets a new password, signs out every device of the account and deletes its pairings, and +//! then, as every redeem does, creates the redeeming computer's device. +//! - A disabled account, or one marked for removal, redeems nothing ([`MetaError::AccountDisabled`]). That is decided +//! after the account's row is locked, so a disable that commits meanwhile is not followed by a device. //! - A pairing works only while the device that made it is signed in: revoking that device deletes it, and a redeem //! checks it too. +//! - An account with two-factor sign-in needs its second factor to redeem an invite or a reset code ([`Proof`]): the +//! factor is checked and used up in the same transaction as the invite and the device, a wrong one leaves the invite +//! as it was (its counters are committed on their own), and none is [`MetaError::TwoFactorRequired`] without using the +//! code up. A reset code the admin made with `reset_two_factor` takes the second factor away instead of asking for it. +//! A pairing needs none: only a signed-in device can make one. //! - Expired rows are deleted on the next write to their table (a new invite or a new pairing). use craft_cloud_proto::api::PairingState; @@ -17,9 +28,61 @@ use craft_cloud_proto::{DeviceId, NodeId, SpaceId}; use sqlx::Row; use uuid::Uuid; +use crate::audit::{AuditAction, AuditDetails, NewEvent, record_in}; use crate::codec::{uuid_bytes, uuid_from}; -use crate::users::{check_device_name, insert_device, insert_user}; -use crate::{Meta, MetaError, Role, UserId, valid_username}; +use crate::two_factor::{self, Proof}; +use crate::users::{DeviceClient, account_blocked, check_device_name, delete_join_codes, insert_device, insert_user, lock_user, revoke_devices}; +use crate::{Meta, MetaError, RevokeReason, Role, UserId, valid_username}; + +/// What a row of `invites` is for. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Purpose { + /// Adds a computer to the account (and, for an account without a password, sets it). + Invite, + /// Sets a new password and signs the account's devices out. + Reset, +} + +impl Purpose { + pub(crate) fn as_str(self) -> &'static str { + match self { + Purpose::Invite => "invite", + Purpose::Reset => "reset", + } + } + + fn parse(s: &str) -> Result { + match s { + "invite" => Ok(Purpose::Invite), + "reset" => Ok(Purpose::Reset), + _ => Err(MetaError::Corrupt("unknown invite purpose")), + } + } +} + +/// Replaces `user`'s open invite or reset code with one under `code_hash`, in `tx`, after deleting every expired one. +/// `reset_two_factor`: redeeming a reset code also turns the account's two-factor sign-in off. +pub(crate) async fn put_code( + tx: &mut sqlx::Transaction<'static, sqlx::Any>, + user: UserId, + code_hash: &[u8; 32], + purpose: Purpose, + reset_two_factor: bool, + now_ms: i64, + expires_ms: i64, +) -> Result<(), MetaError> { + sqlx::query("DELETE FROM invites WHERE expires_ms <= $1").bind(now_ms).execute(&mut **tx).await?; + sqlx::query("DELETE FROM invites WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + sqlx::query("INSERT INTO invites (user_id, code_hash, expires_ms, purpose, reset_two_factor) VALUES ($1, $2, $3, $4, $5)") + .bind(uuid_bytes(&user.0)) + .bind(code_hash.to_vec()) + .bind(expires_ms) + .bind(purpose.as_str()) + .bind(i64::from(reset_two_factor)) + .execute(&mut **tx) + .await?; + Ok(()) +} /// The account an invite is for, from [`Meta::create_invite`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -31,10 +94,12 @@ pub struct InviteFor { } /// What [`Meta::redeem`] created: a device of `user`'s. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Redeemed { pub user: UserId, pub device: DeviceId, + /// The devices a reset code signed out (empty for every other code), for the caller to end their live connections. + pub revoked: Vec, } impl Meta { @@ -56,13 +121,8 @@ impl Meta { (insert_user(&mut w.tx, &username, "", Role::Member, now_ms, ids).await?.id, true) } }; - sqlx::query("DELETE FROM invites WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut *w.tx).await?; - sqlx::query("INSERT INTO invites (user_id, code_hash, expires_ms) VALUES ($1, $2, $3)") - .bind(uuid_bytes(&user.0)) - .bind(code_hash.to_vec()) - .bind(expires_ms) - .execute(&mut *w.tx) - .await?; + // An open reset code of the account is replaced as well: the command line's invite is an invite. + put_code(&mut w.tx, user, code_hash, Purpose::Invite, false, now_ms, expires_ms).await?; w.commit().await?; Ok(InviteFor { user, new_account }) } @@ -86,6 +146,11 @@ impl Meta { let id = Uuid::now_v7(); let expires_ms = now_ms.checked_add(PAIRING_TTL_MS).ok_or(MetaError::Invalid("the clock is out of range"))?; let mut w = self.begin_write().await?; + // Users first, as a sign-out does: a pairing is never made for a device a sign-out just revoked. + lock_user(&mut w.tx, user).await?; + if account_blocked(&mut w.tx, user).await? { + return Err(MetaError::DeviceRevoked); + } sqlx::query("DELETE FROM pairings WHERE expires_ms <= $1").bind(now_ms).execute(&mut *w.tx).await?; sqlx::query("DELETE FROM pairings WHERE device_id = $1").bind(uuid_bytes(device.as_uuid())).execute(&mut *w.tx).await?; let done = sqlx::query( @@ -139,23 +204,30 @@ impl Meta { w.commit().await } - /// Whether the invite under `code_hash` needs a new password (its account has none): `None` when no unexpired - /// invite has that hash (it may be a pairing's, or wrong). Lets the server answer `password_required` before it - /// spends time hashing a password. + /// Whether the invite under `code_hash` needs a new password (its account has none, or it is a reset code, which + /// always does): `None` when no unexpired invite has that hash (it may be a pairing's, or wrong). Lets the server + /// answer `password_required` before it spends time hashing a password. pub async fn invite_needs_password(&self, code_hash: &[u8; 32], now_ms: i64) -> Result, MetaError> { - let row = sqlx::query("SELECT u.password_hash FROM invites i JOIN users u ON u.id = i.user_id WHERE i.code_hash = $1 AND i.expires_ms > $2") - .bind(code_hash.to_vec()) - .bind(now_ms) - .fetch_optional(&self.pool) - .await?; - row.map(|row| row.try_get::("password_hash").map(|hash| hash.is_empty())).transpose().map_err(MetaError::from) + let row = sqlx::query( + "SELECT u.password_hash, i.purpose FROM invites i JOIN users u ON u.id = i.user_id WHERE i.code_hash = $1 AND i.expires_ms > $2", + ) + .bind(code_hash.to_vec()) + .bind(now_ms) + .fetch_optional(&self.pool) + .await?; + let Some(row) = row else { return Ok(None) }; + let purpose = Purpose::parse(&row.try_get::("purpose")?)?; + Ok(Some(purpose == Purpose::Reset || row.try_get::("password_hash")?.is_empty())) } /// Redeems the code under `code_hash` for a new device named `device_name` with `token_hash`, in one transaction: /// an unexpired invite (its row deleted) or an unused, unexpired pairing whose maker is still signed in (marked /// used by the new device). An invite to an account without a password needs `new_password_hash`, which becomes the /// account's ([`MetaError::PasswordRequired`] without it, and nothing changes); for any other code it is ignored. - /// Anything else is [`MetaError::InvalidCode`]. + /// A reset code always needs it: it becomes the account's password whatever the old one was, every device of the + /// account is signed out first ([`Redeemed::revoked`]) and its pairings are deleted, so the new device is the only + /// one signed in. A disabled account, or one marked for removal, is [`MetaError::AccountDisabled`] and nothing + /// changes. Anything else is [`MetaError::InvalidCode`]. pub async fn redeem( &self, code_hash: &[u8; 32], @@ -163,48 +235,180 @@ impl Meta { token_hash: &[u8; 32], new_password_hash: Option<&str>, now_ms: i64, + ) -> Result { + self.redeem_with(code_hash, device_name, &DeviceClient::default(), token_hash, new_password_hash, now_ms).await + } + + /// [`Meta::redeem`] for a device that says what client it is. An account with two-factor sign-in is + /// [`MetaError::TwoFactorRequired`] here (see [`Meta::redeem_with_second_factor`]). + pub async fn redeem_with( + &self, + code_hash: &[u8; 32], + device_name: &str, + client: &DeviceClient, + token_hash: &[u8; 32], + new_password_hash: Option<&str>, + now_ms: i64, + ) -> Result { + self.redeem_with_second_factor(code_hash, device_name, client, token_hash, new_password_hash, None, now_ms).await + } + + /// [`Meta::redeem_with`] with the second factor the person gave, for an invite or reset code of an account that has + /// two-factor sign-in. Under the account's lock, after the invite is found again and consumed, the account's + /// **current** state decides: with two-factor sign-in on, a reset code made with `reset_two_factor` takes it away and + /// asks for nothing; any other code needs `second_factor`. None is [`MetaError::TwoFactorRequired`]; a wrong one + /// [`MetaError::TwoFactorInvalid`], with the invite still there and the wrong-proof counters committed on their own; + /// a lock in force [`MetaError::TwoFactorLocked`]. A right one is used up in this transaction, with the invite and the + /// device. A pairing code never needs one, and a second factor given for an account that has none is ignored. + #[allow(clippy::too_many_arguments)] + pub async fn redeem_with_second_factor( + &self, + code_hash: &[u8; 32], + device_name: &str, + client: &DeviceClient, + token_hash: &[u8; 32], + new_password_hash: Option<&str>, + second_factor: Option<&Proof<'_>>, + now_ms: i64, ) -> Result { check_device_name(device_name)?; + client.check()?; let device = DeviceId::generate(); let mut w = self.begin_write().await?; - let invite = sqlx::query("DELETE FROM invites WHERE code_hash = $1 AND expires_ms > $2 RETURNING user_id") + // The account's row is locked before the invite is consumed (see `lock_user`): a password reset takes the + // user and then deletes the invite, so taking them the other way round could deadlock on Postgres. The invite + // is found with a plain read first, and consumed under the lock with its conditions checked again. + let found = sqlx::query("SELECT user_id, purpose, reset_two_factor FROM invites WHERE code_hash = $1 AND expires_ms > $2") .bind(code_hash.to_vec()) .bind(now_ms) .fetch_optional(&mut *w.tx) .await?; - let user = match invite { + let mut revoked = Vec::new(); + let mut two_factor_removed = false; + let (user, action) = match found { Some(row) => { let user = UserId(uuid_from(&row.try_get::, _>("user_id")?)?); + let purpose = Purpose::parse(&row.try_get::("purpose")?)?; + let reset_two_factor: i64 = row.try_get("reset_two_factor")?; + lock_user(&mut w.tx, user).await?; + let still_there = sqlx::query( + "SELECT 1 FROM invites WHERE code_hash = $1 AND user_id = $2 AND expires_ms > $3 AND purpose = $4 AND reset_two_factor = $5", + ) + .bind(code_hash.to_vec()) + .bind(uuid_bytes(&user.0)) + .bind(now_ms) + .bind(purpose.as_str()) + .bind(reset_two_factor) + .fetch_optional(&mut *w.tx) + .await?; + if still_there.is_none() { + // Taken, replaced or deleted (a reset) while this waited for the lock. + return Err(MetaError::InvalidCode); + } + // Decided under the account's lock: a disable that committed while this waited is seen here, and one + // that follows waits for this transaction and then signs the new device out with the rest. + if account_blocked(&mut w.tx, user).await? { + return Err(MetaError::AccountDisabled); + } let current: String = sqlx::query("SELECT password_hash FROM users WHERE id = $1") .bind(uuid_bytes(&user.0)) .fetch_optional(&mut *w.tx) .await? .ok_or(MetaError::Corrupt("an invite's account is missing"))? .try_get("password_hash")?; - if current.is_empty() { - // Dropping the transaction puts the invite back. - let new = new_password_hash.filter(|hash| !hash.is_empty()).ok_or(MetaError::PasswordRequired)?; - sqlx::query("UPDATE users SET password_hash = $1 WHERE id = $2").bind(new).bind(uuid_bytes(&user.0)).execute(&mut *w.tx).await?; + // The account's two-factor state as it is under the lock now, whatever it was when the invite was made. + if two_factor::is_enabled(&mut w.tx, user).await? { + if purpose == Purpose::Reset && reset_two_factor == 1 { + two_factor::remove_in(&mut w.tx, user).await?; + two_factor_removed = true; + } else { + let Some(proof) = second_factor else { return Err(MetaError::TwoFactorRequired) }; + let row = two_factor::load_row(&mut w.tx, user).await?.ok_or(MetaError::Corrupt("two-factor row vanished under the lock"))?; + if let two_factor::Checked::Wrong = two_factor::check_proof(&mut w.tx, user, Some(device), false, &row, proof, now_ms).await? + { + // Nothing has been consumed yet: the wrong proof is counted and committed in this very + // transaction, under the account's lock, and the invite stays. + return Err(self.refuse_sign_in_proof(w, user, None, proof, now_ms, false).await); + } + } } - user + // Only now is the invite used up, with everything above decided under the lock. + let consumed = sqlx::query( + "DELETE FROM invites WHERE code_hash = $1 AND user_id = $2 AND expires_ms > $3 AND purpose = $4 AND reset_two_factor = $5", + ) + .bind(code_hash.to_vec()) + .bind(uuid_bytes(&user.0)) + .bind(now_ms) + .bind(purpose.as_str()) + .bind(reset_two_factor) + .execute(&mut *w.tx) + .await?; + if consumed.rows_affected() == 0 { + return Err(MetaError::InvalidCode); + } + match purpose { + Purpose::Reset => { + // Dropping the transaction puts the code back. + let new = new_password_hash.filter(|hash| !hash.is_empty()).ok_or(MetaError::PasswordRequired)?; + sqlx::query("UPDATE users SET password_hash = $1, password_changed_ms = $2, auth_gen = auth_gen + 1 WHERE id = $3") + .bind(new) + .bind(now_ms) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + // Every device goes, then the pairings: the new device is added after, so it is the only + // one signed in. + revoked = revoke_devices(&mut w.tx, user, None, RevokeReason::Reset, now_ms).await?; + delete_join_codes(&mut w.tx, user).await?; + } + Purpose::Invite if current.is_empty() => { + // Dropping the transaction puts the invite back. + let new = new_password_hash.filter(|hash| !hash.is_empty()).ok_or(MetaError::PasswordRequired)?; + sqlx::query("UPDATE users SET password_hash = $1 WHERE id = $2") + .bind(new) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + } + Purpose::Invite => {} + } + (user, if purpose == Purpose::Reset { AuditAction::JoinResetRedeemed } else { AuditAction::JoinInviteRedeemed }) } None => { + // The same for a pairing: its account is locked before the device is added, so a reset that revokes + // the devices of the account cannot miss one that is being added. + let owner = + sqlx::query("SELECT user_id FROM pairings WHERE code_hash = $1").bind(code_hash.to_vec()).fetch_optional(&mut *w.tx).await?; + let Some(owner) = owner else { return Err(MetaError::InvalidCode) }; + let owner = UserId(uuid_from(&owner.try_get::, _>("user_id")?)?); + lock_user(&mut w.tx, owner).await?; + if account_blocked(&mut w.tx, owner).await? { + return Err(MetaError::AccountDisabled); + } let pairing = sqlx::query( "UPDATE pairings SET used_by = $1 \ - WHERE code_hash = $2 AND used_by IS NULL AND expires_ms > $3 \ + WHERE code_hash = $2 AND user_id = $3 AND used_by IS NULL AND expires_ms > $4 \ AND device_id IN (SELECT id FROM devices WHERE revoked = 0) RETURNING user_id", ) .bind(uuid_bytes(device.as_uuid())) .bind(code_hash.to_vec()) + .bind(uuid_bytes(&owner.0)) .bind(now_ms) .fetch_optional(&mut *w.tx) .await? .ok_or(MetaError::InvalidCode)?; - UserId(uuid_from(&pairing.try_get::, _>("user_id")?)?) + (UserId(uuid_from(&pairing.try_get::, _>("user_id")?)?), AuditAction::JoinPairingRedeemed) } }; - insert_device(&mut w.tx, device, user, device_name, token_hash, now_ms).await?; - w.commit().await?; - Ok(Redeemed { user, device }) + insert_device(&mut w.tx, device, user, device_name, client, token_hash, now_ms).await?; + let mut details = AuditDetails::client(client); + if action == AuditAction::JoinResetRedeemed { + details.revoked = Some(u32::try_from(revoked.len()).unwrap_or(u32::MAX)); + // The actor is the account that redeemed the code: the code does not say which admin made it. + details.two_factor_removed = two_factor_removed; + } + record_in(&mut w.tx, now_ms, NewEvent::new(action).by(user, Some(device)).on_device(device).with(details)).await?; + self.commit_audited(w, now_ms).await?; + Ok(Redeemed { user, device, revoked }) } } diff --git a/crates/meta/src/lib.rs b/crates/meta/src/lib.rs index 1784df29..bf77cc8d 100644 --- a/crates/meta/src/lib.rs +++ b/crates/meta/src/lib.rs @@ -7,6 +7,8 @@ //! row, so commits to one space run one at a time and commits to different spaces run in parallel. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +mod admin; +mod audit; mod blobs; mod codec; mod commit; @@ -14,7 +16,9 @@ mod join; mod live; mod schema; mod tree; +mod two_factor; mod users; +mod web; use std::str::FromStr; use std::sync::Arc; @@ -24,6 +28,11 @@ use sqlx::any::{AnyConnectOptions, AnyPoolOptions}; use sqlx::{AnyPool, ConnectOptions}; use tokio::sync::{Mutex, OwnedMutexGuard}; +pub use admin::{AdminActor, AdminUser, AdminVia, Issued, MAX_SEARCH_BYTES, MIN_QUOTA_BYTES, RESET_TTL_MS, ServerCounts, UserPage}; +pub use audit::{ + ActionFilter, AuditAction, AuditCursor, AuditDetails, AuditEvent, AuditFailure, AuditOutcome, AuditQuery, ClientKind, Field, MAX_DETAILS_BYTES, + MAX_RETENTION_DAYS, MIN_RETENTION_DAYS, OPPORTUNISTIC_PRUNE_BATCHES, Origin, PRUNE_BATCH, PRUNE_EVERY_MS, Platform, Pruned, Step, TargetKind, +}; pub use commit::CommitOutcome; pub use craft_cloud_proto::BlobKind; pub use join::{InviteFor, Redeemed}; @@ -32,7 +41,16 @@ pub use live::{ MAX_LIVE_DOCUMENTS_HOLDING_UPDATES, MAX_LIVE_DOCUMENTS_PER_SPACE, MAX_LIVE_PAGE, SnapshotRef, }; pub use tree::{MAX_PAGE_SAVE_BYTES, SpaceInfo}; -pub use users::{DeviceAuth, DeviceRecord, Role, UserId, UserRecord, valid_username}; +pub use two_factor::{ + CHALLENGE_TTL_MS, Completed, DeviceActor, LOCK_AFTER_FAILURES, LOCK_BASE_MS, LOCK_MAX_MS, MAX_CHALLENGE_ATTEMPTS, MAX_OPEN_CHALLENGES, + MAX_SETUP_ATTEMPTS, NewChallenge, Proof, RECOVERY_CODES, SETUP_TTL_MS, SignIn, StoredSecrets, TotpVerifier, TwoFactorInfo, UnreadableSecret, + retry_after_secs, +}; +pub use users::{ + AccountRecord, CLIENT_KINDS, DeviceAuth, DeviceClient, DeviceRecord, MAX_CLIENT_FIELD_BYTES, RevokeReason, Role, UserId, UserRecord, + valid_username, +}; +pub use web::{NewWebSession, WebSession, WebSignIn}; #[derive(Debug, thiserror::Error)] pub enum MetaError { @@ -77,6 +95,42 @@ pub enum MetaError { /// An invite to an account without a password was redeemed without a new one; nothing was changed. #[error("this invite needs a new password for the account")] PasswordRequired, + /// The device that asked for a change was revoked while the request ran (it is no longer signed in); nothing was + /// changed. + #[error("this device is no longer signed in")] + DeviceRevoked, + /// What the request was based on changed while it ran (the password it checked is no longer the account's); + /// nothing was changed and the request may be made again. + #[error("{0}")] + Conflict(&'static str), + /// The admin who asked may not do this (their role changed while the request ran, or the rules about resetting + /// another admin); nothing was changed. + #[error("{0}")] + Forbidden(&'static str), + /// The change would leave the server without an administrator who can sign in; nothing was changed. + #[error("this would leave the server without an enabled administrator")] + LastAdmin, + /// The account is disabled or marked for removal. Said only to someone who proved they hold its password or a + /// code of it, never to a guesser. + #[error("this account is disabled")] + AccountDisabled, + /// The account has two-factor sign-in and the request carried no second factor; nothing was changed (an invite or + /// reset code is not used up). + #[error("this account needs its second factor")] + TwoFactorRequired, + /// A code or recovery code was wrong, or a code that was used before; its counters were committed and nothing else + /// was changed. + #[error("the second factor is wrong")] + TwoFactorInvalid, + /// Too many wrong second factors: every check is refused until `retry_after_ms` have passed. + #[error("the second factor is locked after too many wrong codes")] + TwoFactorLocked { retry_after_ms: i64 }, + /// A set-up of two-factor sign-in is open on another device. + #[error("two-factor set-up is open on another device")] + TwoFactorPending, + /// A sign-in challenge that is unknown, expired, used or no longer fit, never saying which. + #[error("this sign-in step doesn't work any more")] + ChallengeGone, #[error("invalid input: {0}")] Invalid(&'static str), #[error("stored data is damaged: {0}")] @@ -113,6 +167,13 @@ pub struct Meta { live_held_documents_cap: u64, /// The most bytes all of one space's live documents may hold ([`Meta::with_live_bytes_cap`]). live_bytes_cap: u64, + /// How long audit events are kept when a write prunes them ([`Meta::with_audit_retention_days`]); `None`: a write never prunes. + audit_retention_ms: Option, + /// When the last complete pruning ran, and whether one runs now; shared by the clones. + prune_state: Arc, + /// Where the store reads the time of day for what it judges itself (a browser session's expiry inside an admin change); + /// `None`: the system clock ([`Meta::with_clock`]). + clock: Option i64 + Send + Sync>>, } impl Meta { @@ -169,9 +230,25 @@ impl Meta { live_documents_cap: MAX_LIVE_DOCUMENTS_PER_SPACE, live_held_documents_cap: MAX_LIVE_DOCUMENTS_HOLDING_UPDATES, live_bytes_cap: DEFAULT_LIVE_BYTES_PER_SPACE, + audit_retention_ms: None, + prune_state: Arc::new(audit::PruneState::new()), + clock: None, }) } + /// The store with another clock, for what it judges by the time of day itself: whether a browser session is still inside its + /// lifetimes when an admin change runs (read after the change's locks are taken). The server gives it the clock it injects + /// for tests; without one the system clock is read. The clock belongs to this handle and its clones. + pub fn with_clock(mut self, clock: Arc i64 + Send + Sync>) -> Self { + self.clock = Some(clock); + self + } + + /// Milliseconds since the Unix epoch, by the store's clock. + pub(crate) fn clock_ms(&self) -> i64 { + self.clock.as_ref().map_or_else(users::wall_clock_ms, |clock| clock()) + } + /// The store with another cap on the bytes of updates one live document's log may hold (the default is /// [`MAX_LIVE_DOC_BYTES`]); an append over it is refused with [`MetaError::DocFull`]. For tests. pub fn with_live_doc_cap(mut self, cap: u64) -> Self { @@ -226,6 +303,14 @@ impl Meta { self } + /// `sqlite` or `postgres`, for the admin's server overview. + pub fn database_kind(&self) -> &'static str { + match self.dialect { + Dialect::Sqlite => "sqlite", + Dialect::Postgres => "postgres", + } + } + /// Whether a database connection is in use (a query under way, or a connection on its way back to the pool). /// A simulation on a simulated clock (`turmoil`) keeps time still while it is, since SQLite answers from a /// thread of its own, outside the simulation: otherwise how far simulated time got before an answer would depend diff --git a/crates/meta/src/schema.rs b/crates/meta/src/schema.rs index e4c5220d..b7b30ffb 100644 --- a/crates/meta/src/schema.rs +++ b/crates/meta/src/schema.rs @@ -12,6 +12,11 @@ const SQLITE: &[&str] = &[ include_str!("../migrations/sqlite/0004_join.sql"), include_str!("../migrations/sqlite/0005_live_documents.sql"), include_str!("../migrations/sqlite/0006_save_records.sql"), + include_str!("../migrations/sqlite/0007_devices_v2.sql"), + include_str!("../migrations/sqlite/0008_admin_resets.sql"), + include_str!("../migrations/sqlite/0009_audit_events.sql"), + include_str!("../migrations/sqlite/0010_two_factor.sql"), + include_str!("../migrations/sqlite/0011_web_sessions.sql"), ]; const POSTGRES: &[&str] = &[ include_str!("../migrations/postgres/0001_init.sql"), @@ -20,6 +25,11 @@ const POSTGRES: &[&str] = &[ include_str!("../migrations/postgres/0004_join.sql"), include_str!("../migrations/postgres/0005_live_documents.sql"), include_str!("../migrations/postgres/0006_save_records.sql"), + include_str!("../migrations/postgres/0007_devices_v2.sql"), + include_str!("../migrations/postgres/0008_admin_resets.sql"), + include_str!("../migrations/postgres/0009_audit_events.sql"), + include_str!("../migrations/postgres/0010_two_factor.sql"), + include_str!("../migrations/postgres/0011_web_sessions.sql"), ]; /// Arbitrary fixed key for the Postgres advisory lock that serialises migrations between servers sharing one diff --git a/crates/meta/src/two_factor.rs b/crates/meta/src/two_factor.rs new file mode 100644 index 00000000..88664d0e --- /dev/null +++ b/crates/meta/src/two_factor.rs @@ -0,0 +1,967 @@ +//! Two-factor sign-in: the TOTP secret (stored encrypted, the server's `secretbox` makes and opens the ciphertext), the +//! recovery codes (stored as BLAKE3 hashes), the lock after wrong codes, and the challenges of a password sign-in that +//! still needs its second factor. +//! +//! - **One transaction contract for everything that issues a device or consumes a proof.** The account's row is locked +//! first (`users::lock_user`, the order every other transaction on the account uses), then what the caller checked +//! before is read again under the lock (the device still signed in, the password still the one that was verified, +//! the account's current two-factor state, the challenge), and only then is a challenge or a device made. A second +//! factor and the challenge or invite it opens are used up in the same transaction as the device they let in: the +//! proof is never spent by one commit and followed by another. +//! - **Proofs are single use.** A TOTP code is accepted for a time step greater than the last accepted one +//! (`last_accepted_step`, updated with `WHERE last_accepted_step < step`), so two requests with one code can't both +//! succeed, and a second confirmation in the same 30-second step needs the next code. Sign-in and the admin's +//! confirmations share that counter. A recovery code is a row updated once (`WHERE used_ms IS NULL`). +//! - **Wrong proofs are counted in the transaction that refused them, under the account's lock.** Every check runs before +//! anything is consumed, so the refusing transaction commits only the counters (the invite stays, the challenge stays, +//! no device), and the next request, which waited for the lock, sees the attempts and the lock this one made. After +//! [`LOCK_AFTER_FAILURES`] wrong proofs in a +//! row the second factor is locked for [`LOCK_BASE_MS`], and each wrong proof after that doubles the time up to +//! [`LOCK_MAX_MS`]. A lock is never for good. While it lasts every check answers [`MetaError::TwoFactorLocked`] +//! without counting. +//! - **Lock order** inside a transaction: the account's row, then its two-factor row, then challenges (the failed-proof +//! write takes the last two in that order and never the account's row, so it can't deadlock with them). +//! - **Turning it on or off, and a reset, end what an earlier check let through**: `users.auth_gen` goes up, every +//! open challenge, invite and pairing of the account is deleted, and the devices are signed out (all of them for a +//! disable made by someone else, a reset or the command line; all but the asking device for the account's own). + +use craft_cloud_proto::DeviceId; +use sqlx::Row; + +use crate::audit::{AuditAction, AuditDetails, AuditFailure, NewEvent, Step as AuditStep, record_in}; +use crate::codec::{uuid_bytes, uuid_from}; +use crate::users::{DeviceClient, account_blocked, check_device_name, delete_join_codes, insert_device, lock_user, revoke_devices}; +use crate::{Meta, MetaError, RevokeReason, UserId, WriteTx}; + +/// How long a set-up stays open: ten minutes. +pub const SETUP_TTL_MS: i64 = 10 * 60 * 1000; +/// Wrong codes one set-up takes before it is used up. +pub const MAX_SETUP_ATTEMPTS: i64 = 5; +/// How long a challenge works: five minutes. +pub const CHALLENGE_TTL_MS: i64 = 5 * 60 * 1000; +/// Wrong codes one challenge takes before it is deleted. +pub const MAX_CHALLENGE_ATTEMPTS: i64 = 5; +/// Open challenges one account may have; making another replaces the oldest. +pub const MAX_OPEN_CHALLENGES: i64 = 5; +/// Wrong proofs in a row that lock the second factor. +pub const LOCK_AFTER_FAILURES: i64 = 10; +/// The first lock: 15 minutes. Every wrong proof after a lock ran out doubles it… +pub const LOCK_BASE_MS: i64 = 15 * 60 * 1000; +/// …up to 24 hours. +pub const LOCK_MAX_MS: i64 = 24 * 60 * 60 * 1000; +/// Recovery codes made at once. +pub const RECOVERY_CODES: usize = 10; + +/// A stored secret that the caller's key could not open (the wrong key, a damaged value, another account's). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct UnreadableSecret; + +/// Checks a TOTP code against an account's stored secret (the ciphertext, which only the caller can open): the time step +/// of the code, if it is right for a step in the window, else `None`. Called with the transaction open, on the +/// ciphertext that is current under the account's lock. +pub type TotpVerifier<'a> = dyn Fn(UserId, &[u8]) -> Result, UnreadableSecret> + Send + Sync + 'a; + +/// Rewrites one stored ciphertext of an account: the one to store in its place, or `None` to leave it ([`Meta::two_factor_rewrite`]). +pub type Rewrite<'a> = dyn Fn(UserId, &[u8]) -> Result>, UnreadableSecret> + Send + Sync + 'a; + +/// A second factor a caller presents. +pub enum Proof<'a> { + /// A TOTP code, as the check that knows it. + Totp(&'a TotpVerifier<'a>), + /// The BLAKE3 hash of a normalised recovery code. + Recovery([u8; 32]), +} + +/// The signed-in device that asks for a change to its account's two-factor sign-in, as the handler knew it when it +/// verified the password: all of it is checked again under the account's lock. +#[derive(Clone, PartialEq, Eq)] +pub struct DeviceActor { + pub user: UserId, + /// The device the request came with; it must still be signed in. + pub device: DeviceId, + /// The stored password hash the password was verified against; it must still be the account's. + pub password_hash: String, +} + +impl std::fmt::Debug for DeviceActor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DeviceActor").field("user", &self.user).field("device", &self.device).field("password_hash", &"").finish() + } +} + +/// What `GET /account/two-factor` tells. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct TwoFactorInfo { + pub enabled_ms: Option, + pub recovery_codes_left: u32, + /// A set-up is open (not expired, not used up). + pub pending: bool, +} + +/// A new challenge for a password sign-in that needs a second factor. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct NewChallenge { + /// The BLAKE3 hash of the challenge token: all the server keeps of it. + pub id_hash: [u8; 32], + /// The client address the challenge was made for. Only an additional binding: behind a proxy that hides addresses + /// every client has the same one, so the secret token is what binds the challenge. + pub client_key: String, +} + +/// The result of a password sign-in that checked out. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum SignIn { + /// The account has no second factor: the device is made. + Device(DeviceId), + /// The account needs its second factor: the challenge is made and expires at this time. No device. + Challenge { expires_ms: i64 }, +} + +/// What [`Meta::complete_second_factor`] made. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Completed { + pub user: UserId, + pub device: DeviceId, +} + +/// A stored secret for the key tools: the ciphertexts of one account. Its `Debug` output shows only their lengths. +#[derive(Clone, PartialEq, Eq)] +pub struct StoredSecrets { + pub user: UserId, + /// The ciphertext of the active secret, if two-factor sign-in is on. + pub active: Option>, + /// The ciphertext of an open set-up's secret, if there is one that is not expired. + pub pending: Option>, +} + +impl std::fmt::Debug for StoredSecrets { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("StoredSecrets") + .field("user", &self.user) + .field("active", &self.active.as_ref().map(|sealed| format!("<{} bytes>", sealed.len()))) + .field("pending", &self.pending.as_ref().map(|sealed| format!("<{} bytes>", sealed.len()))) + .finish() + } +} + +/// The row of `user_two_factor`. +pub(crate) struct TfRow { + enabled_ms: Option, + secret: Option>, + pending: Option>, + pending_device: Option>, + pending_expires_ms: Option, + last_step: i64, + locked_until_ms: Option, +} + +impl TfRow { + /// Two-factor sign-in is on. + pub(crate) fn active(&self) -> bool { + self.enabled_ms.is_some() && self.secret.is_some() + } +} + +pub(crate) async fn load_row(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId) -> Result, MetaError> { + let row = sqlx::query( + "SELECT enabled_ms, secret_ciphertext, pending_ciphertext, pending_device, pending_expires_ms, last_accepted_step, \ + locked_until_ms FROM user_two_factor WHERE user_id = $1", + ) + .bind(uuid_bytes(&user.0)) + .fetch_optional(&mut **tx) + .await?; + let Some(row) = row else { return Ok(None) }; + Ok(Some(TfRow { + enabled_ms: row.try_get("enabled_ms")?, + secret: row.try_get("secret_ciphertext")?, + pending: row.try_get("pending_ciphertext")?, + pending_device: row.try_get("pending_device")?, + pending_expires_ms: row.try_get("pending_expires_ms")?, + last_step: row.try_get("last_accepted_step")?, + locked_until_ms: row.try_get("locked_until_ms")?, + })) +} + +/// Whether two-factor sign-in is on for `user`, read in `tx`. +pub(crate) async fn is_enabled(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId) -> Result { + Ok(load_row(tx, user).await?.is_some_and(|row| row.active())) +} + +/// How long the second factor stays locked after the `failures`-th wrong proof in a row (`None`: no lock yet). +fn lock_for(failures: i64) -> Option { + if failures < LOCK_AFTER_FAILURES { + return None; + } + let doublings = u32::try_from(failures.saturating_sub(LOCK_AFTER_FAILURES)).unwrap_or(u32::MAX).min(16); + Some(LOCK_BASE_MS.saturating_mul(1i64 << doublings).min(LOCK_MAX_MS)) +} + +/// Seconds a client should wait before trying again, for a `Retry-After` header: rounded up, at least one. +pub fn retry_after_secs(retry_after_ms: i64) -> u32 { + u32::try_from((retry_after_ms.max(0) + 999) / 1000).unwrap_or(u32::MAX).max(1) +} + +pub(crate) enum Checked { + Accepted, + Wrong, +} + +/// Checks `proof` against the account's two-factor row `row` and, when it is right, uses it up and clears the wrong-proof +/// counters, all in `tx` (the account's row is locked). A lock that is in force is [`MetaError::TwoFactorLocked`] and +/// nothing is checked or counted. A recovery code that is used up writes `auth.recovery_code_used` in `tx` (a code that +/// is not used up writes nothing); `device` is the device it was used from, where there is one. +pub(crate) async fn check_proof( + tx: &mut sqlx::Transaction<'static, sqlx::Any>, + user: UserId, + device: Option, + web: bool, + row: &TfRow, + proof: &Proof<'_>, + now_ms: i64, +) -> Result { + if let Some(until) = row.locked_until_ms + && until > now_ms + { + return Err(MetaError::TwoFactorLocked { retry_after_ms: until - now_ms }); + } + match proof { + Proof::Totp(verify) => { + let secret = row.secret.as_deref().ok_or(MetaError::Corrupt("a two-factor row has no secret"))?; + let step = verify(user, secret).map_err(|_| MetaError::Corrupt("a stored two-factor secret can't be read"))?; + // A step that was accepted before, or an older one, is a replay. + let Some(step) = step.filter(|step| *step > row.last_step) else { return Ok(Checked::Wrong) }; + let done = sqlx::query( + "UPDATE user_two_factor SET last_accepted_step = $1, failed_attempts = 0, locked_until_ms = NULL \ + WHERE user_id = $2 AND last_accepted_step < $1", + ) + .bind(step) + .bind(uuid_bytes(&user.0)) + .execute(&mut **tx) + .await?; + Ok(if done.rows_affected() == 1 { Checked::Accepted } else { Checked::Wrong }) + } + Proof::Recovery(hash) => { + let done = sqlx::query("UPDATE user_recovery_codes SET used_ms = $1 WHERE user_id = $2 AND code_hash = $3 AND used_ms IS NULL") + .bind(now_ms) + .bind(uuid_bytes(&user.0)) + .bind(hash.to_vec()) + .execute(&mut **tx) + .await?; + if done.rows_affected() != 1 { + return Ok(Checked::Wrong); + } + sqlx::query("UPDATE user_two_factor SET failed_attempts = 0, locked_until_ms = NULL WHERE user_id = $1") + .bind(uuid_bytes(&user.0)) + .execute(&mut **tx) + .await?; + let event = NewEvent::new(AuditAction::AuthRecoveryCodeUsed).by(user, device).on_user(user); + let event = if web { event.with(AuditDetails::web()) } else { event }; + record_in(tx, now_ms, event).await?; + Ok(Checked::Accepted) + } + } +} + +/// Deletes the account's two-factor secret, recovery codes and open challenges, and raises `auth_gen`, in `tx`. Whether +/// there was anything to delete is the answer. +pub(crate) async fn remove_in(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId) -> Result { + sqlx::query("DELETE FROM user_recovery_codes WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + let removed = sqlx::query("DELETE FROM user_two_factor WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + sqlx::query("DELETE FROM login_challenges WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + sqlx::query("UPDATE users SET auth_gen = auth_gen + 1 WHERE id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + Ok(removed.rows_affected() > 0) +} + +/// The account's two-factor sign-in removed by someone else (the command line, an admin): the secret, the codes and the +/// challenges go, every device is signed out ([`RevokeReason::TwoFactor`]) and the invite and pairings are deleted. The +/// devices signed out are returned, and the answer is `None` when the account had none (nothing is changed then). +pub(crate) async fn reset_in(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId, now_ms: i64) -> Result>, MetaError> { + if load_row(tx, user).await?.is_none() { + return Ok(None); + } + remove_in(tx, user).await?; + let revoked = revoke_devices(tx, user, None, RevokeReason::TwoFactor, now_ms).await?; + delete_join_codes(tx, user).await?; + Ok(Some(revoked)) +} + +/// Checks the device and the password a handler verified against what is stored now, under the account's lock. +async fn check_actor(w: &mut WriteTx, actor: &DeviceActor) -> Result<(), MetaError> { + if !lock_user(&mut w.tx, actor.user).await? { + return Err(MetaError::DeviceRevoked); + } + // The device must still be signed in; the no-op update locks its row too, so a revoke running now waits. + let kept = sqlx::query("UPDATE devices SET revoked = 0 WHERE id = $1 AND user_id = $2 AND revoked = 0") + .bind(uuid_bytes(actor.device.as_uuid())) + .bind(uuid_bytes(&actor.user.0)) + .execute(&mut *w.tx) + .await?; + if kept.rows_affected() == 0 || account_blocked(&mut w.tx, actor.user).await? { + return Err(MetaError::DeviceRevoked); + } + let same = sqlx::query("SELECT 1 FROM users WHERE id = $1 AND password_hash = $2") + .bind(uuid_bytes(&actor.user.0)) + .bind(actor.password_hash.as_str()) + .fetch_optional(&mut *w.tx) + .await?; + if same.is_none() { + return Err(MetaError::Conflict("the password changed while this request was running; try again")); + } + Ok(()) +} + +impl Meta { + /// Whether `user` has two-factor sign-in on. + pub async fn two_factor_enabled(&self, user: UserId) -> Result { + let row = sqlx::query("SELECT 1 FROM user_two_factor WHERE user_id = $1 AND enabled_ms IS NOT NULL") + .bind(uuid_bytes(&user.0)) + .fetch_optional(&self.pool) + .await?; + Ok(row.is_some()) + } + + /// How many milliseconds `user`'s second factor stays locked after `now_ms` (`None`: it is not locked, or is off). A plain + /// read, for a caller that wants to say "locked" rather than "too many tries". + pub async fn two_factor_locked_for(&self, user: UserId, now_ms: i64) -> Result, MetaError> { + let row = sqlx::query("SELECT locked_until_ms FROM user_two_factor WHERE user_id = $1 AND enabled_ms IS NOT NULL") + .bind(uuid_bytes(&user.0)) + .fetch_optional(&self.pool) + .await?; + let until: Option = row.map(|row| row.try_get("locked_until_ms")).transpose()?.flatten(); + Ok(until.filter(|until| *until > now_ms).map(|until| until - now_ms)) + } + + /// What `GET /account/two-factor` tells about `user`. + pub async fn two_factor_info(&self, user: UserId, now_ms: i64) -> Result { + let row = sqlx::query("SELECT enabled_ms, pending_ciphertext, pending_expires_ms FROM user_two_factor WHERE user_id = $1") + .bind(uuid_bytes(&user.0)) + .fetch_optional(&self.pool) + .await?; + let Some(row) = row else { return Ok(TwoFactorInfo { enabled_ms: None, recovery_codes_left: 0, pending: false }) }; + let enabled_ms: Option = row.try_get("enabled_ms")?; + let pending = row.try_get::>, _>("pending_ciphertext")?.is_some() + && row.try_get::, _>("pending_expires_ms")?.is_some_and(|expires| expires > now_ms); + let left = if enabled_ms.is_some() { + let n: i64 = sqlx::query("SELECT COUNT(*) AS n FROM user_recovery_codes WHERE user_id = $1 AND used_ms IS NULL") + .bind(uuid_bytes(&user.0)) + .fetch_one(&self.pool) + .await? + .try_get("n")?; + u32::try_from(n).unwrap_or(u32::MAX) + } else { + 0 + }; + Ok(TwoFactorInfo { enabled_ms, recovery_codes_left: left, pending }) + } + + /// Starts a set-up for `actor`'s account: `pending_ciphertext` (the new secret, sealed by the caller) is kept for + /// [`SETUP_TTL_MS`] and bound to the asking device, and its expiry is returned. Only while two-factor sign-in is off + /// (`Conflict` otherwise). A set-up that is open on another device is [`MetaError::TwoFactorPending`]; the same + /// device may start again, which replaces the secret and clears its wrong codes. A device that was signed out is + /// [`MetaError::DeviceRevoked`], a password that changed `Conflict`. + pub async fn two_factor_begin_setup(&self, actor: &DeviceActor, pending_ciphertext: &[u8], now_ms: i64) -> Result { + let expires_ms = now_ms.checked_add(SETUP_TTL_MS).ok_or(MetaError::Invalid("the clock is out of range"))?; + let mut w = self.begin_write().await?; + check_actor(&mut w, actor).await?; + if let Some(row) = load_row(&mut w.tx, actor.user).await? { + if row.active() { + return Err(MetaError::Conflict("two-factor sign-in is on already; turn it off first")); + } + let open = row.pending.is_some() && row.pending_expires_ms.is_some_and(|expires| expires > now_ms); + if open && row.pending_device.as_deref() != Some(actor.device.as_uuid().as_bytes().as_slice()) { + return Err(MetaError::TwoFactorPending); + } + } + sqlx::query( + "INSERT INTO user_two_factor (user_id, pending_ciphertext, pending_device, pending_expires_ms, pending_attempts) \ + VALUES ($1, $2, $3, $4, 0) \ + ON CONFLICT (user_id) DO UPDATE SET pending_ciphertext = $2, pending_device = $3, pending_expires_ms = $4, pending_attempts = 0", + ) + .bind(uuid_bytes(&actor.user.0)) + .bind(pending_ciphertext.to_vec()) + .bind(uuid_bytes(actor.device.as_uuid())) + .bind(expires_ms) + .execute(&mut *w.tx) + .await?; + w.commit().await?; + Ok(expires_ms) + } + + /// Turns two-factor sign-in on with the first code of the set-up that `device` of `user` started: the code must be + /// right for the open set-up's secret (at most [`MAX_SETUP_ATTEMPTS`] wrong ones, then the set-up is used up). In one + /// transaction the pending secret becomes the active one, the account's recovery codes become `recovery_hashes` + /// (`BLAKE3` hashes of the codes the caller shows once), `auth_gen` goes up, every other device is signed out + /// ([`RevokeReason::TwoFactor`]) and the account's invite, pairings and challenges are deleted. Two confirmations + /// at once turn it on once. Returns the devices signed out. No open set-up, one that was started on another device + /// (the code is then not tried) or one that ran out is `Conflict`; a signed-out device [`MetaError::DeviceRevoked`]; + /// a wrong code [`MetaError::TwoFactorInvalid`]. + pub async fn two_factor_confirm( + &self, + user: UserId, + device: DeviceId, + verify: &TotpVerifier<'_>, + recovery_hashes: &[[u8; 32]], + now_ms: i64, + ) -> Result, MetaError> { + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Err(MetaError::DeviceRevoked); + } + let kept = sqlx::query("UPDATE devices SET revoked = 0 WHERE id = $1 AND user_id = $2 AND revoked = 0") + .bind(uuid_bytes(device.as_uuid())) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + if kept.rows_affected() == 0 || account_blocked(&mut w.tx, user).await? { + return Err(MetaError::DeviceRevoked); + } + let row = load_row(&mut w.tx, user).await?; + let Some(row) = row.filter(|row| !row.active()) else { + return Err(MetaError::Conflict("there is no two-factor set-up to confirm: start one first")); + }; + let open = row.pending.is_some() && row.pending_expires_ms.is_some_and(|expires| expires > now_ms); + if !open { + return Err(MetaError::Conflict("the two-factor set-up ran out: start it again")); + } + if row.pending_device.as_deref() != Some(device.as_uuid().as_bytes().as_slice()) { + return Err(MetaError::Conflict("this two-factor set-up was started on another device: confirm it there, or start it again here")); + } + let pending = row.pending.as_deref().ok_or(MetaError::Corrupt("an open set-up has no secret"))?; + let step = verify(user, pending).map_err(|_| MetaError::Corrupt("a stored two-factor secret can't be read"))?; + let Some(step) = step else { + // Nothing was changed yet: the wrong code is counted and committed in this transaction, under the lock. + count_wrong_setup_in(&mut w.tx, user, device).await?; + w.commit().await?; + return Err(MetaError::TwoFactorInvalid); + }; + let done = sqlx::query( + "UPDATE user_two_factor SET secret_ciphertext = pending_ciphertext, enabled_ms = $1, pending_ciphertext = NULL, pending_device = NULL, \ + pending_expires_ms = NULL, pending_attempts = 0, last_accepted_step = $2, failed_attempts = 0, locked_until_ms = NULL \ + WHERE user_id = $3 AND enabled_ms IS NULL AND pending_ciphertext IS NOT NULL", + ) + .bind(now_ms) + .bind(step) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + if done.rows_affected() != 1 { + return Err(MetaError::Conflict("two-factor sign-in changed while this request was running; try again")); + } + replace_recovery_codes(&mut w.tx, user, recovery_hashes).await?; + sqlx::query("UPDATE users SET auth_gen = auth_gen + 1 WHERE id = $1").bind(uuid_bytes(&user.0)).execute(&mut *w.tx).await?; + sqlx::query("DELETE FROM login_challenges WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut *w.tx).await?; + let revoked = revoke_devices(&mut w.tx, user, Some(device), RevokeReason::TwoFactor, now_ms).await?; + delete_join_codes(&mut w.tx, user).await?; + record_in(&mut w.tx, now_ms, NewEvent::new(AuditAction::AccountTwoFactorEnabled).by(user, Some(device)).on_user(user)).await?; + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// Turns `actor`'s two-factor sign-in off with `proof` (a code or a recovery code): in one transaction the secret, + /// the codes and the challenges go, `auth_gen` goes up, every other device is signed out ([`RevokeReason::TwoFactor`]) + /// and the invite and pairings are deleted. Returns the devices signed out. Off already is `Conflict`; a wrong + /// proof [`MetaError::TwoFactorInvalid`] (counted, and it can lock); a lock in force [`MetaError::TwoFactorLocked`]. + pub async fn two_factor_disable(&self, actor: &DeviceActor, proof: &Proof<'_>, now_ms: i64) -> Result, MetaError> { + let mut w = self.begin_write().await?; + check_actor(&mut w, actor).await?; + let row = load_row(&mut w.tx, actor.user).await?.filter(TfRow::active); + let Some(row) = row else { return Err(MetaError::Conflict("two-factor sign-in is not on")) }; + if let Checked::Wrong = check_proof(&mut w.tx, actor.user, Some(actor.device), false, &row, proof, now_ms).await? { + return Err(self.refuse_proof(w, actor.user, None, now_ms, false).await); + } + remove_in(&mut w.tx, actor.user).await?; + let revoked = revoke_devices(&mut w.tx, actor.user, Some(actor.device), RevokeReason::TwoFactor, now_ms).await?; + delete_join_codes(&mut w.tx, actor.user).await?; + let event = NewEvent::new(AuditAction::AccountTwoFactorDisabled).by(actor.user, Some(actor.device)).on_user(actor.user); + record_in(&mut w.tx, now_ms, event).await?; + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// Replaces `actor`'s recovery codes with `recovery_hashes` (the old ones stop working at once), after `proof`. Off + /// is `Conflict`; the proof's errors are as for [`Meta::two_factor_disable`]. + pub async fn two_factor_new_recovery_codes( + &self, + actor: &DeviceActor, + proof: &Proof<'_>, + recovery_hashes: &[[u8; 32]], + now_ms: i64, + ) -> Result<(), MetaError> { + let mut w = self.begin_write().await?; + check_actor(&mut w, actor).await?; + let row = load_row(&mut w.tx, actor.user).await?.filter(TfRow::active); + let Some(row) = row else { return Err(MetaError::Conflict("two-factor sign-in is not on")) }; + if let Checked::Wrong = check_proof(&mut w.tx, actor.user, Some(actor.device), false, &row, proof, now_ms).await? { + return Err(self.refuse_proof(w, actor.user, None, now_ms, false).await); + } + replace_recovery_codes(&mut w.tx, actor.user, recovery_hashes).await?; + let event = NewEvent::new(AuditAction::AccountRecoveryCodesRegenerated).by(actor.user, Some(actor.device)).on_user(actor.user); + record_in(&mut w.tx, now_ms, event).await?; + self.commit_audited(w, now_ms).await + } + + /// The confirmation an admin gives for a change: when `user` has two-factor sign-in on, `proof` is checked and used up + /// (the time step or recovery code can't serve a second change), and the answer is `true`; when it is off, nothing is + /// checked and the answer is `false`. Whether it is on is decided under the account's lock. A missing proof for an + /// account that has it on is [`MetaError::TwoFactorInvalid`] (not counted: nothing was tried); the other errors are as + /// for [`Meta::two_factor_disable`]. + pub async fn confirm_second_factor(&self, user: UserId, proof: Option<&Proof<'_>>, now_ms: i64) -> Result { + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Err(MetaError::DeviceRevoked); + } + let Some(row) = load_row(&mut w.tx, user).await?.filter(TfRow::active) else { return Ok(false) }; + let Some(proof) = proof else { return Err(MetaError::TwoFactorInvalid) }; + if let Checked::Wrong = check_proof(&mut w.tx, user, None, false, &row, proof, now_ms).await? { + return Err(self.refuse_proof(w, user, None, now_ms, false).await); + } + // The proof is used up here, in a transaction of its own that commits before the change it confirms: a recovery + // code used for a change that then fails is still used, and its event stays. + self.commit_audited(w, now_ms).await?; + Ok(true) + } + + /// Takes away `user`'s two-factor sign-in on the command line: the secret, the codes and the challenges go, the lock is + /// gone with the row, every device is signed out ([`RevokeReason::TwoFactor`]) and the invite and pairings are + /// deleted. Returns the devices signed out. `NotFound` for a user that isn't there, `Conflict` for one without + /// two-factor sign-in (nothing is signed out then). + pub async fn reset_two_factor(&self, user: UserId, now_ms: i64) -> Result, MetaError> { + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Err(MetaError::NotFound("user")); + } + let revoked = reset_in(&mut w.tx, user, now_ms).await?.ok_or(MetaError::Conflict("this account has no two-factor sign-in"))?; + let event = NewEvent::new(AuditAction::CliTwoFactorReset).on_user(user).with(AuditDetails::revoked(revoked.len())); + record_in(&mut w.tx, now_ms, event).await?; + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// A password sign-in that checked out, as [`Meta::sign_in_device`], for an account that may have two-factor sign-in: + /// under the account's lock the password hash and `auth_gen` the caller read before verifying the password are + /// compared again, then the account's **current** state decides. Without two-factor sign-in the device is made + /// ([`SignIn::Device`]); with it only `challenge` is made ([`SignIn::Challenge`], good for [`CHALLENGE_TTL_MS`]; the + /// account's expired challenges are deleted and the oldest goes when it would have more than + /// [`MAX_OPEN_CHALLENGES`]) and no device. `None`, a disabled account and the errors are as for `sign_in_device`. + #[allow(clippy::too_many_arguments)] + pub async fn sign_in( + &self, + user: UserId, + checked: (&str, i64), + name: &str, + client: &DeviceClient, + token_hash: &[u8; 32], + challenge: &NewChallenge, + now_ms: i64, + ) -> Result, MetaError> { + check_device_name(name)?; + client.check()?; + let (hash, auth_gen) = checked; + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Ok(None); + } + let same = sqlx::query("SELECT 1 FROM users WHERE id = $1 AND password_hash = $2 AND auth_gen = $3") + .bind(uuid_bytes(&user.0)) + .bind(hash) + .bind(auth_gen) + .fetch_optional(&mut *w.tx) + .await?; + if same.is_none() { + return Ok(None); + } + if account_blocked(&mut w.tx, user).await? { + return Err(MetaError::AccountDisabled); + } + if is_enabled(&mut w.tx, user).await? { + let expires_ms = now_ms.checked_add(CHALLENGE_TTL_MS).ok_or(MetaError::Invalid("the clock is out of range"))?; + sqlx::query("DELETE FROM login_challenges WHERE expires_ms <= $1").bind(now_ms).execute(&mut *w.tx).await?; + let open: i64 = sqlx::query("SELECT COUNT(*) AS n FROM login_challenges WHERE user_id = $1") + .bind(uuid_bytes(&user.0)) + .fetch_one(&mut *w.tx) + .await? + .try_get("n")?; + if open >= MAX_OPEN_CHALLENGES { + sqlx::query( + "DELETE FROM login_challenges WHERE id_hash IN \ + (SELECT id_hash FROM login_challenges WHERE user_id = $1 ORDER BY expires_ms ASC, id_hash ASC LIMIT $2)", + ) + .bind(uuid_bytes(&user.0)) + .bind(open - MAX_OPEN_CHALLENGES + 1) + .execute(&mut *w.tx) + .await?; + } + sqlx::query( + "INSERT INTO login_challenges (id_hash, user_id, kind, client_key, device_name, client_kind, platform, app_version, attempts, expires_ms) \ + VALUES ($1, $2, 'login', $3, $4, $5, $6, $7, 0, $8)", + ) + .bind(challenge.id_hash.to_vec()) + .bind(uuid_bytes(&user.0)) + .bind(challenge.client_key.as_str()) + .bind(name) + .bind(client.kind.as_str()) + .bind(client.platform.as_str()) + .bind(client.app_version.as_str()) + .bind(expires_ms) + .execute(&mut *w.tx) + .await?; + w.commit().await?; + return Ok(Some(SignIn::Challenge { expires_ms })); + } + let device = DeviceId::generate(); + insert_device(&mut w.tx, device, user, name, client, token_hash, now_ms).await?; + // A sign-in that made a device is a login; a challenge (above) is not: nobody is signed in yet. + let login = NewEvent::new(AuditAction::AuthLogin).by(user, Some(device)).on_device(device).with(AuditDetails::client(client)); + record_in(&mut w.tx, now_ms, login).await?; + self.commit_audited(w, now_ms).await?; + Ok(Some(SignIn::Device(device))) + } + + /// The account a challenge was made for and its username, by a plain read: only so the caller can count the try against + /// the account before the real check. `None` for a challenge that is unknown, expired or used. Only the challenges of a + /// device sign-in count: a web sign-in's ([`Meta::web_challenge_owner`]) can never make a device. + pub async fn challenge_owner(&self, id_hash: &[u8; 32], now_ms: i64) -> Result, MetaError> { + let row = sqlx::query( + "SELECT c.user_id, u.username FROM login_challenges c JOIN users u ON u.id = c.user_id WHERE c.id_hash = $1 AND c.kind = 'login' AND c.expires_ms > $2", + ) + .bind(id_hash.to_vec()) + .bind(now_ms) + .fetch_optional(&self.pool) + .await?; + let Some(row) = row else { return Ok(None) }; + Ok(Some((UserId(uuid_from(&row.try_get::, _>("user_id")?)?), row.try_get("username")?))) + } + + /// Finishes a sign-in with the second factor: the account is found by a plain read of the challenge, its row is locked + /// **before** the challenge or the factor is touched, and under the lock the challenge (still there, not expired) and + /// the account's **current** state (two-factor sign-in still on, not disabled) are checked again. Only then are the + /// proof and the challenge used up and the device made, in this one transaction. A challenge that is unknown, expired, + /// used or no longer fit is [`MetaError::ChallengeGone`], the same for every case; a wrong proof is + /// [`MetaError::TwoFactorInvalid`] after its counters were committed on their own (the challenge's attempts, deleted at + /// the fifth; the account's wrong proofs and the lock); a lock in force is [`MetaError::TwoFactorLocked`]. + pub async fn complete_second_factor( + &self, + challenge: &[u8; 32], + proof: &Proof<'_>, + token_hash: &[u8; 32], + now_ms: i64, + ) -> Result { + let Some((user, _)) = self.challenge_owner(challenge, now_ms).await? else { return Err(MetaError::ChallengeGone) }; + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Err(MetaError::ChallengeGone); + } + let found = sqlx::query( + "SELECT device_name, client_kind, platform, app_version, attempts, expires_ms FROM login_challenges WHERE id_hash = $1 AND user_id = $2 AND kind = 'login'", + ) + .bind(challenge.to_vec()) + .bind(uuid_bytes(&user.0)) + .fetch_optional(&mut *w.tx) + .await?; + let Some(found) = found.filter(|row| row.try_get::("expires_ms").is_ok_and(|expires| expires > now_ms)) else { + return Err(MetaError::ChallengeGone); + }; + let attempts: i64 = found.try_get("attempts")?; + let row = load_row(&mut w.tx, user).await?.filter(TfRow::active); + let Some(row) = row else { + // Turned off (or reset) since: the challenge is of no use any more. + sqlx::query("DELETE FROM login_challenges WHERE id_hash = $1").bind(challenge.to_vec()).execute(&mut *w.tx).await?; + w.commit().await?; + return Err(MetaError::ChallengeGone); + }; + if attempts >= MAX_CHALLENGE_ATTEMPTS { + return Err(MetaError::ChallengeGone); + } + let device = DeviceId::generate(); + if let Checked::Wrong = check_proof(&mut w.tx, user, Some(device), false, &row, proof, now_ms).await? { + return Err(self.refuse_sign_in_proof(w, user, Some(challenge), proof, now_ms, false).await); + } + if account_blocked(&mut w.tx, user).await? { + return Err(MetaError::AccountDisabled); + } + let used = sqlx::query("DELETE FROM login_challenges WHERE id_hash = $1 AND user_id = $2") + .bind(challenge.to_vec()) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + if used.rows_affected() != 1 { + return Err(MetaError::ChallengeGone); + } + let client = + DeviceClient { kind: found.try_get("client_kind")?, platform: found.try_get("platform")?, app_version: found.try_get("app_version")? }; + let name: String = found.try_get("device_name")?; + insert_device(&mut w.tx, device, user, &name, &client, token_hash, now_ms).await?; + let login = NewEvent::new(AuditAction::AuthLogin).by(user, Some(device)).on_device(device).with(AuditDetails::client(&client)); + record_in(&mut w.tx, now_ms, login).await?; + self.commit_audited(w, now_ms).await?; + Ok(Completed { user, device }) + } + + /// `w` refused a wrong proof and has changed nothing (every caller checks the proof before it consumes anything): the wrong + /// proof's counters are written and **committed in `w` itself**, with the account's row still locked, and the answer is + /// [`MetaError::TwoFactorInvalid`] (or the error that stopped the counters being written). Counting under the same lock as + /// the check means the next request sees the attempts and the lock this one made: no more than five wrong proofs per + /// challenge, and the lock set by exactly the tenth. + pub(crate) async fn refuse_proof(&self, mut w: WriteTx, user: UserId, challenge: Option<&[u8; 32]>, now_ms: i64, web: bool) -> MetaError { + let counted = async { + let locked = count_wrong_in(&mut w.tx, user, challenge, now_ms).await?; + w.commit().await?; + Ok::(locked) + } + .await; + match counted { + Ok(locked) => { + if locked { + // After the counters and the lock are committed, like a failed sign-in's event: an error in the log + // is reported and changes neither the answer nor what was counted (failing closed here would let a + // fault in the log undo the lock). + if let Err(e) = self.audit_lock_set(user, now_ms, web).await { + tracing::warn!(error = %e, "couldn't write an audit event"); + } + } + MetaError::TwoFactorInvalid + } + Err(e) => e, + } + } + + /// `auth.two_factor_locked`, in a transaction of its own. Nobody proved who they were: no actor. + async fn audit_lock_set(&self, user: UserId, now_ms: i64, web: bool) -> Result<(), MetaError> { + let mut w = self.begin_write().await?; + let event = NewEvent::new(AuditAction::AuthTwoFactorLocked).on_user(user); + let event = if web { event.with(AuditDetails::web()) } else { event }; + record_in(&mut w.tx, now_ms, event).await?; + self.commit_audited(w, now_ms).await + } + + /// [`Meta::refuse_proof`] for a proof given at sign-in (the second step, or a join): after the counters are committed + /// and the answer is decided, the wrong proof is also counted in the audit log, `auth.second_factor_failed`, in a + /// transaction of its own and aggregated per account and minute. Writing it can fail without changing the answer. A + /// wrong proof on a signed-in route (turning it off, new recovery codes, an admin's confirmation) is not written one by + /// one: the lock they can cause is (`auth.two_factor_locked`). + pub(crate) async fn refuse_sign_in_proof( + &self, + w: WriteTx, + user: UserId, + challenge: Option<&[u8; 32]>, + proof: &Proof<'_>, + now_ms: i64, + web: bool, + ) -> MetaError { + let refused = self.refuse_proof(w, user, challenge, now_ms, web).await; + if matches!(refused, MetaError::TwoFactorInvalid) { + let step = match proof { + Proof::Totp(_) => AuditStep::Totp, + Proof::Recovery(_) => AuditStep::Recovery, + }; + let failure = AuditFailure { action: AuditAction::AuthSecondFactorFailed, target: Some(user), step }; + let result = if web { self.audit_record_web_failure(failure, now_ms).await } else { self.audit_record_failure(failure, now_ms).await }; + if let Err(e) = result { + tracing::warn!(error = %e, "couldn't write an audit event"); + } + } + refused + } + + /// The ciphertexts of every account's secrets (the active ones, and the open set-ups' that are not expired), for the + /// server to open them at start and for the key tools. + pub async fn two_factor_secrets(&self, now_ms: i64) -> Result, MetaError> { + let rows = sqlx::query( + "SELECT user_id, secret_ciphertext, pending_ciphertext, pending_expires_ms FROM user_two_factor \ + WHERE secret_ciphertext IS NOT NULL OR pending_ciphertext IS NOT NULL ORDER BY user_id", + ) + .fetch_all(&self.pool) + .await?; + rows.iter() + .map(|row| { + let open = row.try_get::, _>("pending_expires_ms")?.is_some_and(|expires| expires > now_ms); + Ok(StoredSecrets { + user: UserId(uuid_from(&row.try_get::, _>("user_id")?)?), + active: row.try_get("secret_ciphertext")?, + pending: if open { row.try_get("pending_ciphertext")? } else { None }, + }) + }) + .collect() + } + + /// Every account's ciphertexts, the open set-ups' and the expired ones too (a plain read, for the key tools). + pub async fn two_factor_all_secrets(&self) -> Result, MetaError> { + let rows = sqlx::query( + "SELECT user_id, secret_ciphertext, pending_ciphertext FROM user_two_factor \ + WHERE secret_ciphertext IS NOT NULL OR pending_ciphertext IS NOT NULL ORDER BY user_id", + ) + .fetch_all(&self.pool) + .await?; + rows.iter() + .map(|row| { + Ok(StoredSecrets { + user: UserId(uuid_from(&row.try_get::, _>("user_id")?)?), + active: row.try_get("secret_ciphertext")?, + pending: row.try_get("pending_ciphertext")?, + }) + }) + .collect() + } + + /// Rewrites `user`'s ciphertexts, the active secret and the set-up's both, **read fresh under the account's lock** (a + /// confirmation that moved a set-up's secret to the active one meanwhile is seen): `rewrite` gets each and answers the + /// ciphertext to store in its place, or `None` to leave it. Returns how many were rewritten and how many left. An account + /// that is gone, or has none, is `(0, 0)`; a ciphertext `rewrite` can't handle is an error and changes nothing. + pub async fn two_factor_rewrite(&self, user: UserId, rewrite: &Rewrite<'_>) -> Result<(usize, usize), MetaError> { + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Ok((0, 0)); + } + let row = sqlx::query("SELECT secret_ciphertext, pending_ciphertext FROM user_two_factor WHERE user_id = $1") + .bind(uuid_bytes(&user.0)) + .fetch_optional(&mut *w.tx) + .await?; + let Some(row) = row else { return Ok((0, 0)) }; + let (mut rewritten, mut kept) = (0, 0); + for (column, update) in [ + ("secret_ciphertext", "UPDATE user_two_factor SET secret_ciphertext = $1 WHERE user_id = $2"), + ("pending_ciphertext", "UPDATE user_two_factor SET pending_ciphertext = $1 WHERE user_id = $2"), + ] { + let Some(sealed) = row.try_get::>, _>(column)? else { continue }; + match rewrite(user, &sealed).map_err(|_| MetaError::Corrupt("a stored two-factor secret can't be read"))? { + Some(new) => { + sqlx::query(update).bind(new).bind(uuid_bytes(&user.0)).execute(&mut *w.tx).await?; + rewritten += 1; + } + None => kept += 1, + } + } + w.commit().await?; + Ok((rewritten, kept)) + } + + /// Replaces the ciphertext `old` of `user`'s active secret (`pending` false) or open set-up (`pending` true) by `new`, if + /// it still is `old`; `false` when it is not (changed meanwhile). For rewriting secrets under a new key. + pub async fn two_factor_replace_ciphertext(&self, user: UserId, pending: bool, old: &[u8], new: &[u8]) -> Result { + let sql = if pending { + "UPDATE user_two_factor SET pending_ciphertext = $1 WHERE user_id = $2 AND pending_ciphertext = $3" + } else { + "UPDATE user_two_factor SET secret_ciphertext = $1 WHERE user_id = $2 AND secret_ciphertext = $3" + }; + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Ok(false); + } + let done = sqlx::query(sql).bind(new.to_vec()).bind(uuid_bytes(&user.0)).bind(old.to_vec()).execute(&mut *w.tx).await?; + w.commit().await?; + Ok(done.rows_affected() == 1) + } +} + +/// Counts a wrong code against the open set-up of `device`, in `tx`; the fifth uses the set-up up. +async fn count_wrong_setup_in(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId, device: DeviceId) -> Result<(), MetaError> { + sqlx::query( + "UPDATE user_two_factor SET pending_attempts = pending_attempts + 1 \ + WHERE user_id = $1 AND enabled_ms IS NULL AND pending_device = $2 AND pending_ciphertext IS NOT NULL", + ) + .bind(uuid_bytes(&user.0)) + .bind(uuid_bytes(device.as_uuid())) + .execute(&mut **tx) + .await?; + sqlx::query( + "UPDATE user_two_factor SET pending_ciphertext = NULL, pending_device = NULL, pending_expires_ms = NULL, pending_attempts = 0 \ + WHERE user_id = $1 AND enabled_ms IS NULL AND pending_attempts >= $2", + ) + .bind(uuid_bytes(&user.0)) + .bind(MAX_SETUP_ATTEMPTS) + .execute(&mut **tx) + .await?; + Ok(()) +} + +/// Counts one wrong second factor of `user` in `tx` (the account's row is locked by it): the wrong proofs in a row go up, +/// and at [`LOCK_AFTER_FAILURES`] (and every one after) the lock is set or doubled; the challenge's attempts go up too, and +/// the challenge is deleted at [`MAX_CHALLENGE_ATTEMPTS`]. Lock order: the account's row, the two-factor row, the challenge. +/// Answers whether a lock was set or lengthened (for the caller to write its event once the counters are committed). +async fn count_wrong_in( + tx: &mut sqlx::Transaction<'static, sqlx::Any>, + user: UserId, + challenge: Option<&[u8; 32]>, + now_ms: i64, +) -> Result { + let mut locked = false; + let counted = sqlx::query( + "UPDATE user_two_factor SET failed_attempts = failed_attempts + 1 WHERE user_id = $1 AND enabled_ms IS NOT NULL \ + RETURNING failed_attempts, locked_until_ms", + ) + .bind(uuid_bytes(&user.0)) + .fetch_optional(&mut **tx) + .await?; + if let Some(row) = counted + && let Some(duration) = lock_for(row.try_get("failed_attempts")?) + { + let until = now_ms.saturating_add(duration); + let until = row.try_get::, _>("locked_until_ms")?.map_or(until, |current| current.max(until)); + sqlx::query("UPDATE user_two_factor SET locked_until_ms = $1 WHERE user_id = $2") + .bind(until) + .bind(uuid_bytes(&user.0)) + .execute(&mut **tx) + .await?; + // Only here, where the lock is set or doubled, and never for a refusal while it is in force (`check_proof` stops + // those before anything is counted). + locked = true; + } + if let Some(challenge) = challenge { + sqlx::query("UPDATE login_challenges SET attempts = attempts + 1 WHERE id_hash = $1").bind(challenge.to_vec()).execute(&mut **tx).await?; + sqlx::query("DELETE FROM login_challenges WHERE id_hash = $1 AND attempts >= $2") + .bind(challenge.to_vec()) + .bind(MAX_CHALLENGE_ATTEMPTS) + .execute(&mut **tx) + .await?; + } + Ok(locked) +} + +/// Makes `hashes` the account's recovery codes, all unused, in `tx` (the old ones, used or not, are gone). +async fn replace_recovery_codes(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId, hashes: &[[u8; 32]]) -> Result<(), MetaError> { + sqlx::query("DELETE FROM user_recovery_codes WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + for hash in hashes { + sqlx::query("INSERT INTO user_recovery_codes (user_id, code_hash, used_ms) VALUES ($1, $2, NULL)") + .bind(uuid_bytes(&user.0)) + .bind(hash.to_vec()) + .execute(&mut **tx) + .await?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_lock_starts_at_ten_and_doubles_up_to_a_day() { + assert_eq!(lock_for(0), None); + assert_eq!(lock_for(9), None); + assert_eq!(lock_for(10), Some(15 * 60 * 1000)); + assert_eq!(lock_for(11), Some(30 * 60 * 1000)); + assert_eq!(lock_for(12), Some(60 * 60 * 1000)); + assert_eq!(lock_for(15), Some(8 * 60 * 60 * 1000), "8 hours"); + assert_eq!(lock_for(16), Some(16 * 60 * 60 * 1000), "16 hours"); + assert_eq!(lock_for(17), Some(LOCK_MAX_MS), "32 hours is over a day"); + assert_eq!(lock_for(i64::MAX), Some(LOCK_MAX_MS)); + } + + #[test] + fn sealed_secrets_show_only_their_length() { + let secrets = StoredSecrets { user: UserId(uuid::Uuid::nil()), active: Some(vec![7, 8, 9]), pending: None }; + let shown = format!("{secrets:?}"); + assert!(shown.contains("<3 bytes>") && !shown.contains("[7"), "{shown}"); + let device = DeviceActor { user: UserId(uuid::Uuid::nil()), device: DeviceId::generate(), password_hash: "$argon2id$secret".to_owned() }; + assert!(!format!("{device:?}").contains("argon2id")); + } + + #[test] + fn retry_after_rounds_up_and_is_never_zero() { + assert_eq!(retry_after_secs(0), 1); + assert_eq!(retry_after_secs(1), 1); + assert_eq!(retry_after_secs(1000), 1); + assert_eq!(retry_after_secs(1001), 2); + assert_eq!(retry_after_secs(-5), 1); + } +} diff --git a/crates/meta/src/users.rs b/crates/meta/src/users.rs index 1d2b0255..9f02abcb 100644 --- a/crates/meta/src/users.rs +++ b/crates/meta/src/users.rs @@ -3,11 +3,16 @@ //! Passwords are hashed by the caller (Argon2id, in the server); this module only stores the hash string. An account //! an invite created has an empty one until the invite is redeemed: no password matches it, since it is no hash. //! Device tokens are random 256-bit values; only their hash is stored, and a device is found by that hash. +//! +//! A device also records what client made it ([`DeviceClient`], shown in the device list and never a basis for a +//! permission), and, once revoked, when and why ([`RevokeReason`]). The `revoked` flag is the one that is checked: +//! the flag, the time and the reason are always set together, in one transaction, and only by the first revoke. use craft_cloud_proto::{DeviceId, Epoch, NodeId, SpaceId}; use sqlx::Row; use uuid::Uuid; +use crate::audit::{AuditAction, AuditDetails, NewEvent, Origin, record_in}; use crate::codec::{flag, uuid_bytes, uuid_from}; use crate::{Meta, MetaError}; @@ -21,14 +26,14 @@ pub enum Role { } impl Role { - fn as_str(self) -> &'static str { + pub fn as_str(self) -> &'static str { match self { Role::Admin => "admin", Role::Member => "member", } } - fn parse(s: &str) -> Result { + pub(crate) fn parse(s: &str) -> Result { match s { "admin" => Ok(Role::Admin), "member" => Ok(Role::Member), @@ -44,6 +49,9 @@ pub struct UserRecord { pub password_hash: String, pub role: Role, pub space: SpaceId, + /// Counts the changes that end what a sign-in checked before them (a disable, an enable, a removal, a password + /// change or reset). Read together with the password hash, and handed back to [`Meta::sign_in_device`]. + pub auth_gen: i64, } impl std::fmt::Debug for UserRecord { @@ -54,10 +62,114 @@ impl std::fmt::Debug for UserRecord { .field("password_hash", &"") .field("role", &self.role) .field("space", &self.space) + .field("auth_gen", &self.auth_gen) .finish() } } +/// What `GET /account` tells about a user. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct AccountRecord { + pub id: UserId, + pub username: String, + pub role: Role, + pub created_ms: i64, + /// When the password last changed through the server (`None`: never, the password is still the first one). + pub password_changed_ms: Option, + /// Whether signing in asks for a second factor. + pub two_factor_enabled: bool, +} + +/// The client kinds a device may name when it signs in. Empty means the client didn't say. +pub const CLIENT_KINDS: [&str; 3] = ["agent", "desktop", "cli"]; +/// The longest platform or app version, in bytes. +pub const MAX_CLIENT_FIELD_BYTES: usize = 32; + +/// What a device says about the client it signed in with. Display only: no permission ever depends on it. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct DeviceClient { + /// One of [`CLIENT_KINDS`], or empty. + pub kind: String, + pub platform: String, + pub app_version: String, +} + +impl DeviceClient { + /// The client a sign-in names; a field left out is empty. An unknown kind, a field over + /// [`MAX_CLIENT_FIELD_BYTES`] or a control character is `Invalid`. + pub fn new(kind: Option<&str>, platform: Option<&str>, app_version: Option<&str>) -> Result { + let client = Self { + kind: kind.unwrap_or_default().to_owned(), + platform: platform.unwrap_or_default().to_owned(), + app_version: app_version.unwrap_or_default().to_owned(), + }; + client.check()?; + Ok(client) + } + + pub(crate) fn check(&self) -> Result<(), MetaError> { + if !self.kind.is_empty() && !CLIENT_KINDS.contains(&self.kind.as_str()) { + return Err(MetaError::Invalid("client_kind is agent, desktop or cli")); + } + for field in [&self.platform, &self.app_version] { + if field.len() > MAX_CLIENT_FIELD_BYTES || field.chars().any(char::is_control) { + return Err(MetaError::Invalid("platform and app_version are at most 32 bytes without control characters")); + } + } + Ok(()) + } +} + +/// Why a device was revoked, as stored in `devices.revoke_reason`. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RevokeReason { + /// The user revoked this device (`DELETE /devices/{id}`). + User, + /// The user signed out every other device. + Others, + /// The user changed the password. + Password, + /// An admin revoked it, on the command line or through the admin API. + Admin, + /// The account was disabled or marked for removal by an admin. + Disabled, + /// Two-factor sign-in was set up or changed (reserved). + TwoFactor, + /// The device sat idle too long (reserved). + Idle, + /// An admin reset the password (on the command line), or a reset code the admin made was redeemed. + Reset, +} + +impl RevokeReason { + pub fn as_str(self) -> &'static str { + match self { + RevokeReason::User => "user", + RevokeReason::Others => "others", + RevokeReason::Password => "password", + RevokeReason::Admin => "admin", + RevokeReason::Disabled => "disabled", + RevokeReason::TwoFactor => "two_factor", + RevokeReason::Idle => "idle", + RevokeReason::Reset => "reset", + } + } + + fn parse(s: &str) -> Result { + Ok(match s { + "user" => RevokeReason::User, + "others" => RevokeReason::Others, + "password" => RevokeReason::Password, + "admin" => RevokeReason::Admin, + "disabled" => RevokeReason::Disabled, + "two_factor" => RevokeReason::TwoFactor, + "idle" => RevokeReason::Idle, + "reset" => RevokeReason::Reset, + _ => return Err(MetaError::Corrupt("unknown revoke reason")), + }) + } +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct DeviceRecord { pub id: DeviceId, @@ -65,6 +177,11 @@ pub struct DeviceRecord { pub created_ms: i64, pub last_used_ms: i64, pub revoked: bool, + /// What the device said about its client when it signed in (empty for devices from before it was recorded). + pub client: DeviceClient, + /// When it was revoked, and why; both `None` while it isn't (and for devices revoked before they were recorded). + pub revoked_ms: Option, + pub revoke_reason: Option, } /// Who a device token belongs to. @@ -129,7 +246,7 @@ impl Meta { pub async fn user_by_name(&self, username: &str) -> Result, MetaError> { let username = username.to_ascii_lowercase(); let row = sqlx::query( - "SELECT u.id, u.username, u.password_hash, u.role, s.id AS space FROM users u JOIN spaces s ON s.owner = u.id WHERE u.username = $1", + "SELECT u.id, u.username, u.password_hash, u.role, u.auth_gen, s.id AS space FROM users u JOIN spaces s ON s.owner = u.id WHERE u.username = $1", ) .bind(username.as_str()) .fetch_optional(&self.pool) @@ -141,38 +258,93 @@ impl Meta { password_hash: row.try_get("password_hash")?, role: Role::parse(&row.try_get::("role")?)?, space: SpaceId::from_uuid(uuid_from(&row.try_get::, _>("space")?)?), + auth_gen: row.try_get("auth_gen")?, })) } - pub async fn set_password_hash(&self, user: UserId, password_hash: &str) -> Result<(), MetaError> { - let w = self.begin_write().await?; - let mut w = w; - let done = sqlx::query("UPDATE users SET password_hash = $1 WHERE id = $2") - .bind(password_hash) - .bind(uuid_bytes(&user.0)) - .execute(&mut *w.tx) - .await?; - if done.rows_affected() == 0 { - return Err(MetaError::NotFound("user")); - } - w.commit().await + /// Registers a device for `user` under the hash of its token, with no word on its client. + pub async fn create_device(&self, user: UserId, name: &str, token_hash: &[u8; 32], now_ms: i64) -> Result { + self.create_device_with(user, name, &DeviceClient::default(), token_hash, now_ms).await } - /// Registers a device for `user` under the hash of its token. - pub async fn create_device(&self, user: UserId, name: &str, token_hash: &[u8; 32], now_ms: i64) -> Result { + /// [`Meta::create_device`] for a device that says what client it is. + pub async fn create_device_with( + &self, + user: UserId, + name: &str, + client: &DeviceClient, + token_hash: &[u8; 32], + now_ms: i64, + ) -> Result { check_device_name(name)?; + client.check()?; let device = DeviceId::generate(); let mut w = self.begin_write().await?; - insert_device(&mut w.tx, device, user, name, token_hash, now_ms).await?; + // The account first, as every transaction that disables it or revokes its devices takes it: a device is + // never added to an account that was disabled or marked for removal meanwhile. + if !lock_user(&mut w.tx, user).await? { + return Err(MetaError::NotFound("user")); + } + if account_blocked(&mut w.tx, user).await? { + return Err(MetaError::AccountDisabled); + } + insert_device(&mut w.tx, device, user, name, client, token_hash, now_ms).await?; w.commit().await?; Ok(device) } - /// The device holding this token hash, if any and not revoked. + /// Registers a device for `user` after a password sign-in, if the password it was checked against is still the + /// account's (an account with two-factor sign-in is [`MetaError::TwoFactorRequired`] here: it gets a device only from + /// [`Meta::complete_second_factor`], and [`Meta::sign_in`] is the call that knows both): `checked` is the stored hash and the generation ([`UserRecord::password_hash`], [`UserRecord::auth_gen`]) the caller read + /// before it verified the password. The account is locked first and both compared again in the same transaction, so a + /// password change or reset, or a disable followed by an enable (which leaves the hash as it was), that committed + /// while the (slow) check ran is not followed by a device that check let in. `None`: the user is gone or + /// something changed meanwhile; the caller answers as for a wrong password. An account that is disabled or marked for removal is + /// [`MetaError::AccountDisabled`], which the caller may only say after the password was right. + pub async fn sign_in_device( + &self, + user: UserId, + checked: (&str, i64), + name: &str, + client: &DeviceClient, + token_hash: &[u8; 32], + now_ms: i64, + ) -> Result, MetaError> { + check_device_name(name)?; + client.check()?; + let (hash, auth_gen) = checked; + let device = DeviceId::generate(); + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Ok(None); + } + let same = sqlx::query("SELECT 1 FROM users WHERE id = $1 AND password_hash = $2 AND auth_gen = $3") + .bind(uuid_bytes(&user.0)) + .bind(hash) + .bind(auth_gen) + .fetch_optional(&mut *w.tx) + .await?; + if same.is_none() { + return Ok(None); + } + if account_blocked(&mut w.tx, user).await? { + return Err(MetaError::AccountDisabled); + } + if crate::two_factor::is_enabled(&mut w.tx, user).await? { + return Err(MetaError::TwoFactorRequired); + } + insert_device(&mut w.tx, device, user, name, client, token_hash, now_ms).await?; + let login = NewEvent::new(AuditAction::AuthLogin).by(user, Some(device)).on_device(device).with(AuditDetails::client(client)); + record_in(&mut w.tx, now_ms, login).await?; + self.commit_audited(w, now_ms).await?; + Ok(Some(device)) + } + + /// The device holding this token hash, if any, not revoked and of an account that isn't disabled. pub async fn device_by_token(&self, token_hash: &[u8; 32]) -> Result, MetaError> { let row = sqlx::query( "SELECT d.id, d.user_id, u.role, u.username, s.id AS space FROM devices d JOIN users u ON u.id = d.user_id JOIN spaces s ON s.owner = u.id \ - WHERE d.token_hash = $1 AND d.revoked = 0", + WHERE d.token_hash = $1 AND d.revoked = 0 AND u.disabled_ms IS NULL", ) .bind(token_hash.to_vec()) .fetch_optional(&self.pool) @@ -195,21 +367,34 @@ impl Meta { } pub async fn list_devices(&self, user: UserId) -> Result, MetaError> { - let rows = sqlx::query("SELECT id, name, created_ms, last_used_ms, revoked FROM devices WHERE user_id = $1 ORDER BY created_ms, id") - .bind(uuid_bytes(&user.0)) - .fetch_all(&self.pool) - .await?; - rows.iter() - .map(|row| { - Ok(DeviceRecord { - id: DeviceId::from_uuid(uuid_from(&row.try_get::, _>("id")?)?), - name: row.try_get("name")?, - created_ms: row.try_get("created_ms")?, - last_used_ms: row.try_get("last_used_ms")?, - revoked: flag(row, "revoked")?, - }) - }) - .collect() + let rows = sqlx::query( + "SELECT id, name, created_ms, last_used_ms, revoked, client_kind, platform, app_version, revoked_ms, revoke_reason \ + FROM devices WHERE user_id = $1 ORDER BY created_ms, id", + ) + .bind(uuid_bytes(&user.0)) + .fetch_all(&self.pool) + .await?; + rows.iter().map(device_record).collect() + } + + /// What `GET /account` tells about `user`; `NotFound` for a user that isn't there. + pub async fn account(&self, user: UserId) -> Result { + let row = sqlx::query( + "SELECT username, role, created_ms, password_changed_ms, \ + (SELECT COUNT(*) FROM user_two_factor t WHERE t.user_id = users.id AND t.enabled_ms IS NOT NULL) AS two_factor FROM users WHERE id = $1", + ) + .bind(uuid_bytes(&user.0)) + .fetch_optional(&self.pool) + .await? + .ok_or(MetaError::NotFound("user"))?; + Ok(AccountRecord { + id: user, + username: row.try_get("username")?, + role: Role::parse(&row.try_get::("role")?)?, + created_ms: row.try_get("created_ms")?, + password_changed_ms: row.try_get("password_changed_ms")?, + two_factor_enabled: row.try_get::("two_factor")? > 0, + }) } /// Whether `device` exists and isn't revoked (a long-lived connection asks again now and then: the admin CLI may @@ -220,21 +405,269 @@ impl Meta { Ok(row.is_some()) } - /// Revokes one of `user`'s devices, and deletes the pairing it made, if any (its add-a-computer code dies with - /// it). Revoking another user's device is `NotFound`. + /// Revokes one of `user`'s devices as the user's own act ([`RevokeReason::User`]), by the system clock. See + /// [`Meta::revoke_device_as`]. pub async fn revoke_device(&self, user: UserId, device: DeviceId) -> Result<(), MetaError> { + self.revoke_device_as(user, device, RevokeReason::User, wall_clock_ms()).await + } + + /// Revokes one of `user`'s devices, and deletes the pairing it made, if any (its add-a-computer code dies with + /// it). Revoking another user's device is `NotFound`. A device that is revoked already stays as it is, with its + /// first time and reason, and the call succeeds. The audit event is `device.revoked` for the account's own act + /// ([`Origin::Account`]); [`Meta::revoke_device_by`] says which device asked, or that the command line did. + pub async fn revoke_device_as(&self, user: UserId, device: DeviceId, reason: RevokeReason, now_ms: i64) -> Result<(), MetaError> { + self.revoke_device_by(user, device, reason, Origin::Account, now_ms).await + } + + /// [`Meta::revoke_device_as`] with who asked. A device that was revoked already is left as it is and writes no event: + /// nothing changed. + pub async fn revoke_device_by(&self, user: UserId, device: DeviceId, reason: RevokeReason, origin: Origin, now_ms: i64) -> Result<(), MetaError> { let mut w = self.begin_write().await?; - let done = sqlx::query("UPDATE devices SET revoked = 1 WHERE id = $1 AND user_id = $2") + if !lock_user(&mut w.tx, user).await? { + return Err(MetaError::NotFound("device")); + } + let done = sqlx::query("UPDATE devices SET revoked = 1, revoked_ms = $1, revoke_reason = $2 WHERE id = $3 AND user_id = $4 AND revoked = 0") + .bind(now_ms) + .bind(reason.as_str()) .bind(uuid_bytes(device.as_uuid())) .bind(uuid_bytes(&user.0)) .execute(&mut *w.tx) .await?; if done.rows_affected() == 0 { - return Err(MetaError::NotFound("device")); + let owned = sqlx::query("SELECT 1 FROM devices WHERE id = $1 AND user_id = $2") + .bind(uuid_bytes(device.as_uuid())) + .bind(uuid_bytes(&user.0)) + .fetch_optional(&mut *w.tx) + .await?; + if owned.is_none() { + return Err(MetaError::NotFound("device")); + } } sqlx::query("DELETE FROM pairings WHERE device_id = $1").bind(uuid_bytes(device.as_uuid())).execute(&mut *w.tx).await?; - w.commit().await + if done.rows_affected() > 0 { + let details = AuditDetails { reason: Some(reason), device: Some(device), ..AuditDetails::default() }; + let event = match origin { + Origin::Account => NewEvent::new(AuditAction::DeviceRevoked).by(user, None).on_device(device), + Origin::Device(by) => NewEvent::new(AuditAction::DeviceRevoked).by(user, Some(by)).on_device(device), + // The command line acts as no account. The event is about the account, whose own activity then shows it. + Origin::Cli => NewEvent::new(AuditAction::CliDeviceRevoked).on_user(user), + }; + record_in(&mut w.tx, now_ms, event.with(details)).await?; + } + self.commit_audited(w, now_ms).await } + + /// Renames one of `user`'s devices that isn't revoked and returns it. Another user's, a revoked or a missing + /// device is `NotFound`, never a different answer, so nothing says which devices exist. + pub async fn rename_device(&self, user: UserId, device: DeviceId, name: &str) -> Result { + check_device_name(name)?; + let mut w = self.begin_write().await?; + let done = sqlx::query("UPDATE devices SET name = $1 WHERE id = $2 AND user_id = $3 AND revoked = 0") + .bind(name) + .bind(uuid_bytes(device.as_uuid())) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + if done.rows_affected() == 0 { + return Err(MetaError::NotFound("device")); + } + let row = sqlx::query( + "SELECT id, name, created_ms, last_used_ms, revoked, client_kind, platform, app_version, revoked_ms, revoke_reason \ + FROM devices WHERE id = $1", + ) + .bind(uuid_bytes(device.as_uuid())) + .fetch_one(&mut *w.tx) + .await?; + let record = device_record(&row)?; + w.commit().await?; + Ok(record) + } + + /// Revokes every device of `user` but `keep` that isn't revoked (with `reason`), and deletes the pairings those + /// devices made, in one transaction. Returns the devices revoked, for the caller to end their live connections. + pub async fn revoke_other_devices(&self, user: UserId, keep: DeviceId, reason: RevokeReason, now_ms: i64) -> Result, MetaError> { + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Ok(Vec::new()); + } + let revoked = revoke_devices(&mut w.tx, user, Some(keep), reason, now_ms).await?; + // One event with the count, however many devices went, and none when there was nobody to sign out. + if !revoked.is_empty() { + let details = + AuditDetails { reason: Some(reason), revoked: Some(u32::try_from(revoked.len()).unwrap_or(u32::MAX)), ..AuditDetails::default() }; + record_in(&mut w.tx, now_ms, NewEvent::new(AuditAction::DeviceRevokedOthers).by(user, Some(keep)).on_user(user).with(details)).await?; + } + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// [`Meta::revoke_other_devices`] for every device of `user` (the admin's command line, which writes + /// `cli.devices_revoked_all` when there was a device to sign out). + pub async fn revoke_all_devices(&self, user: UserId, reason: RevokeReason, now_ms: i64) -> Result, MetaError> { + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Ok(Vec::new()); + } + let revoked = revoke_devices(&mut w.tx, user, None, reason, now_ms).await?; + if !revoked.is_empty() { + record_in(&mut w.tx, now_ms, NewEvent::new(AuditAction::CliDevicesRevokedAll).on_user(user).with(AuditDetails::revoked(revoked.len()))) + .await?; + } + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// Changes `user`'s password to `new_hash`, in one transaction: sets the hash and `password_changed_ms`, + /// revokes every device but `keep` ([`RevokeReason::Password`]), and deletes the account's open invite and all + /// its pairings (a code made before the change must not work after it). `current_hash` is the hash the caller + /// verified the current password against: if the password changed meanwhile, nothing changes and the answer is + /// `Conflict`; if `keep` was revoked meanwhile, it is `DeviceRevoked`. Returns the devices revoked. A failure at + /// any step changes nothing. + pub async fn change_password( + &self, + user: UserId, + current_hash: &str, + new_hash: &str, + keep: DeviceId, + now_ms: i64, + ) -> Result, MetaError> { + let mut w = self.begin_write().await?; + // The user's row first, as everywhere (see `lock_user`). + lock_user(&mut w.tx, user).await?; + // The caller's device must still be signed in: its Argon2 checks took a while, and a revoke since would + // otherwise leave a password changed by a device that is signed out. The no-op update locks the row on + // both databases, so a revoke running now waits for this transaction (and then finds the device kept). + let kept = sqlx::query("UPDATE devices SET revoked = 0 WHERE id = $1 AND user_id = $2 AND revoked = 0") + .bind(uuid_bytes(keep.as_uuid())) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + if kept.rows_affected() == 0 || account_blocked(&mut w.tx, user).await? { + return Err(MetaError::DeviceRevoked); + } + let done = sqlx::query( + "UPDATE users SET password_hash = $1, password_changed_ms = $2, auth_gen = auth_gen + 1 WHERE id = $3 AND password_hash = $4", + ) + .bind(new_hash) + .bind(now_ms) + .bind(uuid_bytes(&user.0)) + .bind(current_hash) + .execute(&mut *w.tx) + .await?; + if done.rows_affected() == 0 { + return Err(MetaError::Conflict("the password changed while this request was running; try again")); + } + let revoked = revoke_devices(&mut w.tx, user, Some(keep), RevokeReason::Password, now_ms).await?; + delete_join_codes(&mut w.tx, user).await?; + // The devices signed out with it are counted in this one event, not in events of their own. + let changed = + NewEvent::new(AuditAction::AccountPasswordChanged).by(user, Some(keep)).on_user(user).with(AuditDetails::revoked(revoked.len())); + record_in(&mut w.tx, now_ms, changed).await?; + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } + + /// The admin's password reset on the command line (`cli.password_reset`): sets `new_hash` and `password_changed_ms`, deletes the account's open invite and + /// pairings, and, unless `keep_devices`, revokes every device ([`RevokeReason::Reset`]), all in one transaction. + /// `NotFound` for a user that isn't there. Returns the devices revoked. + pub async fn reset_password(&self, user: UserId, new_hash: &str, keep_devices: bool, now_ms: i64) -> Result, MetaError> { + let mut w = self.begin_write().await?; + lock_user(&mut w.tx, user).await?; + let done = sqlx::query("UPDATE users SET password_hash = $1, password_changed_ms = $2, auth_gen = auth_gen + 1 WHERE id = $3") + .bind(new_hash) + .bind(now_ms) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + if done.rows_affected() == 0 { + return Err(MetaError::NotFound("user")); + } + let revoked = if keep_devices { Vec::new() } else { revoke_devices(&mut w.tx, user, None, RevokeReason::Reset, now_ms).await? }; + delete_join_codes(&mut w.tx, user).await?; + record_in(&mut w.tx, now_ms, NewEvent::new(AuditAction::CliPasswordReset).on_user(user).with(AuditDetails::revoked(revoked.len()))).await?; + self.commit_audited(w, now_ms).await?; + Ok(revoked) + } +} + +/// Takes the row lock of `user` (a no-op update, so nothing changes), and says whether the user exists. Every +/// transaction that revokes devices or changes a password takes it first, then the rows of the devices, then those of +/// the pairings: one order for all of them, so two of them at once (a password change from one device, a sign-out of +/// the others from another, an admin reset) wait for each other on Postgres and never deadlock. SQLite writers are +/// serialised already. +pub(crate) async fn lock_user(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId) -> Result { + let done = sqlx::query("UPDATE users SET username = username WHERE id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + Ok(done.rows_affected() > 0) +} + +/// Whether `user` is disabled or marked for removal, read in `tx` after the user's row was locked. +pub(crate) async fn account_blocked(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId) -> Result { + let row = sqlx::query("SELECT disabled_ms, removed_ms FROM users WHERE id = $1").bind(uuid_bytes(&user.0)).fetch_optional(&mut **tx).await?; + let Some(row) = row else { return Ok(false) }; + Ok(row.try_get::, _>("disabled_ms")?.is_some() || row.try_get::, _>("removed_ms")?.is_some()) +} + +fn device_record(row: &sqlx::any::AnyRow) -> Result { + Ok(DeviceRecord { + id: DeviceId::from_uuid(uuid_from(&row.try_get::, _>("id")?)?), + name: row.try_get("name")?, + created_ms: row.try_get("created_ms")?, + last_used_ms: row.try_get("last_used_ms")?, + revoked: flag(row, "revoked")?, + client: DeviceClient { kind: row.try_get("client_kind")?, platform: row.try_get("platform")?, app_version: row.try_get("app_version")? }, + revoked_ms: row.try_get("revoked_ms")?, + revoke_reason: row.try_get::, _>("revoke_reason")?.as_deref().map(RevokeReason::parse).transpose()?, + }) +} + +/// Revokes `user`'s devices that aren't revoked yet, except `keep`, and deletes the pairings of every revoked device +/// of theirs, in `tx`. Time and reason are set with the flag, and only here, so a device revoked before keeps its own. +pub(crate) async fn revoke_devices( + tx: &mut sqlx::Transaction<'static, sqlx::Any>, + user: UserId, + keep: Option, + reason: RevokeReason, + now_ms: i64, +) -> Result, MetaError> { + let rows = + match keep { + Some(keep) => sqlx::query( + "UPDATE devices SET revoked = 1, revoked_ms = $1, revoke_reason = $2 WHERE user_id = $3 AND revoked = 0 AND id <> $4 RETURNING id", + ) + .bind(now_ms) + .bind(reason.as_str()) + .bind(uuid_bytes(&user.0)) + .bind(uuid_bytes(keep.as_uuid())) + .fetch_all(&mut **tx) + .await?, + None => { + sqlx::query("UPDATE devices SET revoked = 1, revoked_ms = $1, revoke_reason = $2 WHERE user_id = $3 AND revoked = 0 RETURNING id") + .bind(now_ms) + .bind(reason.as_str()) + .bind(uuid_bytes(&user.0)) + .fetch_all(&mut **tx) + .await? + } + }; + sqlx::query("DELETE FROM pairings WHERE device_id IN (SELECT id FROM devices WHERE user_id = $1 AND revoked = 1)") + .bind(uuid_bytes(&user.0)) + .execute(&mut **tx) + .await?; + rows.iter().map(|row| Ok(DeviceId::from_uuid(uuid_from(&row.try_get::, _>("id")?)?))).collect() +} + +/// Deletes `user`'s open invite, every pairing of theirs and every open sign-in challenge, in `tx`: a code or challenge +/// made before a password change, a reset or a disable must not work after it. +pub(crate) async fn delete_join_codes(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId) -> Result<(), MetaError> { + sqlx::query("DELETE FROM invites WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + sqlx::query("DELETE FROM pairings WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + sqlx::query("DELETE FROM login_challenges WHERE user_id = $1").bind(uuid_bytes(&user.0)).execute(&mut **tx).await?; + Ok(()) +} + +/// The system clock, for the call that takes no time of its own. +pub(crate) fn wall_clock_ms() -> i64 { + std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| i64::try_from(d.as_millis()).unwrap_or(i64::MAX)).unwrap_or(0) } /// The device name rule: 1–128 bytes without control characters. @@ -284,7 +717,7 @@ pub(crate) async fn insert_user( .bind(uuid_bytes(root.as_uuid())) .execute(&mut **tx) .await?; - Ok(UserRecord { id: user, username: username.to_owned(), password_hash: password_hash.to_owned(), role, space }) + Ok(UserRecord { id: user, username: username.to_owned(), password_hash: password_hash.to_owned(), role, space, auth_gen: 0 }) } /// Inserts a device (its name already checked) in `tx`. @@ -293,16 +726,23 @@ pub(crate) async fn insert_device( device: DeviceId, user: UserId, name: &str, + client: &DeviceClient, token_hash: &[u8; 32], now_ms: i64, ) -> Result<(), MetaError> { - sqlx::query("INSERT INTO devices (id, user_id, name, token_hash, created_ms, last_used_ms, revoked) VALUES ($1, $2, $3, $4, $5, $5, 0)") - .bind(uuid_bytes(device.as_uuid())) - .bind(uuid_bytes(&user.0)) - .bind(name) - .bind(token_hash.to_vec()) - .bind(now_ms) - .execute(&mut **tx) - .await?; + sqlx::query( + "INSERT INTO devices (id, user_id, name, token_hash, created_ms, last_used_ms, revoked, client_kind, platform, app_version) \ + VALUES ($1, $2, $3, $4, $5, $5, 0, $6, $7, $8)", + ) + .bind(uuid_bytes(device.as_uuid())) + .bind(uuid_bytes(&user.0)) + .bind(name) + .bind(token_hash.to_vec()) + .bind(now_ms) + .bind(client.kind.as_str()) + .bind(client.platform.as_str()) + .bind(client.app_version.as_str()) + .execute(&mut **tx) + .await?; Ok(()) } diff --git a/crates/meta/src/web.rs b/crates/meta/src/web.rs new file mode 100644 index 00000000..271ccc9a --- /dev/null +++ b/crates/meta/src/web.rs @@ -0,0 +1,347 @@ +//! Browser sessions of the web admin: the `web_sessions` table, signing in to one (with the second factor, when the +//! account has one), looking one up, logging out and pruning. +//! +//! - **Only a hash is stored.** The session token is 32 random bytes the server makes and puts in a cookie; the table keeps +//! its BLAKE3 hash (`id_hash`), like a device's token. A row holds no password, no token, no address and no user agent. +//! - **A session lives exactly as long as the account's `auth_gen` it was made under.** Every change that raises +//! `users.auth_gen` (a password change or reset, a disable, an enable, a removal, turning two-factor sign-in on or off, an +//! admin's reset of it) ends the account's sessions with it, with no code of its own in those changes. A session is +//! also over when the account is disabled or marked for removal, when it is logged out (`revoked_ms`), when it was +//! made longer ago than the absolute lifetime, and when it was not seen for longer than the idle lifetime. +//! - **Only administrators sign in.** The check is made under the account's lock, together with the password's +//! generation, so a demotion or a password change that ran while the (slow) password check did cannot be followed by a +//! session. A refusal is `None` for every reason (wrong state, not an admin, blocked), which the caller answers like a +//! wrong password: nothing says whether an account exists, is a member, or is disabled. +//! - **The second factor is the device sign-in's, not a copy of it.** A web sign-in's challenge is a row of +//! `login_challenges` of its own kind (`web`), counted, locked and used up by the same code (`two_factor::check_proof`, +//! `refuse_sign_in_proof`), and can never be completed into a device (nor a device sign-in's into a session). +//! - **Pruning is bounded.** [`Meta::web_sessions_prune`] deletes at most a given number of expired or revoked rows at a +//! time, so the server can run it after a sign-in without stalling it, and the command line loops until it is done. + +use craft_cloud_proto::DeviceId; +use sqlx::Row; + +use crate::audit::{AuditAction, AuditDetails, NewEvent, record_in}; +use crate::codec::{to_u64, uuid_bytes, uuid_from}; +use crate::two_factor::{ + CHALLENGE_TTL_MS, Checked, MAX_CHALLENGE_ATTEMPTS, MAX_OPEN_CHALLENGES, NewChallenge, Proof, TfRow, check_proof, is_enabled, load_row, +}; +use crate::users::{account_blocked, lock_user}; +use crate::{Meta, MetaError, Role, UserId, WriteTx}; + +/// A session to make: the hash of its token and how long it may live in all. +#[derive(Clone, PartialEq, Eq)] +pub struct NewWebSession { + /// The BLAKE3 hash of the session token: all the server keeps of it. + pub id_hash: [u8; 32], + /// The most the session may live from now, whatever happens: its absolute lifetime. + pub absolute_ms: i64, +} + +impl std::fmt::Debug for NewWebSession { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("NewWebSession").field("id_hash", &"").field("absolute_ms", &self.absolute_ms).finish() + } +} + +/// The result of a password sign-in to the web admin that checked out. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WebSignIn { + /// The account has no second factor: the session is made. + Session, + /// The account needs its second factor: only the challenge is made, good until this time. + Challenge { expires_ms: i64 }, +} + +/// A session that is valid now, with the account it belongs to as the database has it now. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct WebSession { + pub user: UserId, + pub username: String, + pub role: Role, + pub created_ms: i64, + pub last_seen_ms: i64, + pub absolute_expires_ms: i64, + /// When a second factor was given at sign-in; `None` for an account without one. + pub mfa_verified_ms: Option, +} + +/// The SQL that says a session is over, for `$1` the time now and `$2` the oldest `last_seen_ms` that is still idle-valid. +const OVER: &str = "revoked_ms IS NOT NULL OR absolute_expires_ms <= $1 OR last_seen_ms <= $2"; + +/// Inserts the session of `user` in `tx` (the account's row locked), under the account's current generation. +async fn insert_session( + tx: &mut sqlx::Transaction<'static, sqlx::Any>, + user: UserId, + session: &NewWebSession, + mfa_verified_ms: Option, + now_ms: i64, +) -> Result<(), MetaError> { + let absolute = now_ms.checked_add(session.absolute_ms).ok_or(MetaError::Invalid("the session's lifetime is too long"))?; + let generation: i64 = + sqlx::query("SELECT auth_gen FROM users WHERE id = $1").bind(uuid_bytes(&user.0)).fetch_one(&mut **tx).await?.try_get("auth_gen")?; + sqlx::query( + "INSERT INTO web_sessions (id_hash, user_id, auth_gen, created_ms, last_seen_ms, absolute_expires_ms, revoked_ms, mfa_verified_ms) \ + VALUES ($1, $2, $3, $4, $4, $5, NULL, $6)", + ) + .bind(session.id_hash.to_vec()) + .bind(uuid_bytes(&user.0)) + .bind(generation) + .bind(now_ms) + .bind(absolute) + .bind(mfa_verified_ms) + .execute(&mut **tx) + .await?; + Ok(()) +} + +async fn is_admin(tx: &mut sqlx::Transaction<'static, sqlx::Any>, user: UserId) -> Result { + let row = sqlx::query("SELECT role FROM users WHERE id = $1").bind(uuid_bytes(&user.0)).fetch_optional(&mut **tx).await?; + match row { + Some(row) => Ok(Role::parse(&row.try_get::("role")?)? == Role::Admin), + None => Ok(false), + } +} + +impl Meta { + /// A password sign-in to the web admin that checked out: under the account's lock the password hash and `auth_gen` the + /// caller read before it verified the password are compared again, and the account must still be an administrator, not + /// disabled and not marked for removal. Then, without two-factor sign-in, the session is made and `auth.login` is + /// written (no device acts); with it only a challenge of kind `web` is made (the account's expired challenges are + /// deleted and the oldest goes when it would have more than [`MAX_OPEN_CHALLENGES`]) and no session. `None` for + /// everything that is not a sign-in, whatever the reason. + pub async fn web_sign_in( + &self, + user: UserId, + checked: (&str, i64), + challenge: &NewChallenge, + session: &NewWebSession, + now_ms: i64, + ) -> Result, MetaError> { + let (hash, auth_gen) = checked; + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Ok(None); + } + let same = sqlx::query("SELECT 1 FROM users WHERE id = $1 AND password_hash = $2 AND auth_gen = $3") + .bind(uuid_bytes(&user.0)) + .bind(hash) + .bind(auth_gen) + .fetch_optional(&mut *w.tx) + .await?; + if same.is_none() || account_blocked(&mut w.tx, user).await? || !is_admin(&mut w.tx, user).await? { + return Ok(None); + } + if is_enabled(&mut w.tx, user).await? { + let expires_ms = now_ms.checked_add(CHALLENGE_TTL_MS).ok_or(MetaError::Invalid("the clock is out of range"))?; + sqlx::query("DELETE FROM login_challenges WHERE expires_ms <= $1").bind(now_ms).execute(&mut *w.tx).await?; + let open: i64 = sqlx::query("SELECT COUNT(*) AS n FROM login_challenges WHERE user_id = $1") + .bind(uuid_bytes(&user.0)) + .fetch_one(&mut *w.tx) + .await? + .try_get("n")?; + if open >= MAX_OPEN_CHALLENGES { + sqlx::query( + "DELETE FROM login_challenges WHERE id_hash IN \ + (SELECT id_hash FROM login_challenges WHERE user_id = $1 ORDER BY expires_ms ASC, id_hash ASC LIMIT $2)", + ) + .bind(uuid_bytes(&user.0)) + .bind(open - MAX_OPEN_CHALLENGES + 1) + .execute(&mut *w.tx) + .await?; + } + sqlx::query( + "INSERT INTO login_challenges (id_hash, user_id, kind, client_key, device_name, attempts, expires_ms) \ + VALUES ($1, $2, 'web', $3, '', 0, $4)", + ) + .bind(challenge.id_hash.to_vec()) + .bind(uuid_bytes(&user.0)) + .bind(challenge.client_key.as_str()) + .bind(expires_ms) + .execute(&mut *w.tx) + .await?; + w.commit().await?; + return Ok(Some(WebSignIn::Challenge { expires_ms })); + } + insert_session(&mut w.tx, user, session, None, now_ms).await?; + record_in(&mut w.tx, now_ms, NewEvent::new(AuditAction::AuthLogin).by(user, None::).with(AuditDetails::web())).await?; + self.commit_audited(w, now_ms).await?; + Ok(Some(WebSignIn::Session)) + } + + /// The account a web challenge was made for and its username, by a plain read, so the caller can count the try against + /// the account before the real check. `None` for a challenge that is unknown, expired, used, or a device sign-in's. + pub async fn web_challenge_owner(&self, id_hash: &[u8; 32], now_ms: i64) -> Result, MetaError> { + let row = sqlx::query( + "SELECT c.user_id, u.username FROM login_challenges c JOIN users u ON u.id = c.user_id \ + WHERE c.id_hash = $1 AND c.kind = 'web' AND c.expires_ms > $2", + ) + .bind(id_hash.to_vec()) + .bind(now_ms) + .fetch_optional(&self.pool) + .await?; + let Some(row) = row else { return Ok(None) }; + Ok(Some((UserId(uuid_from(&row.try_get::, _>("user_id")?)?), row.try_get("username")?))) + } + + /// Finishes a web sign-in with the second factor, as [`Meta::complete_second_factor`] finishes a device's: the account's + /// row is locked before the challenge or the factor is touched, the challenge and the account's current state (still an + /// administrator, two-factor sign-in still on, not disabled) are checked again under the lock, and only then are the + /// proof and the challenge used up and the session made, in this one transaction. A challenge that is unknown, + /// expired, used or no longer fit is [`MetaError::ChallengeGone`], the same for every case, an account that is no longer + /// an administrator or is blocked too; a wrong proof is [`MetaError::TwoFactorInvalid`] after its counters were + /// committed on their own; a lock in force is [`MetaError::TwoFactorLocked`]. + pub async fn web_complete_second_factor( + &self, + challenge: &[u8; 32], + proof: &Proof<'_>, + session: &NewWebSession, + now_ms: i64, + ) -> Result { + let Some((user, _)) = self.web_challenge_owner(challenge, now_ms).await? else { return Err(MetaError::ChallengeGone) }; + let mut w = self.begin_write().await?; + if !lock_user(&mut w.tx, user).await? { + return Err(MetaError::ChallengeGone); + } + let found = sqlx::query("SELECT attempts, expires_ms FROM login_challenges WHERE id_hash = $1 AND user_id = $2 AND kind = 'web'") + .bind(challenge.to_vec()) + .bind(uuid_bytes(&user.0)) + .fetch_optional(&mut *w.tx) + .await?; + let Some(found) = found.filter(|row| row.try_get::("expires_ms").is_ok_and(|expires| expires > now_ms)) else { + return Err(MetaError::ChallengeGone); + }; + let attempts: i64 = found.try_get("attempts")?; + let Some(row) = load_row(&mut w.tx, user).await?.filter(TfRow::active) else { + // Turned off (or reset) since: the challenge is of no use any more. + sqlx::query("DELETE FROM login_challenges WHERE id_hash = $1").bind(challenge.to_vec()).execute(&mut *w.tx).await?; + w.commit().await?; + return Err(MetaError::ChallengeGone); + }; + if attempts >= MAX_CHALLENGE_ATTEMPTS || account_blocked(&mut w.tx, user).await? || !is_admin(&mut w.tx, user).await? { + return Err(MetaError::ChallengeGone); + } + if let Checked::Wrong = check_proof(&mut w.tx, user, None, true, &row, proof, now_ms).await? { + return Err(self.refuse_sign_in_proof(w, user, Some(challenge), proof, now_ms, true).await); + } + let used = sqlx::query("DELETE FROM login_challenges WHERE id_hash = $1 AND user_id = $2") + .bind(challenge.to_vec()) + .bind(uuid_bytes(&user.0)) + .execute(&mut *w.tx) + .await?; + if used.rows_affected() != 1 { + return Err(MetaError::ChallengeGone); + } + insert_session(&mut w.tx, user, session, Some(now_ms), now_ms).await?; + record_in(&mut w.tx, now_ms, NewEvent::new(AuditAction::AuthLogin).by(user, None::).with(AuditDetails::web())).await?; + self.commit_audited(w, now_ms).await?; + Ok(user) + } + + /// The session under this token hash, if it is valid at `now_ms`: not logged out, younger than its absolute lifetime, + /// seen within `idle_ms`, made under the account's current `auth_gen`, and of an account that is neither disabled nor + /// marked for removal. The account's role is the one the database has now. + pub async fn web_session(&self, id_hash: &[u8; 32], now_ms: i64, idle_ms: i64) -> Result, MetaError> { + let row = sqlx::query( + "SELECT s.user_id, s.created_ms, s.last_seen_ms, s.absolute_expires_ms, s.mfa_verified_ms, u.username, u.role \ + FROM web_sessions s JOIN users u ON u.id = s.user_id \ + WHERE s.id_hash = $1 AND s.revoked_ms IS NULL AND s.absolute_expires_ms > $2 AND s.last_seen_ms > $3 \ + AND s.auth_gen = u.auth_gen AND u.disabled_ms IS NULL AND u.removed_ms IS NULL", + ) + .bind(id_hash.to_vec()) + .bind(now_ms) + .bind(now_ms.saturating_sub(idle_ms)) + .fetch_optional(&self.pool) + .await?; + let Some(row) = row else { return Ok(None) }; + Ok(Some(WebSession { + user: UserId(uuid_from(&row.try_get::, _>("user_id")?)?), + username: row.try_get("username")?, + role: Role::parse(&row.try_get::("role")?)?, + created_ms: row.try_get("created_ms")?, + last_seen_ms: row.try_get("last_seen_ms")?, + absolute_expires_ms: row.try_get("absolute_expires_ms")?, + mfa_verified_ms: row.try_get("mfa_verified_ms")?, + })) + } + + /// Records that the session was just used (its idle time starts again). Never moves the time back. + pub async fn web_session_seen(&self, id_hash: &[u8; 32], now_ms: i64) -> Result<(), MetaError> { + let mut w = self.begin_write().await?; + sqlx::query("UPDATE web_sessions SET last_seen_ms = $1 WHERE id_hash = $2 AND last_seen_ms < $1 AND revoked_ms IS NULL") + .bind(now_ms) + .bind(id_hash.to_vec()) + .execute(&mut *w.tx) + .await?; + w.commit().await + } + + /// Logs the session out. One that is gone or over already is left as it is, and the call succeeds. + pub async fn web_logout(&self, id_hash: &[u8; 32], now_ms: i64) -> Result<(), MetaError> { + let mut w = self.begin_write().await?; + sqlx::query("UPDATE web_sessions SET revoked_ms = $1 WHERE id_hash = $2 AND revoked_ms IS NULL") + .bind(now_ms) + .bind(id_hash.to_vec()) + .execute(&mut *w.tx) + .await?; + w.commit().await + } + + /// Deletes at most `max_rows` sessions that are over (logged out, past their absolute lifetime, or not seen for `idle_ms`) + /// and says how many went. Rows that are valid are never touched. The rows of sessions that an account change ended + /// (`auth_gen`) are deleted when their other lifetimes run out, which is at most the absolute lifetime. + pub async fn web_sessions_prune(&self, now_ms: i64, idle_ms: i64, max_rows: u32) -> Result { + let mut w = self.begin_write().await?; + let done = sqlx::query(sqlx::AssertSqlSafe(format!( + "DELETE FROM web_sessions WHERE id_hash IN (SELECT id_hash FROM web_sessions WHERE {OVER} LIMIT $3)" + ))) + .bind(now_ms) + .bind(now_ms.saturating_sub(idle_ms)) + .bind(i64::from(max_rows)) + .execute(&mut *w.tx) + .await?; + w.commit().await?; + Ok(done.rows_affected()) + } + + /// How many sessions [`Meta::web_sessions_prune`] would delete now (the command line's dry run). + pub async fn web_sessions_count_over(&self, now_ms: i64, idle_ms: i64) -> Result { + let row = sqlx::query(sqlx::AssertSqlSafe(format!("SELECT COUNT(*) AS n FROM web_sessions WHERE {OVER}"))) + .bind(now_ms) + .bind(now_ms.saturating_sub(idle_ms)) + .fetch_one(&self.pool) + .await?; + to_u64(row.try_get("n")?) + } + + /// How many sessions the account has that are valid at `now_ms`, for tests and the overview. + pub async fn web_sessions_open(&self, user: UserId, now_ms: i64, idle_ms: i64) -> Result { + let row = sqlx::query( + "SELECT COUNT(*) AS n FROM web_sessions s JOIN users u ON u.id = s.user_id \ + WHERE s.user_id = $1 AND s.revoked_ms IS NULL AND s.absolute_expires_ms > $2 AND s.last_seen_ms > $3 AND s.auth_gen = u.auth_gen", + ) + .bind(uuid_bytes(&user.0)) + .bind(now_ms) + .bind(now_ms.saturating_sub(idle_ms)) + .fetch_one(&self.pool) + .await?; + to_u64(row.try_get("n")?) + } +} + +/// Whether the session `id_hash` of `user` is valid at `now_ms`, read inside a change (see `begin_admin`) under the account's +/// lock: not logged out, under the account's current generation, inside its absolute lifetime and used within `idle_ms`. A +/// request that began while the session was valid (a slow body, a wait for a lock or a password check) therefore cannot change +/// anything after the session ended. The account's disabled and removed marks need no look: they raise `auth_gen`. +pub(crate) async fn session_is_live(w: &mut WriteTx, user: UserId, id_hash: &[u8; 32], now_ms: i64, idle_ms: i64) -> Result { + let row = sqlx::query( + "SELECT 1 FROM web_sessions s JOIN users u ON u.id = s.user_id WHERE s.id_hash = $1 AND s.user_id = $2 AND s.revoked_ms IS NULL AND s.auth_gen = u.auth_gen AND s.absolute_expires_ms > $3 AND s.last_seen_ms > $4", + ) + .bind(id_hash.to_vec()) + .bind(uuid_bytes(&user.0)) + .bind(now_ms) + .bind(now_ms.saturating_sub(idle_ms)) + .fetch_optional(&mut *w.tx) + .await?; + Ok(row.is_some()) +} diff --git a/crates/meta/tests/behaviour.rs b/crates/meta/tests/behaviour.rs index d875fc30..27320c1b 100644 --- a/crates/meta/tests/behaviour.rs +++ b/crates/meta/tests/behaviour.rs @@ -3,7 +3,10 @@ //! test creates and drops its own database. CI sets `CRAFT_CLOUD_REQUIRE_BACKENDS`, which turns a missing //! `CRAFT_CLOUD_TEST_PG` into a failure instead of a skip. -use craft_cloud_meta::{BlobKind, InviteFor, Meta, MetaError, Redeemed, Role, UserRecord}; +use craft_cloud_meta::{ + AdminActor, AdminUser, AdminVia, BlobKind, DeviceActor, DeviceClient, DeviceRecord, InviteFor, MIN_QUOTA_BYTES, Meta, MetaError, NewChallenge, + NewWebSession, Proof, RECOVERY_CODES, RESET_TTL_MS, Redeemed, RevokeReason, Role, SignIn, UnreadableSecret, UserId, UserRecord, WebSignIn, +}; use craft_cloud_proto::api::PairingState; use craft_cloud_proto::join::{Code, INVITE_CHARS, INVITE_TTL_MS, PAIRING_CHARS, PAIRING_TTL_MS}; use craft_cloud_proto::limits::MAX_PAGE_NODES; @@ -139,6 +142,71 @@ on_every_backend!( concurrent_redeems_of_one_code_give_one_device, an_invite_to_an_account_with_a_password_needs_none, an_invite_to_a_new_account_needs_a_password, + a_device_records_its_client, + only_own_live_devices_are_renamed, + every_revoke_path_sets_flag_time_and_reason_together, + revoking_others_keeps_the_caller_and_ends_their_pairings, + a_password_change_is_one_step, + an_admin_reset_signs_out_every_device_unless_kept, + concurrent_password_changes_and_sign_outs_never_fail_the_database, + a_sign_in_checked_against_an_old_password_creates_no_device, + redeems_sign_ins_and_resets_at_the_same_moment_leave_no_live_device, + the_admin_list_pages_searches_and_counts, + a_sign_in_checked_before_a_disable_and_an_enable_creates_no_device, + an_admin_makes_an_account_with_a_role_and_an_invite, + an_admin_changes_a_role_and_a_quota, + the_acting_admins_state_is_checked_in_the_transaction, + disabling_signs_everything_out_and_enabling_brings_back_nothing, + issuing_access_while_an_account_is_disabled_leaves_no_usable_token, + invites_and_reset_codes_replace_each_other, + a_reset_code_sets_a_password_and_signs_everything_out, + a_quota_overrides_the_server_wide_cap, + an_admin_lists_and_revokes_a_users_devices, + two_demotions_at_once_keep_an_admin, + two_self_demotions_at_once_keep_an_admin, + two_disables_at_once_keep_an_admin, + a_demotion_and_a_disable_at_once_keep_an_admin, + a_removal_and_a_demotion_at_once_keep_an_admin, + a_self_demotion_and_a_disable_of_the_other_at_once_keep_an_admin, + every_operation_writes_exactly_its_events, + a_refused_change_writes_no_event, + failed_sign_ins_count_up_in_one_row_per_account_and_minute, + concurrent_failures_with_one_key_make_one_row_with_every_count, + audit_lists_filter_and_page_by_cursor, + pruning_works_in_batches_and_keeps_its_time_until_done, + a_removed_accounts_events_stay, + no_event_holds_a_name_or_a_secret, + set_up_confirmed_and_listed, + a_set_up_belongs_to_one_device_expires_and_takes_five_tries, + changes_of_the_settings_recheck_the_device_and_the_password, + two_confirmations_at_once_turn_it_on_once, + a_time_step_is_accepted_once_and_only_later_ones_after_it, + recovery_codes_work_once_and_new_ones_replace_them, + wrong_proofs_lock_for_a_while_double_and_reset_on_success, + wrong_proofs_at_the_same_moment_all_count, + a_challenge_is_made_counted_used_once_and_carries_the_device, + an_account_keeps_five_open_challenges_and_expired_ones_go, + a_challenge_dies_with_every_change_of_the_account, + two_completions_of_one_challenge_give_one_device, + completions_and_changes_of_the_account_at_the_same_moment_leave_no_stray_device, + turning_two_factor_on_and_off_at_the_same_moment_as_a_sign_in, + an_invite_to_a_protected_account_needs_the_second_factor_in_the_same_transaction, + a_reset_code_for_a_protected_account_needs_the_factor_unless_it_turns_it_off, + admins_two_factor_is_checked_in_the_change_and_resets_need_it, + the_command_line_resets_two_factor_and_the_account_shows_it, + secrets_are_listed_and_replaced_only_if_unchanged, + parallel_wrong_proofs_on_one_challenge_count_exactly_five, + parallel_wrong_proofs_lock_for_exactly_fifteen_minutes, + two_factor_changes_write_their_events, + two_factor_sign_in_writes_login_and_counts_failures, + the_lock_writes_one_event_when_it_is_set, + a_join_writes_its_two_factor_events, + resets_and_confirmations_write_their_events, + web_sessions_are_made_looked_up_expire_and_log_out, + a_web_session_ends_with_every_change_of_the_accounts_generation, + a_web_sign_in_with_two_factor_needs_its_own_challenge, + web_sessions_are_pruned_in_bounded_batches, + a_web_actor_changes_accounts_until_its_session_ends, ); // ---- helpers ---- @@ -291,7 +359,7 @@ async fn users_and_devices(meta: &Meta) { assert_eq!(meta.user_by_name("ana").await.unwrap(), Some(ana.clone())); assert_eq!(meta.user_by_name("bob").await.unwrap(), None); - meta.set_password_hash(ana.id, "$argon2id$new").await.unwrap(); + meta.reset_password(ana.id, "$argon2id$new", true, NOW + 1).await.unwrap(); assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$new"); let token = [7u8; 32]; @@ -1771,6 +1839,12 @@ async fn upgrading_counts_what_each_space_already_stores() { // The database as it was before stored bytes were counted. use sqlx::Connection; let mut conn = sqlx::SqliteConnection::connect(&url).await.unwrap(); + // Before the audit log. + sqlx::query("DROP TABLE web_sessions").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE audit_events").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE login_challenges").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_recovery_codes").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_two_factor").execute(&mut conn).await.unwrap(); sqlx::query("ALTER TABLE spaces DROP COLUMN stored_bytes").execute(&mut conn).await.unwrap(); // And before live documents of files. sqlx::query("ALTER TABLE spaces DROP COLUMN live_bytes").execute(&mut conn).await.unwrap(); @@ -1783,6 +1857,17 @@ async fn upgrading_counts_what_each_space_already_stores() { sqlx::query("DROP TABLE invites").execute(&mut conn).await.unwrap(); // And before save records. sqlx::query("ALTER TABLE versions DROP COLUMN save").execute(&mut conn).await.unwrap(); + // And before the admin API (its `invites` column went with the table). + sqlx::query("ALTER TABLE spaces DROP COLUMN quota_bytes").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE users DROP COLUMN removed_ms").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE users DROP COLUMN auth_gen").execute(&mut conn).await.unwrap(); + // And before device sessions. + for column in ["client_kind", "platform", "app_version", "revoked_ms", "revoke_reason"] { + sqlx::query(sqlx::AssertSqlSafe(format!("ALTER TABLE devices DROP COLUMN {column}"))).execute(&mut conn).await.unwrap(); + } + for column in ["password_changed_ms", "disabled_ms"] { + sqlx::query(sqlx::AssertSqlSafe(format!("ALTER TABLE users DROP COLUMN {column}"))).execute(&mut conn).await.unwrap(); + } sqlx::query("DELETE FROM schema_version WHERE version >= 2").execute(&mut conn).await.unwrap(); } let meta = Meta::open(&url).await.unwrap(); @@ -1807,5 +1892,3665 @@ async fn concurrent_opens_migrate_once_on_postgres() { use sqlx::Connection; let mut conn = sqlx::PgConnection::connect(&url).await.unwrap(); let versions: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM schema_version").fetch_one(&mut conn).await.unwrap(); - assert_eq!(versions, 6); + assert_eq!(versions, 11); +} + +// ---- device sessions (migration 0007) ---- + +/// A device's record, read back from the list. +async fn record(meta: &Meta, user: &UserRecord, id: DeviceId) -> DeviceRecord { + meta.list_devices(user.id).await.unwrap().into_iter().find(|d| d.id == id).unwrap() +} + +/// The three columns of a revoke always agree: all set or none. +fn assert_revoke_columns_agree(devices: &[DeviceRecord]) { + for d in devices { + assert_eq!((d.revoked, d.revoked_ms.is_some(), d.revoke_reason.is_some()), (d.revoked, d.revoked, d.revoked), "{d:?}"); + } +} + +fn sorted(mut ids: Vec) -> Vec { + ids.sort_by_key(|id| *id.as_uuid()); + ids +} + +async fn a_device_records_its_client(meta: &Meta) { + let ana = user(meta, "ana").await; + let plain = device(meta, &ana).await; + let client = DeviceClient::new(Some("agent"), Some("windows x86_64"), Some("0.19.0")).unwrap(); + let agent = meta.create_device_with(ana.id, "studio", &client, &token(), NOW + 1).await.unwrap(); + assert_eq!(record(meta, &ana, agent).await.client, client); + let old = record(meta, &ana, plain).await; + assert_eq!((old.client, old.revoked_ms, old.revoke_reason), (DeviceClient::default(), None, None), "no word on the client: empty"); + + // What a sign-in may say: a known kind or none, short free text without control characters. + assert_eq!(DeviceClient::new(None, None, None).unwrap(), DeviceClient::default()); + assert!(DeviceClient::new(Some(""), Some(&"x".repeat(32)), Some(&"y".repeat(32))).is_ok()); + let long = "x".repeat(33); + for (kind, platform, version) in [ + (Some("browser"), None, None), + (Some("AGENT"), None, None), + (None, Some(long.as_str()), None), + (None, None, Some(long.as_str())), + (None, Some("a\nb"), None), + (None, None, Some("1.0\u{7}")), + ] { + assert!(matches!(DeviceClient::new(kind, platform, version), Err(MetaError::Invalid(_))), "{kind:?} {platform:?} {version:?}"); + } + // The store checks too, for a caller that built the value by hand: nothing is created. + let bad = DeviceClient { kind: "browser".into(), ..DeviceClient::default() }; + assert!(matches!(meta.create_device_with(ana.id, "x", &bad, &token(), NOW).await, Err(MetaError::Invalid(_)))); + assert_eq!(meta.list_devices(ana.id).await.unwrap().len(), 2); + + // A join says it too. + let (_, _, pairing, _) = paired(meta, "bob").await; + let desktop = DeviceClient::new(Some("desktop"), Some("macos"), Some("1.2")).unwrap(); + let joined = meta.redeem_with(&pairing.hash(), "mac", &desktop, &token(), None, NOW + 1).await.unwrap(); + let bob = meta.user_by_name("bob").await.unwrap().unwrap(); + assert_eq!(record(meta, &bob, joined.device).await.client, desktop); +} + +async fn only_own_live_devices_are_renamed(meta: &Meta) { + let (ana, bob) = (user(meta, "ana").await, user(meta, "bob").await); + let (mine, theirs) = (device(meta, &ana).await, device(meta, &bob).await); + let renamed = meta.rename_device(ana.id, mine, "studio pc").await.unwrap(); + assert_eq!((renamed.id, renamed.name.as_str()), (mine, "studio pc")); + assert_eq!(record(meta, &ana, mine).await.name, "studio pc"); + // Another account's device, and one that isn't there, are one answer. + assert!(matches!(meta.rename_device(ana.id, theirs, "mine now").await, Err(MetaError::NotFound(_)))); + assert!(matches!(meta.rename_device(ana.id, DeviceId::generate(), "x").await, Err(MetaError::NotFound(_)))); + assert_eq!(record(meta, &bob, theirs).await.name, "laptop"); + // The name rule holds. + for bad in ["", "tab\there", &"n".repeat(129)] { + assert!(matches!(meta.rename_device(ana.id, mine, bad).await, Err(MetaError::Invalid(_))), "{bad:?}"); + } + assert_eq!(record(meta, &ana, mine).await.name, "studio pc"); + // A revoked device keeps its name. + meta.revoke_device_as(ana.id, mine, RevokeReason::User, NOW + 1).await.unwrap(); + assert!(matches!(meta.rename_device(ana.id, mine, "again").await, Err(MetaError::NotFound(_)))); + assert_eq!(record(meta, &ana, mine).await.name, "studio pc"); +} + +async fn every_revoke_path_sets_flag_time_and_reason_together(meta: &Meta) { + // One device revoked by its user, and revoked again later: the first time and reason stay. + let ana = user(meta, "ana").await; + let (a1, a2, a3, a4) = (device(meta, &ana).await, device(meta, &ana).await, device(meta, &ana).await, device(meta, &ana).await); + meta.revoke_device_as(ana.id, a1, RevokeReason::User, NOW + 10).await.unwrap(); + let first = record(meta, &ana, a1).await; + assert_eq!((first.revoked, first.revoked_ms, first.revoke_reason), (true, Some(NOW + 10), Some(RevokeReason::User))); + meta.revoke_device_as(ana.id, a1, RevokeReason::Admin, NOW + 20).await.unwrap(); + assert_eq!(record(meta, &ana, a1).await, first, "a second revoke changes nothing"); + assert!(matches!(meta.revoke_device_as(user(meta, "bob").await.id, a1, RevokeReason::User, NOW).await, Err(MetaError::NotFound(_)))); + // The others, but one: the device revoked before keeps its own reason, the kept one stays. + let revoked = meta.revoke_other_devices(ana.id, a2, RevokeReason::Others, NOW + 30).await.unwrap(); + assert_eq!(sorted(revoked), sorted(vec![a3, a4]), "only the live others"); + for id in [a3, a4] { + let d = record(meta, &ana, id).await; + assert_eq!((d.revoked, d.revoked_ms, d.revoke_reason), (true, Some(NOW + 30), Some(RevokeReason::Others))); + } + assert_eq!(record(meta, &ana, a1).await, first); + let kept = record(meta, &ana, a2).await; + assert_eq!((kept.revoked, kept.revoked_ms, kept.revoke_reason), (false, None, None)); + assert!(meta.revoke_other_devices(ana.id, a2, RevokeReason::Others, NOW + 40).await.unwrap().is_empty(), "nobody left to sign out"); + // Every device (the admin's command). + let bob = meta.user_by_name("bob").await.unwrap().unwrap(); + let (b1, b2) = (device(meta, &bob).await, device(meta, &bob).await); + let revoked = meta.revoke_all_devices(bob.id, RevokeReason::Admin, NOW + 50).await.unwrap(); + assert_eq!(sorted(revoked), sorted(vec![b1, b2])); + assert!(meta.revoke_all_devices(bob.id, RevokeReason::Admin, NOW + 60).await.unwrap().is_empty()); + for id in [b1, b2] { + let d = record(meta, &bob, id).await; + assert_eq!((d.revoked, d.revoked_ms, d.revoke_reason), (true, Some(NOW + 50), Some(RevokeReason::Admin))); + } + // A password change and a reset. + let cy = user(meta, "cy").await; + let (c1, c2) = (device(meta, &cy).await, device(meta, &cy).await); + meta.change_password(cy.id, "$argon2id$stub", "$argon2id$new", c1, NOW + 70).await.unwrap(); + let d = record(meta, &cy, c2).await; + assert_eq!((d.revoked, d.revoked_ms, d.revoke_reason), (true, Some(NOW + 70), Some(RevokeReason::Password))); + let dee = user(meta, "dee").await; + let d1 = device(meta, &dee).await; + meta.reset_password(dee.id, "$argon2id$new", false, NOW + 80).await.unwrap(); + let d = record(meta, &dee, d1).await; + assert_eq!((d.revoked, d.revoked_ms, d.revoke_reason), (true, Some(NOW + 80), Some(RevokeReason::Reset))); + // The plain call (no clock of its own) is the user's act, with a time. + let eve = user(meta, "eve").await; + let e1 = device(meta, &eve).await; + meta.revoke_device(eve.id, e1).await.unwrap(); + let d = record(meta, &eve, e1).await; + assert_eq!((d.revoked, d.revoke_reason), (true, Some(RevokeReason::User))); + assert!(d.revoked_ms.is_some_and(|ms| ms > NOW)); + for u in [&ana, &bob, &cy, &dee, &eve] { + assert_revoke_columns_agree(&meta.list_devices(u.id).await.unwrap()); + } +} + +async fn revoking_others_keeps_the_caller_and_ends_their_pairings(meta: &Meta) { + let (ana, keep, keep_pairing, keep_id) = paired(meta, "ana").await; + let other = device(meta, &ana).await; + let other_id = meta.create_pairing(ana.id, other, &code(PAIRING_CHARS).hash(), NOW).await.unwrap(); + let (bob, bobs, _, bobs_pairing) = paired(meta, "bob").await; + let revoked = meta.revoke_other_devices(ana.id, keep, RevokeReason::Others, NOW + 1).await.unwrap(); + assert_eq!(revoked, vec![other]); + assert!(meta.device_active(keep).await.unwrap() && !meta.device_active(other).await.unwrap()); + assert_eq!(meta.pairing_status(ana.id, keep_id, NOW + 2).await.unwrap(), Some((PairingState::Waiting, None)), "the caller's own stays"); + assert_eq!(meta.pairing_status(ana.id, other_id, NOW + 2).await.unwrap(), None, "a revoked device's pairing dies"); + redeem(meta, &keep_pairing, "pc", NOW + 3).await.unwrap(); + // Another account is never touched. + assert!(meta.device_active(bobs).await.unwrap()); + assert!(meta.pairing_status(bob.id, bobs_pairing, NOW + 2).await.unwrap().is_some()); +} + +async fn a_password_change_is_one_step(meta: &Meta) { + let (ana, keep, _, pairing_id) = paired(meta, "ana").await; + let other = device(meta, &ana).await; + let other_pairing = meta.create_pairing(ana.id, other, &code(PAIRING_CHARS).hash(), NOW).await.unwrap(); + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let (bob, bobs, _, bobs_pairing) = paired(meta, "bob").await; + let bobs_invite = code(INVITE_CHARS); + meta.create_invite("bob", &bobs_invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + + let account = meta.account(ana.id).await.unwrap(); + assert_eq!((account.username.as_str(), account.role, account.created_ms, account.password_changed_ms), ("ana", Role::Member, NOW, None)); + assert!(matches!(meta.account(UserId(uuid::Uuid::now_v7())).await, Err(MetaError::NotFound(_)))); + + // A password that is not the one the caller verified (it changed meanwhile): nothing at all changes. + let stale = meta.change_password(ana.id, "$argon2id$stale", "$argon2id$new", keep, NOW + 5).await; + assert!(matches!(stale, Err(MetaError::Conflict(_))), "{stale:?}"); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$stub"); + assert_eq!(meta.account(ana.id).await.unwrap().password_changed_ms, None); + assert!(meta.device_active(keep).await.unwrap() && meta.device_active(other).await.unwrap()); + assert!(meta.pairing_status(ana.id, pairing_id, NOW + 6).await.unwrap().is_some()); + assert!(meta.pairing_status(ana.id, other_pairing, NOW + 6).await.unwrap().is_some()); + assert_eq!(meta.invite_needs_password(&invite.hash(), NOW + 6).await.unwrap(), Some(false)); + + // The change: new hash and time, the other device signed out, invite and pairings gone, the caller stays. + let revoked = meta.change_password(ana.id, "$argon2id$stub", "$argon2id$new", keep, NOW + 7).await.unwrap(); + assert_eq!(revoked, vec![other]); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$new"); + assert_eq!(meta.account(ana.id).await.unwrap().password_changed_ms, Some(NOW + 7)); + assert!(meta.device_active(keep).await.unwrap() && !meta.device_active(other).await.unwrap()); + let d = record(meta, &ana, other).await; + assert_eq!((d.revoked_ms, d.revoke_reason), (Some(NOW + 7), Some(RevokeReason::Password))); + assert_eq!(meta.pairing_status(ana.id, pairing_id, NOW + 8).await.unwrap(), None); + assert_eq!(meta.pairing_status(ana.id, other_pairing, NOW + 8).await.unwrap(), None); + assert_eq!(meta.invite_needs_password(&invite.hash(), NOW + 8).await.unwrap(), None); + assert!(matches!(redeem(meta, &invite, "pc", NOW + 8).await, Err(MetaError::InvalidCode))); + // The old hash no longer matches, so the same change again (a replayed request) does nothing. + assert!(matches!(meta.change_password(ana.id, "$argon2id$stub", "$argon2id$third", keep, NOW + 9).await, Err(MetaError::Conflict(_)))); + // A caller that was signed out meanwhile changes nothing either. + assert!( + matches!(meta.change_password(ana.id, "$argon2id$new", "$argon2id$third", other, NOW + 9).await, Err(MetaError::DeviceRevoked)), + "revoked" + ); + assert!( + matches!( + meta.change_password(ana.id, "$argon2id$new", "$argon2id$third", DeviceId::generate(), NOW + 9).await, + Err(MetaError::DeviceRevoked) + ), + "unknown" + ); + let bobs_device = device(meta, &user(meta, "cy").await).await; + assert!( + matches!(meta.change_password(ana.id, "$argon2id$new", "$argon2id$third", bobs_device, NOW + 9).await, Err(MetaError::DeviceRevoked)), + "another account's" + ); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$new"); + // Another account is never touched. + assert_eq!(meta.user_by_name("bob").await.unwrap().unwrap().password_hash, "$argon2id$stub"); + assert!(meta.device_active(bobs).await.unwrap()); + assert!(meta.pairing_status(bob.id, bobs_pairing, NOW + 8).await.unwrap().is_some()); + assert_eq!(meta.invite_needs_password(&bobs_invite.hash(), NOW + 8).await.unwrap(), Some(false)); +} + +async fn an_admin_reset_signs_out_every_device_unless_kept(meta: &Meta) { + let (ana, maker, _, pairing_id) = paired(meta, "ana").await; + let other = device(meta, &ana).await; + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let revoked = meta.reset_password(ana.id, "$argon2id$reset", false, NOW + 1).await.unwrap(); + assert_eq!(sorted(revoked), sorted(vec![maker, other])); + assert!(!meta.device_active(maker).await.unwrap() && !meta.device_active(other).await.unwrap()); + assert_eq!(record(meta, &ana, other).await.revoke_reason, Some(RevokeReason::Reset)); + assert_eq!(meta.pairing_status(ana.id, pairing_id, NOW + 2).await.unwrap(), None); + assert_eq!(meta.invite_needs_password(&invite.hash(), NOW + 2).await.unwrap(), None); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$reset"); + assert_eq!(meta.account(ana.id).await.unwrap().password_changed_ms, Some(NOW + 1)); + + // `--keep-devices`: the devices stay, the password changes, and codes made before it are still deleted. + let (bob, bobs, _, bobs_pairing) = paired(meta, "bob").await; + assert!(meta.reset_password(bob.id, "$argon2id$reset", true, NOW + 3).await.unwrap().is_empty()); + assert!(meta.device_active(bobs).await.unwrap()); + assert_eq!(record(meta, &bob, bobs).await.revoke_reason, None); + assert_eq!(meta.pairing_status(bob.id, bobs_pairing, NOW + 4).await.unwrap(), None); + assert_eq!(meta.user_by_name("bob").await.unwrap().unwrap().password_hash, "$argon2id$reset"); + + // A user that isn't there changes nothing. + assert!(matches!(meta.reset_password(UserId(uuid::Uuid::now_v7()), "$argon2id$x", false, NOW).await, Err(MetaError::NotFound(_)))); +} + +/// A password change from one device, a sign-out of the others from another and an admin reset, at the same moment +/// and over and over: each ends as a success or as a conflict or a signed-out caller, never as a database error (on +/// Postgres, two transactions taking the same rows in different orders would deadlock and one would fail). +async fn concurrent_password_changes_and_sign_outs_never_fail_the_database(meta: &Meta) { + for round in 0..15 { + let ana = user(meta, &format!("ana{round}")).await; + let (a, b) = (device(meta, &ana).await, device(meta, &ana).await); + let mut tasks = Vec::new(); + for job in 0..6 { + let meta = meta.clone(); + tasks.push(tokio::spawn(async move { + match job { + 0 => meta.change_password(ana.id, "$argon2id$stub", "$argon2id$one", a, NOW + 1).await.map(|_| ()), + 1 => meta.change_password(ana.id, "$argon2id$stub", "$argon2id$two", b, NOW + 1).await.map(|_| ()), + 2 => meta.revoke_other_devices(ana.id, b, RevokeReason::Others, NOW + 1).await.map(|_| ()), + 3 => meta.revoke_device_as(ana.id, a, RevokeReason::User, NOW + 1).await, + 4 => meta.reset_password(ana.id, "$argon2id$reset", false, NOW + 1).await.map(|_| ()), + _ => meta.revoke_all_devices(ana.id, RevokeReason::Admin, NOW + 1).await.map(|_| ()), + } + })); + } + for task in tasks { + let result = task.await.unwrap(); + assert!(matches!(result, Ok(()) | Err(MetaError::Conflict(_) | MetaError::DeviceRevoked)), "round {round}: {result:?}"); + } + assert_revoke_columns_agree(&meta.list_devices(ana.id).await.unwrap()); + } +} + +/// A device is created after a password sign-in only if the password that was checked is still the account's. +async fn a_sign_in_checked_against_an_old_password_creates_no_device(meta: &Meta) { + let ana = user(meta, "ana").await; + let client = DeviceClient::default(); + let first = meta.sign_in_device(ana.id, ("$argon2id$stub", 0), "pc", &client, &token(), NOW).await.unwrap(); + assert!(first.is_some_and(|d| d != DeviceId::generate())); + assert!(meta.sign_in_device(ana.id, ("$argon2id$other", 0), "pc", &client, &token(), NOW).await.unwrap().is_none(), "not the stored hash"); + assert!( + meta.sign_in_device(UserId(uuid::Uuid::now_v7()), ("$argon2id$stub", 0), "pc", &client, &token(), NOW).await.unwrap().is_none(), + "no such user" + ); + assert!(matches!(meta.sign_in_device(ana.id, ("$argon2id$stub", 0), "", &client, &token(), NOW).await, Err(MetaError::Invalid(_)))); + // The password was checked, then reset: the account's devices are signed out and the late sign-in adds none. + meta.reset_password(ana.id, "$argon2id$reset", false, NOW + 1).await.unwrap(); + assert!(meta.sign_in_device(ana.id, ("$argon2id$stub", 0), "late", &client, &token(), NOW + 2).await.unwrap().is_none()); + let devices = meta.list_devices(ana.id).await.unwrap(); + assert_eq!(devices.len(), 1); + assert!(devices.iter().all(|d| d.revoked)); + // With the new hash it works. + assert!(meta.sign_in_device(ana.id, ("$argon2id$reset", gen_of(meta, "ana").await), "new", &client, &token(), NOW + 3).await.unwrap().is_some()); +} + +/// Redeeming an invite or a pairing, a password sign-in and a reset at the same moment, over and over: no database +/// error (on Postgres, the invite and the user taken in different orders would deadlock), and once the reset is +/// done no device of the account is signed in, whichever of them came first. +async fn redeems_sign_ins_and_resets_at_the_same_moment_leave_no_live_device(meta: &Meta) { + for round in 0..15 { + let (ana, maker, pairing, _) = paired(meta, &format!("ana{round}")).await; + let invite = code(INVITE_CHARS); + meta.create_invite(&format!("ana{round}"), &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let mut tasks = Vec::new(); + for job in 0..5 { + let (meta, invite, pairing) = (meta.clone(), invite.hash(), pairing.hash()); + tasks.push(tokio::spawn(async move { + match job { + 0 => meta.redeem(&invite, "invited", &token(), None, NOW + 1).await.map(|_| ()), + 1 => meta.redeem(&pairing, "paired", &token(), None, NOW + 1).await.map(|_| ()), + 2 => { + meta.sign_in_device(ana.id, ("$argon2id$stub", 0), "signed in", &DeviceClient::default(), &token(), NOW + 1).await.map(|_| ()) + } + 3 => meta.create_pairing(ana.id, maker, &code(PAIRING_CHARS).hash(), NOW + 1).await.map(|_| ()), + _ => meta.reset_password(ana.id, "$argon2id$reset", false, NOW + 2).await.map(|_| ()), + } + })); + } + for task in tasks { + let result = task.await.unwrap(); + assert!(matches!(result, Ok(()) | Err(MetaError::InvalidCode | MetaError::NotFound(_))), "round {round}: {result:?}"); + } + let devices = meta.list_devices(ana.id).await.unwrap(); + assert!(devices.iter().all(|d| d.revoked), "round {round}: {devices:?}"); + assert_revoke_columns_agree(&devices); + } +} + +// ---- the migration from 0006 to 0007 ---- + +/// Drops what migration 0007 added and forgets it ran, which leaves the database as 0006 left it (with its rows). +async fn back_to_0006(url: &str) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + // What migrations 0010, 0009 and 0008 added goes first: they run again with 0007 on the next open. + sqlx::query("DROP TABLE web_sessions").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE audit_events").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE login_challenges").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_recovery_codes").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE invites DROP COLUMN reset_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE invites DROP COLUMN purpose").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE spaces DROP COLUMN quota_bytes").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE users DROP COLUMN removed_ms").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE users DROP COLUMN auth_gen").execute(&mut conn).await.unwrap(); + for column in ["client_kind", "platform", "app_version", "revoked_ms", "revoke_reason"] { + sqlx::query(sqlx::AssertSqlSafe(format!("ALTER TABLE devices DROP COLUMN {column}"))).execute(&mut conn).await.unwrap(); + } + for column in ["password_changed_ms", "disabled_ms"] { + sqlx::query(sqlx::AssertSqlSafe(format!("ALTER TABLE users DROP COLUMN {column}"))).execute(&mut conn).await.unwrap(); + } + sqlx::query("DELETE FROM schema_version WHERE version >= 7").execute(&mut conn).await.unwrap(); +} + +async fn schema_version(url: &str) -> i64 { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query_scalar("SELECT MAX(version) FROM schema_version").fetch_one(&mut conn).await.unwrap() +} + +/// Existing users and devices (live and revoked) survive the migration, read with empty client fields and no revoke +/// time or reason, still work (a live one is found by its token, a revoked one is not), and take the new columns; +/// a database newer than the program is refused. +async fn migrating_from_0006_keeps_the_rows(url: &str) { + sqlx::any::install_default_drivers(); + let (ana, live, revoked, old_token) = { + let meta = Meta::open(url).await.unwrap(); + let ana = user(&meta, "ana").await; + let old_token = token(); + let live = meta.create_device(ana.id, "old pc", &old_token, NOW).await.unwrap(); + let revoked = device(&meta, &ana).await; + meta.revoke_device(ana.id, revoked).await.unwrap(); + (ana, live, revoked, old_token) + }; + back_to_0006(url).await; + assert_eq!(schema_version(url).await, 6); + + let meta = Meta::open(url).await.unwrap(); + assert_eq!(schema_version(url).await, 11); + let old = record(&meta, &ana, live).await; + assert_eq!( + (old.name.as_str(), old.revoked, old.client, old.revoked_ms, old.revoke_reason), + ("old pc", false, DeviceClient::default(), None, None) + ); + let gone = record(&meta, &ana, revoked).await; + assert_eq!((gone.revoked, gone.client, gone.revoked_ms, gone.revoke_reason), (true, DeviceClient::default(), None, None)); + assert_eq!(meta.device_by_token(&old_token).await.unwrap().map(|a| a.device), Some(live)); + assert_eq!(meta.account(ana.id).await.unwrap().password_changed_ms, None); + // The new columns take values. + let client = DeviceClient::new(Some("cli"), Some("linux"), Some("1")).unwrap(); + let fresh = meta.create_device_with(ana.id, "new", &client, &token(), NOW + 1).await.unwrap(); + assert_eq!(record(&meta, &ana, fresh).await.client, client); + meta.change_password(ana.id, "$argon2id$stub", "$argon2id$new", fresh, NOW + 2).await.unwrap(); + assert_eq!(meta.account(ana.id).await.unwrap().password_changed_ms, Some(NOW + 2)); + assert_eq!(record(&meta, &ana, live).await.revoke_reason, Some(RevokeReason::Password)); + + // A database from a newer Craft Cloud is still refused. + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("INSERT INTO schema_version (version) VALUES (12)").execute(&mut conn).await.unwrap(); + drop(conn); + assert!(matches!(Meta::open(url).await, Err(MetaError::Corrupt(_)))); +} + +#[tokio::test] +async fn migrating_from_0006_keeps_the_rows_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + migrating_from_0006_keeps_the_rows(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn migrating_from_0006_keeps_the_rows_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + migrating_from_0006_keeps_the_rows(&url).await; +} + +// ---- a failure in the middle of a transaction ---- + +/// A trigger that makes every delete from `pairings` fail, which is the step after the password and the devices +/// were already written in `change_password` and `reset_password`. Test only: the migrations carry no trigger. +async fn break_pairing_deletes(url: &str, postgres: bool) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + let statements: &[&str] = if postgres { + &[ + "CREATE FUNCTION cc_test_boom() RETURNS trigger AS $$ BEGIN RAISE EXCEPTION 'boom'; END $$ LANGUAGE plpgsql", + "CREATE TRIGGER cc_test_boom BEFORE DELETE ON pairings FOR EACH STATEMENT EXECUTE FUNCTION cc_test_boom()", + ] + } else { + &["CREATE TRIGGER cc_test_boom BEFORE DELETE ON pairings BEGIN SELECT RAISE(ABORT, 'boom'); END"] + }; + for statement in statements { + sqlx::query(sqlx::AssertSqlSafe((*statement).to_owned())).execute(&mut conn).await.unwrap(); + } +} + +async fn mend_pairing_deletes(url: &str) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("DROP TRIGGER cc_test_boom ON pairings").execute(&mut conn).await.ok(); + sqlx::query("DROP TRIGGER cc_test_boom").execute(&mut conn).await.ok(); + sqlx::query("DROP FUNCTION cc_test_boom()").execute(&mut conn).await.ok(); +} + +/// A step that fails after others were written undoes them all: the hash, the time, the devices, the pairings and +/// the invite are as they were, for a password change and for an admin reset; once the step works, the same call +/// goes through. +async fn a_failure_in_the_middle_changes_nothing(url: &str, postgres: bool) { + let meta = Meta::open(url).await.unwrap(); + let (ana, keep, _, pairing_id) = paired(&meta, "ana").await; + let other = device(&meta, &ana).await; + let other_pairing = meta.create_pairing(ana.id, other, &code(PAIRING_CHARS).hash(), NOW).await.unwrap(); + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + break_pairing_deletes(url, postgres).await; + + let invite_hash = invite.hash(); + let unchanged = || async { + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$stub"); + assert_eq!(meta.account(ana.id).await.unwrap().password_changed_ms, None); + let devices = meta.list_devices(ana.id).await.unwrap(); + assert!(devices.iter().all(|d| !d.revoked && d.revoked_ms.is_none() && d.revoke_reason.is_none()), "{devices:?}"); + assert!(meta.pairing_status(ana.id, pairing_id, NOW + 1).await.unwrap().is_some()); + assert!(meta.pairing_status(ana.id, other_pairing, NOW + 1).await.unwrap().is_some()); + assert_eq!(meta.invite_needs_password(&invite_hash, NOW + 1).await.unwrap(), Some(false)); + }; + let failed = meta.change_password(ana.id, "$argon2id$stub", "$argon2id$new", keep, NOW + 2).await; + assert!(matches!(failed, Err(MetaError::Db(_))), "{failed:?}"); + unchanged().await; + let failed = meta.reset_password(ana.id, "$argon2id$new", false, NOW + 2).await; + assert!(matches!(failed, Err(MetaError::Db(_))), "{failed:?}"); + unchanged().await; + let failed = meta.revoke_other_devices(ana.id, keep, RevokeReason::Others, NOW + 2).await; + assert!(matches!(failed, Err(MetaError::Db(_))), "{failed:?}"); + unchanged().await; + + mend_pairing_deletes(url).await; + assert_eq!(meta.change_password(ana.id, "$argon2id$stub", "$argon2id$new", keep, NOW + 3).await.unwrap(), vec![other]); +} + +#[tokio::test] +async fn a_failure_in_the_middle_changes_nothing_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + a_failure_in_the_middle_changes_nothing(&format!("sqlite:{}", dir.path().join("meta.db").display()), false).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_failure_in_the_middle_changes_nothing_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + a_failure_in_the_middle_changes_nothing(&url, true).await; +} + +// ---- the admin API (migration 0008) ---- + +const ADMIN_HASH: &str = "$argon2id$admin"; + +/// An admin account with a device, and the actor an admin request on its behalf would carry. +async fn admin_actor(meta: &Meta, username: &str) -> (UserRecord, AdminActor) { + let u = meta.create_user(username, ADMIN_HASH, Role::Admin, NOW).await.unwrap(); + let device = device(meta, &u).await; + let actor = AdminActor { user: u.id, via: AdminVia::Device(device), password_hash: ADMIN_HASH.to_owned(), two_factor: false }; + (u, actor) +} + +/// The device a device-borne actor acts with. +fn device_of(actor: &AdminActor) -> DeviceId { + match actor.via { + AdminVia::Device(device) => device, + AdminVia::Web { .. } => panic!("a device actor was expected"), + } +} + +/// The generation a sign-in would read for `username` now. +async fn gen_of(meta: &Meta, username: &str) -> i64 { + meta.user_by_name(username).await.unwrap().unwrap().auth_gen +} + +async fn admin_view(meta: &Meta, user: UserId) -> AdminUser { + meta.admin_user(user, NOW).await.unwrap().unwrap() +} + +async fn users_page(meta: &Meta, after: Option<&str>, search: Option<&str>, limit: u32) -> (Vec, Option) { + let page = meta.admin_list_users(after, search, limit, NOW).await.unwrap(); + (page.users.into_iter().map(|u| u.username).collect(), page.next_after) +} + +async fn the_admin_list_pages_searches_and_counts(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + for name in ["ana", "bob", "a_b", "axb", "carol"] { + user(meta, name).await; + } + // By username, in pages; the last page has no continuation. + let (first, next) = users_page(meta, None, None, 3).await; + assert_eq!(first.len(), 3); + let (second, next2) = users_page(meta, next.as_deref(), None, 3).await; + assert_eq!(second.len(), 3); + assert_eq!(next2, None); + let mut all: Vec<_> = first.into_iter().chain(second).collect(); + let mut sorted_all = all.clone(); + sorted_all.sort(); + assert_eq!(all, sorted_all, "pages follow one order"); + all.dedup(); + assert_eq!(all.len(), 6, "no account twice, none missing"); + assert_eq!(users_page(meta, None, None, 6).await.1, None, "exactly a full page has no continuation"); + // A search finds a substring, ignores case, and takes `%` and `_` literally. + assert_eq!(users_page(meta, None, Some("A_B"), 50).await.0, vec!["a_b"]); + assert_eq!(users_page(meta, None, Some("a"), 50).await.0, vec!["a_b", "ana", "axb", "carol"]); + assert_eq!(users_page(meta, None, Some("%"), 50).await.0, Vec::::new()); + assert_eq!(users_page(meta, None, Some("_"), 50).await.0, vec!["a_b"]); + assert_eq!(users_page(meta, None, Some("zzz"), 50).await.0, Vec::::new()); + // A search pages too. + let (one, next) = users_page(meta, None, Some("a"), 1).await; + assert_eq!(one, vec!["a_b"]); + assert_eq!(users_page(meta, next.as_deref(), Some("a"), 2).await.0, vec!["ana", "axb"]); + assert!(matches!(meta.admin_list_users(None, Some(&"x".repeat(65)), 10, NOW).await, Err(MetaError::Invalid(_)))); + meta.admin_list_users(None, Some(&"x".repeat(64)), 10, NOW).await.unwrap(); + + // What a row says. + let ana = meta.user_by_name("ana").await.unwrap().unwrap(); + let dev = device(meta, &ana).await; + meta.add_chunk(ana.space, &random_chunk(), 70, NOW).await.unwrap(); + meta.create_invite("ana", &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let row = admin_view(meta, ana.id).await; + assert_eq!((row.role, row.devices_active, row.stored_bytes, row.quota_bytes, row.invite_pending), (Role::Member, 1, 70, None, true)); + meta.revoke_device(ana.id, dev).await.unwrap(); + assert_eq!(admin_view(meta, ana.id).await.devices_active, 0); + // An expired invite is not pending. + assert!(!meta.admin_user(ana.id, NOW + INVITE_TTL_MS).await.unwrap().unwrap().invite_pending); + // A reset code is not an invite. + let bob = meta.user_by_name("bob").await.unwrap().unwrap(); + meta.admin_issue_reset(&actor, bob.id, &code(INVITE_CHARS).hash(), false, NOW, RESET_TTL_MS).await.unwrap(); + assert!(!admin_view(meta, bob.id).await.invite_pending); + + // The counters: accounts, live devices, bytes. + let counts = meta.admin_server_counts().await.unwrap(); + assert_eq!((counts.users, counts.devices_active, counts.stored_bytes, counts.live_bytes), (6, 1, 70, 0), "only root's device is live"); + meta.admin_remove_user(&actor, ana.id, NOW).await.unwrap(); + assert_eq!(meta.admin_server_counts().await.unwrap().users, 5); + assert!(!users_page(meta, None, None, 50).await.0.contains(&"ana".to_owned()), "a removed account is not listed"); + assert!(meta.admin_user(ana.id, NOW).await.unwrap().is_some(), "but it can still be looked up"); +} + +async fn an_admin_makes_an_account_with_a_role_and_an_invite(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let invite = code(INVITE_CHARS); + let (made, issued) = meta.admin_create_user(&actor, "Newbie", Role::Admin, &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + assert_eq!((made.username.as_str(), made.role, made.devices_active, made.invite_pending), ("newbie", Role::Admin, 0, true)); + assert_eq!(issued.expires_ms, NOW + INVITE_TTL_MS); + // A name that exists, in any case, any role, is taken, and nothing is changed. + for name in ["newbie", "NEWBIE", "root"] { + let again = meta.admin_create_user(&actor, name, Role::Member, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await; + assert!(matches!(again, Err(MetaError::UsernameTaken)), "{name}: {again:?}"); + } + assert!(matches!( + meta.admin_create_user(&actor, "bad name", Role::Member, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await, + Err(MetaError::Invalid(_)) + )); + // It has no password: the invite asks for one, and sets it. The account keeps its role and has its own space. + assert_eq!(meta.invite_needs_password(&invite.hash(), NOW + 1).await.unwrap(), Some(true)); + assert!(matches!(meta.redeem(&invite.hash(), "pc", &token(), None, NOW + 1).await, Err(MetaError::PasswordRequired))); + let joined = meta.redeem(&invite.hash(), "pc", &token(), Some("$argon2id$chosen"), NOW + 2).await.unwrap(); + assert_eq!(joined.user, made.id); + let record = meta.user_by_name("newbie").await.unwrap().unwrap(); + assert_eq!((record.role, record.password_hash.as_str()), (Role::Admin, "$argon2id$chosen")); + assert!(meta.space_info(record.space).await.is_ok()); + assert!(!admin_view(meta, made.id).await.invite_pending); +} + +async fn an_admin_changes_a_role_and_a_quota(meta: &Meta) { + let (root, actor) = admin_actor(meta, "root").await; + let ana = user(meta, "ana").await; + // Nothing to change is invalid. + assert!(matches!(meta.admin_update_user(&actor, ana.id, None, None, NOW).await, Err(MetaError::Invalid(_)))); + let promoted = meta.admin_update_user(&actor, ana.id, Some(Role::Admin), None, NOW).await.unwrap(); + assert_eq!((promoted.role, promoted.quota_bytes), (Role::Admin, None)); + // Role and quota together; a missing quota leaves it, `Some(None)` removes it. + let both = meta.admin_update_user(&actor, ana.id, Some(Role::Member), Some(Some(MIN_QUOTA_BYTES)), NOW).await.unwrap(); + assert_eq!((both.role, both.quota_bytes), (Role::Member, Some(MIN_QUOTA_BYTES))); + let role_only = meta.admin_update_user(&actor, ana.id, Some(Role::Member), None, NOW).await.unwrap(); + assert_eq!(role_only.quota_bytes, Some(MIN_QUOTA_BYTES), "a missing quota leaves it"); + let cleared = meta.admin_update_user(&actor, ana.id, None, Some(None), NOW).await.unwrap(); + assert_eq!(cleared.quota_bytes, None); + // Bounds: a chunk up to i64::MAX. + for bad in [0, 1, MIN_QUOTA_BYTES - 1, i64::MAX as u64 + 1, u64::MAX] { + let result = meta.admin_update_user(&actor, ana.id, None, Some(Some(bad)), NOW).await; + assert!(matches!(result, Err(MetaError::Invalid(_))), "{bad}: {result:?}"); + } + for good in [MIN_QUOTA_BYTES, i64::MAX as u64] { + assert_eq!(meta.admin_update_user(&actor, ana.id, None, Some(Some(good)), NOW).await.unwrap().quota_bytes, Some(good)); + } + // Unknown users, and accounts marked for removal. + assert!(matches!(meta.admin_update_user(&actor, UserId(uuid::Uuid::now_v7()), Some(Role::Admin), None, NOW).await, Err(MetaError::NotFound(_)))); + // The last administrator can't be demoted, even by themselves; with another one they can. + let result = meta.admin_update_user(&actor, root.id, Some(Role::Member), None, NOW).await; + assert!(matches!(result, Err(MetaError::LastAdmin)), "{result:?}"); + assert_eq!(admin_view(meta, root.id).await.role, Role::Admin, "and nothing was changed"); + meta.admin_update_user(&actor, ana.id, Some(Role::Admin), None, NOW).await.unwrap(); + meta.admin_update_user(&actor, root.id, Some(Role::Member), None, NOW).await.unwrap(); + // The demoted admin's next change is refused: they are no admin any more. + let result = meta.admin_update_user(&actor, ana.id, Some(Role::Member), None, NOW).await; + assert!(matches!(result, Err(MetaError::Forbidden(_))), "{result:?}"); + let ana_actor = + AdminActor { user: ana.id, via: AdminVia::Device(device(meta, &ana).await), password_hash: "$argon2id$stub".to_owned(), two_factor: false }; + meta.admin_remove_user(&ana_actor, root.id, NOW).await.unwrap(); + let result = meta.admin_update_user(&ana_actor, root.id, Some(Role::Admin), None, NOW).await; + assert!(matches!(result, Err(MetaError::Conflict(_))), "{result:?}"); +} + +/// The acting admin's state is read again inside each change (the role and the password check the handler did can be +/// stale by then): a demotion, a sign-out, a password change or a disable since lets nothing through, and nothing is +/// changed. +async fn the_acting_admins_state_is_checked_in_the_transaction(meta: &Meta) { + let (_, boss) = admin_actor(meta, "boss").await; + let victim = user(meta, "victim").await; + let issue = |actor: AdminActor| { + let meta = meta.clone(); + async move { + meta.admin_create_user( + &actor, + &format!("made{}", uuid::Uuid::now_v7().simple()), + Role::Member, + &code(INVITE_CHARS).hash(), + NOW, + INVITE_TTL_MS, + ) + .await + .map(|_| ()) + } + }; + let users_before = meta.admin_server_counts().await.unwrap().users; + + // A member (a demoted admin) is forbidden. + let (demoted, demoted_actor) = admin_actor(meta, "demoted").await; + meta.admin_update_user(&boss, demoted.id, Some(Role::Member), None, NOW).await.unwrap(); + assert!(matches!(issue(demoted_actor).await, Err(MetaError::Forbidden(_)))); + // A signed-out device is unauthorized. + let (signed_out, signed_out_actor) = admin_actor(meta, "signedout").await; + meta.revoke_device(signed_out.id, device_of(&signed_out_actor)).await.unwrap(); + assert!(matches!(issue(signed_out_actor).await, Err(MetaError::DeviceRevoked))); + // A password changed since it was checked is a conflict. + let (changed, changed_actor) = admin_actor(meta, "changed").await; + meta.reset_password(changed.id, "$argon2id$other", true, NOW).await.unwrap(); + assert!(matches!(issue(changed_actor).await, Err(MetaError::Conflict(_)))); + // A disabled admin is unauthorized (their devices are revoked, and the check says it even for a device kept). + let (off, off_actor) = admin_actor(meta, "off").await; + meta.admin_disable_user(&boss, off.id, NOW).await.unwrap(); + assert!(matches!(issue(off_actor).await, Err(MetaError::DeviceRevoked))); + // Another account's device is not the actor's. + let borrowed = AdminActor { via: AdminVia::Device(device(meta, &victim).await), ..boss.clone() }; + assert!(matches!(issue(borrowed).await, Err(MetaError::DeviceRevoked))); + // A member who is no admin at all. + let member_actor = AdminActor { + user: victim.id, + via: AdminVia::Device(device(meta, &victim).await), + password_hash: "$argon2id$stub".to_owned(), + two_factor: false, + }; + assert!(matches!(meta.admin_disable_user(&member_actor, demoted.id, NOW).await, Err(MetaError::Forbidden(_)))); + assert_eq!(meta.admin_server_counts().await.unwrap().users, users_before + 4, "only the four admins made here, no account from a refused change"); + // Every kind of change checks, not only the first: one of each against a stale actor. + let (stale_user, stale_actor) = admin_actor(meta, "stale").await; + meta.admin_update_user(&boss, stale_user.id, Some(Role::Member), None, NOW).await.unwrap(); + assert!(matches!(meta.admin_update_user(&stale_actor, victim.id, Some(Role::Admin), None, NOW).await, Err(MetaError::Forbidden(_)))); + assert!(matches!(meta.admin_disable_user(&stale_actor, victim.id, NOW).await, Err(MetaError::Forbidden(_)))); + assert!(matches!(meta.admin_enable_user(&stale_actor, victim.id, NOW).await, Err(MetaError::Forbidden(_)))); + assert!(matches!(meta.admin_remove_user(&stale_actor, victim.id, NOW).await, Err(MetaError::Forbidden(_)))); + let hash = code(INVITE_CHARS).hash(); + assert!(matches!(meta.admin_issue_invite(&stale_actor, victim.id, &hash, NOW, INVITE_TTL_MS).await, Err(MetaError::Forbidden(_)))); + assert!(matches!(meta.admin_revoke_invite(&stale_actor, victim.id, NOW).await, Err(MetaError::Forbidden(_)))); + assert!(matches!(meta.admin_issue_reset(&stale_actor, victim.id, &hash, false, NOW, RESET_TTL_MS).await, Err(MetaError::Forbidden(_)))); + let victims_device = device(meta, &victim).await; + assert!(matches!(meta.admin_revoke_device(&stale_actor, victim.id, victims_device, NOW).await, Err(MetaError::Forbidden(_)))); + assert!(meta.device_active(victims_device).await.unwrap()); + assert_eq!(admin_view(meta, victim.id).await.role, Role::Member); +} + +/// A disabled account has nothing: its devices are signed out (with the reason), its codes gone, and no token, sign-in, +/// join or pairing works; enabling it brings back the account but never a device. +async fn disabling_signs_everything_out_and_enabling_brings_back_nothing(meta: &Meta) { + let (root, actor) = admin_actor(meta, "root").await; + let (ana, maker, pairing, pairing_id) = paired(meta, "ana").await; + let other = device(meta, &ana).await; + let other_token = token(); + let third = meta.create_device(ana.id, "third", &other_token, NOW).await.unwrap(); + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + + // Not oneself. + assert!(matches!(meta.admin_disable_user(&actor, root.id, NOW).await, Err(MetaError::Invalid(_)))); + assert!(matches!(meta.admin_remove_user(&actor, root.id, NOW).await, Err(MetaError::Invalid(_)))); + let revoked = meta.admin_disable_user(&actor, ana.id, NOW + 1).await.unwrap(); + assert_eq!(sorted(revoked), sorted(vec![maker, other, third])); + let view = admin_view(meta, ana.id).await; + assert_eq!((view.disabled_ms, view.devices_active, view.invite_pending), (Some(NOW + 1), 0, false)); + assert_eq!(meta.device_by_token(&other_token).await.unwrap(), None); + for d in meta.list_devices(ana.id).await.unwrap() { + assert_eq!((d.revoked, d.revoked_ms, d.revoke_reason), (true, Some(NOW + 1), Some(RevokeReason::Disabled))); + } + assert_eq!(meta.pairing_status(ana.id, pairing_id, NOW + 2).await.unwrap(), None); + // Disabling again is harmless. + assert!(meta.admin_disable_user(&actor, ana.id, NOW + 5).await.unwrap().is_empty()); + assert_eq!(admin_view(meta, ana.id).await.disabled_ms, Some(NOW + 1), "the first time stays"); + + // Nothing gets in: a sign-in that checked the right password, a device, a code the command line makes, a pairing. + let sign_in = meta.sign_in_device(ana.id, ("$argon2id$stub", gen_of(meta, "ana").await), "pc", &DeviceClient::default(), &token(), NOW + 3).await; + assert!(matches!(sign_in, Err(MetaError::AccountDisabled)), "{sign_in:?}"); + assert!(matches!(meta.create_device(ana.id, "pc", &token(), NOW + 3).await, Err(MetaError::AccountDisabled))); + let late_invite = code(INVITE_CHARS); + meta.create_invite("ana", &late_invite.hash(), NOW + 3, INVITE_TTL_MS).await.unwrap(); + assert!(matches!(redeem(meta, &late_invite, "pc", NOW + 4).await, Err(MetaError::AccountDisabled))); + assert!(matches!(redeem(meta, &pairing, "pc", NOW + 4).await, Err(MetaError::InvalidCode)), "its pairing is gone"); + assert!(meta.create_pairing(ana.id, maker, &code(PAIRING_CHARS).hash(), NOW + 4).await.is_err()); + assert_eq!(meta.list_devices(ana.id).await.unwrap().len(), 3, "no device was made"); + + // Enabling: sign-in works again; what was signed out stays signed out. + meta.admin_enable_user(&actor, ana.id, NOW).await.unwrap(); + assert_eq!(admin_view(meta, ana.id).await.disabled_ms, None); + assert!(meta.list_devices(ana.id).await.unwrap().iter().all(|d| d.revoked)); + assert_eq!(meta.device_by_token(&other_token).await.unwrap(), None); + assert!( + meta.sign_in_device(ana.id, ("$argon2id$stub", gen_of(meta, "ana").await), "pc", &DeviceClient::default(), &token(), NOW + 6) + .await + .unwrap() + .is_some() + ); + // The code made while the account was disabled works now, as an invite made for an enabled account would. + redeem(meta, &late_invite, "pc2", NOW + 7).await.unwrap(); + + // Marked for removal: disabled, not listed, signed out, can't be enabled, and the name stays taken. + let (bob, bobs, _, _) = paired(meta, "bob").await; + assert_eq!(meta.admin_remove_user(&actor, bob.id, NOW + 8).await.unwrap(), vec![bobs]); + let removed = admin_view(meta, bob.id).await; + assert_eq!((removed.disabled_ms, removed.devices_active), (Some(NOW + 8), 0)); + assert!(matches!(meta.admin_enable_user(&actor, bob.id, NOW).await, Err(MetaError::Conflict(_)))); + assert!(matches!(meta.create_user("bob", "x", Role::Member, NOW).await, Err(MetaError::UsernameTaken))); + assert!(matches!( + meta.admin_create_user(&actor, "bob", Role::Member, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await, + Err(MetaError::UsernameTaken) + )); + assert!(matches!( + meta.admin_issue_reset(&actor, bob.id, &code(INVITE_CHARS).hash(), false, NOW, RESET_TTL_MS).await, + Err(MetaError::Conflict(_)) + )); + let sign_in = meta.sign_in_device(bob.id, ("$argon2id$stub", gen_of(meta, "bob").await), "pc", &DeviceClient::default(), &token(), NOW + 9).await; + assert!(matches!(sign_in, Err(MetaError::AccountDisabled)), "{sign_in:?}"); + assert!(meta.admin_remove_user(&actor, bob.id, NOW + 10).await.unwrap().is_empty(), "removing again is harmless"); + assert_eq!(meta.admin_server_counts().await.unwrap().users, 2); +} + +/// A sign-in, a join with an invite, a join with a pairing, a new pairing and a plain device, all at the moment an +/// admin disables the account: each either finishes before and is signed out by the disable, or finds the account +/// disabled. Either way no device that is signed in is left, and enabling afterwards brings none back. +async fn issuing_access_while_an_account_is_disabled_leaves_no_usable_token(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + for round in 0..12 { + let name = format!("ana{round}"); + let (ana, maker, pairing, _) = paired(meta, &name).await; + let invite = code(INVITE_CHARS); + meta.create_invite(&name, &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let mut tasks = Vec::new(); + for job in 0..6 { + let (meta, actor, invite, pairing) = (meta.clone(), actor.clone(), invite.hash(), pairing.hash()); + tasks.push(tokio::spawn(async move { + match job { + 0 => meta.redeem(&invite, "invited", &token(), None, NOW + 1).await.map(|_| ()), + 1 => meta.redeem(&pairing, "paired", &token(), None, NOW + 1).await.map(|_| ()), + 2 => { + meta.sign_in_device(ana.id, ("$argon2id$stub", 0), "signed in", &DeviceClient::default(), &token(), NOW + 1).await.map(|_| ()) + } + 3 => meta.create_pairing(ana.id, maker, &code(PAIRING_CHARS).hash(), NOW + 1).await.map(|_| ()), + 4 => meta.create_device(ana.id, "plain", &token(), NOW + 1).await.map(|_| ()), + _ => meta.admin_disable_user(&actor, ana.id, NOW + 2).await.map(|_| ()), + } + })); + } + for task in tasks { + let result = task.await.unwrap(); + assert!( + matches!( + result, + Ok(()) | Err(MetaError::InvalidCode | MetaError::NotFound(_) | MetaError::AccountDisabled | MetaError::DeviceRevoked) + ), + "round {round}: {result:?}" + ); + } + meta.admin_enable_user(&actor, ana.id, NOW).await.unwrap(); + let devices = meta.list_devices(ana.id).await.unwrap(); + assert!(devices.iter().all(|d| d.revoked), "round {round}: {devices:?}"); + assert_revoke_columns_agree(&devices); + } +} + +/// A sign-in reads the hash and generation, verifies the password (slowly), and asks to create the device. A disable and +/// an enable in between leave the hash as it was but not the generation: the device is not created. So are a password +/// change and a removal. The interleaving is played out by calling the API in that order. +async fn a_sign_in_checked_before_a_disable_and_an_enable_creates_no_device(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let ana = user(meta, "ana").await; + let bob = user(meta, "bob").await; + let client = DeviceClient::default(); + for (name, id) in [("ana", ana.id), ("bob", bob.id)] { + let read = meta.user_by_name(name).await.unwrap().unwrap(); + if name == "ana" { + meta.admin_disable_user(&actor, id, NOW).await.unwrap(); + meta.admin_enable_user(&actor, id, NOW).await.unwrap(); + } else { + meta.admin_remove_user(&actor, id, NOW).await.unwrap(); + } + let late = meta.sign_in_device(id, (&read.password_hash, read.auth_gen), "pc", &client, &token(), NOW + 1).await; + match name { + "ana" => assert!(matches!(late, Ok(None)), "{late:?}"), + _ => assert!(matches!(late, Ok(None) | Err(MetaError::AccountDisabled)), "{late:?}"), + } + assert!(meta.list_devices(id).await.unwrap().is_empty(), "{name}: no device was made"); + } + // A sign-in that read the state after the enable goes through. + let fresh = meta.user_by_name("ana").await.unwrap().unwrap(); + assert!(fresh.auth_gen >= 2); + assert!(meta.sign_in_device(ana.id, (&fresh.password_hash, fresh.auth_gen), "pc", &client, &token(), NOW + 2).await.unwrap().is_some()); + // A password change moves the generation too. + let before = gen_of(meta, "ana").await; + meta.reset_password(ana.id, "$argon2id$new", true, NOW + 3).await.unwrap(); + assert!(gen_of(meta, "ana").await > before); +} + +async fn invites_and_reset_codes_replace_each_other(meta: &Meta) { + let (root, actor) = admin_actor(meta, "root").await; + let (fresh, _) = meta.admin_create_user(&actor, "fresh", Role::Member, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let ana = user(meta, "ana").await; + let (first, second, reset) = (code(INVITE_CHARS), code(INVITE_CHARS), code(INVITE_CHARS)); + + // An invite only for an account without a password; it replaces the open one, and can be revoked. + meta.admin_issue_invite(&actor, fresh.id, &first.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + meta.admin_issue_invite(&actor, fresh.id, &second.hash(), NOW + 1, INVITE_TTL_MS).await.unwrap(); + assert_eq!(meta.invite_needs_password(&first.hash(), NOW + 2).await.unwrap(), None); + assert_eq!(meta.invite_needs_password(&second.hash(), NOW + 2).await.unwrap(), Some(true)); + let conflict = meta.admin_issue_invite(&actor, ana.id, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await; + assert!(matches!(conflict, Err(MetaError::Conflict(_))), "an account with a password gets none: {conflict:?}"); + assert_eq!(meta.invite_needs_password(&second.hash(), NOW + 2).await.unwrap(), Some(true)); + meta.admin_revoke_invite(&actor, fresh.id, NOW).await.unwrap(); + assert_eq!(meta.invite_needs_password(&second.hash(), NOW + 2).await.unwrap(), None); + assert!(matches!(meta.admin_revoke_invite(&actor, fresh.id, NOW).await, Err(MetaError::NotFound(_))), "nothing left to revoke"); + assert!(matches!(meta.admin_revoke_invite(&actor, UserId(uuid::Uuid::now_v7()), NOW).await, Err(MetaError::NotFound(_)))); + + // A reset code replaces an invite and the other way round, and the account has one row either way. + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + meta.admin_issue_reset(&actor, ana.id, &reset.hash(), false, NOW + 1, RESET_TTL_MS).await.unwrap(); + assert_eq!(meta.invite_needs_password(&invite.hash(), NOW + 2).await.unwrap(), None); + assert_eq!(meta.invite_needs_password(&reset.hash(), NOW + 2).await.unwrap(), Some(true), "a reset always needs a password, though ana has one"); + let invite_again = code(INVITE_CHARS); + meta.create_invite("ana", &invite_again.hash(), NOW + 3, INVITE_TTL_MS).await.unwrap(); + assert_eq!(meta.invite_needs_password(&reset.hash(), NOW + 4).await.unwrap(), None, "the command line's invite replaces a reset code"); + assert_eq!(meta.invite_needs_password(&invite_again.hash(), NOW + 4).await.unwrap(), Some(false)); + meta.admin_issue_reset(&actor, ana.id, &reset.hash(), false, NOW + 5, RESET_TTL_MS).await.unwrap(); + meta.admin_revoke_invite(&actor, ana.id, NOW).await.unwrap(); + assert_eq!(meta.invite_needs_password(&reset.hash(), NOW + 6).await.unwrap(), None, "a revoke ends a reset code too"); + + // Not oneself, and not another admin. + assert!(matches!(meta.admin_issue_reset(&actor, root.id, &reset.hash(), false, NOW, RESET_TTL_MS).await, Err(MetaError::Invalid(_)))); + let (boss, _) = admin_actor(meta, "boss").await; + let result = meta.admin_issue_reset(&actor, boss.id, &reset.hash(), false, NOW, RESET_TTL_MS).await; + assert!(matches!(result, Err(MetaError::Forbidden(_))), "{result:?}"); + assert_eq!(meta.invite_needs_password(&reset.hash(), NOW + 6).await.unwrap(), None); + // Not a disabled account. + meta.admin_disable_user(&actor, ana.id, NOW).await.unwrap(); + assert!(matches!(meta.admin_issue_reset(&actor, ana.id, &reset.hash(), false, NOW, RESET_TTL_MS).await, Err(MetaError::Conflict(_)))); + assert!(matches!( + meta.admin_issue_reset(&actor, UserId(uuid::Uuid::now_v7()), &reset.hash(), false, NOW, RESET_TTL_MS).await, + Err(MetaError::NotFound(_)) + )); +} + +async fn a_reset_code_sets_a_password_and_signs_everything_out(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let (ana, maker, pairing, pairing_id) = paired(meta, "ana").await; + let other = device(meta, &ana).await; + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&actor, ana.id, &reset.hash(), false, NOW, RESET_TTL_MS).await.unwrap(); + // Nothing changes until it is redeemed. + assert!(meta.device_active(maker).await.unwrap()); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$stub"); + + // A reset without a new password is refused, and the code stays. + assert!(matches!(meta.redeem(&reset.hash(), "pc", &token(), None, NOW + 1).await, Err(MetaError::PasswordRequired))); + assert!(matches!(meta.redeem(&reset.hash(), "pc", &token(), Some(""), NOW + 1).await, Err(MetaError::PasswordRequired))); + assert!(meta.device_active(maker).await.unwrap()); + // With one: the password changes (though the account had one), every device is signed out, the pairings are gone, + // and the redeeming computer is the one device signed in. + let new_token = token(); + let joined = meta.redeem(&reset.hash(), "recovery pc", &new_token, Some("$argon2id$fresh"), NOW + 2).await.unwrap(); + assert_eq!(sorted(joined.revoked.clone()), sorted(vec![maker, other])); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$fresh"); + assert_eq!(meta.account(ana.id).await.unwrap().password_changed_ms, Some(NOW + 2)); + let devices = meta.list_devices(ana.id).await.unwrap(); + for d in &devices { + if d.id == joined.device { + assert!(!d.revoked); + } else { + assert_eq!((d.revoked, d.revoke_reason), (true, Some(RevokeReason::Reset))); + } + } + assert_revoke_columns_agree(&devices); + assert_eq!(meta.device_by_token(&new_token).await.unwrap().map(|a| a.device), Some(joined.device)); + assert_eq!(meta.pairing_status(ana.id, pairing_id, NOW + 3).await.unwrap(), None); + assert!(matches!(redeem(meta, &pairing, "late", NOW + 3).await, Err(MetaError::InvalidCode))); + // Once only. + assert!(matches!(meta.redeem(&reset.hash(), "again", &token(), Some("$argon2id$x"), NOW + 4).await, Err(MetaError::InvalidCode))); + + // It lasts an hour. + let late = code(INVITE_CHARS); + meta.admin_issue_reset(&actor, ana.id, &late.hash(), false, NOW + 10, RESET_TTL_MS).await.unwrap(); + let result = meta.redeem(&late.hash(), "late", &token(), Some("$argon2id$x"), NOW + 10 + RESET_TTL_MS).await; + assert!(matches!(result, Err(MetaError::InvalidCode)), "{result:?}"); + assert_eq!(RESET_TTL_MS, 3_600_000); + meta.redeem(&late.hash(), "in time", &token(), Some("$argon2id$x"), NOW + 10 + RESET_TTL_MS - 1).await.unwrap(); + + // Disabling the account deletes the reset code it still held. + let disabled_code = code(INVITE_CHARS); + meta.admin_issue_reset(&actor, ana.id, &disabled_code.hash(), false, NOW + 20, RESET_TTL_MS).await.unwrap(); + meta.admin_disable_user(&actor, ana.id, NOW + 21).await.unwrap(); + assert_eq!(meta.invite_needs_password(&disabled_code.hash(), NOW + 22).await.unwrap(), None, "disabling deletes it"); +} + +/// The quota of a space is its limit when set, with or without the server-wide cap, in the look before the bytes are +/// stored and in the count that follows. +async fn a_quota_overrides_the_server_wide_cap(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let ana = user(meta, "ana").await; + let bob = user(meta, "bob").await; + let quota = MIN_QUOTA_BYTES; + meta.admin_update_user(&actor, ana.id, None, Some(Some(quota)), NOW).await.unwrap(); + let (c1, c2, c3) = (random_chunk(), random_chunk(), random_chunk()); + + // No cap at all: the quota still holds, and a space without one is unlimited. + meta.check_room(ana.space, BlobKind::Chunk, c1.as_bytes(), quota).await.unwrap(); + let over = meta.check_room(ana.space, BlobKind::Chunk, c1.as_bytes(), quota + 1).await; + assert!(matches!(over, Err(MetaError::QuotaExceeded { used: 0, limit }) if limit == quota), "{over:?}"); + meta.check_room(bob.space, BlobKind::Chunk, c1.as_bytes(), quota * 10).await.unwrap(); + meta.add_chunk(ana.space, &c1, quota - 10, NOW).await.unwrap(); + let over = meta.add_chunk(ana.space, &c2, 11, NOW).await; + assert!(matches!(over, Err(MetaError::QuotaExceeded { limit, .. }) if limit == quota), "{over:?}"); + assert_eq!(meta.stored_bytes(ana.space).await.unwrap(), quota - 10, "nothing was recorded"); + assert!(matches!(meta.add_chunks(ana.space, &[(c2, 6), (c3, 6)], NOW).await, Err(MetaError::QuotaExceeded { .. }))); + meta.add_chunks(ana.space, &[(c2, 5), (c3, 5)], NOW).await.unwrap(); + assert_eq!(meta.stored_bytes(ana.space).await.unwrap(), quota); + meta.add_chunk(bob.space, &c1, quota * 3, NOW).await.unwrap(); + // What the space has already costs nothing to look at. + meta.check_room(ana.space, BlobKind::Chunk, c1.as_bytes(), quota * 5).await.unwrap(); + + // A cap larger than the quota: the quota wins. A cap smaller than the quota: the quota wins, too (it is the + // account's limit, whichever way it differs). + let big_cap = meta.clone().with_stored_bytes_cap(Some(quota * 100)); + assert!(matches!(big_cap.check_room(ana.space, BlobKind::Chunk, random_chunk().as_bytes(), 1).await, Err(MetaError::QuotaExceeded { .. }))); + assert!(matches!(big_cap.add_chunk(ana.space, &random_chunk(), 1, NOW).await, Err(MetaError::QuotaExceeded { .. }))); + let small_cap = meta.clone().with_stored_bytes_cap(Some(100)); + meta.admin_update_user(&actor, bob.id, None, Some(Some(quota * 10)), NOW).await.unwrap(); + small_cap.check_room(bob.space, BlobKind::Chunk, random_chunk().as_bytes(), quota).await.unwrap(); + small_cap.add_chunk(bob.space, &random_chunk(), quota, NOW).await.unwrap(); + // Removing the quota gives the cap back; a quota under the use deletes nothing and refuses new bytes. + meta.admin_update_user(&actor, ana.id, None, Some(None), NOW).await.unwrap(); + big_cap.add_chunk(ana.space, &random_chunk(), 1, NOW).await.unwrap(); + assert!(matches!(small_cap.add_chunk(ana.space, &random_chunk(), 1, NOW).await, Err(MetaError::QuotaExceeded { .. }))); + meta.admin_update_user(&actor, bob.id, None, Some(Some(MIN_QUOTA_BYTES)), NOW).await.unwrap(); + assert!(meta.stored_bytes(bob.space).await.unwrap() > MIN_QUOTA_BYTES); + assert!(matches!(meta.add_chunk(bob.space, &random_chunk(), 1, NOW).await, Err(MetaError::QuotaExceeded { .. }))); +} + +async fn an_admin_lists_and_revokes_a_users_devices(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let (ana, maker, _, pairing_id) = paired(meta, "ana").await; + let bob = user(meta, "bob").await; + let bobs = device(meta, &bob).await; + let other = device(meta, &ana).await; + meta.revoke_device(ana.id, other).await.unwrap(); + // Every device, revoked ones with their reason. + let listed = meta.admin_devices(ana.id).await.unwrap(); + assert_eq!(listed.len(), 2); + assert_eq!(listed.iter().find(|d| d.id == other).unwrap().revoke_reason, Some(RevokeReason::User)); + assert!(matches!(meta.admin_devices(UserId(uuid::Uuid::now_v7())).await, Err(MetaError::NotFound(_)))); + + meta.admin_revoke_device(&actor, ana.id, maker, NOW + 1).await.unwrap(); + let revoked = record(meta, &ana, maker).await; + assert_eq!((revoked.revoked, revoked.revoked_ms, revoked.revoke_reason), (true, Some(NOW + 1), Some(RevokeReason::Admin))); + assert_eq!(meta.pairing_status(ana.id, pairing_id, NOW + 2).await.unwrap(), None, "its pairing dies with it"); + // Again: the first time and reason stay. Another user's device, an unknown device or user: not found, nothing changed. + meta.admin_revoke_device(&actor, ana.id, maker, NOW + 5).await.unwrap(); + assert_eq!(record(meta, &ana, maker).await.revoked_ms, Some(NOW + 1)); + assert_eq!(record(meta, &ana, other).await.revoke_reason, Some(RevokeReason::User)); + assert!(matches!(meta.admin_revoke_device(&actor, ana.id, bobs, NOW).await, Err(MetaError::NotFound(_)))); + assert!(matches!(meta.admin_revoke_device(&actor, ana.id, DeviceId::generate(), NOW).await, Err(MetaError::NotFound(_)))); + assert!(matches!(meta.admin_revoke_device(&actor, UserId(uuid::Uuid::now_v7()), bobs, NOW).await, Err(MetaError::NotFound(_)))); + assert!(meta.device_active(bobs).await.unwrap()); +} + +// Two admins at the same moment, each taking the other (or oneself) away: whatever the order, one enabled admin is +// left, and no statement fails inside the database (on Postgres, rows taken in different orders would deadlock). + +#[derive(Clone, Copy, Debug)] +enum Act { + Demote(usize), + Disable(usize), + Remove(usize), +} + +/// Runs `acts` (who acts, what) at the same moment on a survivor and a new admin; exactly one enabled admin must be +/// left, and the second change of two must fail with a reason the rules give. Returns the admin left. +async fn race_two_admins(meta: &Meta, round: usize, survivor: (UserRecord, AdminActor), acts: [(usize, Act); 2]) -> (UserRecord, AdminActor) { + let fresh = admin_actor(meta, &format!("fresh{round}")).await; + let both = [survivor, fresh]; + let mut tasks = Vec::new(); + for (who, act) in acts { + let (meta, actor) = (meta.clone(), both[who].1.clone()); + let target = both[match act { + Act::Demote(t) | Act::Disable(t) | Act::Remove(t) => t, + }] + .0 + .id; + tasks.push(tokio::spawn(async move { + match act { + Act::Demote(_) => meta.admin_update_user(&actor, target, Some(Role::Member), None, NOW).await.map(|_| ()), + Act::Disable(_) => meta.admin_disable_user(&actor, target, NOW).await.map(|_| ()), + Act::Remove(_) => meta.admin_remove_user(&actor, target, NOW).await.map(|_| ()), + } + })); + } + for (task, (_, act)) in tasks.into_iter().zip(acts) { + let result = task.await.unwrap(); + assert!( + matches!(result, Ok(()) | Err(MetaError::LastAdmin | MetaError::Forbidden(_) | MetaError::DeviceRevoked | MetaError::Invalid(_))), + "round {round}, {act:?}: {result:?}" + ); + } + let page = meta.admin_list_users(None, None, 200, NOW).await.unwrap(); + let left: Vec<_> = page.users.iter().filter(|u| u.role == Role::Admin && u.disabled_ms.is_none()).collect(); + assert_eq!(left.len(), 1, "round {round}, {acts:?}: {left:?}"); + let left = left[0].id; + let [a, b] = both; + if a.0.id == left { a } else { b } +} + +async fn two_admins_racing_keep_one(meta: &Meta, combos: &[[(usize, Act); 2]]) { + let mut survivor = admin_actor(meta, "first").await; + for round in 0..8 { + for (i, combo) in combos.iter().enumerate() { + survivor = race_two_admins(meta, round * 10 + i, survivor, *combo).await; + } + } +} + +async fn two_demotions_at_once_keep_an_admin(meta: &Meta) { + two_admins_racing_keep_one(meta, &[[(0, Act::Demote(1)), (1, Act::Demote(0))]]).await; +} + +/// Each admin steps down at the same moment: their rows are different, so only the `last_admin` lock keeps one of them. +async fn two_self_demotions_at_once_keep_an_admin(meta: &Meta) { + two_admins_racing_keep_one(meta, &[[(0, Act::Demote(0)), (1, Act::Demote(1))]]).await; +} + +async fn two_disables_at_once_keep_an_admin(meta: &Meta) { + two_admins_racing_keep_one(meta, &[[(0, Act::Disable(1)), (1, Act::Disable(0))]]).await; +} + +async fn a_demotion_and_a_disable_at_once_keep_an_admin(meta: &Meta) { + two_admins_racing_keep_one(meta, &[[(0, Act::Demote(1)), (1, Act::Disable(0))], [(0, Act::Disable(1)), (1, Act::Demote(0))]]).await; +} + +async fn a_removal_and_a_demotion_at_once_keep_an_admin(meta: &Meta) { + two_admins_racing_keep_one(meta, &[[(0, Act::Remove(1)), (1, Act::Demote(0))], [(0, Act::Demote(1)), (1, Act::Remove(0))]]).await; +} + +async fn a_self_demotion_and_a_disable_of_the_other_at_once_keep_an_admin(meta: &Meta) { + two_admins_racing_keep_one(meta, &[[(0, Act::Demote(0)), (0, Act::Disable(1))], [(1, Act::Demote(1)), (1, Act::Disable(0))]]).await; +} + +// ---- the migration from 0007 to 0008 ---- + +/// An account, its invite and the audit log survive the two-factor migration (0010, which runs after the audit log's 0009). +async fn migrating_from_0009_keeps_the_rows(url: &str) { + use sqlx::{Connection, Row}; + sqlx::any::install_default_drivers(); + let (ana, invite, audit_rows) = { + let meta = Meta::open(url).await.unwrap(); + let ana = user(&meta, "ana").await; + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + meta.sign_in_device(ana.id, (&ana.password_hash, ana.auth_gen), "pc", &DeviceClient::default(), &token(), NOW).await.unwrap().unwrap(); + let events = all_events(&meta).await; + assert!(!events.is_empty(), "the audit log has rows to keep"); + (ana, invite, events.len()) + }; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("DROP TABLE web_sessions").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE login_challenges").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_recovery_codes").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE invites DROP COLUMN reset_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("DELETE FROM schema_version WHERE version >= 10").execute(&mut conn).await.unwrap(); + drop(conn); + assert_eq!(schema_version(url).await, 9, "the audit log's migration stays"); + let meta = Meta::open(url).await.unwrap(); + assert_eq!(schema_version(url).await, 11); + assert_eq!(all_events(&meta).await.len(), audit_rows, "the audit rows are kept"); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().id, ana.id); + assert_eq!(meta.invite_needs_password(&invite.hash(), NOW + 1).await.unwrap(), Some(false)); + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + let row = sqlx::query("SELECT reset_two_factor FROM invites").fetch_one(&mut conn).await.unwrap(); + assert_eq!(row.try_get::("reset_two_factor").unwrap(), 0); +} + +#[tokio::test] +async fn migrating_from_0009_keeps_the_rows_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + migrating_from_0009_keeps_the_rows(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn migrating_from_0009_keeps_the_rows_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + migrating_from_0009_keeps_the_rows(&url).await; +} + +/// Drops what migration 0008 added and forgets it ran, which leaves the database as 0007 left it (with its rows). +async fn back_to_0007(url: &str) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + // The audit log (migration 0009) and two-factor sign-in (migration 0010) go first: they run again with 0008 on the next open. + sqlx::query("DROP TABLE web_sessions").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE audit_events").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE login_challenges").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_recovery_codes").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE invites DROP COLUMN reset_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE invites DROP COLUMN purpose").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE spaces DROP COLUMN quota_bytes").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE users DROP COLUMN removed_ms").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE users DROP COLUMN auth_gen").execute(&mut conn).await.unwrap(); + sqlx::query("DELETE FROM schema_version WHERE version >= 8").execute(&mut conn).await.unwrap(); +} + +/// A database from the release before the admin API keeps its accounts, devices and invites: an old invite reads as an +/// invite and still works, no space has a quota and no user a removal mark, and the new calls work on it. +async fn migrating_from_0007_keeps_the_rows(url: &str) { + sqlx::any::install_default_drivers(); + let (ana, old_token, invite) = { + let meta = Meta::open(url).await.unwrap(); + let ana = user(&meta, "ana").await; + let old_token = token(); + meta.create_device(ana.id, "old pc", &old_token, NOW).await.unwrap(); + meta.add_chunk(ana.space, &random_chunk(), 42, NOW).await.unwrap(); + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + (ana, old_token, invite) + }; + back_to_0007(url).await; + assert_eq!(schema_version(url).await, 7); + + let meta = Meta::open(url).await.unwrap(); + assert_eq!(schema_version(url).await, 11); + assert_eq!(meta.device_by_token(&old_token).await.unwrap().map(|a| a.user), Some(ana.id)); + let row = admin_view(&meta, ana.id).await; + assert_eq!((row.disabled_ms, row.quota_bytes, row.stored_bytes, row.devices_active, row.invite_pending), (None, None, 42, 1, true)); + assert_eq!(meta.invite_needs_password(&invite.hash(), NOW + 1).await.unwrap(), Some(false), "an old invite is an invite"); + let (_, actor) = admin_actor(&meta, "root").await; + meta.admin_update_user(&actor, ana.id, None, Some(Some(MIN_QUOTA_BYTES)), NOW).await.unwrap(); + meta.admin_disable_user(&actor, ana.id, NOW).await.unwrap(); + assert_eq!(meta.device_by_token(&old_token).await.unwrap(), None); + + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("INSERT INTO schema_version (version) VALUES (12)").execute(&mut conn).await.unwrap(); + drop(conn); + assert!(matches!(Meta::open(url).await, Err(MetaError::Corrupt(_)))); +} + +#[tokio::test] +async fn migrating_from_0007_keeps_the_rows_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + migrating_from_0007_keeps_the_rows(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn migrating_from_0007_keeps_the_rows_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + migrating_from_0007_keeps_the_rows(&url).await; +} + +// ---- the audit log (migration 0009) ---- + +use craft_cloud_meta::{ + ActionFilter, AuditAction, AuditCursor, AuditEvent, AuditFailure, AuditOutcome, AuditQuery, Origin, PRUNE_BATCH, PRUNE_EVERY_MS, Step, +}; + +/// Every event, newest first. +async fn all_events(meta: &Meta) -> Vec { + meta.audit_list(&AuditQuery { limit: 1000, ..AuditQuery::default() }).await.unwrap() +} + +/// Remembers which events a test has looked at, and hands out what was written since, oldest first. +struct Seen<'a> { + meta: &'a Meta, + ids: std::collections::HashSet, +} + +impl<'a> Seen<'a> { + async fn new(meta: &'a Meta) -> Self { + let mut seen = Self { meta, ids: Default::default() }; + seen.fresh().await; + seen + } + + async fn fresh(&mut self) -> Vec { + let Self { meta, ids } = self; + let mut fresh: Vec = all_events(meta).await.into_iter().filter(|e| ids.insert(e.id)).collect(); + fresh.reverse(); + fresh + } + + /// The names of the events written since the last look, sorted (two events of one change have no order to rely on). + async fn actions(&mut self) -> Vec<&'static str> { + let mut names: Vec<&'static str> = self.fresh().await.iter().map(|e| e.action.as_str()).collect(); + names.sort_unstable(); + names + } + + /// The one event written since the last look. + async fn one(&mut self) -> AuditEvent { + let mut fresh = self.fresh().await; + assert_eq!(fresh.len(), 1, "{:?}", fresh.iter().map(|e| e.action.as_str()).collect::>()); + fresh.remove(0) + } +} + +fn details(e: &AuditEvent) -> serde_json::Value { + serde_json::from_str(&e.details).unwrap() +} + +/// An event of the day-to-day, shorthand for the matrix below. +fn json(text: &str) -> serde_json::Value { + serde_json::from_str(text).unwrap() +} + +/// Every operation of the matrix in `docs/design/sync.md` writes exactly the events it names, with the actor, target and +/// typed details it names; a repeat or a no-op writes none; a bulk sign-out writes one event with the count. Creating +/// an account or a device by the test seams (`create_user`, `create_device`) writes none. +async fn every_operation_writes_exactly_its_events(meta: &Meta) { + let (root, actor) = admin_actor(meta, "root").await; + let ana = user(meta, "ana").await; + let mut seen = Seen::new(meta).await; + assert!(all_events(meta).await.is_empty(), "the test seams write no events"); + + // A sign-in: the account, the new device, and the client reduced to the closed lists. + let client = DeviceClient::new(Some("agent"), Some("Linux"), Some("1.2.3")).unwrap(); + let dev = meta.sign_in_device(ana.id, (&ana.password_hash, ana.auth_gen), "pc", &client, &token(), NOW).await.unwrap().unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.outcome, e.actor_user, e.actor_device, e.count), + (AuditAction::AuthLogin, AuditOutcome::Success, Some(ana.id), Some(dev), 1) + ); + assert_eq!((e.target_kind.as_str(), e.target_id), ("device", Some(*dev.as_uuid()))); + assert_eq!(details(&e), json(r#"{"client_kind":"agent","platform":"linux"}"#)); + assert_eq!(e.at_ms, NOW); + // A sign-in that is refused writes nothing. + assert!(meta.sign_in_device(ana.id, ("$argon2id$other", 0), "pc", &client, &token(), NOW).await.unwrap().is_none()); + assert!(seen.actions().await.is_empty()); + + // Signing out one device: from a device, then the same again (nothing changed), then as the account without a device. + let (d2, d3) = (device(meta, &ana).await, device(meta, &ana).await); + meta.revoke_device_by(ana.id, d2, RevokeReason::User, Origin::Device(dev), NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device, e.target_id), (AuditAction::DeviceRevoked, Some(ana.id), Some(dev), Some(*d2.as_uuid()))); + assert_eq!(details(&e), json(r#"{"reason":"user"}"#), "the device is the event's target"); + meta.revoke_device_by(ana.id, d2, RevokeReason::User, Origin::Device(dev), NOW).await.unwrap(); + assert!(seen.actions().await.is_empty(), "a device that is out already: no change, no event"); + meta.revoke_device(ana.id, d3).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device), (AuditAction::DeviceRevoked, Some(ana.id), None)); + + // Signing out the others: one event with the count; with nobody to sign out, none. + let (_e1, _e2) = (device(meta, &ana).await, device(meta, &ana).await); + meta.revoke_other_devices(ana.id, dev, RevokeReason::Others, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_device, e.target_id), (AuditAction::DeviceRevokedOthers, Some(dev), Some(ana.id.0))); + assert_eq!(details(&e), json(r#"{"reason":"others","revoked":2}"#)); + meta.revoke_other_devices(ana.id, dev, RevokeReason::Others, NOW).await.unwrap(); + assert!(seen.actions().await.is_empty()); + + // A password change: one event, the devices signed out with it are only in `revoked`. + let (_f1, _f2) = (device(meta, &ana).await, device(meta, &ana).await); + meta.change_password(ana.id, &ana.password_hash, "$argon2id$new", dev, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device), (AuditAction::AccountPasswordChanged, Some(ana.id), Some(dev))); + assert_eq!(details(&e), json(r#"{"revoked":2}"#)); + // A change that fails writes nothing: the password it checked is not the account's. + assert!(matches!(meta.change_password(ana.id, "$argon2id$wrong", "$argon2id$x", dev, NOW).await, Err(MetaError::Conflict(_)))); + assert!(seen.actions().await.is_empty()); + + // Accounts: created with a role; the invite that goes with it is not an event of its own. + let invite = code(INVITE_CHARS); + let (newbie, _) = meta.admin_create_user(&actor, "newbie", Role::Member, &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.actor_user, e.actor_device, e.target_id), + (AuditAction::AdminUserCreated, Some(root.id), Some(device_of(&actor)), Some(newbie.id.0)) + ); + assert_eq!(details(&e), json(r#"{"role_to":"member"}"#)); + // Invites: issued, revoked (a second revoke has nothing to revoke). + meta.admin_issue_invite(&actor, newbie.id, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await.unwrap(); + assert_eq!(seen.actions().await, ["admin.invite_issued"]); + meta.admin_revoke_invite(&actor, newbie.id, NOW).await.unwrap(); + assert_eq!(seen.actions().await, ["admin.invite_revoked"]); + assert!(matches!(meta.admin_revoke_invite(&actor, newbie.id, NOW).await, Err(MetaError::NotFound(_)))); + assert!(seen.actions().await.is_empty()); + + // A role and a quota in one request: one event for each, in the same transaction. The same again changes nothing. + meta.admin_update_user(&actor, ana.id, Some(Role::Admin), Some(Some(MIN_QUOTA_BYTES)), NOW).await.unwrap(); + let both = seen.fresh().await; + let mut names: Vec<&str> = both.iter().map(|e| e.action.as_str()).collect(); + names.sort_unstable(); + assert_eq!(names, ["admin.quota_changed", "admin.role_changed"]); + for e in &both { + assert_eq!((e.actor_user, e.target_id), (Some(root.id), Some(ana.id.0))); + match e.action { + AuditAction::AdminRoleChanged => assert_eq!(details(e), json(r#"{"role_from":"member","role_to":"admin"}"#)), + _ => assert_eq!(details(e), json(&format!(r#"{{"quota_bytes":{MIN_QUOTA_BYTES}}}"#))), + } + } + meta.admin_update_user(&actor, ana.id, Some(Role::Admin), Some(Some(MIN_QUOTA_BYTES)), NOW).await.unwrap(); + assert!(seen.actions().await.is_empty(), "the role and quota it has already: no change, no event"); + meta.admin_update_user(&actor, ana.id, Some(Role::Member), None, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, details(&e)), (AuditAction::AdminRoleChanged, json(r#"{"role_from":"admin","role_to":"member"}"#))); + meta.admin_update_user(&actor, ana.id, None, Some(None), NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, details(&e)), (AuditAction::AdminQuotaChanged, json(r#"{"quota_bytes":null}"#)), "removing the quota says so"); + meta.admin_update_user(&actor, ana.id, None, Some(None), NOW).await.unwrap(); + assert!(seen.actions().await.is_empty()); + + // A reset code, and its redemption: the devices it signs out are counted in the one event. + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&actor, ana.id, &reset.hash(), false, NOW, RESET_TTL_MS).await.unwrap(); + assert_eq!(seen.actions().await, ["admin.password_reset_issued"]); + let active = meta.list_devices(ana.id).await.unwrap().iter().filter(|d| !d.revoked).count(); + let redeemed = meta.redeem_with(&reset.hash(), "recovery", &client, &token(), Some("$argon2id$reset"), NOW).await.unwrap(); + assert_eq!(redeemed.revoked.len(), active); + let e = seen.one().await; + assert_eq!( + (e.action, e.actor_user, e.actor_device, e.target_id), + (AuditAction::JoinResetRedeemed, Some(ana.id), Some(redeemed.device), Some(*redeemed.device.as_uuid())) + ); + assert_eq!(details(&e), json(&format!(r#"{{"client_kind":"agent","platform":"linux","revoked":{active}}}"#))); + + // An invite redeemed with a password for the new account, and a pairing redeemed from its device. + meta.admin_issue_invite(&actor, newbie.id, &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap(); + seen.fresh().await; + let joined = meta.redeem(&invite.hash(), "newbie pc", &token(), Some("$argon2id$newbie"), NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device), (AuditAction::JoinInviteRedeemed, Some(newbie.id), Some(joined.device))); + assert_eq!(details(&e), json("{}"), "no client named: no client in the event"); + let pairing = code(PAIRING_CHARS); + meta.create_pairing(newbie.id, joined.device, &pairing.hash(), NOW).await.unwrap(); + assert!(seen.actions().await.is_empty(), "making a code is not an event"); + let paired = meta.redeem(&pairing.hash(), "second pc", &token(), None, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device), (AuditAction::JoinPairingRedeemed, Some(newbie.id), Some(paired.device))); + // A code that does not work writes nothing. + assert!(matches!(meta.redeem(&pairing.hash(), "again", &token(), None, NOW).await, Err(MetaError::InvalidCode))); + assert!(seen.actions().await.is_empty()); + + // Disable (the devices go with it), a second disable, enable, a second enable. + let (_g1, _g2) = (device(meta, &ana).await, device(meta, &ana).await); + let signed_out = meta.admin_disable_user(&actor, ana.id, NOW).await.unwrap().len(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.target_id), (AuditAction::AdminUserDisabled, Some(root.id), Some(ana.id.0))); + assert_eq!(details(&e), json(&format!(r#"{{"revoked":{signed_out}}}"#))); + meta.admin_disable_user(&actor, ana.id, NOW).await.unwrap(); + assert!(seen.actions().await.is_empty(), "disabled already: no change, no event"); + meta.admin_enable_user(&actor, ana.id, NOW).await.unwrap(); + assert_eq!(seen.actions().await, ["admin.user_enabled"]); + meta.admin_enable_user(&actor, ana.id, NOW).await.unwrap(); + assert!(seen.actions().await.is_empty()); + + // An admin signs out one device: about the account, naming the device; again, nothing. + let anas = device(meta, &ana).await; + meta.admin_revoke_device(&actor, ana.id, anas, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.actor_user, e.actor_device, e.target_kind.as_str(), e.target_id), + (AuditAction::AdminDeviceRevoked, Some(root.id), Some(device_of(&actor)), "user", Some(ana.id.0)) + ); + assert_eq!(details(&e), json(&format!(r#"{{"device":"{anas}","reason":"admin"}}"#))); + meta.admin_revoke_device(&actor, ana.id, anas, NOW).await.unwrap(); + assert!(seen.actions().await.is_empty()); + + // Marked for removal, once. + let _ = device(meta, &ana).await; + meta.admin_remove_user(&actor, ana.id, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.target_id), (AuditAction::AdminUserRemoved, Some(ana.id.0))); + assert_eq!(details(&e), json(r#"{"revoked":1}"#)); + meta.admin_remove_user(&actor, ana.id, NOW).await.unwrap(); + assert!(seen.actions().await.is_empty()); + + // The command line acts as no account. + let cara = user(meta, "cara").await; + let (_h1, _h2) = (device(meta, &cara).await, device(meta, &cara).await); + meta.reset_password(cara.id, "$argon2id$cli", false, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device, e.target_id), (AuditAction::CliPasswordReset, None, None, Some(cara.id.0))); + assert_eq!(details(&e), json(r#"{"revoked":2}"#)); + let one = device(meta, &cara).await; + meta.revoke_device_by(cara.id, one, RevokeReason::Admin, Origin::Cli, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device, e.target_id), (AuditAction::CliDeviceRevoked, None, None, Some(cara.id.0))); + assert_eq!(details(&e), json(&format!(r#"{{"device":"{one}","reason":"admin"}}"#))); + meta.revoke_device_by(cara.id, one, RevokeReason::Admin, Origin::Cli, NOW).await.unwrap(); + assert!(seen.actions().await.is_empty()); + let (_i1, _i2, _i3) = (device(meta, &cara).await, device(meta, &cara).await, device(meta, &cara).await); + meta.revoke_all_devices(cara.id, RevokeReason::Admin, NOW).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.target_id), (AuditAction::CliDevicesRevokedAll, None, Some(cara.id.0))); + assert_eq!(details(&e), json(r#"{"revoked":3}"#)); + meta.revoke_all_devices(cara.id, RevokeReason::Admin, NOW).await.unwrap(); + assert!(seen.actions().await.is_empty()); + // A reset that keeps the devices counts none. + meta.reset_password(cara.id, "$argon2id$cli2", true, NOW).await.unwrap(); + assert_eq!(details(&seen.one().await), json(r#"{"revoked":0}"#)); +} + +/// A change that fails, or is refused, leaves no event behind, and the last administrator stays one. +async fn a_refused_change_writes_no_event(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let ana = user(meta, "ana").await; + let mut seen = Seen::new(meta).await; + // The only administrator cannot demote or disable themselves out of the job, and the role stays. + assert!(matches!(meta.admin_update_user(&actor, actor.user, Some(Role::Member), None, NOW).await, Err(MetaError::LastAdmin))); + assert!(matches!(meta.admin_disable_user(&actor, actor.user, NOW).await, Err(MetaError::Invalid(_)))); + // An actor whose role is gone, one whose password changed since, and a target that is not there. + let stale = AdminActor { password_hash: "$argon2id$old".to_owned(), ..actor.clone() }; + assert!(matches!(meta.admin_disable_user(&stale, ana.id, NOW).await, Err(MetaError::Conflict(_)))); + assert!(matches!(meta.admin_update_user(&actor, UserId(uuid::Uuid::now_v7()), Some(Role::Admin), None, NOW).await, Err(MetaError::NotFound(_)))); + // A reset code without the new password, a name that is taken, a device of someone else. + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&actor, ana.id, &reset.hash(), false, NOW, RESET_TTL_MS).await.unwrap(); + seen.fresh().await; + assert!(matches!(meta.redeem(&reset.hash(), "pc", &token(), None, NOW).await, Err(MetaError::PasswordRequired))); + assert!(matches!( + meta.admin_create_user(&actor, "ana", Role::Member, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await, + Err(MetaError::UsernameTaken) + )); + assert!(matches!(meta.revoke_device_as(ana.id, DeviceId::generate(), RevokeReason::User, NOW).await, Err(MetaError::NotFound(_)))); + assert!(seen.actions().await.is_empty()); + assert_eq!(meta.account(actor.user).await.unwrap().role, Role::Admin); +} + +/// A sign-in step that failed is one row per account, action, client kind and minute, whose count goes up and whose identity, time and +/// details stay those of the first try; names no account has share one row per minute; an event that is not a failed step +/// cannot be aggregated. +async fn failed_sign_ins_count_up_in_one_row_per_account_and_minute(meta: &Meta) { + let (ana, bob) = (user(meta, "ana").await, user(meta, "bob").await); + let minute = NOW - NOW % 60_000; + let login = |target: Option| AuditFailure { action: AuditAction::AuthLoginFailed, target, step: Step::Password }; + meta.audit_record_failure(login(Some(ana.id)), minute + 1_000).await.unwrap(); + let first = all_events(meta).await; + assert_eq!(first.len(), 1); + let first = first[0].clone(); + assert_eq!( + (first.action, first.outcome, first.count, first.actor_user, first.target_id), + (AuditAction::AuthLoginFailed, AuditOutcome::Failure, 1, None, Some(ana.id.0)) + ); + assert_eq!((first.target_kind.as_str(), details(&first)), ("user", json(r#"{"step":"password"}"#))); + meta.audit_record_web_failure(login(Some(ana.id)), minute + 1_000).await.unwrap(); + let rows = all_events(meta).await; + assert_eq!(rows.len(), 2, "device and web failure aggregates stay separate"); + assert!(rows.iter().any(|event| details(event) == json(r#"{"client_kind":"web","step":"password"}"#))); + // The same minute: the count goes up, nothing else changes. + meta.audit_record_failure(login(Some(ana.id)), minute + 59_000).await.unwrap(); + meta.audit_record_failure(login(Some(ana.id)), minute + 59_999).await.unwrap(); + let rows = all_events(meta).await; + assert_eq!(rows.len(), 2); + let device_row = rows.iter().find(|event| event.id == first.id).unwrap(); + assert_eq!((device_row.id, device_row.at_ms, device_row.count, &device_row.details), (first.id, minute + 1_000, 3, &first.details)); + // The next minute is a new row; another account, another action, another row. + meta.audit_record_failure(login(Some(ana.id)), minute + 60_000).await.unwrap(); + meta.audit_record_failure(login(Some(bob.id)), minute + 1_000).await.unwrap(); + meta.audit_record_failure(AuditFailure { action: AuditAction::AuthSecondFactorFailed, target: Some(ana.id), step: Step::Totp }, minute + 1_000) + .await + .unwrap(); + let rows = all_events(meta).await; + assert_eq!(rows.len(), 5); + let mut counts: Vec<(&str, u64)> = rows.iter().map(|e| (e.action.as_str(), e.count)).collect(); + counts.sort_unstable(); + assert_eq!( + counts, + [("auth.login_failed", 1), ("auth.login_failed", 1), ("auth.login_failed", 1), ("auth.login_failed", 3), ("auth.second_factor_failed", 1)] + ); + // Names nobody has: one row a minute whatever was typed, with no account in it. + for _ in 0..4 { + meta.audit_record_failure(login(None), minute + 2_000).await.unwrap(); + } + meta.audit_record_failure(login(None), minute + 62_000).await.unwrap(); + let unknown: Vec = all_events(meta).await.into_iter().filter(|e| e.target_id.is_none()).collect(); + assert_eq!(unknown.len(), 2); + assert!(unknown.iter().all(|e| e.actor_user.is_none() && e.target_kind.is_empty() && e.actor_device.is_none())); + let mut counts: Vec = unknown.iter().map(|e| e.count).collect(); + counts.sort_unstable(); + assert_eq!(counts, [1, 4]); + // Only the failed steps are aggregated. + for action in [AuditAction::AuthLogin, AuditAction::AdminUserCreated, AuditAction::CliPasswordReset] { + assert!(matches!( + meta.audit_record_failure(AuditFailure { action, target: None, step: Step::Password }, NOW).await, + Err(MetaError::Invalid(_)) + )); + } + // Other events have no key, so any number of them is allowed. + let (_, actor) = admin_actor(meta, "root").await; + for i in 0..3 { + meta.admin_update_user(&actor, ana.id, Some(if i % 2 == 0 { Role::Admin } else { Role::Member }), None, NOW).await.unwrap(); + } + assert_eq!(all_events(meta).await.iter().filter(|e| e.action == AuditAction::AdminRoleChanged).count(), 3); +} + +/// Many tries at one moment on one key: exactly one row, counting every one, and the first row's identity and time stay. +/// Run on both databases (on Postgres the collisions really are concurrent). +async fn concurrent_failures_with_one_key_make_one_row_with_every_count(meta: &Meta) { + let ana = user(meta, "ana").await; + let minute = NOW - NOW % 60_000; + const TRIES: u64 = 24; + for round in 0..3i64 { + let at = minute + round * 60_000; + let tries: Vec<_> = (0..TRIES) + .map(|i| { + let meta = meta.clone(); + let failure = AuditFailure { action: AuditAction::AuthLoginFailed, target: Some(ana.id), step: Step::Password }; + tokio::spawn(async move { meta.audit_record_failure(failure, at + i as i64).await }) + }) + .collect(); + for t in tries { + t.await.unwrap().unwrap(); + } + } + let rows = all_events(meta).await; + assert_eq!(rows.len(), 3, "one row for each minute"); + assert!(rows.iter().all(|e| e.count == TRIES), "{:?}", rows.iter().map(|e| e.count).collect::>()); + // Each row kept the time of the try that made it: one of the first moment's tries, within its minute. + for (round, row) in rows.iter().rev().enumerate() { + let start = minute + round as i64 * 60_000; + assert!((start..start + TRIES as i64).contains(&row.at_ms), "{} not in {}..", row.at_ms, start); + } +} + +/// Lists: newest first in `(at_ms, id)` order, filters by account and by name or group of names, `before_ms` and an end +/// time, and a cursor that is exclusive so that events with one timestamp are neither repeated nor lost. +async fn audit_lists_filter_and_page_by_cursor(meta: &Meta) { + let (root, actor) = admin_actor(meta, "root").await; + let (ana, bob) = (user(meta, "ana").await, user(meta, "bob").await); + // Seven events at one moment. + for i in 0..7 { + meta.admin_update_user(&actor, ana.id, Some(if i % 2 == 0 { Role::Admin } else { Role::Member }), None, NOW).await.unwrap(); + } + meta.sign_in_device(bob.id, (&bob.password_hash, bob.auth_gen), "pc", &DeviceClient::default(), &token(), NOW + 5).await.unwrap().unwrap(); + let minute = NOW - NOW % 60_000 + 60_000; + meta.audit_record_failure(AuditFailure { action: AuditAction::AuthLoginFailed, target: Some(bob.id), step: Step::Password }, minute) + .await + .unwrap(); + meta.audit_record_failure(AuditFailure { action: AuditAction::AuthLoginFailed, target: None, step: Step::Password }, minute + 60_000) + .await + .unwrap(); + + let all = all_events(meta).await; + assert_eq!(all.len(), 10); + let order: Vec<_> = all.iter().map(|e| (e.at_ms, e.id)).collect(); + assert!(order.windows(2).all(|w| w[0] > w[1]), "newest first, by (at_ms, id) descending"); + + // Pages of three through the cursor of the last event of each: all ten once, in order. + let mut paged = Vec::new(); + let mut after: Option = None; + for _ in 0..10 { + let page = meta.audit_list(&AuditQuery { after, limit: 3, ..AuditQuery::default() }).await.unwrap(); + let Some(last) = page.last() else { break }; + after = Some(last.cursor()); + paged.extend(page); + } + assert_eq!(paged, all); + // A cursor behind everything gives nothing; a cursor at an event never gives that event. + assert!( + meta.audit_list(&AuditQuery { after: Some(AuditCursor { at_ms: 0, id: uuid::Uuid::nil() }), limit: 10, ..AuditQuery::default() }) + .await + .unwrap() + .is_empty() + ); + let sevens: Vec<&AuditEvent> = all.iter().filter(|e| e.at_ms == NOW).collect(); + assert_eq!(sevens.len(), 7); + let page = meta.audit_list(&AuditQuery { after: Some(sevens[2].cursor()), limit: 100, ..AuditQuery::default() }).await.unwrap(); + assert_eq!(page.len(), 4, "the four behind the third of the seven that share a time"); + + // Times: before is exclusive, the end time inclusive. + let before = |ms| AuditQuery { before_ms: Some(ms), limit: 100, ..AuditQuery::default() }; + assert_eq!(meta.audit_list(&before(NOW)).await.unwrap().len(), 0); + assert_eq!(meta.audit_list(&before(NOW + 1)).await.unwrap().len(), 7); + assert_eq!(meta.audit_list(&AuditQuery { until_ms: Some(NOW), limit: 100, ..AuditQuery::default() }).await.unwrap().len(), 7); + assert_eq!(meta.audit_list(&AuditQuery { until_ms: Some(NOW - 1), limit: 100, ..AuditQuery::default() }).await.unwrap().len(), 0); + + // Accounts: the actor or the target. + let by_user = |u: UserId| AuditQuery { user: Some(u), limit: 100, ..AuditQuery::default() }; + assert_eq!(meta.audit_list(&by_user(ana.id)).await.unwrap().len(), 7); + assert_eq!(meta.audit_list(&by_user(root.id)).await.unwrap().len(), 7); + assert_eq!(meta.audit_list(&by_user(bob.id)).await.unwrap().len(), 2, "his sign-in, and the guess about his password"); + assert_eq!(meta.audit_list(&by_user(UserId(uuid::Uuid::now_v7()))).await.unwrap().len(), 0); + + // Names: exact, or a group. + let by_action = |text: &str| AuditQuery { action: ActionFilter::parse(text), limit: 100, ..AuditQuery::default() }; + assert_eq!(meta.audit_list(&by_action("admin.role_changed")).await.unwrap().len(), 7); + assert_eq!(meta.audit_list(&by_action("auth.login")).await.unwrap().len(), 1, "exact: not auth.login_failed"); + assert_eq!(meta.audit_list(&by_action("auth.")).await.unwrap().len(), 3); + assert_eq!(meta.audit_list(&by_action("admin.")).await.unwrap().len(), 7); + assert_eq!(meta.audit_list(&by_action("cli.")).await.unwrap().len(), 0); + // Both at once, and with a page. + let both = AuditQuery { user: Some(bob.id), action: ActionFilter::parse("auth."), limit: 1, ..AuditQuery::default() }; + assert_eq!(meta.audit_list(&both).await.unwrap().len(), 1); +} + +/// Pruning deletes what is older than the limit and nothing else, a batch at a time; a pruning that leaves a backlog +/// does not count as done, so the next write goes on; one that finished holds off the next for an hour; an error is not +/// done either. +async fn pruning_works_in_batches_and_keeps_its_time_until_done(meta: &Meta) { + let ana = user(meta, "ana").await; + let day = 24 * 60 * 60 * 1000i64; + let old = NOW - 3 * day; + // More old events than one batch holds, each in a minute of its own. + let n = PRUNE_BATCH + 100; + let writers: Vec<_> = (0..8) + .map(|w| { + let meta = meta.clone(); + tokio::spawn(async move { + let mut i = w; + while i < n { + let failure = AuditFailure { action: AuditAction::AuthLoginFailed, target: None, step: Step::Password }; + meta.audit_record_failure(failure, old + i * 60_000).await.unwrap(); + i += 8; + } + }) + }) + .collect(); + for w in writers { + w.await.unwrap(); + } + // Recent events, which pruning leaves. + for i in 0..3 { + let failure = AuditFailure { action: AuditAction::AuthLoginFailed, target: Some(ana.id), step: Step::Password }; + meta.audit_record_failure(failure, NOW - day / 2 + i * 60_000).await.unwrap(); + } + let limit = NOW - day; + assert_eq!(meta.audit_count_before(limit).await.unwrap(), n as u64); + assert_eq!(meta.audit_count_before(old).await.unwrap(), 0, "the limit is exclusive"); + + // Not configured: a write prunes nothing. + assert!(!meta.audit_prune_if_due(NOW).await.unwrap()); + // A second handle that prunes when asked. The writes below go through `meta`, which never starts a pruning of its own. + let pruner = meta.clone().with_audit_retention_days(1); + // One batch, then the rest: the first leaves a backlog and so does not hold off the next. + assert!(pruner.audit_prune_if_due_in(NOW, 1).await.unwrap()); + assert_eq!(meta.audit_count_before(limit).await.unwrap(), 100, "one batch of {PRUNE_BATCH}"); + assert!(pruner.audit_prune_if_due_in(NOW + 1, 1).await.unwrap(), "a backlog is not done"); + assert_eq!(meta.audit_count_before(limit).await.unwrap(), 0); + assert_eq!(all_events(meta).await.len(), 3, "the recent events stay"); + // Done: the next hour is left alone, then it runs again. + let failure = AuditFailure { action: AuditAction::AuthLoginFailed, target: None, step: Step::Password }; + meta.audit_record_failure(failure, old).await.unwrap(); + assert!(!pruner.audit_prune_if_due_in(NOW + 2, 1).await.unwrap()); + assert_eq!(meta.audit_count_before(limit).await.unwrap(), 1); + assert!(pruner.audit_prune_if_due_in(NOW + 1 + PRUNE_EVERY_MS, 1).await.unwrap()); + assert_eq!(meta.audit_count_before(limit).await.unwrap(), 0); + + // `audit_prune` itself takes everything before the limit in batches, and reports the count. + for i in 0..(PRUNE_BATCH + 5) { + let failure = AuditFailure { action: AuditAction::AuthLoginFailed, target: None, step: Step::Password }; + meta.audit_record_failure(failure, old + i * 60_000).await.unwrap(); + } + assert_eq!(meta.audit_prune(limit).await.unwrap(), (PRUNE_BATCH + 5) as u64); + assert_eq!(meta.audit_prune(limit).await.unwrap(), 0); + assert_eq!(all_events(meta).await.len(), 3); +} + +/// Events outlive the account they are about: marking it for removal deletes nothing, and the account's ID still finds them. +async fn a_removed_accounts_events_stay(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let ana = user(meta, "ana").await; + meta.sign_in_device(ana.id, (&ana.password_hash, ana.auth_gen), "pc", &DeviceClient::default(), &token(), NOW).await.unwrap().unwrap(); + meta.audit_record_failure(AuditFailure { action: AuditAction::AuthLoginFailed, target: Some(ana.id), step: Step::Password }, NOW).await.unwrap(); + meta.admin_remove_user(&actor, ana.id, NOW + 1).await.unwrap(); + let mine = meta.audit_list(&AuditQuery { user: Some(ana.id), limit: 100, ..AuditQuery::default() }).await.unwrap(); + let mut names: Vec<&str> = mine.iter().map(|e| e.action.as_str()).collect(); + names.sort_unstable(); + assert_eq!(names, ["admin.user_removed", "auth.login", "auth.login_failed"]); +} + +/// Nothing a client typed or a secret is in any event, whatever the account, the device or the client called itself. +async fn no_event_holds_a_name_or_a_secret(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + let hash = "$argon2id$SECRETHASH"; + let zelda = meta.create_user("zelda", hash, Role::Member, NOW).await.unwrap(); + // A platform may be any printable text of 32 bytes: here an address, a file name, and 32 bytes of multi-byte characters. + let hostile = + ["10.0.0.1", "secret.psd", "fe80::1%eth0", "\u{20ac}\u{20ac}\u{20ac}\u{20ac}\u{20ac}\u{20ac}\u{20ac}\u{20ac}\u{20ac}\u{20ac}", "windows 11"]; + let mut clock = NOW; + for platform in hostile { + let client = DeviceClient::new(Some("desktop"), Some(platform), Some("9.9.9-secretbuild")).unwrap(); + clock += 1; + meta.sign_in_device(zelda.id, (hash, 0), "zelda-private-laptop", &client, &token(), clock).await.unwrap().unwrap(); + let e = all_events(meta).await.remove(0); + assert_eq!(details(&e), json(r#"{"client_kind":"desktop","platform":"other"}"#), "{platform:?}"); + } + let known = DeviceClient::new(Some("cli"), Some("MacOS"), None).unwrap(); + meta.sign_in_device(zelda.id, (hash, 0), "zelda-private-laptop", &known, &token(), NOW + 100).await.unwrap().unwrap(); + assert_eq!(details(&all_events(meta).await.remove(0)), json(r#"{"client_kind":"cli","platform":"macos"}"#)); + let no_platform = DeviceClient::new(Some("agent"), None, None).unwrap(); + meta.sign_in_device(zelda.id, (hash, 0), "zelda-private-laptop", &no_platform, &token(), NOW + 200).await.unwrap().unwrap(); + assert_eq!(details(&all_events(meta).await.remove(0)), json(r#"{"client_kind":"agent"}"#), "no platform: no field"); + // A few more changes, and the same scan over everything written. + meta.change_password(zelda.id, hash, "$argon2id$NEWSECRETHASH", meta.list_devices(zelda.id).await.unwrap()[0].id, NOW).await.unwrap(); + meta.admin_update_user(&actor, zelda.id, Some(Role::Admin), Some(Some(MIN_QUOTA_BYTES)), NOW).await.unwrap(); + meta.audit_record_failure(AuditFailure { action: AuditAction::AuthLoginFailed, target: None, step: Step::Password }, NOW).await.unwrap(); + let events = all_events(meta).await; + assert!(events.len() > 8); + let shown = format!("{events:?}"); + for secret in ["zelda", "SECRETHASH", "secretbuild", "10.0.0.1", "secret.psd", "fe80", "127.0.0.1", "\u{20ac}", "argon2", "laptop", "ZELDA"] { + assert!(!shown.contains(secret), "{secret:?} in {shown}"); + } + // Every `details` is an object of the allowed words and numbers only. + for e in &events { + let value = details(e); + let object = value.as_object().unwrap(); + for key in object.keys() { + assert!( + ["client_kind", "platform", "reason", "step", "role_from", "role_to", "quota_bytes", "revoked", "device", "truncated"] + .contains(&key.as_str()), + "{key}" + ); + } + assert!(e.details.len() <= 2048); + } +} + +/// The log is append-only in the code: the sources of this crate update and delete in `audit_events` in exactly the two +/// places the design names (the counter of an aggregated failure, the pruning). +#[test] +fn the_sources_change_audit_rows_only_to_count_and_to_prune() { + fn sources(dir: &std::path::Path, out: &mut Vec<(std::path::PathBuf, String)>) { + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + if path.is_dir() { + sources(&path, out); + } else if path.extension().is_some_and(|e| e == "rs") { + let text = std::fs::read_to_string(&path).unwrap(); + out.push((path, text)); + } + } + } + let mut files = Vec::new(); + sources(&std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src"), &mut files); + assert!(files.len() > 5, "the sources were found"); + let count = |needle: &str| -> Vec { + files + .iter() + .flat_map(|(path, text)| { + // Comments may name statements, only code counts. + text.lines() + .filter(|l| !l.trim_start().starts_with("//") && l.contains(needle)) + .map(move |_| path.file_name().unwrap().to_string_lossy().into_owned()) + }) + .collect() + }; + assert!(count("UPDATE audit_events").is_empty(), "no statement updates the log"); + assert!(count("audit_events SET").is_empty()); + assert_eq!(count("DELETE FROM audit_events"), ["audit.rs"], "exactly one deletion, the pruning"); + assert_eq!(count("DO UPDATE SET agg_count = audit_events.agg_count + 1"), ["audit.rs"], "exactly one update, the counter"); + assert!(count("TRUNCATE").is_empty() && count("DROP TABLE audit").is_empty()); +} + +// ---- an error in the log ---- + +/// Makes every use of the log fail (the table is renamed away), which stands for any error of the database in the log +/// and needs no code in the product to cause it. +async fn break_audit(url: &str) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("ALTER TABLE audit_events RENAME TO audit_events_away").execute(&mut conn).await.unwrap(); +} + +async fn repair_audit(url: &str) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("ALTER TABLE audit_events_away RENAME TO audit_events").execute(&mut conn).await.unwrap(); +} + +/// A change whose event cannot be written is not made (fail closed), whichever it is; a failed sign-in step's event is +/// only reported to its caller, which ignores it; pruning that fails is not done, so the next write tries again. +async fn an_error_in_the_log_fails_every_change_and_changes_nothing(url: &str) { + sqlx::any::install_default_drivers(); + let meta = Meta::open(url).await.unwrap(); + let pruner = meta.clone().with_audit_retention_days(1); + let (_, actor) = admin_actor(&meta, "root").await; + let ana = user(&meta, "ana").await; + let dev = device(&meta, &ana).await; + let others = [device(&meta, &ana).await, device(&meta, &ana).await]; + let invite = code(INVITE_CHARS); + let fresh = meta.admin_create_user(&actor, "fresh", Role::Member, &invite.hash(), NOW, INVITE_TTL_MS).await.unwrap().0; + let events_before = all_events(&meta).await.len(); + let active = |meta: &Meta, user: UserId| { + let meta = meta.clone(); + async move { meta.list_devices(user).await.unwrap().iter().filter(|d| !d.revoked).count() } + }; + assert_eq!(active(&meta, ana.id).await, 3); + + break_audit(url).await; + let is_db = |r: Result<(), MetaError>| assert!(matches!(r, Err(MetaError::Db(_))), "{r:?}"); + is_db(meta.sign_in_device(ana.id, (&ana.password_hash, 0), "new", &DeviceClient::default(), &token(), NOW).await.map(|_| ())); + is_db(meta.revoke_device_by(ana.id, others[0], RevokeReason::User, Origin::Device(dev), NOW).await); + is_db(meta.revoke_other_devices(ana.id, dev, RevokeReason::Others, NOW).await.map(|_| ())); + is_db(meta.change_password(ana.id, &ana.password_hash, "$argon2id$new", dev, NOW).await.map(|_| ())); + is_db(meta.reset_password(ana.id, "$argon2id$cli", false, NOW).await.map(|_| ())); + is_db(meta.revoke_all_devices(ana.id, RevokeReason::Admin, NOW).await.map(|_| ())); + is_db(meta.admin_update_user(&actor, ana.id, Some(Role::Admin), Some(Some(MIN_QUOTA_BYTES)), NOW).await.map(|_| ())); + is_db(meta.admin_disable_user(&actor, ana.id, NOW).await.map(|_| ())); + is_db(meta.admin_remove_user(&actor, ana.id, NOW).await.map(|_| ())); + is_db(meta.admin_issue_reset(&actor, ana.id, &code(INVITE_CHARS).hash(), false, NOW, RESET_TTL_MS).await.map(|_| ())); + is_db(meta.admin_revoke_device(&actor, ana.id, dev, NOW).await); + is_db(meta.admin_issue_invite(&actor, fresh.id, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await.map(|_| ())); + is_db(meta.admin_revoke_invite(&actor, fresh.id, NOW).await); + is_db(meta.admin_create_user(&actor, "nobody", Role::Member, &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await.map(|_| ())); + is_db(meta.redeem(&invite.hash(), "pc", &token(), Some("$argon2id$x"), NOW).await.map(|_| ())); + // A sign-in step that failed has no change to undo: the error is the caller's, and nothing else is touched. + is_db(meta.audit_record_failure(AuditFailure { action: AuditAction::AuthLoginFailed, target: Some(ana.id), step: Step::Password }, NOW).await); + assert!(pruner.audit_prune_if_due(NOW).await.is_err(), "pruning with the log broken is an error"); + repair_audit(url).await; + + // Nothing was changed by any of it. + assert_eq!(all_events(&meta).await.len(), events_before, "no event, no change"); + assert_eq!(active(&meta, ana.id).await, 3, "no device was signed out or added"); + let row = meta.admin_user(ana.id, NOW).await.unwrap().unwrap(); + assert_eq!((row.role, row.disabled_ms, row.quota_bytes), (Role::Member, None, None)); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, ana.password_hash); + assert!(meta.user_by_name("nobody").await.unwrap().is_none(), "the account was not made"); + assert!(meta.admin_user(fresh.id, NOW).await.unwrap().unwrap().invite_pending, "the invite is still there"); + // And the same changes work once the log does, the invite included. + meta.redeem(&invite.hash(), "pc", &token(), Some("$argon2id$x"), NOW).await.unwrap(); + meta.admin_update_user(&actor, ana.id, Some(Role::Admin), None, NOW).await.unwrap(); + // A pruning that failed left no time behind: the next write's goes ahead at once. + assert!(pruner.audit_prune_if_due(NOW + 1).await.unwrap()); +} + +#[tokio::test] +async fn an_error_in_the_log_fails_every_change_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + an_error_in_the_log_fails_every_change_and_changes_nothing(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn an_error_in_the_log_fails_every_change_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + an_error_in_the_log_fails_every_change_and_changes_nothing(&url).await; +} + +/// A write prunes old events after its commit, in the background, when the store is told how long to keep them. +#[tokio::test] +async fn a_write_prunes_old_events_after_its_commit() { + let dir = tempfile::tempdir().unwrap(); + let url = format!("sqlite:{}", dir.path().join("meta.db").display()); + let plain = Meta::open(&url).await.unwrap(); + let ana = user(&plain, "ana").await; + let day = 24 * 60 * 60 * 1000i64; + for i in 0..4 { + let failure = AuditFailure { action: AuditAction::AuthLoginFailed, target: None, step: Step::Password }; + plain.audit_record_failure(failure, NOW - 40 * day + i * 60_000).await.unwrap(); + } + // Without a retention a write leaves them. + plain.sign_in_device(ana.id, (&ana.password_hash, ana.auth_gen), "pc", &DeviceClient::default(), &token(), NOW).await.unwrap().unwrap(); + tokio::time::sleep(std::time::Duration::from_millis(200)).await; + assert_eq!(plain.audit_count_before(NOW - 30 * day).await.unwrap(), 4); + // With one, the next write starts the pruning. + let meta = plain.clone().with_audit_retention_days(30); + meta.sign_in_device(ana.id, (&ana.password_hash, ana.auth_gen), "pc", &DeviceClient::default(), &token(), NOW + 1).await.unwrap().unwrap(); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while meta.audit_count_before(NOW - 30 * day).await.unwrap() > 0 { + assert!(std::time::Instant::now() < deadline, "the old events were not pruned"); + tokio::time::sleep(std::time::Duration::from_millis(20)).await; + } + assert_eq!(all_events(&meta).await.len(), 2, "the two sign-ins stay"); +} + +// ---- the migration from 0008 to 0009 (and on to 0010) ---- + +/// Drops what migrations 0009 (the audit log) and 0010 (two-factor sign-in) added and forgets they ran, which leaves the database as 0008 left it (with its rows). +async fn back_to_0008(url: &str) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("DROP TABLE web_sessions").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE audit_events").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE login_challenges").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_recovery_codes").execute(&mut conn).await.unwrap(); + sqlx::query("DROP TABLE user_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("ALTER TABLE invites DROP COLUMN reset_two_factor").execute(&mut conn).await.unwrap(); + sqlx::query("DELETE FROM schema_version WHERE version >= 9").execute(&mut conn).await.unwrap(); +} + +/// A database from the release before the audit log keeps its accounts and devices, gets an empty log that works, and a +/// database newer than the program is still refused. +async fn migrating_from_0008_adds_the_audit_log(url: &str) { + sqlx::any::install_default_drivers(); + let (ana, old_token) = { + let meta = Meta::open(url).await.unwrap(); + let ana = user(&meta, "ana").await; + let old_token = token(); + meta.create_device(ana.id, "old pc", &old_token, NOW).await.unwrap(); + meta.sign_in_device(ana.id, (&ana.password_hash, ana.auth_gen), "pc", &DeviceClient::default(), &token(), NOW).await.unwrap().unwrap(); + assert_eq!(all_events(&meta).await.len(), 1); + (ana, old_token) + }; + back_to_0008(url).await; + assert_eq!(schema_version(url).await, 8); + + let meta = Meta::open(url).await.unwrap(); + assert_eq!(schema_version(url).await, 11, "the audit log's migration is 9, two-factor sign-in's 10, web sessions' 11: all run"); + assert_eq!(meta.device_by_token(&old_token).await.unwrap().map(|a| a.user), Some(ana.id)); + assert!(all_events(&meta).await.is_empty(), "a new log is empty"); + meta.sign_in_device(ana.id, (&ana.password_hash, ana.auth_gen), "pc", &DeviceClient::default(), &token(), NOW).await.unwrap().unwrap(); + meta.audit_record_failure(AuditFailure { action: AuditAction::AuthLoginFailed, target: Some(ana.id), step: Step::Password }, NOW).await.unwrap(); + assert_eq!(all_events(&meta).await.len(), 2); + + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("INSERT INTO schema_version (version) VALUES (12)").execute(&mut conn).await.unwrap(); + drop(conn); + assert!(matches!(Meta::open(url).await, Err(MetaError::Corrupt(_)))); +} + +#[tokio::test] +async fn migrating_from_0008_adds_the_audit_log_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + migrating_from_0008_adds_the_audit_log(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn migrating_from_0008_adds_the_audit_log_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + migrating_from_0008_adds_the_audit_log(&url).await; +} + +// ---- two-factor sign-in (migration 0010) ---- + +type Check = Box Result, UnreadableSecret> + Send + Sync>; + +/// A TOTP check that says every code is right for time step `step` (the crypto is the server's; here only the order of +/// operations and what the database does with the answer are under test). +fn right(step: i64) -> Check { + Box::new(move |_, _| Ok(Some(step))) +} + +/// A TOTP check that says every code is wrong. +fn wrong() -> Check { + Box::new(|_, _| Ok(None)) +} + +fn totp(check: &Check) -> Proof<'_> { + Proof::Totp(&**check) +} + +/// The admin's confirmation with a TOTP check that says `step` (`None`: a wrong code). +async fn confirm_with(meta: &Meta, user: UserId, step: Option, now: i64) -> Result { + let check = step.map_or_else(wrong, right); + meta.confirm_second_factor(user, Some(&totp(&check)), now).await +} + +async fn redeem_with_factor(meta: &Meta, hash: &[u8; 32], proof: Option<&Proof<'_>>, now: i64) -> Result { + meta.redeem_with_second_factor(hash, "new pc", &DeviceClient::default(), &token(), None, proof, now).await.map(|r| r.device) +} + +const FIRST_STEP: i64 = 1_000; +const SETUP_AT: i64 = NOW + 1_000; +const MINUTE: i64 = 60_000; + +/// An account with a password, two devices (`first` asked for the set-up and stays signed in, `second` is signed out by +/// turning it on), and two-factor sign-in on from [`SETUP_AT`] (its first code was step [`FIRST_STEP`]). +struct Protected { + user: UserRecord, + first: DeviceId, + second: DeviceId, + codes: Vec, +} + +impl Protected { + fn actor(&self) -> DeviceActor { + DeviceActor { user: self.user.id, device: self.first, password_hash: "$argon2id$stub".to_owned() } + } + + fn hashes(&self) -> Vec<[u8; 32]> { + self.codes.iter().map(Code::hash).collect() + } +} + +async fn protected(meta: &Meta, username: &str) -> Protected { + let user = user(meta, username).await; + let first = device(meta, &user).await; + let second = device(meta, &user).await; + let codes: Vec = (0..10).map(|_| code(INVITE_CHARS)).collect(); + let me = Protected { user, first, second, codes }; + meta.two_factor_begin_setup(&me.actor(), b"cipher-of-the-secret", SETUP_AT).await.unwrap(); + let revoked = meta.two_factor_confirm(me.user.id, me.first, &*right(FIRST_STEP), &me.hashes(), SETUP_AT + 1).await.unwrap(); + assert_eq!(revoked, vec![me.second]); + me +} + +fn new_challenge() -> NewChallenge { + NewChallenge { id_hash: token(), client_key: "ip:203.0.113.7".to_owned() } +} + +async fn live_devices(meta: &Meta, user: &UserRecord) -> Vec { + meta.list_devices(user.id).await.unwrap().into_iter().filter(|d| !d.revoked).map(|d| d.id).collect() +} + +async fn set_up_confirmed_and_listed(meta: &Meta) { + let ana = user(meta, "ana").await; + let (first, second) = (device(meta, &ana).await, device(meta, &ana).await); + let info = meta.two_factor_info(ana.id, SETUP_AT).await.unwrap(); + assert_eq!((info.enabled_ms, info.recovery_codes_left, info.pending), (None, 0, false)); + assert!(!meta.two_factor_enabled(ana.id).await.unwrap()); + meta.create_invite("ana", &code(INVITE_CHARS).hash(), NOW, INVITE_TTL_MS).await.unwrap(); + let pairing = code(PAIRING_CHARS); + meta.create_pairing(ana.id, second, &pairing.hash(), NOW).await.unwrap(); + + let actor = DeviceActor { user: ana.id, device: first, password_hash: "$argon2id$stub".to_owned() }; + let expires = meta.two_factor_begin_setup(&actor, b"cipher-of-the-secret", SETUP_AT).await.unwrap(); + assert_eq!(expires, SETUP_AT + 10 * MINUTE); + let info = meta.two_factor_info(ana.id, SETUP_AT + 1).await.unwrap(); + assert_eq!((info.enabled_ms, info.pending), (None, true), "a set-up is not two-factor sign-in"); + assert!(!meta.two_factor_enabled(ana.id).await.unwrap()); + // A password sign-in still gives a device while it is only set up. + let generation = gen_of(meta, "ana").await; + let signed = + meta.sign_in(ana.id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &new_challenge(), SETUP_AT).await.unwrap(); + assert!(matches!(signed, Some(SignIn::Device(_)))); + let extra = live_devices(meta, &ana).await.len(); + assert_eq!(extra, 3); + + // A wrong first code is refused and the set-up stays. + assert!(matches!(meta.two_factor_confirm(ana.id, first, &*wrong(), &[], SETUP_AT + 2).await, Err(MetaError::TwoFactorInvalid))); + assert!(!meta.two_factor_enabled(ana.id).await.unwrap()); + let hashes: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + let before = gen_of(meta, "ana").await; + let revoked = meta.two_factor_confirm(ana.id, first, &*right(FIRST_STEP), &hashes, SETUP_AT + 3).await.unwrap(); + assert_eq!(revoked.len(), 2, "every other device is signed out"); + assert_eq!(live_devices(meta, &ana).await, vec![first]); + let devices = meta.list_devices(ana.id).await.unwrap(); + assert!(devices.iter().filter(|d| d.revoked).all(|d| d.revoke_reason == Some(RevokeReason::TwoFactor))); + assert_revoke_columns_agree(&devices); + assert_eq!(gen_of(meta, "ana").await, before + 1, "what a sign-in checked before no longer counts"); + assert!(!meta.revoke_invite("ana").await.unwrap(), "the invite is gone"); + assert!(matches!(meta.redeem(&pairing.hash(), "x", &token(), None, SETUP_AT + 4).await, Err(MetaError::InvalidCode)), "pairings are deleted"); + let info = meta.two_factor_info(ana.id, SETUP_AT + 5).await.unwrap(); + assert_eq!((info.enabled_ms, info.recovery_codes_left, info.pending), (Some(SETUP_AT + 3), 10, false)); + assert!(meta.two_factor_enabled(ana.id).await.unwrap()); + assert!(meta.account(ana.id).await.unwrap().two_factor_enabled); + // It is on: a set-up is a conflict, and so is a second confirmation. + assert!(matches!(meta.two_factor_begin_setup(&actor, b"other", SETUP_AT + 6).await, Err(MetaError::Conflict(_)))); + assert!(matches!(meta.two_factor_confirm(ana.id, first, &*right(FIRST_STEP + 5), &hashes, SETUP_AT + 7).await, Err(MetaError::Conflict(_)))); +} + +async fn a_set_up_belongs_to_one_device_expires_and_takes_five_tries(meta: &Meta) { + let ana = user(meta, "ana").await; + let (first, second) = (device(meta, &ana).await, device(meta, &ana).await); + let actor = |device| DeviceActor { user: ana.id, device, password_hash: "$argon2id$stub".to_owned() }; + meta.two_factor_begin_setup(&actor(first), b"one", SETUP_AT).await.unwrap(); + // Open on the first device: the second may not start, nor confirm (and its code is not tried at all). + assert!(matches!(meta.two_factor_begin_setup(&actor(second), b"two", SETUP_AT + 1).await, Err(MetaError::TwoFactorPending))); + assert!(matches!(meta.two_factor_confirm(ana.id, second, &*right(FIRST_STEP), &[], SETUP_AT + 1).await, Err(MetaError::Conflict(_)))); + // The first may start again; that replaces the secret and gives it five new tries. + for round in 0..2 { + meta.two_factor_begin_setup(&actor(first), b"again", SETUP_AT + 2).await.unwrap(); + for _ in 0..4 { + assert!( + matches!(meta.two_factor_confirm(ana.id, first, &*wrong(), &[], SETUP_AT + 3).await, Err(MetaError::TwoFactorInvalid)), + "round {round}" + ); + } + assert!(meta.two_factor_info(ana.id, SETUP_AT + 3).await.unwrap().pending, "four wrong codes leave it open"); + } + // The fifth wrong code uses it up: even the right code then is a conflict, and the set-up has to start again. + assert!(matches!(meta.two_factor_confirm(ana.id, first, &*wrong(), &[], SETUP_AT + 3).await, Err(MetaError::TwoFactorInvalid))); + assert!(!meta.two_factor_info(ana.id, SETUP_AT + 3).await.unwrap().pending); + assert!(matches!(meta.two_factor_confirm(ana.id, first, &*right(FIRST_STEP), &[], SETUP_AT + 4).await, Err(MetaError::Conflict(_)))); + // After the ten minutes it ran out; another device may start one. + meta.two_factor_begin_setup(&actor(first), b"fresh", SETUP_AT + 5).await.unwrap(); + assert!(matches!( + meta.two_factor_confirm(ana.id, first, &*right(FIRST_STEP), &[], SETUP_AT + 5 + 10 * MINUTE).await, + Err(MetaError::Conflict(_)) + )); + assert!(!meta.two_factor_info(ana.id, SETUP_AT + 5 + 10 * MINUTE).await.unwrap().pending); + meta.two_factor_begin_setup(&actor(second), b"second's", SETUP_AT + 6 + 10 * MINUTE).await.unwrap(); + assert!(!meta.two_factor_enabled(ana.id).await.unwrap(), "nothing was ever turned on"); +} + +/// Set-up, confirm, disable and new recovery codes check, under the account's lock, that the device is still signed in and +/// that the password that was verified is still the account's. +async fn changes_of_the_settings_recheck_the_device_and_the_password(meta: &Meta) { + let me = protected(meta, "ana").await; + let revoked_actor = DeviceActor { device: me.second, ..me.actor() }; + let stale_actor = DeviceActor { password_hash: "$argon2id$other".to_owned(), ..me.actor() }; + let hashes = me.hashes(); + for (name, actor, expect_gone) in [("revoked device", &revoked_actor, true), ("changed password", &stale_actor, false)] { + let results = [ + meta.two_factor_begin_setup(actor, b"x", SETUP_AT + 10).await.map(|_| ()), + meta.two_factor_disable(actor, &Proof::Recovery(hashes[0]), SETUP_AT + 10).await.map(|_| ()), + meta.two_factor_new_recovery_codes(actor, &totp(&right(FIRST_STEP + 50)), &hashes, SETUP_AT + 10).await, + ]; + for result in results { + if expect_gone { + assert!(matches!(result, Err(MetaError::DeviceRevoked)), "{name}: {result:?}"); + } else { + assert!(matches!(result, Err(MetaError::Conflict(_))), "{name}: {result:?}"); + } + } + } + // Confirming from a device that was signed out is refused too. + assert!(matches!(meta.two_factor_confirm(me.user.id, me.second, &*right(FIRST_STEP), &[], SETUP_AT + 11).await, Err(MetaError::DeviceRevoked))); + // Nothing changed: it is still on, with all ten codes, and no proof was spent. + let info = meta.two_factor_info(me.user.id, SETUP_AT + 12).await.unwrap(); + assert_eq!((info.enabled_ms.is_some(), info.recovery_codes_left), (true, 10)); + // A disabled account's device is gone as well. + meta.reset_password(me.user.id, "$argon2id$stub", false, SETUP_AT + 13).await.unwrap(); + assert!(matches!(meta.two_factor_begin_setup(&me.actor(), b"x", SETUP_AT + 14).await, Err(MetaError::DeviceRevoked))); +} + +async fn two_confirmations_at_once_turn_it_on_once(meta: &Meta) { + for round in 0..8 { + let ana = user(meta, &format!("ana{round}")).await; + let first = device(meta, &ana).await; + let actor = DeviceActor { user: ana.id, device: first, password_hash: "$argon2id$stub".to_owned() }; + meta.two_factor_begin_setup(&actor, b"secret", SETUP_AT).await.unwrap(); + let mut tasks = Vec::new(); + for _ in 0..4 { + let (meta, id) = (meta.clone(), ana.id); + let hashes: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + tasks.push(tokio::spawn(async move { meta.two_factor_confirm(id, first, &*right(FIRST_STEP), &hashes, SETUP_AT + 1).await })); + } + let mut turned_on = 0; + for task in tasks { + match task.await.unwrap() { + Ok(_) => turned_on += 1, + Err(MetaError::Conflict(_)) => {} + other => panic!("round {round}: {other:?}"), + } + } + assert_eq!(turned_on, 1, "round {round}"); + assert_eq!(meta.two_factor_info(ana.id, SETUP_AT + 2).await.unwrap().recovery_codes_left, 10, "one set of codes, round {round}"); + } +} + +/// The step of a code is accepted once, and only a later one after it. The admin's confirmation and the sign-in share it. +async fn a_time_step_is_accepted_once_and_only_later_ones_after_it(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + let at = SETUP_AT + 100; + // The first code of the set-up was step 1000: it can't be used again, nor an earlier step. + for step in [FIRST_STEP, FIRST_STEP - 1, 0] { + assert!(matches!(confirm_with(meta, id, Some(step), at).await, Err(MetaError::TwoFactorInvalid)), "step {step}"); + } + assert!(matches!(confirm_with(meta, id, Some(FIRST_STEP + 1), at).await, Ok(true))); + assert!(matches!(confirm_with(meta, id, Some(FIRST_STEP + 1), at).await, Err(MetaError::TwoFactorInvalid)), "the same step twice"); + assert!(matches!(confirm_with(meta, id, None, at).await, Err(MetaError::TwoFactorInvalid))); + // Eight requests with one code at once: exactly one gets through. + for round in 0..6 { + let step = FIRST_STEP + 10 + round; + let mut tasks = Vec::new(); + for _ in 0..8 { + let meta = meta.clone(); + tasks.push(tokio::spawn(async move { confirm_with(&meta, id, Some(step), at).await })); + } + let mut accepted = 0; + for task in tasks { + match task.await.unwrap() { + Ok(true) => accepted += 1, + Err(MetaError::TwoFactorInvalid | MetaError::TwoFactorLocked { .. }) => {} + other => panic!("round {round}: {other:?}"), + } + } + assert_eq!(accepted, 1, "round {round}: one time step, one use"); + } + // An account without two-factor sign-in asks for nothing, and a missing proof for one with it is refused unspent. + let bob = user(meta, "bob").await; + assert!(matches!(meta.confirm_second_factor(bob.id, None, at).await, Ok(false))); + assert!(matches!(confirm_with(meta, bob.id, Some(1), at).await, Ok(false))); + assert!(matches!(meta.confirm_second_factor(id, None, at).await, Err(MetaError::TwoFactorInvalid))); +} + +async fn recovery_codes_work_once_and_new_ones_replace_them(meta: &Meta) { + let me = protected(meta, "ana").await; + let (id, hashes) = (me.user.id, me.hashes()); + let at = SETUP_AT + 100; + assert!(matches!(meta.confirm_second_factor(id, Some(&Proof::Recovery(hashes[0])), at).await, Ok(true))); + assert!(matches!(meta.confirm_second_factor(id, Some(&Proof::Recovery(hashes[0])), at).await, Err(MetaError::TwoFactorInvalid))); + assert!(matches!(meta.confirm_second_factor(id, Some(&Proof::Recovery(code(INVITE_CHARS).hash())), at).await, Err(MetaError::TwoFactorInvalid))); + assert_eq!(meta.two_factor_info(id, at).await.unwrap().recovery_codes_left, 9); + // The same code at the same moment: one wins. + let mut tasks = Vec::new(); + for _ in 0..6 { + let (meta, hash) = (meta.clone(), hashes[1]); + tasks.push(tokio::spawn(async move { meta.confirm_second_factor(id, Some(&Proof::Recovery(hash)), at).await })); + } + let mut accepted = 0; + for task in tasks { + if matches!(task.await.unwrap(), Ok(true)) { + accepted += 1; + } + } + assert_eq!(accepted, 1); + // New codes: the old ones, used or not, are dead, and the new ones all live. + let new: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + meta.two_factor_new_recovery_codes(&me.actor(), &totp(&right(FIRST_STEP + 1)), &new, at).await.unwrap(); + assert_eq!(meta.two_factor_info(id, at).await.unwrap().recovery_codes_left, 10); + assert!(matches!(meta.confirm_second_factor(id, Some(&Proof::Recovery(hashes[5])), at).await, Err(MetaError::TwoFactorInvalid))); + assert!(matches!(meta.confirm_second_factor(id, Some(&Proof::Recovery(new[3])), at).await, Ok(true))); + // A repeated code can't be stored (the primary key), and then nothing changes: the old set is still there. + let again = vec![new[0]; 2]; + assert!(meta.two_factor_new_recovery_codes(&me.actor(), &totp(&right(FIRST_STEP + 2)), &again, at).await.is_err()); + assert_eq!(meta.two_factor_info(id, at).await.unwrap().recovery_codes_left, 9); + assert!(matches!(meta.confirm_second_factor(id, Some(&Proof::Recovery(new[7])), at).await, Ok(true))); + // A wrong proof for new codes changes none. + assert!(matches!(meta.two_factor_new_recovery_codes(&me.actor(), &totp(&wrong()), &new, at).await, Err(MetaError::TwoFactorInvalid))); +} + +/// Ten wrong proofs in a row lock the second factor for 15 minutes; each wrong one after that doubles it, to 24 hours; a +/// right one resets all of it. While locked even a right proof is refused and nothing is counted. +async fn wrong_proofs_lock_for_a_while_double_and_reset_on_success(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + let mut now = SETUP_AT + 100; + for n in 1..=9 { + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid)), "wrong proof {n}"); + } + // The tenth locks. + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid))); + let locked = confirm_with(meta, id, Some(FIRST_STEP + 1), now + 1_000).await; + assert!(matches!(locked, Err(MetaError::TwoFactorLocked { retry_after_ms }) if retry_after_ms == 15 * MINUTE - 1_000), "{locked:?}"); + // Every path refuses while it lasts: recovery codes, disable, new codes, a challenge. + let actor = me.actor(); + assert!(matches!(meta.confirm_second_factor(id, Some(&Proof::Recovery(me.hashes()[0])), now + 2).await, Err(MetaError::TwoFactorLocked { .. }))); + assert!(matches!(meta.two_factor_disable(&actor, &Proof::Recovery(me.hashes()[1]), now + 2).await, Err(MetaError::TwoFactorLocked { .. }))); + assert!(matches!( + meta.two_factor_new_recovery_codes(&actor, &totp(&right(5)), &me.hashes(), now + 2).await, + Err(MetaError::TwoFactorLocked { .. }) + )); + let generation = gen_of(meta, "ana").await; + let chal = new_challenge(); + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &chal, now + 2).await.unwrap(); + assert!(matches!( + meta.complete_second_factor(&chal.id_hash, &Proof::Recovery(me.hashes()[2]), &token(), now + 3).await, + Err(MetaError::TwoFactorLocked { .. }) + )); + assert_eq!(meta.two_factor_info(id, now + 2).await.unwrap().recovery_codes_left, 10, "a refused check uses nothing up"); + // Over, a wrong proof locks again for twice as long: 30 minutes, then 60. + now += 15 * MINUTE + 1; + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid))); + let locked = confirm_with(meta, id, Some(FIRST_STEP + 1), now + 1).await; + assert!(matches!(locked, Err(MetaError::TwoFactorLocked { retry_after_ms }) if retry_after_ms == 30 * MINUTE - 1), "{locked:?}"); + now += 30 * MINUTE + 1; + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid))); + assert!(matches!( + confirm_with(meta, id, Some(FIRST_STEP + 1), now + 1).await, + Err(MetaError::TwoFactorLocked { retry_after_ms }) if retry_after_ms == 60 * MINUTE - 1 + )); + // A right proof after it ran out resets the counters: nine more wrong ones do not lock. + now += 60 * MINUTE + 1; + assert!(matches!(confirm_with(meta, id, Some(FIRST_STEP + 1), now).await, Ok(true))); + for n in 1..=9 { + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid)), "after the reset, wrong proof {n}"); + } + assert!(matches!(confirm_with(meta, id, Some(FIRST_STEP + 2), now).await, Ok(true)), "nine are not ten"); + // The command line lifts any lock. + for _ in 0..10 { + let _ = confirm_with(meta, id, None, now).await; + } + assert!(matches!(confirm_with(meta, id, Some(FIRST_STEP + 3), now + 1).await, Err(MetaError::TwoFactorLocked { .. }))); + meta.reset_two_factor(id, now + 2).await.unwrap(); + assert!(matches!(confirm_with(meta, id, Some(FIRST_STEP + 3), now + 3).await, Ok(false)), "off, and nothing is locked"); +} + +/// The counters of wrong proofs are written outside the transaction that refused them, so requests at the same moment all +/// count: twenty wrong proofs lock it. +async fn wrong_proofs_at_the_same_moment_all_count(meta: &Meta) { + let me = protected(meta, "ana").await; + let (id, now) = (me.user.id, SETUP_AT + 100); + let mut tasks = Vec::new(); + for _ in 0..12 { + let meta = meta.clone(); + tasks.push(tokio::spawn(async move { confirm_with(&meta, id, None, now).await })); + } + for task in tasks { + assert!(matches!(task.await.unwrap(), Err(MetaError::TwoFactorInvalid | MetaError::TwoFactorLocked { .. }))); + } + assert!(matches!(confirm_with(meta, id, Some(FIRST_STEP + 1), now + 1).await, Err(MetaError::TwoFactorLocked { .. }))); +} + +async fn a_challenge_is_made_counted_used_once_and_carries_the_device(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + let generation = gen_of(meta, "ana").await; + let client = DeviceClient::new(Some("desktop"), Some("linux"), Some("1.2")).unwrap(); + let chal = new_challenge(); + let made = meta.sign_in(id, ("$argon2id$stub", generation), "work laptop", &client, &token(), &chal, SETUP_AT + 10).await.unwrap(); + assert_eq!(made, Some(SignIn::Challenge { expires_ms: SETUP_AT + 10 + 5 * MINUTE })); + assert_eq!(live_devices(meta, &me.user).await, vec![me.first], "no device without the second factor"); + // The old call, for a caller that doesn't know, refuses an account that has two-factor sign-in. + assert!(matches!( + meta.sign_in_device(id, ("$argon2id$stub", generation), "x", &client, &token(), SETUP_AT + 10).await, + Err(MetaError::TwoFactorRequired) + )); + assert_eq!(meta.challenge_owner(&chal.id_hash, SETUP_AT + 11).await.unwrap(), Some((id, "ana".to_owned()))); + // Four wrong codes are counted, the challenge stays, and the right one then works (a fifth wrong would delete it). + for _ in 0..4 { + let result = meta.complete_second_factor(&chal.id_hash, &totp(&wrong()), &token(), SETUP_AT + 12).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid)), "{result:?}"); + } + let tok = token(); + let done = meta.complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 1)), &tok, SETUP_AT + 13).await.unwrap(); + assert_eq!(done.user, id); + let auth = meta.device_by_token(&tok).await.unwrap().unwrap(); + assert_eq!(auth.device, done.device); + let made = meta.list_devices(id).await.unwrap().into_iter().find(|d| d.id == done.device).unwrap(); + assert_eq!((made.name.as_str(), made.client), ("work laptop", client.clone()), "the device is what the first call said"); + // Once: the same challenge again is gone, for a right proof as for a wrong one. + for proof in [totp(&right(FIRST_STEP + 2)), totp(&wrong())] { + assert!(matches!(meta.complete_second_factor(&chal.id_hash, &proof, &token(), SETUP_AT + 14).await, Err(MetaError::ChallengeGone))); + } + assert!(meta.challenge_owner(&chal.id_hash, SETUP_AT + 14).await.unwrap().is_none()); + + // Five wrong codes delete the challenge: the right one is too late. + let chal = new_challenge(); + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &client, &token(), &chal, SETUP_AT + 20).await.unwrap(); + for _ in 0..5 { + assert!(matches!( + meta.complete_second_factor(&chal.id_hash, &totp(&wrong()), &token(), SETUP_AT + 21).await, + Err(MetaError::TwoFactorInvalid) + )); + } + assert!(matches!( + meta.complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 3)), &token(), SETUP_AT + 22).await, + Err(MetaError::ChallengeGone) + )); + // One that ran out is as gone, and an unknown one. + let chal = new_challenge(); + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &client, &token(), &chal, SETUP_AT + 30).await.unwrap(); + assert!(matches!( + meta.complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 3)), &token(), SETUP_AT + 30 + 5 * MINUTE).await, + Err(MetaError::ChallengeGone) + )); + assert!(matches!( + meta.complete_second_factor(&token(), &totp(&right(FIRST_STEP + 3)), &token(), SETUP_AT + 30).await, + Err(MetaError::ChallengeGone) + )); + // The recovery code finishes a sign-in too, once. + let chal = new_challenge(); + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &client, &token(), &chal, SETUP_AT + 40).await.unwrap(); + meta.complete_second_factor(&chal.id_hash, &Proof::Recovery(me.hashes()[4]), &token(), SETUP_AT + 41).await.unwrap(); + let chal = new_challenge(); + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &client, &token(), &chal, SETUP_AT + 42).await.unwrap(); + assert!(matches!( + meta.complete_second_factor(&chal.id_hash, &Proof::Recovery(me.hashes()[4]), &token(), SETUP_AT + 43).await, + Err(MetaError::TwoFactorInvalid) + )); +} + +/// Each account keeps at most five open challenges (the oldest goes), and expired ones are deleted by the next sign-in. +async fn an_account_keeps_five_open_challenges_and_expired_ones_go(meta: &Meta) { + let me = protected(meta, "ana").await; + let generation = gen_of(meta, "ana").await; + let mut made = Vec::new(); + for n in 0..7 { + let chal = new_challenge(); + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &chal, SETUP_AT + 10 + n).await.unwrap(); + made.push(chal); + } + for (n, chal) in made.iter().enumerate() { + let open = meta.challenge_owner(&chal.id_hash, SETUP_AT + 20).await.unwrap().is_some(); + assert_eq!(open, n >= 2, "challenge {n}: the two oldest were replaced"); + } + // The next sign-in after they ran out deletes them (nothing else would). + let later = SETUP_AT + 10 + 5 * MINUTE + 100; + let fresh = new_challenge(); + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &fresh, later).await.unwrap(); + assert!(meta.challenge_owner(&fresh.id_hash, later).await.unwrap().is_some()); + // A stale password hash makes neither a challenge nor a device. + let stale = new_challenge(); + assert!( + meta.sign_in(me.user.id, ("$argon2id$old", generation), "pc", &DeviceClient::default(), &token(), &stale, later).await.unwrap().is_none() + ); + assert!( + meta.sign_in(me.user.id, ("$argon2id$stub", generation - 1), "pc", &DeviceClient::default(), &token(), &stale, later) + .await + .unwrap() + .is_none() + ); + assert!(meta.challenge_owner(&stale.id_hash, later).await.unwrap().is_none()); + assert_eq!(live_devices(meta, &me.user).await, vec![me.first]); +} + +/// A challenge made before a password change, a reset, a turn-off, a reset of the second factor or a disable gives nothing after. +async fn a_challenge_dies_with_every_change_of_the_account(meta: &Meta) { + let (_, actor) = admin_actor(meta, "root").await; + for round in 0..6 { + let me = protected(meta, &format!("ana{round}")).await; + let generation = gen_of(meta, &format!("ana{round}")).await; + let chal = new_challenge(); + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &chal, SETUP_AT + 10).await.unwrap(); + let at = SETUP_AT + 11; + match round { + 0 => drop(meta.change_password(me.user.id, "$argon2id$stub", "$argon2id$new", me.first, at).await.unwrap()), + 1 => drop(meta.reset_password(me.user.id, "$argon2id$new", true, at).await.unwrap()), + 2 => drop(meta.two_factor_disable(&me.actor(), &Proof::Recovery(me.hashes()[0]), at).await.unwrap()), + 3 => drop(meta.reset_two_factor(me.user.id, at).await.unwrap()), + 4 => drop(meta.admin_disable_user(&actor, me.user.id, at).await.unwrap()), + _ => drop(meta.admin_reset_two_factor(&actor, me.user.id, at).await.unwrap()), + } + assert!(meta.challenge_owner(&chal.id_hash, at).await.unwrap().is_none(), "round {round}"); + let result = meta.complete_second_factor(&chal.id_hash, &Proof::Recovery(me.hashes()[1]), &token(), at + 1).await; + assert!(matches!(result, Err(MetaError::ChallengeGone)), "round {round}: {result:?}"); + assert!(live_devices(meta, &me.user).await.iter().all(|d| *d == me.first), "round {round}: no new device"); + } +} + +async fn two_completions_of_one_challenge_give_one_device(meta: &Meta) { + for round in 0..8 { + let me = protected(meta, &format!("ana{round}")).await; + let generation = gen_of(meta, &format!("ana{round}")).await; + let chal = new_challenge(); + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &chal, SETUP_AT + 10).await.unwrap(); + let mut tasks = Vec::new(); + for job in 0..6 { + let (meta, hash, recovery) = (meta.clone(), chal.id_hash, me.hashes()[job % 2]); + tasks.push(tokio::spawn(async move { + // The same TOTP step for some, two recovery codes for the others: a code is no way past the challenge. + if job % 2 == 0 { + meta.complete_second_factor(&hash, &totp(&right(FIRST_STEP + 1)), &token(), SETUP_AT + 11).await + } else { + meta.complete_second_factor(&hash, &Proof::Recovery(recovery), &token(), SETUP_AT + 11).await + } + })); + } + let mut done = 0; + for task in tasks { + match task.await.unwrap() { + Ok(_) => done += 1, + Err(MetaError::ChallengeGone | MetaError::TwoFactorInvalid) => {} + other => panic!("round {round}: {other:?}"), + } + } + assert_eq!(done, 1, "round {round}"); + assert_eq!(live_devices(meta, &me.user).await.len(), 2, "round {round}: the first device and one more"); + } +} + +/// Completing a sign-in and a password change, a reset, a turn-off or the command line's reset at the same moment, over and +/// over: no database error, and once the change is done no device of the account but the one that asked is signed in, +/// whichever came first. +async fn completions_and_changes_of_the_account_at_the_same_moment_leave_no_stray_device(meta: &Meta) { + for round in 0..12 { + let name = format!("ana{round}"); + let me = protected(meta, &name).await; + let generation = gen_of(meta, &name).await; + let chal = new_challenge(); + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &chal, SETUP_AT + 10).await.unwrap(); + // A password change keeps the device that asked; a reset of the second factor or of the password signs out all. + let all_gone = round % 3 != 0; + let (completing, changing) = { + let (m1, m2) = (meta.clone(), meta.clone()); + let (hash, id, first, recovery) = (chal.id_hash, me.user.id, me.first, me.hashes()); + let completing = + tokio::spawn( + async move { m1.complete_second_factor(&hash, &Proof::Recovery(recovery[0]), &token(), SETUP_AT + 11).await.map(|_| ()) }, + ); + let changing = tokio::spawn(async move { + match round % 3 { + 0 => m2.change_password(id, "$argon2id$stub", "$argon2id$new", first, SETUP_AT + 12).await.map(|_| ()), + 1 => m2.reset_two_factor(id, SETUP_AT + 12).await.map(|_| ()), + _ => m2.reset_password(id, "$argon2id$new", false, SETUP_AT + 12).await.map(|_| ()), + } + }); + (completing, changing) + }; + let (completed, changed) = (completing.await.unwrap(), changing.await.unwrap()); + assert!(matches!(completed, Ok(()) | Err(MetaError::ChallengeGone)), "round {round}: {completed:?}"); + changed.unwrap_or_else(|e| panic!("round {round}: {e:?}")); + let live = live_devices(meta, &me.user).await; + if all_gone { + assert!(live.is_empty(), "round {round}: {live:?}"); + } else { + assert!(live.iter().all(|d| *d == me.first), "round {round}: {live:?}"); + } + assert_revoke_columns_agree(&meta.list_devices(me.user.id).await.unwrap()); + } +} + +/// Turning two-factor sign-in on or off while a password sign-in is under way: a device made without a second factor +/// before it is signed out by the turn-on, and one checked before it makes no device at all. +async fn turning_two_factor_on_and_off_at_the_same_moment_as_a_sign_in(meta: &Meta) { + for round in 0..8 { + let name = format!("on{round}"); + let ana = user(meta, &name).await; + let first = device(meta, &ana).await; + let actor = DeviceActor { user: ana.id, device: first, password_hash: "$argon2id$stub".to_owned() }; + meta.two_factor_begin_setup(&actor, b"cipher", SETUP_AT).await.unwrap(); + let generation = gen_of(meta, &name).await; + let (m1, m2) = (meta.clone(), meta.clone()); + let id = ana.id; + let signing = tokio::spawn(async move { + m1.sign_in(id, ("$argon2id$stub", generation), "late", &DeviceClient::default(), &token(), &new_challenge(), SETUP_AT + 5).await + }); + let hashes: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + let enabling = tokio::spawn(async move { m2.two_factor_confirm(id, first, &*right(FIRST_STEP), &hashes, SETUP_AT + 5).await }); + let (signed, enabled) = (signing.await.unwrap(), enabling.await.unwrap()); + enabled.unwrap(); + assert!(matches!(signed, Ok(None | Some(SignIn::Device(_)))), "round {round}: a sign-in that checked before the turn-on makes no challenge"); + assert_eq!(live_devices(meta, &ana).await, vec![first], "round {round}: only the device that turned it on"); + + // And off: a challenge made under it works once, but a device it made is signed out by the turn-off. + let generation = gen_of(meta, &name).await; + let chal = new_challenge(); + meta.sign_in(id, ("$argon2id$stub", generation), "x", &DeviceClient::default(), &token(), &chal, SETUP_AT + 6).await.unwrap(); + let (m1, m2) = (meta.clone(), meta.clone()); + let (hash, other_code) = (chal.id_hash, code(INVITE_CHARS).hash()); + let completing = + tokio::spawn(async move { m1.complete_second_factor(&hash, &Proof::Recovery(other_code), &token(), SETUP_AT + 7).await.map(|_| ()) }); + let actor = DeviceActor { user: id, device: first, password_hash: "$argon2id$stub".to_owned() }; + let disabling = tokio::spawn(async move { m2.two_factor_disable(&actor, &totp(&right(FIRST_STEP + 1)), SETUP_AT + 7).await.map(|_| ()) }); + let completed = completing.await.unwrap(); + disabling.await.unwrap().unwrap(); + assert!(matches!(completed, Err(MetaError::TwoFactorInvalid | MetaError::ChallengeGone)), "round {round}: {completed:?}"); + assert_eq!(live_devices(meta, &ana).await, vec![first]); + } +} + +async fn an_invite_to_a_protected_account_needs_the_second_factor_in_the_same_transaction(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), SETUP_AT + 10, INVITE_TTL_MS).await.unwrap(); + // No second factor: refused and the invite is kept (not used up), and nothing is counted. + assert!(matches!(redeem_with_factor(meta, &invite.hash(), None, SETUP_AT + 11).await, Err(MetaError::TwoFactorRequired))); + let old_call = meta.redeem_with(&invite.hash(), "x", &DeviceClient::default(), &token(), None, SETUP_AT + 11).await; + assert!(matches!(old_call, Err(MetaError::TwoFactorRequired))); + // Wrong factors: refused, the invite stays, and nine of them leave the counters just short of the lock. + for n in 1..=9 { + let result = redeem_with_factor(meta, &invite.hash(), Some(&totp(&wrong())), SETUP_AT + 12).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid)), "wrong factor {n}: {result:?}"); + } + assert_eq!(meta.invite_needs_password(&invite.hash(), SETUP_AT + 12).await.unwrap(), Some(false), "the invite is still there"); + // The tenth locks, with the invite still there, and a right one is refused while it lasts. + assert!(matches!(redeem_with_factor(meta, &invite.hash(), Some(&totp(&wrong())), SETUP_AT + 12).await, Err(MetaError::TwoFactorInvalid))); + let locked = redeem_with_factor(meta, &invite.hash(), Some(&totp(&right(FIRST_STEP + 1))), SETUP_AT + 13).await; + assert!(matches!(locked, Err(MetaError::TwoFactorLocked { .. })), "{locked:?}"); + // A step that was used is wrong, even after the lock. + let after = SETUP_AT + 12 + 15 * MINUTE + 1; + let replay = redeem_with_factor(meta, &invite.hash(), Some(&totp(&right(FIRST_STEP))), after).await; + assert!(matches!(replay, Err(MetaError::TwoFactorInvalid)), "the set-up's own step: {replay:?}"); + assert_eq!(meta.invite_needs_password(&invite.hash(), after).await.unwrap(), Some(false), "still there"); + assert_eq!(live_devices(meta, &me.user).await, vec![me.first], "no device was made by any of it"); + let _ = id; + + // A fresh account: a new step is right, and the invite, the step and the device go together. + let bea = protected(meta, "bea").await; + let invite = code(INVITE_CHARS); + meta.create_invite("bea", &invite.hash(), SETUP_AT + 10, INVITE_TTL_MS).await.unwrap(); + let device = redeem_with_factor(meta, &invite.hash(), Some(&totp(&right(FIRST_STEP + 1))), SETUP_AT + 20).await.unwrap(); + assert_eq!(live_devices(meta, &bea.user).await.len(), 2); + assert!(live_devices(meta, &bea.user).await.contains(&device)); + assert_eq!(meta.invite_needs_password(&invite.hash(), SETUP_AT + 21).await.unwrap(), None, "used up with the device"); + // Nobody else can use that step now (sign-in and join share the counter). + assert!(matches!(confirm_with(meta, bea.user.id, Some(FIRST_STEP + 1), SETUP_AT + 22).await, Err(MetaError::TwoFactorInvalid))); + // A pairing needs no second factor: only a signed-in device can make one. + let pairing = code(PAIRING_CHARS); + meta.create_pairing(bea.user.id, bea.first, &pairing.hash(), SETUP_AT + 30).await.unwrap(); + meta.redeem_with(&pairing.hash(), "paired", &DeviceClient::default(), &token(), None, SETUP_AT + 31).await.unwrap(); +} + +async fn a_reset_code_for_a_protected_account_needs_the_factor_unless_it_turns_it_off(meta: &Meta) { + let (_, admin) = admin_actor(meta, "root").await; + let admin = AdminActor { two_factor: false, ..admin }; + let me = protected(meta, "ana").await; + let (id, stale) = (me.user.id, me.hashes()); + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&admin, id, &reset.hash(), false, SETUP_AT + 10, RESET_TTL_MS).await.unwrap(); + assert!(matches!( + meta.redeem_with(&reset.hash(), "pc", &DeviceClient::default(), &token(), Some("$argon2id$fresh"), SETUP_AT + 11).await, + Err(MetaError::TwoFactorRequired) + )); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$stub", "nothing changed"); + assert!(matches!( + meta.redeem_with_second_factor( + &reset.hash(), + "pc", + &DeviceClient::default(), + &token(), + Some("$argon2id$fresh"), + Some(&totp(&wrong())), + SETUP_AT + 11 + ) + .await, + Err(MetaError::TwoFactorInvalid) + )); + assert_eq!(live_devices(meta, &me.user).await, vec![me.first], "the failed redeem signed nothing out"); + let first_done = meta + .redeem_with_second_factor( + &reset.hash(), + "pc", + &DeviceClient::default(), + &token(), + Some("$argon2id$fresh"), + Some(&Proof::Recovery(stale[0])), + SETUP_AT + 12, + ) + .await + .unwrap(); + let done = &first_done; + assert_eq!(done.revoked, vec![me.first]); + assert!(meta.two_factor_enabled(id).await.unwrap(), "the code did not turn two-factor sign-in off"); + assert_eq!(meta.user_by_name("ana").await.unwrap().unwrap().password_hash, "$argon2id$fresh"); + + // A code made to turn it off asks for no second factor, turns it off and signs everything out. + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&admin, id, &reset.hash(), true, SETUP_AT + 20, RESET_TTL_MS).await.unwrap(); + let done = meta.redeem_with(&reset.hash(), "pc2", &DeviceClient::default(), &token(), Some("$argon2id$newer"), SETUP_AT + 21).await.unwrap(); + assert_eq!(done.revoked, vec![first_done.device], "the computer of the first redeem is signed out with the rest"); + assert!(!meta.two_factor_enabled(id).await.unwrap()); + assert_eq!(meta.two_factor_info(id, SETUP_AT + 22).await.unwrap().recovery_codes_left, 0); + assert_eq!(live_devices(meta, &me.user).await, vec![done.device], "the redeeming computer is the only one signed in"); + // An account without two-factor sign-in redeems a reset code as it always did, whatever the flag. + let plain = user(meta, "plain").await; + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&admin, plain.id, &reset.hash(), false, SETUP_AT + 30, RESET_TTL_MS).await.unwrap(); + meta.redeem_with(&reset.hash(), "pc", &DeviceClient::default(), &token(), Some("$argon2id$fresh"), SETUP_AT + 31).await.unwrap(); +} + +/// The admin's own two-factor state is compared again in every change, and resetting someone's two-factor sign-in, or another +/// admin's password, needs the acting admin's own. +async fn admins_two_factor_is_checked_in_the_change_and_resets_need_it(meta: &Meta) { + let (root, plain_actor) = admin_actor(meta, "root").await; + let (other, _) = admin_actor(meta, "other").await; + let member = protected(meta, "member").await; + let (boss_admin, boss_actor) = { + let (u, a) = admin_actor(meta, "boss").await; + (u, a) + }; + // Turn two-factor sign-in on for the acting admin "boss" (device `device_of(&boss_actor)`). + let actor_for = DeviceActor { user: boss_admin.id, device: device_of(&boss_actor), password_hash: ADMIN_HASH.to_owned() }; + meta.two_factor_begin_setup(&actor_for, b"cipher", SETUP_AT).await.unwrap(); + meta.two_factor_confirm(boss_admin.id, device_of(&boss_actor), &*right(FIRST_STEP), &[code(INVITE_CHARS).hash()], SETUP_AT + 1).await.unwrap(); + // A confirmation that was given when it was off, and a change after it turned on, conflict (and the other way round). + let hash = code(INVITE_CHARS).hash(); + let result = meta.admin_issue_invite(&boss_actor, member.user.id, &hash, SETUP_AT + 2, INVITE_TTL_MS).await; + assert!(matches!(result, Err(MetaError::Conflict(_))), "{result:?}"); + let with_factor = AdminActor { two_factor: true, ..boss_actor.clone() }; + let result = meta.admin_issue_invite(&with_factor, member.user.id, &hash, SETUP_AT + 2, INVITE_TTL_MS).await; + assert!(matches!(result, Err(MetaError::Conflict(_))), "it has a password: {result:?}"); + let result = + meta.admin_issue_invite(&AdminActor { two_factor: true, ..plain_actor.clone() }, member.user.id, &hash, SETUP_AT + 2, INVITE_TTL_MS).await; + assert!(matches!(result, Err(MetaError::Conflict(_))), "root has none: {result:?}"); + + // Resetting a member's two-factor sign-in: without the admin's own it works for a member, not for an admin. + let result = meta.admin_reset_two_factor(&plain_actor, other.id, SETUP_AT + 3).await; + assert!(matches!(result, Err(MetaError::Forbidden(_))), "another admin, no two-factor of one's own: {result:?}"); + let result = meta.admin_issue_reset(&plain_actor, other.id, &hash, false, SETUP_AT + 3, RESET_TTL_MS).await; + assert!(matches!(result, Err(MetaError::Forbidden(_))), "{result:?}"); + assert!(matches!(meta.admin_reset_two_factor(&plain_actor, root.id, SETUP_AT + 3).await, Err(MetaError::Invalid(_))), "not one's own"); + assert!(matches!(meta.admin_reset_two_factor(&plain_actor, UserId(uuid::Uuid::now_v7()), SETUP_AT + 3).await, Err(MetaError::NotFound(_)))); + // An admin with it may reset another admin's password, and that admin's two-factor sign-in (once they have one). + meta.admin_issue_reset(&with_factor, other.id, &hash, false, SETUP_AT + 4, RESET_TTL_MS).await.unwrap(); + let result = meta.admin_reset_two_factor(&with_factor, other.id, SETUP_AT + 4).await; + assert!(matches!(result, Err(MetaError::Conflict(_))), "the other admin has none: {result:?}"); + // A member's: everything of it goes, every device is signed out, the lock goes with it. + let revoked = meta.admin_reset_two_factor(&plain_actor, member.user.id, SETUP_AT + 5).await.unwrap(); + assert_eq!(revoked, vec![member.first]); + assert!(!meta.two_factor_enabled(member.user.id).await.unwrap()); + assert!(live_devices(meta, &member.user).await.is_empty()); + let devices = meta.list_devices(member.user.id).await.unwrap(); + assert!(devices.iter().any(|d| d.revoke_reason == Some(RevokeReason::TwoFactor))); + assert!(matches!(meta.admin_reset_two_factor(&plain_actor, member.user.id, SETUP_AT + 6).await, Err(MetaError::Conflict(_))), "twice"); + let row = admin_view(meta, boss_admin.id).await; + assert!(row.two_factor_enabled); + assert!(!admin_view(meta, member.user.id).await.two_factor_enabled); +} + +async fn the_command_line_resets_two_factor_and_the_account_shows_it(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + assert!(meta.account(id).await.unwrap().two_factor_enabled); + assert!(admin_view(meta, id).await.two_factor_enabled); + let generation = gen_of(meta, "ana").await; + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &new_challenge(), SETUP_AT + 5).await.unwrap(); + meta.create_invite("ana", &code(INVITE_CHARS).hash(), SETUP_AT + 5, INVITE_TTL_MS).await.unwrap(); + let revoked = meta.reset_two_factor(id, SETUP_AT + 6).await.unwrap(); + assert_eq!(revoked, vec![me.first]); + assert!(!meta.account(id).await.unwrap().two_factor_enabled); + assert!(!admin_view(meta, id).await.two_factor_enabled); + assert!(!meta.revoke_invite("ana").await.unwrap()); + assert!(matches!(meta.reset_two_factor(id, SETUP_AT + 7).await, Err(MetaError::Conflict(_))), "nothing to reset: nothing is signed out"); + assert!(matches!(meta.reset_two_factor(UserId(uuid::Uuid::now_v7()), SETUP_AT + 7).await, Err(MetaError::NotFound(_)))); + // After it a password sign-in gives a device again, and the account can set it up anew. + let generation = gen_of(meta, "ana").await; + let signed = + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &new_challenge(), SETUP_AT + 8).await.unwrap(); + assert!(matches!(signed, Some(SignIn::Device(_)))); +} + +/// The key tools read the ciphertexts and replace one only if it is still the one they read. +async fn secrets_are_listed_and_replaced_only_if_unchanged(meta: &Meta) { + let me = protected(meta, "ana").await; + let other = user(meta, "bob").await; + let dev = device(meta, &other).await; + let actor = DeviceActor { user: other.id, device: dev, password_hash: "$argon2id$stub".to_owned() }; + meta.two_factor_begin_setup(&actor, b"cipher-pending", SETUP_AT).await.unwrap(); + let listed = meta.two_factor_secrets(SETUP_AT + 1).await.unwrap(); + assert_eq!(listed.len(), 2); + let ana = listed.iter().find(|s| s.user == me.user.id).unwrap(); + assert_eq!((ana.active.as_deref(), ana.pending.as_deref()), (Some(b"cipher-of-the-secret".as_slice()), None)); + let bob = listed.iter().find(|s| s.user == other.id).unwrap(); + assert_eq!((bob.active.as_deref(), bob.pending.as_deref()), (None, Some(b"cipher-pending".as_slice()))); + let later = meta.two_factor_secrets(SETUP_AT + 11 * MINUTE).await.unwrap(); + assert!(later.iter().find(|s| s.user == other.id).is_some_and(|s| s.pending.is_none()), "an expired set-up is not listed"); + assert!(meta.two_factor_replace_ciphertext(me.user.id, false, b"cipher-of-the-secret", b"new-cipher").await.unwrap()); + assert!( + !meta.two_factor_replace_ciphertext(me.user.id, false, b"cipher-of-the-secret", b"newer").await.unwrap(), + "no longer the one that was read" + ); + assert!(meta.two_factor_replace_ciphertext(other.id, true, b"cipher-pending", b"new-pending").await.unwrap()); + let listed = meta.two_factor_secrets(SETUP_AT + 1).await.unwrap(); + assert_eq!(listed.iter().find(|s| s.user == me.user.id).unwrap().active.as_deref(), Some(b"new-cipher".as_slice())); + // A secret that isn't the account's is never touched. + assert!(!meta.two_factor_replace_ciphertext(UserId(uuid::Uuid::now_v7()), false, b"x", b"y").await.unwrap()); +} + +/// Wrong proofs are counted under the lock that checked them: of many at once on one challenge exactly five are counted (the +/// fifth deletes it, the rest find it gone), and they all count against the account's ten. +async fn parallel_wrong_proofs_on_one_challenge_count_exactly_five(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + let generation = gen_of(meta, "ana").await; + for round in 0..4 { + let chal = new_challenge(); + meta.sign_in(id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &chal, SETUP_AT + 10 + round).await.unwrap(); + let mut tasks = Vec::new(); + for _ in 0..14 { + let (meta, hash) = (meta.clone(), chal.id_hash); + tasks.push(tokio::spawn(async move { meta.complete_second_factor(&hash, &totp(&wrong()), &token(), SETUP_AT + 20).await.map(|_| ()) })); + } + let (mut counted, mut gone) = (0, 0); + for task in tasks { + match task.await.unwrap() { + Err(MetaError::TwoFactorInvalid) => counted += 1, + Err(MetaError::ChallengeGone) => gone += 1, + Err(MetaError::TwoFactorLocked { .. }) => {} + other => panic!("round {round}: {other:?}"), + } + } + assert!(meta.challenge_owner(&chal.id_hash, SETUP_AT + 21).await.unwrap().is_none(), "round {round}: five wrong proofs delete it"); + // Locked rounds stop counting; until then exactly five per challenge, never six. + if round == 0 { + assert_eq!((counted, gone), (5, 9), "round {round}"); + } + assert!(counted <= 5, "round {round}: {counted} proofs were checked against one challenge"); + if meta.two_factor_locked_for(id, SETUP_AT + 21).await.unwrap().is_some() { + break; + } + } +} + +/// Many wrong proofs at once, crossing ten: the lock is set by exactly the tenth, for exactly 15 minutes, and the ones after it +/// find it locked and count nothing (so it never jumps to 30). +async fn parallel_wrong_proofs_lock_for_exactly_fifteen_minutes(meta: &Meta) { + for round in 0..6 { + let me = protected(meta, &format!("ana{round}")).await; + let (id, now) = (me.user.id, SETUP_AT + 100); + let mut tasks = Vec::new(); + for _ in 0..24 { + let meta = meta.clone(); + tasks.push(tokio::spawn(async move { confirm_with(&meta, id, None, now).await })); + } + let (mut wrong_ones, mut locked) = (0, 0); + for task in tasks { + match task.await.unwrap() { + Err(MetaError::TwoFactorInvalid) => wrong_ones += 1, + Err(MetaError::TwoFactorLocked { .. }) => locked += 1, + other => panic!("round {round}: {other:?}"), + } + } + assert_eq!((wrong_ones, locked), (10, 14), "round {round}"); + assert_eq!(meta.two_factor_locked_for(id, now).await.unwrap(), Some(15 * MINUTE), "round {round}: 15 minutes, not 30"); + } +} + +// ---- the audit log of two-factor sign-in ---- + +/// The events of `action` in the log, oldest first (an aggregated row counts as one). +async fn events_of(meta: &Meta, action: AuditAction) -> Vec { + let mut events: Vec = all_events(meta).await.into_iter().filter(|e| e.action == action).collect(); + events.reverse(); + events +} + +/// Turning it on, new recovery codes and turning it off each write their event in their own transaction, as the account's own +/// act from the device that asked; a wrong proof on these routes writes nothing, and a recovery code that is used up writes +/// `auth.recovery_code_used` with the change it served. +async fn two_factor_changes_write_their_events(meta: &Meta) { + let ana = user(meta, "ana").await; + let (first, _second) = (device(meta, &ana).await, device(meta, &ana).await); + let actor = DeviceActor { user: ana.id, device: first, password_hash: "$argon2id$stub".to_owned() }; + let mut seen = Seen::new(meta).await; + let hashes: Vec<[u8; 32]> = (0..10).map(|_| code(INVITE_CHARS).hash()).collect(); + + meta.two_factor_begin_setup(&actor, b"cipher-of-the-secret", SETUP_AT).await.unwrap(); + assert!(seen.actions().await.is_empty(), "a set-up is not a change"); + assert!(matches!(meta.two_factor_confirm(ana.id, first, &*wrong(), &hashes, SETUP_AT + 1).await, Err(MetaError::TwoFactorInvalid))); + assert!(seen.actions().await.is_empty(), "a wrong first code writes nothing"); + meta.two_factor_confirm(ana.id, first, &*right(FIRST_STEP), &hashes, SETUP_AT + 2).await.unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.outcome, e.actor_user, e.actor_device, e.target_id, e.at_ms), + (AuditAction::AccountTwoFactorEnabled, AuditOutcome::Success, Some(ana.id), Some(first), Some(ana.id.0), SETUP_AT + 2) + ); + assert_eq!(e.target_kind.as_str(), "user"); + assert_eq!(details(&e), json("{}")); + + // New recovery codes: a wrong code writes nothing (the lock it can cause would), a right one the event. + let result = meta.two_factor_new_recovery_codes(&actor, &totp(&wrong()), &hashes, SETUP_AT + 3).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid))); + assert!(seen.actions().await.is_empty(), "a wrong proof on a signed-in route is not written one by one"); + meta.two_factor_new_recovery_codes(&actor, &totp(&right(FIRST_STEP + 1)), &hashes, SETUP_AT + 4).await.unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.actor_user, e.actor_device, e.target_id), + (AuditAction::AccountRecoveryCodesRegenerated, Some(ana.id), Some(first), Some(ana.id.0)) + ); + assert_eq!(details(&e), json("{}")); + + // Turning it off with a recovery code: the change and the code's use, from the same device. + meta.two_factor_disable(&actor, &Proof::Recovery(hashes[0]), SETUP_AT + 5).await.unwrap(); + assert_eq!(seen.actions().await, ["account.two_factor_disabled", "auth.recovery_code_used"]); + let used = &events_of(meta, AuditAction::AuthRecoveryCodeUsed).await[0]; + assert_eq!((used.actor_user, used.actor_device, used.target_id, used.at_ms), (Some(ana.id), Some(first), Some(ana.id.0), SETUP_AT + 5)); + assert_eq!(details(used), json("{}"), "never the code"); + let off = &events_of(meta, AuditAction::AccountTwoFactorDisabled).await[0]; + assert_eq!((off.actor_user, off.actor_device), (Some(ana.id), Some(first))); + // Off: nothing to turn off, nothing written. + assert!(matches!(meta.two_factor_disable(&actor, &totp(&right(FIRST_STEP + 2)), SETUP_AT + 6).await, Err(MetaError::Conflict(_)))); + assert!(seen.actions().await.is_empty()); +} + +/// A sign-in that made a device is `auth.login` whichever way it went (no second factor, a code, a recovery code); the +/// challenge writes nothing; a wrong second step is one aggregated `auth.second_factor_failed` row per account and minute, +/// written after the answer is decided. +async fn two_factor_sign_in_writes_login_and_counts_failures(meta: &Meta) { + let me = protected(meta, "ana").await; + let plain = user(meta, "bob").await; + let mut seen = Seen::new(meta).await; + let client = DeviceClient::new(Some("desktop"), Some("Windows"), Some("1.0")).unwrap(); + let now = SETUP_AT + 100; + + // No two-factor sign-in: a login at once. + let generation = gen_of(meta, "bob").await; + let signed = meta.sign_in(plain.id, ("$argon2id$stub", generation), "pc", &client, &token(), &new_challenge(), now).await.unwrap(); + let Some(SignIn::Device(bobs)) = signed else { panic!("{signed:?}") }; + let e = seen.one().await; + assert_eq!( + (e.action, e.outcome, e.actor_user, e.actor_device, e.target_id), + (AuditAction::AuthLogin, AuditOutcome::Success, Some(plain.id), Some(bobs), Some(*bobs.as_uuid())) + ); + assert_eq!(details(&e), json(r#"{"client_kind":"desktop","platform":"windows"}"#)); + + // Two-factor sign-in: the challenge is no login. + let generation = gen_of(meta, "ana").await; + let chal = new_challenge(); + let signed = meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &client, &token(), &chal, now).await.unwrap(); + assert!(matches!(signed, Some(SignIn::Challenge { .. }))); + assert!(seen.actions().await.is_empty(), "no one is signed in yet"); + // A wrong code: one failure row, no login, the actor unknown (nobody proved who they were). + let result = meta.complete_second_factor(&chal.id_hash, &totp(&wrong()), &token(), now + 1).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid))); + let e = seen.one().await; + assert_eq!( + (e.action, e.outcome, e.actor_user, e.actor_device, e.target_id, e.count), + (AuditAction::AuthSecondFactorFailed, AuditOutcome::Failure, None, None, Some(me.user.id.0), 1) + ); + assert_eq!(details(&e), json(r#"{"step":"totp"}"#)); + // More in the same minute make the same row count up (a recovery code that is wrong too). + let result = meta.complete_second_factor(&chal.id_hash, &Proof::Recovery(code(INVITE_CHARS).hash()), &token(), now + 2).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid))); + assert!(seen.actions().await.is_empty(), "the row was written already; only its count goes up"); + let rows = events_of(meta, AuditAction::AuthSecondFactorFailed).await; + assert_eq!((rows.len(), rows[0].count), (1, 2)); + // A right code: the login of the new device. + let done = meta.complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 1)), &token(), now + 3).await.unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.actor_user, e.actor_device, e.target_id), + (AuditAction::AuthLogin, Some(me.user.id), Some(done.device), Some(*done.device.as_uuid())) + ); + assert_eq!(details(&e), json(r#"{"client_kind":"desktop","platform":"windows"}"#)); + + // A recovery code: the login and the code's use, the device the same. + let chal = new_challenge(); + let generation = gen_of(meta, "ana").await; + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &client, &token(), &chal, now + 4).await.unwrap(); + let done = meta.complete_second_factor(&chal.id_hash, &Proof::Recovery(me.hashes()[0]), &token(), now + 5).await.unwrap(); + assert_eq!(seen.actions().await, ["auth.login", "auth.recovery_code_used"]); + let used = &events_of(meta, AuditAction::AuthRecoveryCodeUsed).await[0]; + assert_eq!((used.actor_user, used.actor_device, used.target_id), (Some(me.user.id), Some(done.device), Some(me.user.id.0))); + // The same code again is a wrong recovery code: a failure, not a use. + let chal = new_challenge(); + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &client, &token(), &chal, now + 6).await.unwrap(); + let result = meta.complete_second_factor(&chal.id_hash, &Proof::Recovery(me.hashes()[0]), &token(), now + 7).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid))); + assert_eq!(events_of(meta, AuditAction::AuthRecoveryCodeUsed).await.len(), 1); + assert_eq!(events_of(meta, AuditAction::AuthSecondFactorFailed).await[0].count, 3); +} + +/// The lock writes `auth.two_factor_locked` once, when it is set or lengthened, and never for a refusal while it holds; wrong +/// proofs on a signed-in route or an admin's confirmation count towards it without an event of their own. +async fn the_lock_writes_one_event_when_it_is_set(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + let mut seen = Seen::new(meta).await; + let mut now = SETUP_AT + 100; + for n in 1..=9 { + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid)), "wrong proof {n}"); + } + assert!(seen.actions().await.is_empty(), "nine wrong proofs are not written one by one"); + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid))); + let e = seen.one().await; + assert_eq!( + (e.action, e.outcome, e.actor_user, e.actor_device, e.target_id, e.at_ms), + (AuditAction::AuthTwoFactorLocked, AuditOutcome::Success, None, None, Some(id.0), now) + ); + assert_eq!(details(&e), json("{}")); + // Refused while it holds, by every path: no further event. + for offset in 1..4 { + let locked = confirm_with(meta, id, Some(FIRST_STEP + 1), now + offset).await; + assert!(matches!(locked, Err(MetaError::TwoFactorLocked { .. })), "{locked:?}"); + } + assert!(matches!(meta.two_factor_disable(&me.actor(), &Proof::Recovery(me.hashes()[1]), now + 5).await, Err(MetaError::TwoFactorLocked { .. }))); + assert!(seen.actions().await.is_empty(), "a refusal under the lock writes nothing"); + // Over, one more wrong proof sets a longer lock: a new event. + now += 15 * MINUTE + 1; + assert!(matches!(confirm_with(meta, id, None, now).await, Err(MetaError::TwoFactorInvalid))); + let e = seen.one().await; + assert_eq!((e.action, e.at_ms), (AuditAction::AuthTwoFactorLocked, now)); + assert_eq!(events_of(meta, AuditAction::AuthTwoFactorLocked).await.len(), 2); + assert!(events_of(meta, AuditAction::AuthSecondFactorFailed).await.is_empty(), "these are not sign-in steps"); +} + +/// The second factor at a join: a wrong one is one failure row and the invite stays; a recovery code used for it is written +/// with the redemption; a reset code made with `reset_two_factor` takes it away and the redemption says so, by the account that +/// redeemed it (no admin acts at that moment). +async fn a_join_writes_its_two_factor_events(meta: &Meta) { + let me = protected(meta, "ana").await; + let id = me.user.id; + let (_, admin) = admin_actor(meta, "root").await; + let mut seen = Seen::new(meta).await; + let now = SETUP_AT + 100; + + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), now, INVITE_TTL_MS).await.unwrap(); + assert!(matches!(redeem_with_factor(meta, &invite.hash(), None, now + 1).await, Err(MetaError::TwoFactorRequired))); + assert!(seen.actions().await.is_empty(), "asking for the factor writes nothing"); + assert!(matches!(redeem_with_factor(meta, &invite.hash(), Some(&totp(&wrong())), now + 2).await, Err(MetaError::TwoFactorInvalid))); + let e = seen.one().await; + assert_eq!( + (e.action, e.outcome, e.actor_user, e.target_id, e.count), + (AuditAction::AuthSecondFactorFailed, AuditOutcome::Failure, None, Some(id.0), 1) + ); + assert_eq!(details(&e), json(r#"{"step":"totp"}"#)); + // With a recovery code: the redemption and the code's use, by the new device. + let device = redeem_with_factor(meta, &invite.hash(), Some(&Proof::Recovery(me.hashes()[0])), now + 3).await.unwrap(); + assert_eq!(seen.actions().await, ["auth.recovery_code_used", "join.invite_redeemed"]); + let used = &events_of(meta, AuditAction::AuthRecoveryCodeUsed).await[0]; + assert_eq!((used.actor_user, used.actor_device), (Some(id), Some(device))); + + // A reset code without the removal of two-factor: the right code is needed; the event has no such detail. + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&admin, id, &reset.hash(), false, now + 4, RESET_TTL_MS).await.unwrap(); + assert_eq!(seen.actions().await, ["admin.password_reset_issued"]); + let redeemed = meta + .redeem_with_second_factor( + &reset.hash(), + "new pc", + &DeviceClient::default(), + &token(), + Some("$argon2id$new"), + Some(&totp(&right(FIRST_STEP + 1))), + now + 5, + ) + .await + .unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device), (AuditAction::JoinResetRedeemed, Some(id), Some(redeemed.device))); + assert_eq!(details(&e), json(&format!(r#"{{"revoked":{}}}"#, redeemed.revoked.len())), "two-factor sign-in is still on"); + assert!(meta.two_factor_enabled(id).await.unwrap()); + + // A reset code made with `reset_two_factor`: it takes the second factor away and the event says so. + let reset = code(INVITE_CHARS); + meta.admin_issue_reset(&admin, id, &reset.hash(), true, now + 6, RESET_TTL_MS).await.unwrap(); + assert_eq!(seen.actions().await, ["admin.password_reset_issued"]); + let redeemed = meta + .redeem_with_second_factor(&reset.hash(), "newer pc", &DeviceClient::default(), &token(), Some("$argon2id$newer"), None, now + 7) + .await + .unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.actor_user, e.actor_device, e.target_id), + (AuditAction::JoinResetRedeemed, Some(id), Some(redeemed.device), Some(*redeemed.device.as_uuid())) + ); + assert_eq!(details(&e), json(&format!(r#"{{"revoked":{},"two_factor_removed":true}}"#, redeemed.revoked.len()))); + assert!(!meta.two_factor_enabled(id).await.unwrap()); + assert!(events_of(meta, AuditAction::AdminTwoFactorReset).await.is_empty(), "no admin is made up for it"); +} + +/// An admin's reset of another account's two-factor sign-in and the command line's each write their event; the admin's +/// confirmation by recovery code writes the code's use, for the admin's account, even if the change it confirms fails. +async fn resets_and_confirmations_write_their_events(meta: &Meta) { + let (_, admin) = admin_actor(meta, "root").await; + let member = protected(meta, "member").await; + let other = protected(meta, "other").await; + let mut seen = Seen::new(meta).await; + let now = SETUP_AT + 100; + + let revoked = meta.admin_reset_two_factor(&admin, member.user.id, now).await.unwrap(); + let e = seen.one().await; + assert_eq!( + (e.action, e.outcome, e.actor_user, e.actor_device, e.target_id), + (AuditAction::AdminTwoFactorReset, AuditOutcome::Success, Some(admin.user), Some(device_of(&admin)), Some(member.user.id.0)) + ); + assert_eq!(details(&e), json("{}")); + assert_eq!(revoked.len(), 1); + assert!(matches!(meta.admin_reset_two_factor(&admin, member.user.id, now + 1).await, Err(MetaError::Conflict(_)))); + assert!(seen.actions().await.is_empty(), "nothing to reset: nothing written"); + + let revoked = meta.reset_two_factor(other.user.id, now + 2).await.unwrap(); + let e = seen.one().await; + assert_eq!((e.action, e.actor_user, e.actor_device, e.target_id), (AuditAction::CliTwoFactorReset, None, None, Some(other.user.id.0))); + assert_eq!(details(&e), json(&format!(r#"{{"revoked":{}}}"#, revoked.len()))); + assert!(matches!(meta.reset_two_factor(other.user.id, now + 3).await, Err(MetaError::Conflict(_)))); + assert!(seen.actions().await.is_empty()); + + // An account's confirmation by recovery code, as an admin gives it: the code's use, no device. + let bea = protected(meta, "bea").await; + let mut seen = Seen::new(meta).await; + assert!(meta.confirm_second_factor(bea.user.id, Some(&Proof::Recovery(bea.hashes()[3])), now + 4).await.unwrap()); + let e = seen.one().await; + assert_eq!( + (e.action, e.actor_user, e.actor_device, e.target_id), + (AuditAction::AuthRecoveryCodeUsed, Some(bea.user.id), None, Some(bea.user.id.0)) + ); + // A wrong proof, or the same code again: nothing written. + assert!(matches!(confirm_with(meta, bea.user.id, None, now + 5).await, Err(MetaError::TwoFactorInvalid))); + assert!(matches!( + meta.confirm_second_factor(bea.user.id, Some(&Proof::Recovery(bea.hashes()[3])), now + 6).await, + Err(MetaError::TwoFactorInvalid) + )); + assert!(seen.actions().await.is_empty()); +} + +/// An error in the log fails every two-factor change and changes nothing; a wrong second step is still answered as wrong, since +/// its event is written after the answer is decided and only reported. +async fn an_error_in_the_log_fails_every_two_factor_change(url: &str) { + sqlx::any::install_default_drivers(); + let meta = Meta::open(url).await.unwrap(); + let me = protected(&meta, "ana").await; + let (_, admin) = admin_actor(&meta, "root").await; + let setup = user(&meta, "setup").await; + let setup_device = device(&meta, &setup).await; + let setup_actor = DeviceActor { user: setup.id, device: setup_device, password_hash: "$argon2id$stub".to_owned() }; + meta.two_factor_begin_setup(&setup_actor, b"cipher", SETUP_AT).await.unwrap(); + let plain = user(&meta, "plain").await; + let locker = protected(&meta, "locker").await; + let events_before = all_events(&meta).await.len(); + let now = SETUP_AT + 100; + let generation = gen_of(&meta, "ana").await; + let chal = new_challenge(); + meta.sign_in(me.user.id, ("$argon2id$stub", generation), "pc", &DeviceClient::default(), &token(), &chal, now).await.unwrap(); + let invite = code(INVITE_CHARS); + meta.create_invite("ana", &invite.hash(), now, INVITE_TTL_MS).await.unwrap(); + + break_audit(url).await; + let is_db = |r: Result<(), MetaError>| assert!(matches!(r, Err(MetaError::Db(_))), "{r:?}"); + let hashes = me.hashes(); + is_db(meta.two_factor_confirm(setup.id, setup_device, &*right(FIRST_STEP), &hashes, now).await.map(|_| ())); + is_db(meta.two_factor_new_recovery_codes(&me.actor(), &totp(&right(FIRST_STEP + 1)), &hashes, now).await); + is_db(meta.two_factor_disable(&me.actor(), &Proof::Recovery(hashes[0]), now).await.map(|_| ())); + is_db(meta.confirm_second_factor(me.user.id, Some(&Proof::Recovery(hashes[1])), now).await.map(|_| ())); + is_db(meta.complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 1)), &token(), now).await.map(|_| ())); + is_db(redeem_with_factor(&meta, &invite.hash(), Some(&Proof::Recovery(hashes[2])), now).await.map(|_| ())); + is_db(meta.sign_in(plain.id, ("$argon2id$stub", 0), "pc", &DeviceClient::default(), &token(), &new_challenge(), now).await.map(|_| ())); + is_db(meta.admin_reset_two_factor(&admin, me.user.id, now).await.map(|_| ())); + is_db(meta.reset_two_factor(me.user.id, now).await.map(|_| ())); + // A wrong second step: the answer is the same, whether or not the log works. + let result = meta.complete_second_factor(&chal.id_hash, &totp(&wrong()), &token(), now).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid)), "{result:?}"); + // The lock is set by the tenth wrong proof although its event can't be written: failing closed there would let a fault in + // the log undo the lock. The answer is the same and the counters and the lock are committed. + for n in 1..=10 { + let result = confirm_with(&meta, locker.user.id, None, now).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid)), "wrong proof {n}: {result:?}"); + } + assert_eq!(meta.two_factor_locked_for(locker.user.id, now).await.unwrap(), Some(15 * MINUTE), "the lock holds"); + repair_audit(url).await; + + // Nothing was changed by any of it: still on, no code used, nobody signed out, the set-up still open. + assert_eq!(all_events(&meta).await.len(), events_before, "no event, no change (the lock's event was lost, only reported)"); + assert!(meta.two_factor_enabled(me.user.id).await.unwrap()); + assert_eq!(meta.two_factor_info(me.user.id, now).await.unwrap().recovery_codes_left, 10); + assert_eq!(live_devices(&meta, &me.user).await, vec![me.first]); + assert!(meta.two_factor_info(setup.id, now).await.unwrap().pending); + assert!(!meta.two_factor_enabled(setup.id).await.unwrap()); + assert_eq!(meta.invite_needs_password(&invite.hash(), now).await.unwrap(), Some(false), "the invite is still there"); + // And the same changes work once the log does. + meta.two_factor_new_recovery_codes(&me.actor(), &totp(&right(FIRST_STEP + 2)), &hashes, now + 1).await.unwrap(); +} + +#[tokio::test] +async fn an_error_in_the_log_fails_every_two_factor_change_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + an_error_in_the_log_fails_every_two_factor_change(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn an_error_in_the_log_fails_every_two_factor_change_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + an_error_in_the_log_fails_every_two_factor_change(&url).await; +} + +// ---- web sessions (migration 0011) ---- + +const WEB_IDLE_MS: i64 = 8 * 60 * MINUTE; +const WEB_ABSOLUTE_MS: i64 = 7 * 24 * 60 * MINUTE; + +fn new_session() -> NewWebSession { + NewWebSession { id_hash: token(), absolute_ms: WEB_ABSOLUTE_MS } +} + +/// An administrator with a password hash the web sign-ins below check against. +async fn web_admin(meta: &Meta, username: &str) -> UserRecord { + meta.create_user(username, ADMIN_HASH, Role::Admin, NOW).await.unwrap() +} + +/// A web sign-in of `user` as the handler would make it after verifying the password. +async fn web_sign_in(meta: &Meta, user: &UserRecord, session: &NewWebSession, now: i64) -> Option { + let generation = gen_of(meta, &user.username).await; + meta.web_sign_in(user.id, (&user.password_hash, generation), &new_challenge(), session, now).await.unwrap() +} + +async fn session_is_valid(meta: &Meta, session: &NewWebSession, now: i64) -> bool { + meta.web_session(&session.id_hash, now, WEB_IDLE_MS).await.unwrap().is_some() +} + +/// Only administrators get a session; it is found by its token hash, ends by idle time, by its absolute lifetime and by +/// logout, and the sign-in is written to the log without a device. +async fn web_sessions_are_made_looked_up_expire_and_log_out(meta: &Meta) { + let root = web_admin(meta, "root").await; + let member = user(meta, "ana").await; + // A member with the right password gets nothing, the same as a wrong generation or a wrong hash. + assert_eq!(web_sign_in(meta, &member, &new_session(), NOW).await, None); + let stale = meta.web_sign_in(root.id, (&root.password_hash, 99), &new_challenge(), &new_session(), NOW).await.unwrap(); + assert_eq!(stale, None, "the password was checked against another generation"); + let wrong_hash = meta.web_sign_in(root.id, ("$argon2id$other", root.auth_gen), &new_challenge(), &new_session(), NOW).await.unwrap(); + assert_eq!(wrong_hash, None); + assert_eq!(meta.web_sessions_open(member.id, NOW, WEB_IDLE_MS).await.unwrap(), 0); + + let session = new_session(); + assert_eq!(web_sign_in(meta, &root, &session, NOW).await, Some(WebSignIn::Session)); + let found = meta.web_session(&session.id_hash, NOW + 1, WEB_IDLE_MS).await.unwrap().unwrap(); + assert_eq!((found.user, found.username.as_str(), found.role, found.mfa_verified_ms), (root.id, "root", Role::Admin, None)); + assert_eq!((found.created_ms, found.last_seen_ms, found.absolute_expires_ms), (NOW, NOW, NOW + WEB_ABSOLUTE_MS)); + assert!(meta.web_session(&token(), NOW + 1, WEB_IDLE_MS).await.unwrap().is_none(), "an unknown token"); + let events = all_events(meta).await; + let login = events.iter().find(|e| e.action == AuditAction::AuthLogin).unwrap(); + assert_eq!((login.actor_user, login.actor_device), (Some(root.id), None)); + assert_eq!(details(login), json(r#"{"client_kind":"web"}"#)); + assert!(DeviceClient::new(Some("web"), None, None).is_err(), "devices cannot claim web"); + + // Idle: valid just inside the idle time, over at it, and a use moves it. + assert!(session_is_valid(meta, &session, NOW + WEB_IDLE_MS - 1).await); + assert!(!session_is_valid(meta, &session, NOW + WEB_IDLE_MS).await); + meta.web_session_seen(&session.id_hash, NOW + WEB_IDLE_MS - 1).await.unwrap(); + assert!(session_is_valid(meta, &session, NOW + 2 * WEB_IDLE_MS - 2).await, "a use starts the idle time again"); + // The time never moves back. + meta.web_session_seen(&session.id_hash, NOW + 5).await.unwrap(); + assert_eq!(meta.web_session(&session.id_hash, NOW + WEB_IDLE_MS, WEB_IDLE_MS).await.unwrap().unwrap().last_seen_ms, NOW + WEB_IDLE_MS - 1); + + // Absolute: keeping it busy does not help. + let busy = new_session(); + assert_eq!(web_sign_in(meta, &root, &busy, NOW).await, Some(WebSignIn::Session)); + let mut at = NOW; + while at + WEB_IDLE_MS / 2 < NOW + WEB_ABSOLUTE_MS { + at += WEB_IDLE_MS / 2; + meta.web_session_seen(&busy.id_hash, at).await.unwrap(); + assert!(session_is_valid(meta, &busy, at).await); + } + assert!(!session_is_valid(meta, &busy, NOW + WEB_ABSOLUTE_MS).await, "the absolute lifetime ends it"); + + // Logout ends it at once, and a second logout, or one of an unknown session, is no error. + let out = new_session(); + assert_eq!(web_sign_in(meta, &root, &out, NOW + 10).await, Some(WebSignIn::Session)); + assert!(session_is_valid(meta, &out, NOW + 11).await); + meta.web_logout(&out.id_hash, NOW + 12).await.unwrap(); + meta.web_logout(&out.id_hash, NOW + 13).await.unwrap(); + meta.web_logout(&token(), NOW + 13).await.unwrap(); + assert!(!session_is_valid(meta, &out, NOW + 14).await); + meta.web_session_seen(&out.id_hash, NOW + 15).await.unwrap(); + assert!(!session_is_valid(meta, &out, NOW + 16).await, "a use does not revive a logged-out session"); +} + +/// An administrator with two-factor sign-in on, and a session made with it (the sign-in's second step). `step` is the time +/// step of the code that made the session. +async fn web_admin_with_factor(meta: &Meta, username: &str, step: i64, now: i64) -> (UserRecord, DeviceId, NewWebSession) { + let me = web_admin(meta, username).await; + let first = device(meta, &me).await; + let hashes: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + let setup = DeviceActor { user: me.id, device: first, password_hash: ADMIN_HASH.to_owned() }; + meta.two_factor_begin_setup(&setup, b"cipher-of-the-secret", now).await.unwrap(); + meta.two_factor_confirm(me.id, first, &*right(step), &hashes, now + 1).await.unwrap(); + let (chal, session) = (new_challenge(), new_session()); + let generation = gen_of(meta, username).await; + let made = meta.web_sign_in(me.id, (ADMIN_HASH, generation), &chal, &session, now + 2).await.unwrap(); + assert!(matches!(made, Some(WebSignIn::Challenge { .. }))); + meta.web_complete_second_factor(&chal.id_hash, &totp(&right(step + 1)), &session, now + 3).await.unwrap(); + assert!(session_is_valid(meta, &session, now + 4).await); + (me, first, session) +} + +/// Every change that raises `users.auth_gen` ends the account's sessions: a password change and reset, a disable, an enable +/// after it, a removal, and two-factor sign-in turned on, off, or reset by an admin or the command line. +async fn a_web_session_ends_with_every_change_of_the_accounts_generation(meta: &Meta) { + // The acting admin has two-factor sign-in, which resetting another administrator's needs. + let (boss, plain) = admin_actor(meta, "boss").await; + let boss_device = device_of(&plain); + let hashes: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + let setup = DeviceActor { user: boss.id, device: boss_device, password_hash: ADMIN_HASH.to_owned() }; + meta.two_factor_begin_setup(&setup, b"cipher-of-the-secret", NOW).await.unwrap(); + meta.two_factor_confirm(boss.id, boss_device, &*right(FIRST_STEP), &hashes, NOW + 1).await.unwrap(); + let actor = AdminActor { two_factor: true, ..plain }; + let mut now = NOW + 10; + + // A password reset by the command line. + let root = web_admin(meta, "root").await; + let s = new_session(); + assert_eq!(web_sign_in(meta, &root, &s, now).await, Some(WebSignIn::Session)); + meta.reset_password(root.id, "$argon2id$new", true, now + 1).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 2).await, "a password reset ends the session"); + now += 10; + + // A password change by a device. + let (ana, ana_actor) = admin_actor(meta, "ana").await; + let s = new_session(); + assert_eq!(web_sign_in(meta, &ana, &s, now).await, Some(WebSignIn::Session)); + meta.change_password(ana.id, ADMIN_HASH, "$argon2id$changed", device_of(&ana_actor), now + 1).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 2).await, "a password change ends the session"); + now += 10; + + // Disable ends it, and an enable afterwards does not bring it back. + let target = web_admin(meta, "target").await; + let s = new_session(); + assert_eq!(web_sign_in(meta, &target, &s, now).await, Some(WebSignIn::Session)); + meta.admin_disable_user(&actor, target.id, now + 1).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 2).await, "a disabled account's session is over"); + meta.admin_enable_user(&actor, target.id, now + 3).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 4).await, "enabling does not revive it"); + now += 10; + + // A removal. + let doomed = web_admin(meta, "doomed").await; + let s = new_session(); + assert_eq!(web_sign_in(meta, &doomed, &s, now).await, Some(WebSignIn::Session)); + meta.admin_remove_user(&actor, doomed.id, now + 1).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 2).await, "a removed account's session is over"); + now += 10; + + // Two-factor sign-in turned on: a session from before it is over (a set-up that is not confirmed changes nothing). + let guarded = web_admin(meta, "guarded").await; + let guarded_device = device(meta, &guarded).await; + let s = new_session(); + assert_eq!(web_sign_in(meta, &guarded, &s, now).await, Some(WebSignIn::Session)); + let setup = DeviceActor { user: guarded.id, device: guarded_device, password_hash: ADMIN_HASH.to_owned() }; + meta.two_factor_begin_setup(&setup, b"cipher-of-the-secret", now + 1).await.unwrap(); + assert!(session_is_valid(meta, &s, now + 2).await, "an open set-up is no change"); + let hashes: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + meta.two_factor_confirm(guarded.id, guarded_device, &*right(FIRST_STEP), &hashes, now + 3).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 4).await, "turning two-factor sign-in on ends the session"); + now += 10; + + // Turned off by the account itself. + let (own, own_device, s) = web_admin_with_factor(meta, "own", FIRST_STEP, now).await; + let setup = DeviceActor { user: own.id, device: own_device, password_hash: ADMIN_HASH.to_owned() }; + meta.two_factor_disable(&setup, &totp(&right(FIRST_STEP + 5)), now + 10).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 11).await, "turning two-factor sign-in off ends the session"); + now += 20; + + // Reset by an admin, and by the command line. + let (by_admin, _, s) = web_admin_with_factor(meta, "byadmin", FIRST_STEP, now).await; + meta.admin_reset_two_factor(&actor, by_admin.id, now + 10).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 11).await, "an admin's reset ends the session"); + now += 20; + let (by_cli, _, s) = web_admin_with_factor(meta, "bycli", FIRST_STEP, now).await; + meta.reset_two_factor(by_cli.id, now + 10).await.unwrap(); + assert!(!session_is_valid(meta, &s, now + 11).await, "the command line's reset ends the session"); + + // Another account's session is untouched by all of that. + let bystander = web_admin(meta, "bystander").await; + let s = new_session(); + assert_eq!(web_sign_in(meta, &bystander, &s, now + 30).await, Some(WebSignIn::Session)); + meta.reset_password(root.id, "$argon2id$again", true, now + 31).await.unwrap(); + assert!(session_is_valid(meta, &s, now + 32).await); +} + +/// An admin with two-factor sign-in gets a challenge of kind `web`, which no device sign-in can use and which makes no +/// device, and completes it with a code, once. The second factor's counters and lock are the device sign-in's. +async fn a_web_sign_in_with_two_factor_needs_its_own_challenge(meta: &Meta) { + let me = web_admin(meta, "root").await; + let first = device(meta, &me).await; + let hashes: Vec<[u8; 32]> = (0..RECOVERY_CODES).map(|_| code(INVITE_CHARS).hash()).collect(); + let setup = DeviceActor { user: me.id, device: first, password_hash: ADMIN_HASH.to_owned() }; + meta.two_factor_begin_setup(&setup, b"cipher-of-the-secret", SETUP_AT).await.unwrap(); + meta.two_factor_confirm(me.id, first, &*right(FIRST_STEP), &hashes, SETUP_AT + 1).await.unwrap(); + + let generation = gen_of(meta, "root").await; + let chal = new_challenge(); + let session = new_session(); + let made = meta.web_sign_in(me.id, (ADMIN_HASH, generation), &chal, &session, SETUP_AT + 10).await.unwrap(); + assert_eq!(made, Some(WebSignIn::Challenge { expires_ms: SETUP_AT + 10 + 5 * MINUTE })); + assert!(!session_is_valid(meta, &session, SETUP_AT + 11).await, "no session without the second factor"); + assert_eq!(live_devices(meta, &me).await, vec![first], "and no device"); + // The challenge is seen as a web one only. + assert_eq!(meta.web_challenge_owner(&chal.id_hash, SETUP_AT + 11).await.unwrap(), Some((me.id, "root".to_owned()))); + assert_eq!(meta.challenge_owner(&chal.id_hash, SETUP_AT + 11).await.unwrap(), None, "a device sign-in does not see it"); + let as_device = meta.complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 1)), &token(), SETUP_AT + 12).await; + assert!(matches!(as_device, Err(MetaError::ChallengeGone)), "{as_device:?}"); + assert_eq!(live_devices(meta, &me).await, vec![first], "still no new device, and the code was not used"); + // And the other way round. + let device_chal = new_challenge(); + let made = meta.sign_in(me.id, (ADMIN_HASH, generation), "pc", &DeviceClient::default(), &token(), &device_chal, SETUP_AT + 13).await.unwrap(); + assert!(matches!(made, Some(SignIn::Challenge { .. }))); + let as_web = meta.web_complete_second_factor(&device_chal.id_hash, &totp(&right(FIRST_STEP + 1)), &session, SETUP_AT + 14).await; + assert!(matches!(as_web, Err(MetaError::ChallengeGone)), "{as_web:?}"); + assert!(!session_is_valid(meta, &session, SETUP_AT + 15).await); + + // Wrong codes are counted (four, and the fifth would delete the challenge), then the right one makes the session, once. + for _ in 0..4 { + let result = meta.web_complete_second_factor(&chal.id_hash, &totp(&wrong()), &session, SETUP_AT + 20).await; + assert!(matches!(result, Err(MetaError::TwoFactorInvalid)), "{result:?}"); + } + let done = meta.web_complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 1)), &session, SETUP_AT + 21).await.unwrap(); + assert_eq!(done, me.id); + let found = meta.web_session(&session.id_hash, SETUP_AT + 22, WEB_IDLE_MS).await.unwrap().unwrap(); + assert_eq!(found.mfa_verified_ms, Some(SETUP_AT + 21), "the session says when the second factor was given"); + assert_eq!(live_devices(meta, &me).await, vec![first], "a web sign-in makes no device"); + let login = all_events(meta).await.into_iter().find(|event| event.action == AuditAction::AuthLogin).unwrap(); + assert_eq!(details(&login), json(r#"{"client_kind":"web"}"#)); + let failed = all_events(meta).await.into_iter().find(|event| event.action == AuditAction::AuthSecondFactorFailed).unwrap(); + assert_eq!(details(&failed), json(r#"{"client_kind":"web","step":"totp"}"#)); + let again = meta.web_complete_second_factor(&chal.id_hash, &totp(&right(FIRST_STEP + 2)), &new_session(), SETUP_AT + 23).await; + assert!(matches!(again, Err(MetaError::ChallengeGone)), "once: {again:?}"); + + // The same time step is not accepted again (the counter is the device sign-in's and the admin confirmation's). + let generation = gen_of(meta, "root").await; + let replay = new_challenge(); + meta.web_sign_in(me.id, (ADMIN_HASH, generation), &replay, &new_session(), SETUP_AT + 24).await.unwrap(); + let replayed = meta.web_complete_second_factor(&replay.id_hash, &totp(&right(FIRST_STEP + 1)), &new_session(), SETUP_AT + 25).await; + assert!(matches!(replayed, Err(MetaError::TwoFactorInvalid)), "{replayed:?}"); + // Wrong proofs lock the second factor for every way of using it: a device sign-in's step too. + for n in 0..12 { + let _ = confirm_with(meta, me.id, None, SETUP_AT + 30 + n).await; + } + let locked = meta.web_complete_second_factor(&replay.id_hash, &totp(&right(FIRST_STEP + 9)), &new_session(), SETUP_AT + 50).await; + assert!(matches!(locked, Err(MetaError::TwoFactorLocked { .. })), "{locked:?}"); + let locked = meta.complete_second_factor(&device_chal.id_hash, &totp(&right(FIRST_STEP + 9)), &token(), SETUP_AT + 51).await; + assert!(matches!(locked, Err(MetaError::TwoFactorLocked { .. })), "{locked:?}"); +} + +/// Pruning deletes sessions that are over (logged out, expired, idle), never a valid one, and at most the batch asked for. +async fn web_sessions_are_pruned_in_bounded_batches(meta: &Meta) { + let root = web_admin(meta, "root").await; + let late = NOW + WEB_ABSOLUTE_MS - 2 * WEB_IDLE_MS; + let now = NOW + WEB_ABSOLUTE_MS - WEB_IDLE_MS; + let keep = new_session(); + assert_eq!(web_sign_in(meta, &root, &keep, now - MINUTE).await, Some(WebSignIn::Session)); + for n in 0..5 { + assert_eq!(web_sign_in(meta, &root, &new_session(), NOW + n).await, Some(WebSignIn::Session)); + } + let out = new_session(); + assert_eq!(web_sign_in(meta, &root, &out, late).await, Some(WebSignIn::Session)); + meta.web_logout(&out.id_hash, late + 1).await.unwrap(); + + assert_eq!(meta.web_sessions_count_over(now, WEB_IDLE_MS).await.unwrap(), 6, "five idle ones and the logged-out one"); + assert_eq!(meta.web_sessions_prune(now, WEB_IDLE_MS, 4).await.unwrap(), 4, "a batch is bounded"); + assert_eq!(meta.web_sessions_count_over(now, WEB_IDLE_MS).await.unwrap(), 2); + assert_eq!(meta.web_sessions_prune(now, WEB_IDLE_MS, 4).await.unwrap(), 2); + assert_eq!(meta.web_sessions_prune(now, WEB_IDLE_MS, 4).await.unwrap(), 0, "nothing is left to delete"); + assert!(session_is_valid(meta, &keep, now).await, "a valid session is never pruned"); + // Past the absolute lifetime the last one goes too. + let end = NOW + 2 * WEB_ABSOLUTE_MS; + assert_eq!(meta.web_sessions_prune(end, WEB_IDLE_MS, 100).await.unwrap(), 1); + assert_eq!(meta.web_sessions_count_over(end, WEB_IDLE_MS).await.unwrap(), 0); +} + +/// An admin change made on a browser session is audited without a device, and a session that was logged out, or whose account +/// changed, can no longer make one. +async fn a_web_actor_changes_accounts_until_its_session_ends(meta: &Meta) { + // The store judges the session by its own clock, which follows the time each call below is made at. + let clock = std::sync::Arc::new(std::sync::atomic::AtomicI64::new(NOW)); + let ticking = clock.clone(); + let meta = &meta.clone().with_clock(std::sync::Arc::new(move || ticking.load(std::sync::atomic::Ordering::SeqCst))); + let at_ms = |t: i64| { + clock.store(t, std::sync::atomic::Ordering::SeqCst); + t + }; + let root = web_admin(meta, "root").await; + let session = new_session(); + assert_eq!(web_sign_in(meta, &root, &session, NOW).await, Some(WebSignIn::Session)); + let actor = AdminActor { + user: root.id, + via: AdminVia::Web { id_hash: session.id_hash, idle_ms: WEB_IDLE_MS }, + password_hash: ADMIN_HASH.to_owned(), + two_factor: false, + }; + assert!(!format!("{actor:?}").contains(&format!("{:?}", session.id_hash)), "the session's hash is not in Debug"); + let ana = user(meta, "ana").await; + meta.admin_update_user(&actor, ana.id, None, Some(Some(MIN_QUOTA_BYTES)), at_ms(NOW + 1)).await.unwrap(); + let events = all_events(meta).await; + let changed = events.iter().find(|e| e.action == AuditAction::AdminQuotaChanged).unwrap(); + assert_eq!((changed.actor_user, changed.actor_device), (Some(root.id), None), "no device acted"); + // Another account's session is not this admin's. + let other = web_admin(meta, "other").await; + let theirs = new_session(); + assert_eq!(web_sign_in(meta, &other, &theirs, NOW).await, Some(WebSignIn::Session)); + let borrowed = AdminActor { via: AdminVia::Web { id_hash: theirs.id_hash, idle_ms: WEB_IDLE_MS }, ..actor.clone() }; + assert!(matches!(meta.admin_update_user(&borrowed, ana.id, None, Some(None), at_ms(NOW + 2)).await, Err(MetaError::DeviceRevoked))); + // A session that does not exist. + let unknown = AdminActor { via: AdminVia::Web { id_hash: token(), idle_ms: WEB_IDLE_MS }, ..actor.clone() }; + assert!(matches!(meta.admin_update_user(&unknown, ana.id, None, Some(None), at_ms(NOW + 3)).await, Err(MetaError::DeviceRevoked))); + // Logged out: refused, and nothing changed. + meta.web_logout(&session.id_hash, NOW + 4).await.unwrap(); + assert!(matches!(meta.admin_update_user(&actor, ana.id, None, Some(None), at_ms(NOW + 5)).await, Err(MetaError::DeviceRevoked))); + assert_eq!(admin_view(meta, ana.id).await.quota_bytes, Some(MIN_QUOTA_BYTES)); + // A session that expired between the request's start and the change (the handler's check was passed, the transaction runs + // later): refused by the idle time and by the absolute lifetime, and nothing changed. + let slow = new_session(); + assert_eq!(web_sign_in(meta, &root, &slow, NOW + 20).await, Some(WebSignIn::Session)); + let slow_actor = AdminActor { via: AdminVia::Web { id_hash: slow.id_hash, idle_ms: WEB_IDLE_MS }, ..actor.clone() }; + let idle = meta.admin_update_user(&slow_actor, ana.id, None, Some(None), at_ms(NOW + 20 + WEB_IDLE_MS)).await; + assert!(matches!(idle, Err(MetaError::DeviceRevoked)), "idle: {idle:?}"); + meta.admin_update_user(&slow_actor, ana.id, None, Some(None), at_ms(NOW + 20 + WEB_IDLE_MS - 1)).await.unwrap(); + let busy = new_session(); + assert_eq!(web_sign_in(meta, &root, &busy, NOW + 30).await, Some(WebSignIn::Session)); + let busy_actor = AdminActor { via: AdminVia::Web { id_hash: busy.id_hash, idle_ms: WEB_IDLE_MS }, ..actor.clone() }; + let mut at = NOW + 30; + while at + WEB_IDLE_MS / 2 < NOW + 30 + WEB_ABSOLUTE_MS { + at += WEB_IDLE_MS / 2; + meta.web_session_seen(&busy.id_hash, at).await.unwrap(); + } + meta.admin_update_user(&busy_actor, ana.id, None, Some(None), at_ms(at)).await.unwrap(); + let absolute = meta.admin_update_user(&busy_actor, ana.id, None, Some(Some(MIN_QUOTA_BYTES)), at_ms(NOW + 30 + WEB_ABSOLUTE_MS)).await; + assert!(matches!(absolute, Err(MetaError::DeviceRevoked)), "absolute: {absolute:?}"); + assert_eq!(admin_view(meta, ana.id).await.quota_bytes, None); + // A session of a changed account: the generation moved on. + let late = new_session(); + assert_eq!(web_sign_in(meta, &root, &late, NOW + 6).await, Some(WebSignIn::Session)); + let late_actor = AdminActor { via: AdminVia::Web { id_hash: late.id_hash, idle_ms: WEB_IDLE_MS }, ..actor.clone() }; + meta.admin_update_user(&late_actor, ana.id, None, Some(None), at_ms(NOW + 7)).await.unwrap(); + meta.reset_password(root.id, "$argon2id$new", true, NOW + 8).await.unwrap(); + let after = meta.admin_update_user(&late_actor, ana.id, None, Some(Some(MIN_QUOTA_BYTES)), at_ms(NOW + 9)).await; + assert!(matches!(after, Err(MetaError::DeviceRevoked)), "{after:?}"); + assert_eq!(admin_view(meta, ana.id).await.quota_bytes, None); +} + +// ---- the migration from 0010 to 0011 ---- + +/// Drops what migration 0011 added and forgets it ran, which leaves the database as 0010 left it (with its rows). +async fn back_to_0010(url: &str) { + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("DROP TABLE web_sessions").execute(&mut conn).await.unwrap(); + sqlx::query("DELETE FROM schema_version WHERE version >= 11").execute(&mut conn).await.unwrap(); +} + +/// A database from the release before the web admin keeps its accounts, devices and audit log, gets an empty session table +/// that works (also after the migration is rolled back and run again), and a database newer than the program is still refused. +async fn migrating_from_0010_adds_the_web_sessions(url: &str) { + sqlx::any::install_default_drivers(); + let (root, old_token, events) = { + let meta = Meta::open(url).await.unwrap(); + let root = web_admin(&meta, "root").await; + let old_token = token(); + meta.create_device(root.id, "old pc", &old_token, NOW).await.unwrap(); + meta.sign_in_device(root.id, (&root.password_hash, root.auth_gen), "pc", &DeviceClient::default(), &token(), NOW).await.unwrap().unwrap(); + let events = all_events(&meta).await.len(); + (root, old_token, events) + }; + back_to_0010(url).await; + assert_eq!(schema_version(url).await, 10); + + let meta = Meta::open(url).await.unwrap(); + assert_eq!(schema_version(url).await, 11, "web sessions are migration 11"); + assert_eq!(meta.device_by_token(&old_token).await.unwrap().map(|a| a.user), Some(root.id)); + assert_eq!(all_events(&meta).await.len(), events, "the audit rows are kept"); + let session = new_session(); + assert_eq!(web_sign_in(&meta, &root, &session, NOW + 5).await, Some(WebSignIn::Session)); + assert!(session_is_valid(&meta, &session, NOW + 6).await); + + // The rollback: the table goes, the version is forgotten, and opening again makes the table again. + drop(meta); + back_to_0010(url).await; + assert_eq!(schema_version(url).await, 10); + let meta = Meta::open(url).await.unwrap(); + assert_eq!(schema_version(url).await, 11); + assert!(!session_is_valid(&meta, &session, NOW + 6).await, "the table is new and empty"); + drop(meta); + + // A database from a newer Craft Cloud is still refused. + use sqlx::Connection; + let mut conn = sqlx::AnyConnection::connect(url).await.unwrap(); + sqlx::query("INSERT INTO schema_version (version) VALUES (12)").execute(&mut conn).await.unwrap(); + drop(conn); + assert!(matches!(Meta::open(url).await, Err(MetaError::Corrupt(_)))); +} + +#[tokio::test] +async fn migrating_from_0010_adds_the_web_sessions_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + migrating_from_0010_adds_the_web_sessions(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn migrating_from_0010_adds_the_web_sessions_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + migrating_from_0010_adds_the_web_sessions(&url).await; +} + +// ---- a change that waits for its locks while its session ends ---- + +/// The time a change judges its session by is read after the change has its locks: one that waited while the session went idle +/// is refused, and changes nothing. +async fn a_session_that_ends_while_a_change_waits_for_its_locks_does_not_authorise_it(url: &str) { + use sqlx::Connection; + use std::sync::Arc; + use std::sync::atomic::{AtomicI64, Ordering}; + sqlx::any::install_default_drivers(); + let now = Arc::new(AtomicI64::new(NOW)); + let ticking = now.clone(); + let meta = Meta::open(url).await.unwrap().with_clock(Arc::new(move || ticking.load(Ordering::SeqCst))); + let root = web_admin(&meta, "root").await; + let ana = user(&meta, "ana").await; + let session = new_session(); + assert_eq!(web_sign_in(&meta, &root, &session, NOW).await, Some(WebSignIn::Session)); + let actor = AdminActor { + user: root.id, + via: AdminVia::Web { id_hash: session.id_hash, idle_ms: WEB_IDLE_MS }, + password_hash: ADMIN_HASH.to_owned(), + two_factor: false, + }; + // Another connection holds the write lock (SQLite) or the admin's row (Postgres) until the test lets go. + let mut holder = sqlx::AnyConnection::connect(url).await.unwrap(); + if url.starts_with("sqlite:") { + sqlx::query("BEGIN IMMEDIATE").execute(&mut holder).await.unwrap(); + } else { + sqlx::query("BEGIN").execute(&mut holder).await.unwrap(); + sqlx::query("UPDATE users SET username = username WHERE username = 'root'").execute(&mut holder).await.unwrap(); + } + let change = { + let (meta, actor, ana) = (meta.clone(), actor.clone(), ana.id); + tokio::spawn(async move { meta.admin_update_user(&actor, ana, None, Some(Some(MIN_QUOTA_BYTES)), NOW + 1).await }) + }; + tokio::time::sleep(std::time::Duration::from_millis(400)).await; + assert!(!change.is_finished(), "the change is waiting for the lock"); + now.store(NOW + WEB_IDLE_MS + 5, Ordering::SeqCst); + sqlx::query("COMMIT").execute(&mut holder).await.unwrap(); + let result = change.await.unwrap(); + assert!(matches!(result, Err(MetaError::DeviceRevoked)), "{result:?}"); + assert_eq!(admin_view(&meta, ana.id).await.quota_bytes, None, "nothing changed"); + // The same change while the session is valid goes through. + now.store(NOW + 2, Ordering::SeqCst); + meta.admin_update_user(&actor, ana.id, None, Some(Some(MIN_QUOTA_BYTES)), NOW + 2).await.unwrap(); +} + +#[tokio::test] +async fn a_session_that_ends_while_a_change_waits_for_its_locks_does_not_authorise_it_on_sqlite() { + let dir = tempfile::tempdir().unwrap(); + a_session_that_ends_while_a_change_waits_for_its_locks_does_not_authorise_it(&format!("sqlite:{}", dir.path().join("meta.db").display())).await; +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn a_session_that_ends_while_a_change_waits_for_its_locks_does_not_authorise_it_on_postgres() { + let Some((url, _guard)) = postgres_database().await else { + eprintln!("skipped: CRAFT_CLOUD_TEST_PG not set"); + return; + }; + a_session_that_ends_while_a_change_waits_for_its_locks_does_not_authorise_it(&url).await; } diff --git a/crates/proto/src/api.rs b/crates/proto/src/api.rs index 6ff15c5d..810fd87f 100644 --- a/crates/proto/src/api.rs +++ b/crates/proto/src/api.rs @@ -9,7 +9,10 @@ use crate::ids::{DeviceId, Epoch, NodeId, SpaceId}; use crate::ops::{Conflict, Op}; /// What apps see of these bodies lives in `craft-cloud-api` (no postcard), re-exported here. -pub use craft_cloud_api::api::{DeviceInfo, ErrorAnswer, PairingState, PairingStatus, codes}; +pub use craft_cloud_api::api::{ + AccountInfo, DeviceInfo, ErrorAnswer, PairingState, PairingStatus, RecoveryCodes, RevokedOthers, TwoFactorChallenge, TwoFactorSetupResponse, + TwoFactorStatus, codes, +}; /// Protocol versions this build speaks. A client and server must share at least one. pub const PROTOCOL_MIN: u32 = 1; @@ -46,13 +49,21 @@ pub struct SpaceResponse { pub username: String, } -/// `POST /api/v1/auth/login`. -#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +/// `POST /api/v1/auth/login`. `client_kind`, `platform` and `app_version` are optional and only shown in the device +/// list, never a basis for a permission: `client_kind` is `agent`, `desktop` or `cli`, the others are free text of at +/// most 32 bytes (all without control characters). A body without them works as before. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct LoginRequest { pub username: String, pub password: String, /// Shown in the device list, e.g. the computer's name. pub device_name: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub client_kind: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub platform: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub app_version: Option, } impl std::fmt::Debug for LoginRequest { @@ -61,6 +72,9 @@ impl std::fmt::Debug for LoginRequest { .field("username", &self.username) .field("password", &"") .field("device_name", &self.device_name) + .field("client_kind", &self.client_kind) + .field("platform", &self.platform) + .field("app_version", &self.app_version) .finish() } } @@ -97,14 +111,27 @@ impl std::fmt::Debug for PairingCreated { /// `POST /api/v1/join` (no auth): redeem an invite or an add-a-computer code (normalised or as typed) for a new /// device; answered with a [`LoginResponse`]. `password` is required, and only taken, for an invite to an account -/// that has none yet. Its `Debug` output hides the code and the password. -#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +/// that has none yet. `client_kind`, `platform` and `app_version` are as in [`LoginRequest`]. `totp_code` or +/// `recovery_code` (one of them) is the second factor of an account that has two-factor sign-in: the server answers +/// `two_factor_required` without using the code up, and the same body is sent again with it. Its `Debug` output hides +/// the code, the password and the second factor. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct JoinRequest { pub code: String, /// Shown in the device list, e.g. the computer's name. pub device_name: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub password: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub client_kind: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub platform: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub app_version: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, } impl std::fmt::Debug for JoinRequest { @@ -113,10 +140,397 @@ impl std::fmt::Debug for JoinRequest { .field("code", &"") .field("device_name", &self.device_name) .field("password", &self.password.as_ref().map(|_| "")) + .field("client_kind", &self.client_kind) + .field("platform", &self.platform) + .field("app_version", &self.app_version) + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +/// `POST /api/v1/account/password` (device auth): changes the password, answered `204`. Every other device of the +/// account is signed out, and its open invite and pairings are deleted. A wrong `current_password` is `403 +/// wrong_password` (never 401), a `new_password` that breaks the rules `400 weak_password`. Its `Debug` output hides +/// both passwords. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct ChangePasswordRequest { + pub current_password: String, + pub new_password: String, +} + +impl std::fmt::Debug for ChangePasswordRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("ChangePasswordRequest").field("current_password", &"").field("new_password", &"").finish() + } +} + +/// `PATCH /api/v1/devices/{id}` (device auth): renames one of the account's devices that isn't revoked; answered with +/// its [`DeviceInfo`]. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct RenameDeviceRequest { + pub name: String, +} + +// ---- two-factor sign-in (`/api/v1/account/two-factor/…`, `/api/v1/auth/login/second-factor`) ---- +// +// The second factor is a TOTP code (`totp_code`, six digits) or a recovery code (`recovery_code`, 16 letters and +// digits, with dashes or without). A wrong one is `403 two_factor_invalid` on a signed-in route and `401` at sign-in and +// join (which are not signed in); too many wrong ones lock the account's second factor for a while (`429 +// two_factor_locked` with `Retry-After`). Every type here that holds a secret hides it in `Debug`. + +/// `POST /account/two-factor/setup`: the account's password starts setting two-factor sign-in up. Its `Debug` output +/// hides the password. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct TwoFactorSetupRequest { + pub password: String, +} + +impl std::fmt::Debug for TwoFactorSetupRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TwoFactorSetupRequest").field("password", &"").finish() + } +} + +/// `POST /account/two-factor/confirm`: the first code from the authenticator app turns two-factor sign-in on. Its +/// `Debug` output hides the code. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct TwoFactorConfirmRequest { + pub totp_code: String, +} + +impl std::fmt::Debug for TwoFactorConfirmRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TwoFactorConfirmRequest").field("totp_code", &"").finish() + } +} + +/// `POST /account/two-factor/disable` and `POST /account/two-factor/recovery-codes`: the password and one second +/// factor (`recovery-codes` takes `totp_code` only). Its `Debug` output hides all three. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct TwoFactorProofRequest { + pub password: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, +} + +impl std::fmt::Debug for TwoFactorProofRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TwoFactorProofRequest") + .field("password", &"") + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) .finish() } } +/// `POST /auth/login/second-factor`: the challenge `two_factor_required` gave, and one second factor. Answered with a +/// [`LoginResponse`]. Its `Debug` output hides all of it. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct SecondFactorRequest { + pub challenge: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, +} + +impl std::fmt::Debug for SecondFactorRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("SecondFactorRequest") + .field("challenge", &"") + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +// ---- the admin API (`/api/v1/admin/…`, an admin's device token) ---- +// +// Every change asks for a fresh confirmation in its body: the acting admin's `password`. A device token never expires, +// so a stolen admin token alone must not be enough. A missing or mistyped field is `400 invalid`, a wrong password `403 +// wrong_password` (never 401). An admin with two-factor sign-in adds `totp_code` (or `recovery_code`) to the same +// bodies; a body without them keeps working for an admin without it, and fields a server does not know are ignored. + +/// The body of an admin change that needs nothing but the confirmation: `POST …/disable`, `POST …/enable`, +/// `DELETE /admin/users/{id}`, `POST` and `DELETE …/invite`, `POST …/reset-two-factor` and `DELETE …/devices/{id}`. Its +/// `Debug` output hides the password and the second factor. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminConfirm { + /// The acting admin's current password. Missing: `400 invalid`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub password: Option, + /// The acting admin's code, when their account has two-factor sign-in. Missing or wrong then: `403 + /// two_factor_invalid`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + /// A recovery code instead of `totp_code`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, +} + +impl std::fmt::Debug for AdminConfirm { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AdminConfirm") + .field("password", &self.password.as_ref().map(|_| "")) + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +/// `POST /admin/users/{id}/password-reset`: the confirmation as in [`AdminConfirm`], and whether redeeming the code also +/// turns the account's two-factor sign-in off (`reset_two_factor`, default `false`: an account that has it then needs +/// its second factor to redeem the code). Its `Debug` output hides the password and the second factor. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminPasswordReset { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub password: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, + #[serde(default)] + pub reset_two_factor: bool, +} + +impl std::fmt::Debug for AdminPasswordReset { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AdminPasswordReset") + .field("password", &self.password.as_ref().map(|_| "")) + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .field("reset_two_factor", &self.reset_two_factor) + .finish() + } +} + +/// `GET /admin/server`: counters and kinds, nothing about paths, addresses or settings. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminServerInfo { + pub version: String, + /// Accounts that are not marked for removal. + pub users: u64, + /// Devices that are not revoked. + pub devices_active: u64, + pub stored_bytes: u64, + pub live_bytes: u64, + /// `disk`, `s3` or `memory`. + pub storage_kind: String, + /// `sqlite` or `postgres`. + pub database_kind: String, +} + +/// An account in the admin API. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminUserInfo { + pub id: Uuid, + pub username: String, + /// `admin` or `member`. + pub role: String, + pub created_ms: i64, + /// When an admin disabled it (`null`: enabled). + pub disabled_ms: Option, + /// Whether the account has two-factor sign-in on. + pub two_factor_enabled: bool, + /// Devices that are not revoked. + pub devices_active: u64, + pub stored_bytes: u64, + /// The quota an admin set for the account (`null`: the server-wide cap, if any, applies). + pub quota_bytes: Option, + /// An unexpired invite is open. + pub invite_pending: bool, +} + +/// `GET /admin/users?cursor&limit&query`: a page of accounts by username. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminUserList { + pub users: Vec, + /// Opaque; give it back as `cursor` for the next page. Missing on the last page. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub next: Option, +} + +/// `POST /admin/users`: a new account without a password, for an invite. Its `Debug` output hides the password. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminCreateUser { + pub username: String, + /// `admin` or `member`. + pub role: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub password: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, +} + +impl std::fmt::Debug for AdminCreateUser { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AdminCreateUser") + .field("username", &self.username) + .field("role", &self.role) + .field("password", &self.password.as_ref().map(|_| "")) + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +/// A join code the admin made, shown once. `join_url` is the whole join string when the server knows its public +/// address, and missing otherwise. Its `Debug` output hides the code and the join string. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminCode { + pub code: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub join_url: Option, + pub expires_ms: i64, +} + +impl std::fmt::Debug for AdminCode { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AdminCode") + .field("code", &"") + .field("join_url", &self.join_url.as_ref().map(|_| "")) + .field("expires_ms", &self.expires_ms) + .finish() + } +} + +/// The answer to `POST /admin/users`. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminCreatedUser { + pub user: AdminUserInfo, + pub invite: AdminCode, +} + +/// `PATCH /admin/users/{id}`: a new role and/or quota. At least one of them; a missing `quota_bytes` leaves the quota, +/// `null` removes it. Its `Debug` output hides the password. +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminUpdateUser { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub role: Option, + #[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")] + pub quota_bytes: Option>, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub password: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub totp_code: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub recovery_code: Option, +} + +/// A field that is there, `null` or not: `Some(None)` for `null`, `Some(Some(x))` for a value (a missing field is +/// `None`, from `#[serde(default)]`). +fn present<'de, D, T>(deserializer: D) -> Result>, D::Error> +where + D: serde::Deserializer<'de>, + T: Deserialize<'de>, +{ + Option::::deserialize(deserializer).map(Some) +} + +impl std::fmt::Debug for AdminUpdateUser { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("AdminUpdateUser") + .field("role", &self.role) + .field("quota_bytes", &self.quota_bytes) + .field("password", &self.password.as_ref().map(|_| "")) + .field("totp_code", &self.totp_code.as_ref().map(|_| "")) + .field("recovery_code", &self.recovery_code.as_ref().map(|_| "")) + .finish() + } +} + +/// `GET /admin/users/{id}/devices`: every device of the account, revoked ones with when and why. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AdminDevices { + pub devices: Vec, +} + +/// The typed values of a security event (`details`). Every field is a closed list of words or a number: nothing a client +/// typed is ever in it. Fields an event does not have are missing, and fields a later release adds are ignored. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct AuditDetails { + /// `agent`, `desktop`, `cli` or `web` (`web` means a browser session of the admin pages). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub client_kind: Option, + /// `windows`, `macos`, `linux`, `android`, `ios` or `other`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub platform: Option, + /// Why a device was signed out: `user`, `others`, `password`, `admin`, `disabled`, `two_factor`, `idle` or `reset`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub reason: Option, + /// How far a failed sign-in got: `password`, `totp`, `recovery` or `challenge_expired`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub step: Option, + /// `admin` or `member`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub role_from: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub role_to: Option, + /// The quota an admin set: `null` when they removed it, missing when the event is not about a quota. + #[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")] + pub quota_bytes: Option>, + /// How many devices an event signed out. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub revoked: Option, + /// The device an admin or the command line signed out. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub device: Option, + /// Set (to `true`) when redeeming a password reset code also turned the account's two-factor sign-in off. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub two_factor_removed: Option, + /// Set when the values were too long for the log and whole fields were left out. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub truncated: Option, +} + +/// One security event, as `GET /account/activity`, `GET /admin/audit` and the export show it. No address, name of a file or +/// device, or secret is ever in it. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AuditEvent { + pub id: Uuid, + pub at_ms: i64, + /// The account that acted; `null` for the command line, and for a sign-in that failed (nobody proved who they were). + pub actor_user: Option, + pub actor_device: Option, + /// A name from the closed list in `docs/design/sync.md`, like `auth.login`. + pub action: String, + /// `user` or `device`, or empty. + pub target_kind: String, + pub target_id: Option, + /// `success` or `failure`. + pub outcome: String, + pub details: AuditDetails, + /// How many tries the event stands for: failed sign-ins are one event per account, client kind and minute. 1 for every other event. + pub count: u64, +} + +/// A page of `GET /account/activity` or `GET /admin/audit`, newest first. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AuditPage { + pub events: Vec, + /// Opaque; give it back as `cursor` for the next (older) page. Missing on the last page. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub next: Option, +} + +/// The last line of `GET /admin/audit/export` (the lines before it are [`AuditEvent`]s). `complete` says the export holds +/// every event up to the moment it started; `truncated` says there were more than the export holds. A stream that ends +/// without either line was cut off and is not a complete export. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct AuditExportEnd { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub complete: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub truncated: Option, + /// The events before this line. + pub exported: u64, +} + /// `POST /api/v1/chunks/missing` and its answer: which of `ids` this space doesn't have. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct MissingRequest { @@ -160,7 +574,8 @@ pub struct QuotaUse { } /// Every error answer. `error` is a stable code, one of [`codes`]; `message` is for people. `conflicts` is -/// set for `conflict`, `missing` for `missing_chunks`, `quota` for `quota_exceeded`. Clients ignore codes and JSON +/// set for `conflict`, `missing` for `missing_chunks`, `quota` for `quota_exceeded`, `two_factor` for +/// `two_factor_required`. Clients ignore codes and JSON /// fields they don't know (the server may add more later), so match on the codes you handle and treat the rest by /// HTTP status. #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)] @@ -173,6 +588,9 @@ pub struct ErrorBody { pub missing: Option>, #[serde(default, skip_serializing_if = "Option::is_none")] pub quota: Option, + /// Set for `two_factor_required`: the challenge to answer at `POST /auth/login/second-factor`. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub two_factor: Option, } /// Response header carrying the space's epoch on every signed-in request. @@ -184,15 +602,115 @@ mod tests { #[test] fn secrets_stay_out_of_debug_output() { - let req = LoginRequest { username: "ana".into(), password: "hunter2-secret".into(), device_name: "pc".into() }; + let req = LoginRequest { username: "ana".into(), password: "hunter2-secret".into(), device_name: "pc".into(), ..LoginRequest::default() }; assert!(!format!("{req:?}").contains("hunter2-secret")); let res = LoginResponse { device: DeviceId::generate(), token: "tok-secret".into() }; assert!(!format!("{res:?}").contains("tok-secret")); - let join = JoinRequest { code: "K7M4Q9TX".into(), device_name: "pc".into(), password: Some("hunter2-secret".into()) }; + let join = + JoinRequest { code: "K7M4Q9TX".into(), device_name: "pc".into(), password: Some("hunter2-secret".into()), ..JoinRequest::default() }; let shown = format!("{join:?}"); assert!(!shown.contains("K7M4Q9TX") && !shown.contains("hunter2-secret"), "{shown}"); let created = PairingCreated { id: Uuid::nil(), code: "K7M4Q9TX".into(), expires_ms: 1 }; assert!(!format!("{created:?}").contains("K7M4Q9TX")); + let change = ChangePasswordRequest { current_password: "old-secret-pw".into(), new_password: "new-secret-pw".into() }; + let shown = format!("{change:?}"); + assert!(!shown.contains("old-secret-pw") && !shown.contains("new-secret-pw"), "{shown}"); + assert_eq!(shown.matches("").count(), 2); + let confirm = AdminConfirm { + password: Some("admin-secret-pw".into()), + totp_code: Some("918273".into()), + recovery_code: Some("RECOVERYSECRET00".into()), + }; + let shown = format!("{confirm:?}"); + assert!(!shown.contains("admin-secret-pw") && !shown.contains("918273") && !shown.contains("RECOVERYSECRET"), "{shown}"); + let reset = + AdminPasswordReset { password: Some("admin-secret-pw".into()), totp_code: Some("918273".into()), ..AdminPasswordReset::default() }; + let shown = format!("{reset:?}"); + assert!(!shown.contains("admin-secret-pw") && !shown.contains("918273"), "{shown}"); + let create = + AdminCreateUser { username: "ana".into(), role: "member".into(), password: Some("admin-secret-pw".into()), ..AdminCreateUser::default() }; + assert!(!format!("{create:?}").contains("admin-secret-pw")); + let update = AdminUpdateUser { password: Some("admin-secret-pw".into()), ..AdminUpdateUser::default() }; + assert!(!format!("{update:?}").contains("admin-secret-pw")); + let code = AdminCode { code: "ABCDEFGHJKMNPQRS".into(), join_url: Some("cloud.example.org/ABCD-EFGH-JKMN-PQRS".into()), expires_ms: 1 }; + let shown = format!("{code:?} {:?}", AdminCreatedUser { user: sample_user(), invite: code.clone() }); + assert!(!shown.contains("ABCDEFGH") && !shown.contains("cloud.example.org"), "{shown}"); + } + + #[test] + fn two_factor_secrets_stay_out_of_debug_output() { + let setup = TwoFactorSetupResponse { + secret: "JBSWY3DPEHPK3PXP".into(), + otpauth_uri: "otpauth://totp/Craft%20Cloud:ana?secret=JBSWY3DPEHPK3PXP".into(), + expires_ms: 5, + }; + let shown = format!("{setup:?}"); + assert!(!shown.contains("JBSWY3DP") && !shown.contains("otpauth:"), "{shown}"); + let codes = RecoveryCodes { recovery_codes: vec!["ABCD-EFGH-JKMN-PQRS".into(), "WXYZ-0123-4567-89AB".into()] }; + let shown = format!("{codes:?}"); + assert!(!shown.contains("ABCD") && !shown.contains("WXYZ"), "{shown}"); + let second = + SecondFactorRequest { challenge: "challenge-secret".into(), totp_code: Some("123456".into()), recovery_code: Some("RC-SECRET".into()) }; + let shown = format!("{second:?}"); + assert!(!shown.contains("challenge-secret") && !shown.contains("123456") && !shown.contains("RC-SECRET"), "{shown}"); + let proof = TwoFactorProofRequest { password: "pw-secret-1".into(), totp_code: Some("654321".into()), recovery_code: None }; + let shown = format!("{proof:?}"); + assert!(!shown.contains("pw-secret-1") && !shown.contains("654321"), "{shown}"); + assert!(!format!("{:?}", TwoFactorSetupRequest { password: "pw-secret-1".into() }).contains("pw-secret-1")); + assert!(!format!("{:?}", TwoFactorConfirmRequest { totp_code: "654321".into() }).contains("654321")); + let join = JoinRequest { + code: "K7M4Q9TX".into(), + totp_code: Some("111222".into()), + recovery_code: Some("RC-SECRET".into()), + ..JoinRequest::default() + }; + let shown = format!("{join:?}"); + assert!(!shown.contains("111222") && !shown.contains("RC-SECRET"), "{shown}"); + let body = ErrorBody { + error: codes::TWO_FACTOR_REQUIRED.into(), + message: "m".into(), + two_factor: Some(TwoFactorChallenge { challenge: "challenge-secret".into(), expires_ms: 1 }), + ..ErrorBody::default() + }; + assert!(!format!("{body:?}").contains("challenge-secret")); + // The challenge travels in the error body, and an older client that doesn't know the field ignores it. + let json = serde_json::to_value(&body).unwrap(); + assert_eq!(json["two_factor"], serde_json::json!({ "challenge": "challenge-secret", "expires_ms": 1 })); + assert_eq!(serde_json::from_value::(json).unwrap(), body); + } + + fn sample_user() -> AdminUserInfo { + AdminUserInfo { + id: Uuid::nil(), + username: "ana".into(), + role: "member".into(), + created_ms: 1, + disabled_ms: None, + two_factor_enabled: false, + devices_active: 0, + stored_bytes: 0, + quota_bytes: None, + invite_pending: false, + } + } + + #[test] + fn an_update_tells_a_missing_quota_from_a_null_one() { + let missing: AdminUpdateUser = serde_json::from_value(serde_json::json!({ "role": "admin", "password": "p" })).unwrap(); + assert_eq!(missing.quota_bytes, None); + let null: AdminUpdateUser = serde_json::from_value(serde_json::json!({ "quota_bytes": null })).unwrap(); + assert_eq!(null.quota_bytes, Some(None)); + let set: AdminUpdateUser = serde_json::from_value(serde_json::json!({ "quota_bytes": 5_000_000 })).unwrap(); + assert_eq!(set.quota_bytes, Some(Some(5_000_000))); + assert!(serde_json::from_value::(serde_json::json!({ "quota_bytes": -1 })).is_err()); + assert_eq!(serde_json::to_value(&set).unwrap(), serde_json::json!({ "quota_bytes": 5_000_000 })); + } + + #[test] + fn an_admin_body_ignores_fields_it_does_not_know() { + let confirm: AdminConfirm = serde_json::from_value(serde_json::json!({ "password": "p", "totp_code": "123456", "later": 1 })).unwrap(); + assert_eq!((confirm.password.as_deref(), confirm.totp_code.as_deref(), confirm.recovery_code), (Some("p"), Some("123456"), None)); + assert_eq!(serde_json::from_value::(serde_json::json!({})).unwrap().password, None); } #[test] @@ -204,11 +722,42 @@ mod tests { assert_eq!(waiting, PairingStatus { state: PairingState::Waiting, device_name: None }); let join: JoinRequest = serde_json::from_value(serde_json::json!({ "code": "c", "device_name": "d" })).unwrap(); assert_eq!(join.password, None); + assert_eq!((join.client_kind, join.platform, join.app_version), (None, None, None)); + let login = LoginRequest { username: "ana".into(), password: "pw".into(), device_name: "pc".into(), ..LoginRequest::default() }; + assert_eq!(serde_json::to_value(&login).unwrap(), serde_json::json!({ "username": "ana", "password": "pw", "device_name": "pc" })); assert_eq!(codes::INVALID_CODE, "invalid_code"); assert_eq!(codes::PASSWORD_REQUIRED, "password_required"); assert_eq!(codes::WEAK_PASSWORD, "weak_password"); } + #[test] + fn audit_bodies_have_a_plain_json_form() { + let details: AuditDetails = serde_json::from_str(r#"{"revoked":2,"a_later_field":"x","quota_bytes":null}"#).unwrap(); + assert_eq!(details.revoked, Some(2)); + assert_eq!(details.quota_bytes, Some(None)); + assert_eq!(serde_json::to_string(&details).unwrap(), r#"{"quota_bytes":null,"revoked":2}"#); + assert_eq!(serde_json::from_str::("{}").unwrap(), AuditDetails::default()); + assert_eq!(serde_json::to_string(&AuditDetails::default()).unwrap(), "{}"); + let end = AuditExportEnd { complete: Some(true), truncated: None, exported: 3 }; + assert_eq!(serde_json::to_string(&end).unwrap(), r#"{"complete":true,"exported":3}"#); + let event = AuditEvent { + id: Uuid::nil(), + at_ms: 5, + actor_user: None, + actor_device: None, + action: "auth.login_failed".into(), + target_kind: String::new(), + target_id: None, + outcome: "failure".into(), + details: AuditDetails { step: Some("password".into()), ..AuditDetails::default() }, + count: 4, + }; + let json = serde_json::to_value(&event).unwrap(); + assert_eq!(json["actor_user"], serde_json::Value::Null); + assert_eq!(json["count"], 4); + assert_eq!(serde_json::from_value::(json).unwrap(), event); + } + #[test] fn error_bodies_omit_empty_extras() { let body = ErrorBody { error: "not_found".into(), message: "no such chunk".into(), ..ErrorBody::default() }; diff --git a/crates/store/src/disk.rs b/crates/store/src/disk.rs index cea80de5..19354d38 100644 --- a/crates/store/src/disk.rs +++ b/crates/store/src/disk.rs @@ -73,6 +73,10 @@ async fn blocking(f: impl FnOnce() -> io::Result + Send + #[async_trait] impl Backend for DiskBackend { + fn kind(&self) -> &'static str { + "disk" + } + async fn put_raw(&self, path: &str, data: Bytes) -> Result<(), StoreError> { let target = self.full_path(path); blocking(move || write_atomic(&target, &data)).await diff --git a/crates/store/src/lib.rs b/crates/store/src/lib.rs index 43f37475..e50caaee 100644 --- a/crates/store/src/lib.rs +++ b/crates/store/src/lib.rs @@ -89,6 +89,11 @@ pub enum StoreError { /// Moves raw bytes. Implementations don't check anything; [`Store`] does. #[async_trait] pub trait Backend: Send + Sync + 'static { + /// What kind of storage this is, for the admin's server overview (`disk`, `s3` or `memory`). A backend that + /// doesn't say is `other`. Never a path, an address or a setting. + fn kind(&self) -> &'static str { + "other" + } /// Writes the object atomically: a reader sees the old object, the new one, or none, never a mix. async fn put_raw(&self, path: &str, data: Bytes) -> Result<(), StoreError>; /// `None` when there is no such object. @@ -108,6 +113,11 @@ impl Store { Self { backend: Arc::new(backend) } } + /// The backend's kind (`disk`, `s3`, `memory` or `other`). + pub fn kind(&self) -> &'static str { + self.backend.kind() + } + /// Stores `data` under `key` after checking its size and hash. Storing a blob that is already there /// succeeds and changes nothing. pub async fn put(&self, key: BlobKey, data: &[u8]) -> Result<(), StoreError> { diff --git a/crates/store/src/memory.rs b/crates/store/src/memory.rs index 47a007ce..a1fcc825 100644 --- a/crates/store/src/memory.rs +++ b/crates/store/src/memory.rs @@ -32,6 +32,10 @@ impl MemoryBackend { #[async_trait] impl Backend for MemoryBackend { + fn kind(&self) -> &'static str { + "memory" + } + async fn put_raw(&self, path: &str, data: Bytes) -> Result<(), StoreError> { self.lock()?.insert(path.to_owned(), data); Ok(()) diff --git a/crates/store/src/s3.rs b/crates/store/src/s3.rs index 0557eba2..4b231a14 100644 --- a/crates/store/src/s3.rs +++ b/crates/store/src/s3.rs @@ -69,6 +69,10 @@ fn backend_error(e: object_store::Error) -> StoreError { #[async_trait] impl Backend for S3Backend { + fn kind(&self) -> &'static str { + "s3" + } + async fn put_raw(&self, path: &str, data: Bytes) -> Result<(), StoreError> { self.store.put(&self.object_path(path), PutPayload::from(data)).await.map_err(backend_error)?; Ok(()) diff --git a/docs/agent.md b/docs/agent.md index 18e2a394..0e870232 100644 --- a/docs/agent.md +++ b/docs/agent.md @@ -59,6 +59,18 @@ printf '%s\n' 'your password' | ./target/release/craft-cloud-agent login https:/ and `--adopt` as for `login`) signs it in. An admin's invite (`user invite`, in [`server.md`](server.md)) works the same way; for a new account, `join` asks for its password twice on standard input. A code that doesn't work says so, without saying why: make a new one. +- An account with two-factor sign-in (turned on in the app's Security page; see "Two-factor sign-in" in + [`server.md`](server.md)) asks for a second step at `login` and at `join`: after the password, the command prints + `This account has two-factor sign-in. Type the code from your authenticator app (or a recovery code):` and reads one line + from standard input, so a script gives the password line and then the code line. The code is never an argument and + is never stored; a wrong one may be typed again twice (the server itself allows five wrong codes per sign-in). Six digits + are a code from the app; anything else is taken as a recovery code, which works once. Too many wrong codes lock the + account's second step for a while: the command says how long, and the lock does not sign anything out. For a `join` that + needs a code the invite isn't used up until the code is right. A running agent (`run`) does the same through its local + API; the Craft Cloud app asks for the code in its own window. +- Rolling this out: update the server first, then the agents and the app. An agent from before two-factor sign-in can't + complete a sign-in for an account that has it, and it reports that as a wrong password; update the agent, nothing is + broken. The app on a server that doesn't know two-factor sign-in says so on its Security page instead of failing. - Through the running agent: `pause` and `resume` syncing (a pause holds across restarts; recents keep working), `quit` the agent, and `devices` to list the account's machines (`devices remove ` takes one out; to take this machine out, use `logout`). diff --git a/docs/design/sync.md b/docs/design/sync.md index 52d60652..36082406 100644 --- a/docs/design/sync.md +++ b/docs/design/sync.md @@ -578,6 +578,225 @@ changes locally, then pushes local changes. These rules came out of the simulati database (not a supported setup) would each count their own, so twice the sign-in attempts and twice the writes. - **Setup**: `craft-cloud-server init` writes the config, creates the data directory and the first admin. Solo use is that one admin account and nothing more. +- **Device sessions** (migration 0007): a device token has no expiry and its format is unchanged (43 characters, stored as a + BLAKE3 hash); what changed is what a device records and how an account ends them. A device records the `client_kind`, + `platform` and `app_version` it said at sign-in (display only; the agent says `agent`, its operating system and its version on every + sign-in and join), and, once revoked, when (`revoked_ms`) and why + (`revoke_reason`). The `revoked` flag is still the check: the three are set together in one transaction and only by + the first revoke, so a second revoke changes neither. A user keeps `password_changed_ms`, and `disabled_ms`, which the + admin API sets and the token check, sign-in, join and pairing read (see "Admin API and roles"). + - **Changing the password** (`POST /account/password`) checks the current password under one of the server-wide + password-check slots (all busy: `429` with `Retry-After`, and the try is given back), then in one transaction sets + the new hash and `password_changed_ms`, revokes every other device (`revoke_reason = password`) and deletes the + account's open invite and all its pairings, so no code made before the change works after it; if the password + changed meanwhile nothing changes (`409 conflict`), and neither does it if the calling device was signed out meanwhile (`401`). Afterwards each revoked device's live connections end at once. The same two + steps run for `user reset-password` (all devices, reason `reset`, unless `--keep-devices`) and `device revoke-all` + (reason `admin`) on the command line, which cannot reach the server's live connections: those end at the next ping + (within about 30 s) and a change-feed long-poll within 60 s. + - **Sign-in and join under a change**: a password sign-in creates its device only if the stored hash is still the one + it checked (the account is locked first), and answers as for a wrong password otherwise, so a device is never + added after a reset or change that signed the others out. Every transaction that revokes devices, changes a + password, redeems an invite or makes a pairing takes the account's row first, which keeps them from deadlocking. + - **Limits**: `password_changes` counts every try, a wrong password included, 5 per account per 15 minutes; the + password and device-name bodies are read up to 4 KiB (the sign-in bodies stay at 1 MiB). +- **Admin API and roles** (migration 0008): an account is an `admin` or a `member`, and every route under + `/api/v1/admin` is behind the `require_admin` layer, which runs after the device check and reads the role the + database gave for that very request (nothing is cached, so a demoted admin is out with the next request). A member + gets `403 forbidden`, a missing or revoked token `401`. The layer is only the first look: every change checks the + acting admin again inside its own transaction. + - **Confirmation.** A device token never expires, so a stolen admin token alone must not be enough to create an + admin, issue a code or sign others out. Every change (creating, changing, disabling, enabling, removing an account, + issuing or revoking an invite, issuing a password reset code, revoking another user's device) carries the acting + admin's current `password` in its JSON body, also the `DELETE` routes (at most 4 KiB). A missing or mistyped body + field is `400 invalid`; a wrong password is `403 wrong_password`, never 401, because the agent reads every 401 on a + signed-in route as a revoked device. Tries are counted per account in the password change's limiter (5 wrong + passwords in 15 minutes, then `429`; a right password is given back), and every check takes one of the + server-wide password-check slots. **An admin whose account has two-factor sign-in** adds `totp_code` (or a + `recovery_code`) to the same body: missing or wrong is `403 two_factor_invalid` (the field is a credential there, not + a form field; a code that is not six digits is `400 invalid`), too many wrong ones `429 two_factor_locked` with + `Retry-After`. The code is checked and used up by the confirmation, on the same counters, limiter and time-step rule + as sign-in's second step (below): **one 30-second step serves one change**, the next change takes the next code or + a recovery code. The password is told first: a wrong one is `403 wrong_password` whatever the code. Resetting another + **administrator's** password or two-factor sign-in takes the acting admin's own two-factor sign-in (`403 forbidden` + without it; the command line on the host is the way then). The change compares the admin's two-factor state with + the one the confirmation saw, under the account's lock, so a confirmation given before they turned it on or off is + `409 conflict`. + - **The acting admin in the transaction.** After the password check (a slow Argon2id run) the admin's role, device or + password can have changed. Every change locks, in this order, (1) the `last_admin` lock if it can take an + administrator away, (2) the user rows of the actor and the target in ascending ID, (3) the rows derived from them, + and then checks that the actor's device is not revoked and the account neither disabled nor marked for removal + (`401`), that the role is still `admin` (`403 forbidden`) and that the password hash is the one verified (`409 + conflict`). Sign-ins, joins and pairings take the user row first too, so none of these can deadlock. + - **The last administrator.** Demoting, disabling or removing an admin who is the last enabled one is `409 + last_admin`. A count cannot be locked with `FOR UPDATE`, and locking each target's row would not stop two admins + from each taking the other away, so every change that can reduce the admins first takes one transaction-wide lock + and counts after it (Postgres `pg_advisory_xact_lock` with a fixed key, SQLite the write transaction, which is + the same thing there). Nobody can disable or remove their own account (`400 invalid`) but may step down when + another admin is left. The command line does not enforce the rule or the confirmation: the operator has the files. + - **Disabled accounts.** Disabling signs every device out (`revoke_reason = disabled`), deletes the open invite or + reset code and the pairings, and sets `disabled_ms`; enabling clears it and brings back no device. A disabled + account's old tokens are `401 unauthorized`; sign-in and join answer `403 account_disabled`, but only after the + password or code was right (a wrong one is `401` or `404 invalid_code` as ever). Issuing is serialised with + disabling: a sign-in, a redeem, a pairing and a new device lock the user row first and look at `disabled_ms` and + `removed_ms` in their own transaction, so a password checked before the disable never turns into a device after + it, and enabling later cannot make such a device usable. **Removing** an account (`DELETE /admin/users/{id}`, `202`) + also sets `removed_ms`: it is disabled, left out of the lists and cannot be enabled, its username stays taken, + and nothing it stored is deleted (freeing it comes with garbage collection). + - **Codes.** `POST /admin/users` makes an account without a password (role `admin` or `member`) and a 7-day invite; + the person chooses the password when they redeem it. An invite for an existing account is only made for one + that still has no password: for an account with a password it would add a computer without asking for it, so + `409 conflict`, and a password reset code is the way in. A **password reset code** (`purpose = reset`, 16 + characters, one hour, one per account like an invite, so each replaces the other) is redeemed at `POST /join`: it + always asks for a new password (`password_required`, `weak_password`), sets it, signs **every** device of the + account out (`revoke_reason = reset`), deletes its pairings, and, as every redeem does, signs the redeeming computer + in. Codes are shown once in the answer (with the whole join link when `public_url` is set) and never logged. + - **Quota.** An admin may set `quota_bytes` for one account (from one chunk, 4 MiB, to `i64::MAX`; `null` removes + it). The limit of a space is its quota, else `stored_bytes_per_user`, else none, and it holds in the look before a + chunk or manifest is stored (`check_room`, also when there is no server-wide cap) and in the count that follows. + A quota under what the account stores deletes nothing and refuses new bytes with `507 quota_exceeded`. + - **Live connections.** The process that revokes a device ends its live connections at once; another server process on + the same Postgres database, and the command line, are noticed at the connection's next ping (about 30 s). + - Logs carry the kind of event and the target's ID at `info`, never a username, password, code or link. +- **Security audit log** (migration 0009): the events that matter for who may do what are rows of `audit_events`, + written by the change they record, in its transaction and before its commit, so a change whose event cannot be written + is not made (fail closed). The names are a closed list in the code: `auth.login`, `auth.login_failed`, + `auth.second_factor_failed`, `auth.recovery_code_used`, `auth.two_factor_locked`, `device.revoked`, + `device.revoked_others`, `account.password_changed`, `account.two_factor_enabled`, `account.two_factor_disabled`, + `account.recovery_codes_regenerated`, `admin.user_created`, `admin.user_disabled`, `admin.user_enabled`, + `admin.user_removed`, `admin.role_changed`, `admin.quota_changed`, `admin.invite_issued`, `admin.invite_revoked`, + `admin.password_reset_issued`, `admin.two_factor_reset`, `admin.device_revoked`, `join.invite_redeemed`, + `join.reset_redeemed`, `join.pairing_redeemed`, `cli.password_reset`, `cli.two_factor_reset`, `cli.device_revoked`, + `cli.devices_revoked_all` and `cli.user_removed`. A name whose change does not exist yet (`user remove`) is listed so + the vocabulary a client filters by does not change; its writer arrives with that change. + - **Rows.** `id` (UUIDv7), `at_ms`, `actor_user` and `actor_device` (`NULL` for the command line, and for a sign-in that + failed: nobody proved who they were), `action`, `target_kind` (`user`, `device` or empty) and `target_id`, `outcome` + (`success`, or `failure` for a failed sign-in), `details`, and for aggregated rows `agg_key` and `agg_count` (the API + calls it `count`). There are no foreign keys: an event keeps the ID of an account that was removed, and the name is + gone because it was never stored. + - **What each operation writes.** A sign-in `auth.login` (actor: the account and its new device); a failed one + `auth.login_failed`. `DELETE /devices/{id}` `device.revoked`; `POST /devices/revoke-others` **one** + `device.revoked_others` with the count (none when no device was left); `POST /account/password` one + `account.password_changed` whose `revoked` counts the devices that went with it. Admin changes write their + `admin.*` event, a `PATCH` with a role and a quota one event for each field that changed, in one transaction; the + devices a disable or removal signs out are counted in its one event. Redeeming an invite, a reset code or a + pairing writes `join.invite_redeemed`, `join.reset_redeemed` (with `revoked`) or `join.pairing_redeemed`. The + command line writes `cli.password_reset`, `cli.device_revoked` and `cli.devices_revoked_all`, with no actor. **A + repeat or a no-op writes nothing**, since nothing changed: disabling a disabled account, signing out a device that is + out, giving a role or quota the account has, revoking an invite that is not there, and so a retried request after a + dropped connection leaves the event of its first run only. Files, uploads and live documents are not audited. + The admin and command line events about a device are about its **account** (`target_kind = user`) and carry the device + in `details`, so the account's own activity shows them. + - **`details` is typed.** Each event has its own few fields, each a closed list of words or a number: `client_kind` + (`agent`, `desktop`, `cli`, `web`) and `platform` (`windows`, `macos`, `linux`, `android`, `ios`, `other`) for a sign-in or + join, `reason` (as in the device list), `step` (`password`, `totp`, `recovery`, `challenge_expired`), `role_from` and + `role_to`, `quota_bytes` (a number, `null` when the quota was removed), `revoked` (a count), `device` (an ID) and `two_factor_removed` (`true`, only on `join.reset_redeemed`). A + device may call its platform any printable text of 32 bytes, an address or a file name included, so anything not in + the list is `other`; `web` means a browser session of the admin pages and cannot be named by a device. Nothing a client typed ever reaches a row. The text is at most 2,048 bytes: if it would be more, + whole fields go, the least important first, and `"truncated": true` is added; bytes are never cut off, which would + leave invalid JSON. **No address, file name, path, device name, name of an account that does not exist, password, + code, challenge, token, key or e-mail address is stored anywhere in a row.** + - **Two-factor sign-in** writes these, all in the transaction of the change unless said otherwise. A sign-in that made a + device writes `auth.login` whichever way it went (no second factor, a code, a recovery code); the challenge `POST + /auth/login` gives writes nothing, since nobody is signed in yet. Turning it on, new recovery codes and turning it off write + `account.two_factor_enabled`, `account.recovery_codes_regenerated` and `account.two_factor_disabled` (the account, from the + device that asked). A recovery code that is used up writes `auth.recovery_code_used` in the transaction that uses it up + (sign-in, join, turning it off, an admin's confirmation, which commits on its own before the change it confirms), by the + account and the device where there is one; never the code. An admin's reset writes `admin.two_factor_reset` (admin, target) + and the command line `cli.two_factor_reset` (no actor, with `revoked`); a reset code made with `reset_two_factor` that is + redeemed writes `join.reset_redeemed` with `two_factor_removed: true`, by the account that redeemed it: the code does not + say which admin made it, and none is made up. The wrong second step of a sign-in or a join is `auth.second_factor_failed` + (`step`: `totp` or `recovery`; `challenge_expired` for a challenge that is unknown, expired, used or out of tries, with no + account when it was not found), aggregated and written after the answer is decided, like `auth.login_failed`. **A wrong proof + on a signed-in route** (turning it off, new recovery codes, an admin's confirmation) **is not written one by one**; the lock + it can cause is: `auth.two_factor_locked`, once when a lock is set or lengthened and never for a refusal while it holds. That + event is written like a failed sign-in's, in a transaction of its own after the counters and the lock are committed, so a + fault in the log can neither undo the lock nor change the answer; it is logged without content and the event is lost. + - **Failed sign-ins** are one row per account, action, client kind and minute, `agg_key` = action + client kind + account + minute, written + after the answer is decided and in a transaction of its own: `INSERT … ON CONFLICT (agg_key) DO UPDATE SET agg_count = + audit_events.agg_count + 1` (the column is qualified, since a bare one is ambiguous against `excluded` on Postgres), + so many tries at one moment make one row that counts them all and keeps the identity, time and details of the first. + Tries with a name no account has share one row per client kind and minute with no account in it, so a guesser can neither put text in + the log nor make it grow without bound. A failure to write this row is logged (no content) and changes neither the + answer nor the limits on tries, which are not stored in the log at all. + - **Reading.** Pages are newest first by `(at_ms, id)` descending. `cursor` is that pair, opaque, and exclusive: the next + page starts strictly behind it, so events with one timestamp are neither repeated nor lost. The export reads 500 rows + per query through a channel of two pages, so a slow client slows the reading instead of filling memory; the end of the + log is fixed when it starts (`at_ms` up to then), at most 100,000 event rows are sent, and the reading stops when the + client goes away. The last line says how it ended; a database error in the middle breaks the connection (and is logged + without content) so that no client can take half an export for a whole one. + - **Append-only, in the code.** `Meta` has no function that changes or deletes a row of `audit_events` except + `audit_prune`, which deletes events older than the retention, and the counter of an aggregated row. A test reads the + sources of `craft-cloud-meta` and allows exactly those two statements. **This deviates from a design with + database triggers and separate roles**: it is a design choice so that the same code runs on both databases (SQLite has no database roles; triggers exist but are not used), and an operator + with access to the database can still change the log. Protection against that is outside this server. + - **Retention.** `[audit] retention_days` (1 to 3650, default 365, the section is optional). Old events are deleted by + `audit prune` (`--dry-run` only counts) and by a write that finds the last complete pruning an hour or more past (kept + in memory, set only when a pruning deleted everything that was due), after its commit and never inside the security + transaction, in batches of 1,000 rows each in a transaction of its own and at most 20 batches a time. A server + that nobody writes to keeps expired events until `audit prune` runs: schedule it (cron, the task scheduler). There + is no periodic task. + - **Personal data.** A row holds account and device IDs and times, which is personal data. `docs/server.md` says how long + it is kept and why; the server stores no address. + - Reading the log is not itself an event. +- **Two-factor sign-in** (migration 0010, after the audit log's 0009): an account may turn on a TOTP second factor (RFC 6238: HMAC-SHA-1, six + digits, 30-second steps, one step either way for a clock that is a little off; the codes authenticator apps make) with + ten recovery codes. With it on, a right password gives **no token**: `POST /auth/login` answers `401 + two_factor_required` with `two_factor: {challenge, expires_ms}`, and `POST /auth/login/second-factor` with the + challenge and a `totp_code` (or `recovery_code`) makes the device. + - **The secret** is 160 random bits, shown once (Base32 and an `otpauth://` URI) and stored only as an AES-256-GCM + ciphertext: a key-name byte, a 12-byte nonce and the ciphertext, with `craft-cloud:totp:v1:` and the account's ID as + associated data, so a ciphertext moved to another account does not open. The key is 32 random bytes in the file the + config names (`[two_factor] key_file`, relative to `config.toml`), which `init` makes and the **server never makes** + (a second server on the same Postgres database, or a restore without the key, would otherwise seal secrets + nobody can open); the AES key is BLAKE3 `derive_key("craft-cloud 2026 totp secret v1", key)` of it. The key's name is + the first byte of the BLAKE3 hash of the key file: one of 256 values, only there to tell the current key from the old one + (the server refuses to start when they are equal). It does not identify a key (two different keys can share a name), and + the GCM authentication tag refuses a wrong key whatever the name says. + **Fail closed**: when the database holds secrets, the server opens **every** one at start and refuses to run if the key + is missing or one does not open, naming the file. With no secrets and no key it runs, and set-up answers `503 + two_factor_unavailable`. **Rotation**: the new key in `key_file`, the old in `key_file_old`, every instance restarted with + both (a running server holds the keys it started with), then `two-factor rekey` rewrites the secrets under the new one + (one account at a time, each read fresh under the account's lock, a final scan, repeatable, refusing when a secret has a + key that is neither), then `key_file_old` goes, after `rekey` reported that nothing is left under another key. Without the key the secrets of a backup are unusable: keep it apart from backups. + - **Recovery codes** are ten of 16 characters in the join codes' alphabet (80 bits, shown in groups of four), stored as + the BLAKE3 hash of the normalised code, no pepper: 80 bits can't be searched even from a leaked database, and the codes + work whatever happens to the key. Each works once; new ones replace all the old. A recovery code stands in for a + code at sign-in, join, turning it off and the admin's confirmation. + - **Set-up** (`POST /account/two-factor/setup`, the password again) keeps a new secret for 10 minutes, bound to the + device that asked and apart from any active secret; another device gets `409 two_factor_pending`; five wrong codes use + it up. `confirm` with the first code turns it on, shows the recovery codes and, in the same transaction, signs every + other device out (`revoke_reason = two_factor`), raises `users.auth_gen` and deletes the account's invite, + pairings and challenges. Two confirmations at once turn it on once. Turning it off (`disable`: password and a code or + recovery code) does the same, and so do `user reset-2fa` and the admin's `reset-two-factor` (which sign **all** devices out). + - **The transaction contract.** Everything that issues a challenge or a device, or uses a proof up, locks the account's + row first (the order every other change of the account uses), checks again under the lock what the handler checked + before (the device still signed in, the password hash still the verified one, `auth_gen`, the account's **current** + two-factor state, the challenge), and only then acts. Sign-in's challenge is made in that transaction: a stale hash + is `401 unauthorized` like a wrong password, an account with it on gets a challenge and no device. The second + step finds the challenge by a plain read to learn the owner, locks the account, checks the challenge again, then uses up + the time step (or recovery code) **and** the challenge and makes the device in one transaction with one commit. + `join` does the same for an invite or reset code. A **time step is accepted once and only a later one after it** + (`last_accepted_step`, updated `WHERE last_accepted_step < step`, shared by sign-in and the admin's confirmations), so + two requests with one code can't both succeed. A challenge is 32 random bytes, kept as a BLAKE3 hash, good 5 minutes, + five tries, once; at most five are open per account (the oldest goes); the address it was made for is only an extra + binding (behind a proxy that hides addresses all clients share one), the secret token is what binds it. A password + change, a reset, turning it off or on and a disable delete the account's challenges. + - **Wrong proofs** are counted and committed in the transaction that refused them, under the account's lock (every check + runs before anything is consumed, so the invite stays, the challenge stays, no device is made): the next request sees the + attempts and the lock, so a challenge takes five wrong proofs and no more, and the tenth sets the lock. Ten wrong ones in a row lock the account's second factor for 15 + minutes; each wrong one after a lock ran out doubles it, up to 24 hours; a right one resets it; `user reset-2fa` lifts + it. While it lasts every path (second step, join, turning off, new recovery codes, the admin's confirmation) answers + `429 two_factor_locked` with `Retry-After`. It is never permanent, so nobody with the bare password can lock an + account out for good. Separate limiter `second_factors`: 10 wrong codes + per account and 30 per address in 5 minutes (a right one is given back), and they count against the sign-in limiter. + - **Join**: an invite or reset code of an account with it on needs `totp_code` or `recovery_code` in the same body; + without it the answer is `401 two_factor_required` (its `challenge` is empty: `/join` has no challenge row, the same + body is sent again with the code) and the code is **not** used up; a wrong factor is `401 two_factor_invalid`, is not + given back to the address's join tries and counts against the account. A reset code made with `reset_two_factor` + turns it off instead of asking. **A pairing needs no second factor** (only a signed-in device can make one, and turning + two-factor sign-in on signs the other devices out), so **any valid device token can add devices**. + - **Status codes**: a wrong code is `401` at sign-in and join (not signed in) and `403` on every signed-in route, where a + 401 would read as a revoked device. The 2FA bodies are at most 4 KiB and a malformed one is `400 invalid`. - **Join codes**: a machine can also get its device token with a code instead of a password. Codes are Crockford base32 (`0-9` and letters without I, L, O, U; on input case is ignored, O reads as 0, I and L as 1, dashes and spaces are dropped), shown in groups of four (`K7M4-Q9TX`), and stored only as their BLAKE3 hash (migration 0004: @@ -630,25 +849,99 @@ changes locally, then pushes local changes. These rules came out of the simulati admins who back up the bucket themselves (bucket versioning or replication); GC must stay paused until that bucket backup is done, or the admin keeps the bucket's own deleted-object retention longer than the GC grace. **Restore** sets a new epoch for every space. -- **Admin CLI**: `init`, `serve`, `user add|reset-password|invite [--revoke]`, `device list|revoke` and `reset-epoch` - exist; `user remove|quota`, `gc`, `verify` (re-hash every chunk), `backup ` and `restore ` come with M4. +- **Web admin and browser sessions** (migration 0011, off unless `[web_admin] enabled = true`): the admin API's operations + also have pages in the browser, under `/web`, **server-rendered HTML in Rust**: no script, no build step, no Node, one + stylesheet served by the server itself (`GET /web/style.css`). The pages are an adapter over the same `craft_cloud_meta` calls + and the same confirmation as the JSON routes, so the rules (last administrator, who may reset whom, quotas) have one home, and the + JSON routes stay as they were. This replaces "no web UI in v1" for **administrators only**: there is no page for a member and + none for files. + - **Setup is explicit.** The pages exist only with `[web_admin] enabled = true`, which the config accepts only with + `behind_proxy = true` and `public_url = "https://host[:port]"` without a path (a TLS proxy in front; `public_url` is the + origin the pages check forms against). Otherwise `/web` is `404` like any unknown route. `idle_minutes` (5 to 1440, default 480) + and `absolute_hours` (1 to 720, default 168) set the session lifetimes; unknown keys are refused. + - **Sessions** (`web_sessions`): the cookie `__Host-craft_cloud_session` holds 32 random bytes (base64url, 43 characters); the + table keeps only their BLAKE3 hash, the account, the account's `auth_gen` at sign-in, `created_ms`, `last_seen_ms`, + `absolute_expires_ms`, `revoked_ms` and `mfa_verified_ms` (when a second factor was given; `NULL` for an account without one). + The cookie is `Secure; HttpOnly; SameSite=Strict; Path=/`, with no `Domain` and no lifetime of its own (the server decides, so + a copied cookie can't outlive its session). A session is valid while it is not logged out, was made less than + `absolute_hours` ago, was used within `idle_minutes` (its last-seen time is moved at most once a minute, by the next + request), the account is neither disabled nor marked for removal, **and the account's `auth_gen` is still the one it + was made under**. Every change that raises `auth_gen` therefore ends the account's sessions with no code of its own: a password + change or reset (device, command line or redeemed code), a disable, an enable, a removal, turning two-factor sign-in on or off, + and a reset of it by an admin or the command line. The role is read from the database for every request, so a demoted admin + gets `403` at once. A change made on a session checks, in its transaction under the account's lock, that the session is still + logged in under the account's current `auth_gen` (the same re-check a device gets) **and still inside its idle and absolute lifetimes at the time + of the change** (the time is read after the change has taken its locks, so a wait for them does not extend a session), so a request that began before the session ended (a slow body, a wait for a lock or the password check) cannot + change anything after it. + - **Sign-in** (`POST /web/login`, then `POST /web/login/second-factor` for an account with two-factor sign-in): the JSON + sign-in's own steps in the same order (name and password shape first, a password-check slot, then the counts, a dummy + hash for a name nobody has), with limiters of their own (`AppState::web_logins`: 30 per client address and 10 per account + in five minutes), so an attack on the pages does not lock the API and the other way round. **Only administrators get a + session**, and every refusal of the password step (unknown name, wrong password, a member, a disabled or removed account) + is the same `401` page, so nothing says which accounts exist or what role they have. The second step uses the second factor's + own code (`totp_code` or `recovery_code`, a time step serving once, five wrong codes deleting the challenge, ten locking the + account for 15 minutes and doubling after that, with the per-account and per-address limits of the device sign-in): the + lock is the account's, so it holds for pages and devices alike. The challenge is a row of `login_challenges` of kind `web`; a + web challenge can never make a device and a device's can never make a session. The sign-in writes `auth.login` with no + device; a failed one the usual `auth.login_failed`. + - **One credential at a time.** The pages know the cookie only; the JSON admin routes know the bearer token only (no JSON + route takes the cookie). A request to either with an `Authorization` header **and** the session cookie is `400 invalid`, + so a client can't be unsure which credential acted. + - **CSRF and origin.** Every form carries `csrf`, a value derived from the session token under a key-derivation context of + its own (so the stored hash can't make it), compared in constant time; missing, wrong or another session's is `403`. Every + request that is not a `GET` or `HEAD`, the sign-in too, must carry an `Origin` equal to the origin of `public_url` or, + without one, a `Referer` on it; an `Origin` that differs (including `null`) is `403` whatever the `Referer`. No `GET` + changes anything (the audit export is a read). The sign-in form has no CSRF token, as no session exists to bind one to: + the origin check is what stops another site from signing a browser in. + - **Headers on every answer under `/web`**, errors and redirects included: `Content-Security-Policy: default-src 'self'; + script-src 'none'; style-src 'self'; img-src 'self'; connect-src 'none'; object-src 'none'; base-uri 'none'; form-action + 'self'; frame-ancestors 'none'`, `X-Content-Type-Options: nosniff`, `Referrer-Policy: no-referrer`, `X-Frame-Options: DENY`, + `Cross-Origin-Opener-Policy` and `Cross-Origin-Resource-Policy: same-origin`, a restrictive `Permissions-Policy`, and + `Cache-Control: no-store` (the stylesheet `no-cache`). No page has a script, an inline style, an event handler or a file from + another host. `Strict-Transport-Security` is the proxy's to send. + - **Statuses** are one contract for the pages: `401` for a missing, expired or unknown session (a `GET` is sent to `/web/login` + with `303` instead; a form post gets the sign-in page), for a wrong username or password at sign-in, and for a sign-in step + (challenge) that is unknown, used up or expired; `403` for a member or demoted admin, a wrong second factor (the page of the + second step again, with the same challenge), a wrong confirmation password or code, a bad CSRF token or origin; `400` for a + malformed form, **including a body that is not a form (no `415`)**; `429` for the limits and the lock, **always with + `Retry-After`** (the end of the window that refused, or the lock's); `413` over 4 KiB; `409`/`404` as the store says. A sign-in + for an account that has no password yet (an open invite) spends the same password check as an unknown name and fails the + same way, in the pages and in `POST /auth/login`. `/web/` has the same headers and origin check as the other paths. Every change asks for the **confirmation in its form**: the admin's password, and `totp_code` or + `recovery_code` when their account has two-factor sign-in (the server asks whatever the page showed), checked by the + JSON routes' function with their counters, so a wrong password counts against the same five-in-fifteen-minutes window. + - **Pages**: overview (counters, kinds), users (search, 50 a page by cursor, create), one user (role and quota, disable, enable, + remove, invite, password reset code, end the open code, reset two-factor, devices with revoke), the audit log (filters + `user`, `action`, 50 a page by cursor, and `GET /web/audit/export`, the JSON export's NDJSON download). Invite and reset + codes are the answer to the `POST` that made them, shown once and kept nowhere; a reload asks the browser to post the form again. + Every value not written by the server's code is HTML-escaped on the way in, including a device's name and platform. + - **Cleanup without a periodic task.** A sign-in deletes up to 100 sessions that are over (logged out, past their lifetime, + idle); `craft-cloud-server web-sessions prune [--dry-run]` deletes all of them (run it from cron, as `audit prune`). Neither + touches a valid session. + - Nothing secret is logged (cookie, CSRF token, challenge, password, codes); events are logged at `info` with kind and target ID. +- **Admin CLI** (the way in without HTTP: it asks for no confirmation and does not enforce `last_admin`, since whoever runs it + has the files): `init` (which also makes the two-factor key file), `serve`, `user add|reset-password [--keep-devices]|invite [--revoke]|reset-2fa`, + `device list|revoke|revoke-all`, `web-sessions prune [--dry-run]`, `two-factor rekey` and `reset-epoch` exist; `user remove|quota`, `gc`, `verify` (re-hash every chunk), `backup ` and `restore ` come with M4. `user invite` prints the join string and link when `public_url` is in the config, else the code and how to finish it; it logs that an invite was made (the username at debug only, never the code). -- No web UI in v1. No telemetry, no update check. +- No web UI for files, and none for members. No telemetry, no update check. API (`/api/v1`, JSON except chunk and manifest bodies; every signed-in response carries the space's epoch in the `craft-epoch` header, and commits/tree/changes carry it in the body too). Errors are -`{error, message, conflicts?, missing?, quota?}` with stable codes (`conflict` 409, `missing_chunks` 409, +`{error, message, conflicts?, missing?, quota?, two_factor?}` with stable codes (`conflict` 409, `missing_chunks` 409, `resync_required` 410, `unauthorized` 401, `rate_limited` 429, `invalid` 400, `not_found` 404, `too_large` 413, `timeout` 408, `quota_exceeded` 507, -`username_taken` 409, `invalid_code` 404, `password_required` 400, `weak_password` 400, `internal` 500; the codes are constants in `craft-cloud-proto::api::codes`). Unknown routes and +`username_taken` 409, `invalid_code` 404, `password_required` 400, `weak_password` 400, `wrong_password` 403, `forbidden` 403 (an +admin route called by a member), `account_disabled` 403 (sign-in and join only), `last_admin` 409, `two_factor_required` 401 (with +`two_factor`), `two_factor_invalid` (401 at sign-in and join, 403 on signed-in routes), `two_factor_unavailable` 503, +`two_factor_pending` 409, `two_factor_locked` 429 (with `Retry-After`), `internal` 500; the codes are constants in `craft-cloud-proto::api::codes`). Unknown routes and methods answer `not_found` / `invalid` (405) the same way: | Call | Purpose | |---|---| | `GET /info` (also unversioned at `/api/info`) | protocol versions, chunker parameter sets, limits (no auth); a client probes `/api/info` first | | `GET /space` | the signed-in user's space: space ID, root node ID, epoch, current seq, username | -| `POST /auth/login` | username + password + device name → device ID + token (shown once); 10 tries per account and 30 per address per 5 minutes; a wrong password and an unknown user look the same | +| `POST /auth/login` | username + password + device name (+ optional `client_kind` `agent`\|`desktop`\|`cli`, `platform` and `app_version`, each at most 32 bytes, shown in the device list and never a basis for a permission; an unknown kind or a control character is `invalid`, checked before the try is counted) → device ID + token (shown once); 10 tries per account and 30 per address per 5 minutes; a wrong password and an unknown user look the same. For an account with two-factor sign-in a right password gives **no token**: `401 two_factor_required` with `two_factor: {challenge, expires_ms}` (the challenge is good 5 minutes, five tries, once) | +| `POST /auth/login/second-factor` | `{challenge, totp_code}` or `{challenge, recovery_code}` (no auth, at most 4 KiB) → device ID + token as a sign-in; a challenge that is unknown, expired, used or no longer fit is `401 unauthorized` (the same for each), a wrong code `401 two_factor_invalid`, a malformed one `400 invalid`, a lock `429 two_factor_locked` with `Retry-After`; `second_factors` limiter | | `GET /tree?after_seq=…&after_node=…&limit=…` | full listing for a first sync or a reconcile, in pages of up to 5,000 (cursor = changed seq + node ID) | | `GET /changes?since=N&after_seq=…&after_node=…&limit=…&wait=30` | changes after N, in pages, long-poll up to 60 s; `resync_required` if N is too old or ahead of the server | | `POST /chunks/missing` | which of these chunk IDs (up to 10,000) this space lacks | @@ -656,10 +949,33 @@ methods answer `not_found` / `invalid` (405) the same way: | `PUT /manifests/{id}` / `GET /manifests/{id}` | one manifest (binary); the server checks the ID, the limits and that every chunk is present | | `POST /commit` | batch of version/mkdir/move/delete operations, one transaction; `409 conflict` listing every conflicting operation (a version whose manifest isn't uploaded is a `missing_manifest` conflict) | | `GET /nodes/{id}/history` / `POST /nodes/{id}/restore` | list versions / make an old one current (M4) | -| `GET /devices`, `DELETE /devices/{id}` | your devices | +| `GET /account` | the signed-in account, flat: `{user_id, username, role, created_ms, password_changed_ms, two_factor_enabled}` (`password_changed_ms` is `null` until the password changes through the server) | +| `GET /account/two-factor` | `{enabled, enabled_ms, recovery_codes_left, pending}` | +| `POST /account/two-factor/setup` | `{password}` → `{secret, otpauth_uri, expires_ms}` (secret Base32 without padding, shown once; the set-up lasts 10 minutes, belongs to this device, replaces none that is on). `409 conflict` when it is on, `409 two_factor_pending` when another device has a set-up open, `503 two_factor_unavailable` without a key, `403 wrong_password`; the password's tries are the password change's (5 wrong per account per 15 minutes) and take a check slot | +| `POST /account/two-factor/confirm` | `{totp_code}` → `{recovery_codes: [10]}` shown once. Turns it on, signs every other device out, deletes the invite, pairings and challenges. At most 5 wrong codes per set-up; `403 two_factor_invalid`; `409 conflict` for no set-up, an expired one, or one another device started | +| `POST /account/two-factor/disable` | `{password, totp_code}` or `{password, recovery_code}` → 204. Deletes the secret, codes and challenges and signs every other device out; wrong code `403 two_factor_invalid`, `429 two_factor_locked` | +| `POST /account/two-factor/recovery-codes` | `{password, totp_code}` → `{recovery_codes: [10]}`; the old ones stop working | +| `POST /account/password` | `{current_password, new_password}` → 204. Signs every *other* device out and deletes the account's open invite and pairings, in one transaction; the device that asked stays signed in. A wrong current password is `403 wrong_password` (never 401: see below), a new one that breaks the rules or equals the current one `400 weak_password`; 5 tries per account per 15 minutes (`429 rate_limited`); body at most 4 KiB, passwords at most 1,024 bytes | +| `GET /devices` | your devices: `{id, name, created_ms, last_used_ms, revoked, current, client_kind, platform, app_version, revoked_ms, revoke_reason}`; the last five are new and a client may ignore them (`revoke_reason` is `user`, `others`, `password`, `admin`, `disabled`, `two_factor`, `idle` or `reset`) | +| `PATCH /devices/{id}` | `{name}` (at most 4 KiB, the device-name rule) → 200 with the device. Only your own devices that are not revoked, otherwise `404 not_found` | +| `POST /devices/revoke-others` | signs out every other device of the account (and deletes their pairings) → `{revoked: n}`, 0 when there is none | +| `DELETE /devices/{id}` | signs one of your devices out → 204; its time and reason (`user`) are kept, and a second call changes neither | | `POST /pairings` | a new add-a-computer code from this device → `{id, code, expires_ms}` (the code shown once, normalised) | | `GET /pairings/{id}`, `DELETE /pairings/{id}` | one of your pairings: `{state: waiting\|used\|expired, device_name?}` / cancel it (204) | -| `POST /join` | code + device name (+ password for an invite to a new account) → device ID + token, as a sign-in (no auth); `invalid_code`, `password_required`, `weak_password`, `rate_limited` | +| `POST /join` | code + device name (+ password for an invite to a new account **and always for a password reset code**, + the optional client fields of a sign-in) → device ID + token, as a sign-in (no auth); `invalid_code`, `password_required`, `weak_password`, `account_disabled`, `rate_limited`. A reset code signs every other device of the account out. Optional `totp_code` or `recovery_code`: an invite or reset code of an account with two-factor sign-in needs one (`401 two_factor_required` without, code not used up; `401 two_factor_invalid`, not given back to the address's tries), unless the reset code was made with `reset_two_factor`; a pairing code never does | +| `GET /admin/server` | (admin) `{version, users, devices_active, stored_bytes, live_bytes, storage_kind (disk\|s3\|memory), database_kind (sqlite\|postgres)}`: counters and kinds only, no path, address or setting | +| `GET /admin/users?cursor&limit&query` | (admin) `{users: [{id, username, role, created_ms, disabled_ms, two_factor_enabled, devices_active, stored_bytes, quota_bytes, invite_pending}], next?}` by username; `limit` 1 to 200 (default 50, otherwise `invalid`); `cursor` is opaque (the last username, base64url) and `next` is missing on the last page; `query` finds usernames containing it (case-insensitive, at most 64 bytes of printable characters, `%` and `_` are ordinary characters). Accounts marked for removal are left out | +| `POST /admin/users` | (admin, confirmation) `{username, role, password}` → `{user, invite: {code, join_url?, expires_ms}}`: an account with no password and a 7-day invite; `username_taken` 409 (also for an account marked for removal), `invalid` for a bad name or role | +| `PATCH /admin/users/{id}` | (admin, confirmation) `{role?, quota_bytes?, password}` → the user. Neither field is `invalid`; a missing `quota_bytes` leaves it, `null` removes it; `last_admin` 409 | +| `POST /admin/users/{id}/disable`, `/enable` | (admin, confirmation) → 204. Disable signs every device out and deletes the codes and pairings; not oneself (`invalid`), `last_admin`; enabling an account marked for removal is `conflict` | +| `DELETE /admin/users/{id}` | (admin, confirmation) → 202. Marks the account for removal as above; nothing is deleted | +| `POST /admin/users/{id}/invite`, `DELETE …/invite` | (admin, confirmation) → `{code, join_url?, expires_ms}` / 204: an invite only for an account that has no password (`conflict` otherwise); the delete ends the account's invite or reset code (`not_found` when there is none) | +| `POST /admin/users/{id}/password-reset` | (admin, confirmation) `{reset_two_factor?: bool}` → `{code, join_url?, expires_ms}`: a one-hour reset code, redeemed at `POST /join`; with `reset_two_factor` redeeming it also turns the account's two-factor sign-in off, else an account that has it needs its second factor to redeem. Another admin's needs two-factor sign-in on the acting admin's own (`forbidden`), one's own is `invalid` | +| `POST /admin/users/{id}/reset-two-factor` | (admin, confirmation) → 204: takes the account's two-factor sign-in away, deletes its challenges and signs **every** device out (`revoke_reason = two_factor`); not one's own (`invalid`), none to reset is `409 conflict`, another admin's needs the acting admin's own two-factor sign-in (`forbidden`) | +| `GET /admin/users/{id}/devices`, `DELETE …/devices/{device_id}` | (admin; the delete with confirmation) → `{devices: [device as in GET /devices]}`, revoked ones too / 204 (`revoke_reason = admin`); another user's device or an unknown user is `not_found` | +| `GET /account/activity?cursor&before_ms&limit` | the signed-in account's own security events, newest first: `{events: [event], next?}`; the events it acted in or was the target of, with the device shown only for its own acts, and the details only of events about itself (not of what a former administrator once did to others). `limit` 1 to 100 (default 50, otherwise `invalid`); `cursor` is opaque and exclusive and wins over `before_ms`, which only limits the first page; a cursor this server did not make is `invalid` | +| `GET /admin/audit?cursor&before_ms&limit&user&action` | (admin) the whole log in the same form (`limit` up to 200); `user` is an account ID (events it acted in or was the target of), `action` an event name or a group ending in a dot like `auth.`; anything else is `invalid` | +| `GET /admin/audit/export?user&action&before_ms` | (admin) the same filters (`before_ms` must not be negative) as NDJSON (`application/x-ndjson`, always offered as `craft-cloud-audit.ndjson`), streamed: one event per line, newest first, then one last line, `{"complete":true,"exported":n}`, or `{"truncated":true,"exported":n}` when there were more events than the export holds (100,000). A stream that ends without either line broke off and is not a whole export | **Client contract** (what the sync agent relies on): @@ -672,7 +988,13 @@ methods answer `not_found` / `invalid` (405) the same way: `version_exists` conflicts for exactly the retried operations mean the first attempt applied. - A long-poll refused because 16 are already waiting on the space answers `429 rate_limited` with `Retry-After`; wait that long before polling again. -- Unknown JSON fields are ignored in both directions, so later protocol versions can add fields. +- Unknown JSON fields are ignored in both directions, so later protocol versions can add fields. The device list's client + fields and revoke time and reason were added that way: a body without them reads as empty. +- **A signed-in route never answers 401 for anything but the token.** The agent reads every 401 on a signed-in route as + "this device was revoked" and signs out. A wrong password on a route that asks for one (`POST /account/password`) is + therefore `403 wrong_password`; 401 is for a missing, unknown or revoked token (a disabled account's tokens are revoked), and for + the sign-in and join routes. The admin routes follow the same rule: a wrong confirmation password is 403, a member is 403. + A wrong code or recovery code is `403 two_factor_invalid` on a signed-in route and `401` only at sign-in's second step and join. - After an admin restores a backup they run `craft-cloud-server reset-epoch --all`; agents then reconcile. ## Agent and launcher API @@ -691,6 +1013,16 @@ methods answer `not_found` / `invalid` (405) the same way: Calls: `status`, `login`, `logout`, `folders` (get/set synced or cloud-only), `history`, `restore`, `conflicts`, `pending-deletes` (approve or reject a mass-delete pause), `pause`, `resume`. + + Two-factor sign-in (see [`docs/agent.md`](../agent.md)): `login` and `join` answer 401 `two_factor_required` with the + server's challenge when the account has it; `POST /v1/login/second-factor {challenge, totp_code | recovery_code}` finishes + a login, and `join` is sent again with `totp_code` or `recovery_code` (its challenge is empty: the invite isn't consumed + by the first try). `GET /v1/account/two-factor`, `POST .../setup`, `.../confirm`, `.../disable`, `.../recovery-codes`, + `POST /v1/account/password` and `POST /v1/devices/revoke-others` pass through to the server for the app's Security + page. The local answers keep the server's code: `two_factor_invalid` (401 at sign-in, 403 when signed in), + `two_factor_locked` (429 with `Retry-After`), `wrong_password`, `two_factor_pending`, `two_factor_unavailable`; a gone + challenge is 409 `two_factor_expired`. Only a 401 on a signed-in call means this device was signed out; a 403 or 429 + never does. Codes, passwords and secrets are never stored and are redacted in every `Debug`. - **CLI**: `craft-cloud-agent login | status | pin | unpin | history | restore | approve-deletes | pause | resume`. Same operations as the local API, so it is usable and testable before any launcher work. @@ -1158,7 +1490,7 @@ From the reviews of M3c, less what their fix rounds did: ## Out of scope for Phase 1 -Hosted service; collaboration and sharing; locking; web UI; mobile; virtual files and on-demand download; +Hosted service; collaboration and sharing; locking; a web UI for users' files (the administrators' pages, see "Web admin and browser sessions", are in); mobile; virtual files and on-demand download; end-to-end encryption (not built, but chunk IDs are opaque and stored chunks have a format byte so it can come later); per-chunk compression (same format byte); a packed small-chunk upload endpoint (one request per chunk until measurements say otherwise); compatibility with any other sync service. diff --git a/docs/server.md b/docs/server.md index bf2a6492..94f27f7f 100644 --- a/docs/server.md +++ b/docs/server.md @@ -25,6 +25,9 @@ For a non-loopback `--listen`, `init` also needs `--behind-proxy` and the proxy' For Postgres or S3, edit `[database]` and `[storage]` in `config.toml`. +`init` also makes `two-factor.key` next to `config.toml` and names it in the config (`[two_factor] key_file`). It is the key +for two-factor sign-in (below). **Back it up, apart from your database backups.** + To cap what one account may store, set `stored_bytes_per_user` (in bytes; there is no cap by default) in `config.toml`. It counts every chunk and manifest an account uploaded once, and nothing frees space until garbage collection arrives, including what a refused file stored before it was refused. A file that would go @@ -55,9 +58,243 @@ invite replaces the old one, and `user invite --revoke name` ends it. Every late that is signed in (`craft-cloud-agent add-computer`, see [`agent.md`](agent.md)). Codes that don't work are refused alike (wrong, expired or used), and an address that tries ten in a minute is held off for the rest of it. +## Devices, passwords and signing out + +An account's devices (computers and apps) each hold a token that does not expire. Accounts end them like this: + +- A person changes their own password in a Craft Cloud app that offers it (`POST /api/v1/account/password`). That signs + out every other device of the account, deletes its open invite and add-a-computer codes, and keeps the device + that asked. Five tries (wrong passwords count) are allowed per account every 15 minutes. +- `craft-cloud-server user --config cc-data/config.toml reset-password name` sets a new password (read from standard + input), **signs out every device of the account** and deletes its open invite and add-a-computer codes: a token that + was stolen does not survive the reset. It prints how many devices were signed out. Add `--keep-devices` to leave the + devices signed in (the old behaviour); the codes are deleted either way. +- `craft-cloud-server device --config cc-data/config.toml revoke-all name` signs out every device of an account + without touching the password. `device revoke name DEVICE_ID` signs out one; `device list name` shows them. + +The command line runs in its own process, so it cannot reach the server's open connections. A signed-out device's live +connection ends at its next ping (within about 30 seconds), and a change-feed long-poll within 60 seconds (when it +ends, it is answered `401 unauthorized`, never with changes); every other request is refused at once (`401 +unauthorized`). Devices signed out through the API end their live connections +immediately. + +## Two-factor sign-in + +An account can turn on a second factor in an app that offers it: a code from an authenticator app (TOTP: Aegis, 1Password, +Google Authenticator and the like), with ten recovery codes to keep. With it on, a right password does not sign anyone in: +the app also asks for the code, and so does joining a computer with an invite or an admin's password reset code. A +computer joined with an add-a-computer code (a pairing) asks for nothing, since only a computer that is already signed in +can make one: **any signed-in computer can add more**, and turning two-factor sign-in on signs every other one out. + +- **The key.** The authenticator's secret is stored only encrypted (AES-256-GCM, bound to its account), with the key in the + file `[two_factor] key_file` names (32 random bytes; a relative path is relative to `config.toml`). `init` makes it; **the + server never makes it** (a second server on the same database, or a restore without the key, would otherwise seal + secrets nobody can open). **Without this key the secrets in a database backup are unusable**: keep a copy apart from + the backups, and keep it out of everyone's reach. On Unix `init` makes it owner-only; + **on Windows the server sets no file permissions**, so put it somewhere only the account that runs the server can read. + The recovery codes do not depend on the key, so they still work if it is lost; the authenticator codes do not. +- **Failing closed.** When the database holds two-factor secrets, `serve` opens every one of them at start and refuses to + run if the key is missing, unreadable, or does not open all of them; the error names the key file the config expects. + A server without a key and without any secrets runs, and setting it up answers `503 two_factor_unavailable`. Several + servers on one Postgres database must all have the same key. +- **Rotating the key.** A running server keeps the keys it started with, so rotate in this order: (1) make a new key file, + put it in `key_file` and the old one in `key_file_old`; (2) **restart every server instance on the database** with that + config (until then an instance still holds only the old key and goes on sealing new secrets with it); (3) run + `craft-cloud-server two-factor rekey --config cc-data/config.toml` (it rewrites every secret, active and set-up, under the + new key, one account at a time, reads each account's secrets fresh under its lock so a set-up confirmed meanwhile is not + missed, scans again at the end, can be run again, prints only counts, and refuses without changing anything if a secret + was sealed with a key that is neither); (4) remove `key_file_old` and restart **only after `rekey` has reported that every + secret is under the current key** (an error that says secrets are still under another key means it is not done: restart the + instances you missed and run it again). The server runs in the middle of a rotation, with both keys. +- **A locked-out account.** A person who lost their authenticator and their recovery codes: `craft-cloud-server user + --config cc-data/config.toml reset-2fa name` deletes its two-factor secret, recovery codes and sign-in challenges, lifts + any lock, **signs every device out** (it prints how many) and deletes its open invite and add-a-computer codes. They sign + in with their password and can set it up again. Over HTTP an admin does the same with `POST /admin/users/{id}/reset-two-factor` + (below). Both need no key. +- **Wrong codes lock for a while.** Ten wrong codes or recovery codes in a row (at sign-in, joining, turning it off, + making new recovery codes, an admin's confirmation) lock the account's second factor for 15 minutes; each wrong one after + that doubles the time, up to 24 hours; a right one resets it. While it lasts everything answers `429 two_factor_locked` + with `Retry-After`. The lock is never for good, so someone who only has a password can not lock an account out forever. + Wrong codes also count against the sign-in limit (10 per account and 30 per address in 5 minutes, then `429`). +- **Recovery codes** are ten of 16 letters and digits (shown in groups of four), each good once. Only a hash is stored. +- **Older apps.** An agent from before this release shows every `401` at sign-in as "wrong password", so an account with + two-factor sign-in can't sign in with it (it never gets a token). Update the server first, then the Craft Cloud + desktop package with its agent, then stand-alone agents. +- **Logs** never carry a secret, code, recovery code, challenge, password or key. + +## Managing accounts over HTTP + +Accounts have a role, `admin` or `member`, and the routes under `/api/v1/admin` are for admins only (the first admin is +still made by `init`). They are an API for a tool or an admin app; the server's own admin pages are an option, see "The admin +pages in the browser" below. Every change asks +for **a fresh confirmation in its body**, the acting admin's own current password (and, for an admin who has two-factor +sign-in, a `totp_code` or `recovery_code` too), because a device's token never expires and must not be enough on its own: + +```sh +curl -s -X POST https://cloud.example.org/api/v1/admin/users/USER_ID/disable \ + -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -d '{"password":"your password"}' +``` + +- `GET /admin/server` shows counters (accounts, signed-in devices, stored bytes, live-document bytes) and the kinds of + database and storage, nothing else. `GET /admin/users` lists accounts 50 at a time (`limit` up to 200, `cursor` from + the last answer's `next`, `query` to find a username). +- `POST /admin/users` makes an account (role `admin` or `member`) with no password and gives an **invite**, shown once: + the person chooses their password when they join with it (a whole join link is included when `public_url` is set). + `POST /admin/users/{id}/invite` makes a new invite, but only for an account that still has no password; for one with a + password, make a **password reset code** (`POST …/password-reset`, good for an hour). Whoever redeems + it in Craft Cloud chooses a new password, **every device of the account is signed out, and the computer that redeems + it is signed in**. If the account has two-factor sign-in, redeeming the code needs its second factor too, unless the + admin asked for `"reset_two_factor": true`, which turns it off with the redeem. Codes are only shown in the answer, + never in the log. +- `PATCH /admin/users/{id}` changes the role and/or `quota_bytes`, a limit on what that account may store that replaces + `stored_bytes_per_user` for it (at least 4 MiB; `null` removes it; it also holds when there is no server-wide cap; one + below what the account stores deletes nothing and refuses new bytes). +- `POST …/disable` signs every device of an account out and refuses its sign-in and joins (`account_disabled`); + `POST …/enable` allows them again but signs nothing back in. `DELETE /admin/users/{id}` marks an account for removal: + it is disabled for good and leaves the list, its name stays taken, and **nothing it stored is deleted yet** (that comes with + garbage collection). You cannot disable or remove yourself, and the last enabled admin cannot be demoted, disabled or + removed (`last_admin`). +- `GET /admin/users/{id}/devices` lists an account's devices and `DELETE …/devices/{device_id}` signs one out. +- `POST …/reset-two-factor` takes an account's two-factor sign-in away (it lost its authenticator and its recovery codes): + its secret, recovery codes and sign-in challenges go, and **every device is signed out**. Not your own account (turn + that off in your account), and nothing to reset is `409 conflict`. +- Resetting another **admin's** password or two-factor sign-in over HTTP needs two-factor sign-in on your own account + (`403 forbidden` without it); the command line on the host is the way if no admin has it. +- **A code serves one change.** The server accepts one 30-second step of an account's authenticator once, and only a later + step after it; sign-in and the admin's confirmations share that. So a second change in the same 30 seconds takes the + next code (wait for it) or an unused recovery code. + +A wrong confirmation password is `403 wrong_password` (never 401); five wrong ones in 15 minutes hold the account off for +the rest of the window. A wrong or missing code for an admin who has two-factor sign-in is `403 two_factor_invalid`. A signed-out device's live connection ends at once in the server process that did it; another +server process on the same database, or the command line, finds out at the connection's next ping (within about 30 seconds). +The command line stays as the way in without HTTP and enforces neither the confirmation nor `last_admin`. + +## The admin pages in the browser + +The server can serve admin pages: users, invites and reset codes, a user's devices, the audit log and its download, in +plain server-rendered HTML (no script, no Node, nothing to build). They are **off by default** and only administrators can +sign in. They do the same things as the admin API with the same confirmation (your password again for every change, and a +code when your account has two-factor sign-in). + +```toml +public_url = "https://cloud.example.org" # the address of the pages, https, no path +behind_proxy = true +trusted_proxies = ["10.0.0.2"] + +[web_admin] +enabled = true +idle_minutes = 480 # a session ends when unused for this long (5 to 1440, default 8 hours) +absolute_hours = 168 # and at the latest this long after signing in (1 to 720, default 7 days) +``` + +Open `https://cloud.example.org/web` and sign in as an admin. The server refuses to start with `enabled = true` unless +`behind_proxy = true` and `public_url` is an `https://` address with no path: the pages' cookie is `Secure` (a browser only +sends it over HTTPS) and every form is checked against the origin of `public_url`. + +**Behind the proxy.** + +- The proxy terminates TLS and forwards `/web` and `/api` (and `/join`) to the server's `listen` address. It must pass the + `Origin` and `Cookie` headers through and must not rewrite the `Host` to something that changes the origin the browser + sees: browsers send `Origin: https://cloud.example.org`, and anything else is refused with `403`. Serving the pages under a + path prefix is not supported (`public_url` may not have a path). +- Put the proxy in `trusted_proxies` and let it **overwrite** `X-Forwarded-For` with the client's address; the sign-in limits + count the address that yields. A proxy that passes a client's own `X-Forwarded-For` along lets a visitor pick the address + the limits count. A misconfigured server shows itself at start (the config is refused) or as `403` on every form (the + proxy changed the origin). +- Send `Strict-Transport-Security` from the proxy; the server does not. Do not cache `/web` in the proxy: every answer says + `Cache-Control: no-store`. +- Bind `listen` to the proxy-facing address only. The cookie never leaves the browser over plain HTTP, but the plain HTTP + port must not be reachable by anyone but the proxy. + +**Sessions.** The session is a random value in the cookie `__Host-craft_cloud_session` (`Secure`, `HttpOnly`, +`SameSite=Strict`); the database keeps only its hash. A session ends when you sign out, after `idle_minutes` without use, +after `absolute_hours`, and **whenever the account changes**: a password change or reset, disabling, enabling or removing it, +and turning its two-factor sign-in on or off or resetting it. A demoted admin is out at the next request. Signing in again +makes a new session; there is no "remember me". Because the cookie is `SameSite=Strict`, opening the pages from a link on +another site shows the sign-in page once, and the next click on the pages themselves works. + +**Cleaning up.** Expired sessions are deleted a few at a time when someone signs in. On a server that is signed in to rarely, +or to empty the table: + +```sh +craft-cloud-server web-sessions --config cc-data/config.toml prune # deletes what is over, prints how many +craft-cloud-server web-sessions --config cc-data/config.toml prune --dry-run # only counts +``` + +Run it from cron or the task scheduler (daily is plenty), like `audit prune`. A valid session is never touched. There is no +background task. + +**What the log shows.** A sign-in to the pages is `auth.login` without a device; changes made there are the usual +`admin.*` events with the admin as the actor and no device. Failed sign-ins, second-factor failures and the lock are the +usual `auth.*` events. Nothing secret is logged: not the cookie, the page's forms, a code or a password. + +**Accessibility.** The pages are plain HTML: labelled fields, tables with captions and header cells, a skip link, visible +focus and no script, so they work with a keyboard and a screen reader and scale with the browser's zoom. They have been +checked by reading the markup and the tests, **not yet with a screen reader**: report what does not work. + +## The security audit log + +The server keeps a log of the events that matter for who may do what, in its database: sign-ins and failed sign-ins (a +row per account, client kind and minute, with a count), devices signed out, password changes, an administrator's changes to accounts, +roles, quotas, invites, reset codes and devices, codes redeemed on a new computer, and what the command line does to an +account (`user reset-password`, `user reset-2fa`, `device revoke`, `device revoke-all`). Two-factor sign-in writes its +changes, a recovery code's use, a lock and the wrong second steps of a sign-in or a join (never a code), but not each wrong code +on a signed-in route. Reading it: + +- Every account sees its own events, and what was done to it, with `GET /api/v1/account/activity`. +- Admins read all of it with `GET /api/v1/admin/audit` (filters `user`, an account ID, and `action`, an event name like + `auth.login` or a group like `auth.`; pages by `cursor`) and export it with `GET /api/v1/admin/audit/export`, newline + delimited JSON, newest first. The **last line** says whether the export is whole: `{"complete":true,"exported":n}`, or + `{"truncated":true,"exported":n}` when the log holds more than the 100,000 events an export carries (narrow it with + `user` and `action`). An export that ends without either line broke off: do not treat it as complete. Reading the log is + not logged. +- On the server's host: `craft-cloud-server audit --config cc-data/config.toml list [--user NAME] [--action auth.] [--limit N]` + prints the newest events as text (accounts by ID, not by name). + +**What is in it, and what is not.** A row has a time, the IDs of the account and device that acted and of what it acted +on, an event name, success or failure, and a few typed values (how many devices were signed out, the role before and after, +which kind of client and which platform out of a short list). Client kinds are `agent`, `desktop`, `cli` and `web`; `web` +means a browser session of the admin pages. It has **no IP addresses, no file names, no device names, no +names of accounts that do not exist, no passwords, codes, tokens or other secrets**; what a client types is never copied +into it. Because it holds account IDs and times it is personal data about the people who use the server: tell them +what you keep it for (finding out who changed what and who signed in or tried to), and for how long. An event keeps the ID +of an account that was removed; the name is not stored. + +**How long.** Events are kept 365 days. Set `retention_days` (1 to 3650) in an optional `[audit]` section of `config.toml`: + +```toml +[audit] +retention_days = 365 +``` + +`craft-cloud-server audit --config cc-data/config.toml prune` deletes the events older than that and prints how many +(`--dry-run` only counts). The server also deletes expired events when it writes a new one, at most once an hour and in small +batches that never hold up a sign-in; **a server nobody uses keeps its expired events** until `prune` runs, so run it from cron +or the task scheduler, for example daily. There is no background task. + +The log is append-only in the server's code, not enforced by the database (a design choice: the same code runs on SQLite and Postgres, and SQLite has no database roles; triggers exist but are not used). Someone +who can write to the database can change it; keep the database file and Postgres user private. + +## Upgrading + +This release adds migrations (`0007`, device sessions, `0008`, the admin API: reset codes, per-account quotas, the +removal mark, `0009`, the audit log, `0010`, two-factor sign-in: the secrets, recovery codes and sign-in challenges, and +`0011`, the admin pages' browser sessions). The server applies them when it starts. A database it +has migrated cannot be opened by an older server: that one refuses to start ("the database was created by a newer Craft +Cloud"). Rolling back therefore means restoring a backup taken before the upgrade, so take one first. Upgrade the +server first: agents and apps keep working unchanged, **except that an account that turns on two-factor sign-in can't sign in +with an agent from before this release** (see "Two-factor sign-in"). Then update the desktop package with its matching agent, +then agents installed on their own. The `init` of a data directory made before this release did not make a key: add one +with `[two_factor] key_file` in `config.toml` (32 random bytes, owner-only) before anyone sets up two-factor sign-in. A config +that has the new `[audit]` or `[web_admin]` section is refused by an older server (it rejects unknown settings), so remove the section before +going back to one. To turn the admin pages on after the upgrade: take the backup, start the new server (it migrates), then add +`[web_admin]` as described above and restart behind the proxy. Several servers on one Postgres database must all run this +release before any of them is given `[web_admin]`, as for every migration. + ## Restoring a backup -With the server stopped, restore the database and the chunk storage from the backup, then run +With the server stopped, restore the database and the chunk storage from the backup (and the two-factor key file, if +accounts use two-factor sign-in: the server will not start without it), then run `craft-cloud-server reset-epoch --config cc-data/config.toml --all`, and only then start `serve` again. Every account then starts a new epoch, so each synced machine reconciles with the restored state instead of trusting its own change counter. `reset-epoch --config … name` resets one account. (A reset run while the server is up still