diff --git a/Cargo.lock b/Cargo.lock index 9a18387..a268962 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,18 +8,6 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" -[[package]] -name = "ahash" -version = "0.8.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" -dependencies = [ - "cfg-if", - "once_cell", - "version_check", - "zerocopy", -] - [[package]] name = "aho-corasick" version = "1.1.5" @@ -133,15 +121,6 @@ dependencies = [ "object", ] -[[package]] -name = "arbitrary" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" -dependencies = [ - "derive_arbitrary", -] - [[package]] name = "argon2" version = "0.5.3" @@ -924,6 +903,15 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "constant_time_eq" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eb6feb175df65d0a3ccd3e73484e940d0ac664e8034f0519fd49a93db3071d9" +dependencies = [ + "rustversion", +] + [[package]] name = "convert_case" version = "0.10.0" @@ -1217,17 +1205,6 @@ dependencies = [ "serde_core", ] -[[package]] -name = "derive_arbitrary" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b034bd7d5f032402a2479444dcc6f74e36a03f31854d41680fb240ef682a1ac" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - [[package]] name = "derive_more" version = "2.1.1" @@ -2085,15 +2062,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -[[package]] -name = "hashbrown" -version = "0.14.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" -dependencies = [ - "ahash", -] - [[package]] name = "hashbrown" version = "0.17.1" @@ -2107,11 +2075,11 @@ dependencies = [ [[package]] name = "hashlink" -version = "0.9.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" +checksum = "a596f1b20ed2cc5ecac41a164aaebc7258057060f06c0cf7a2ba3991ee7990fb" dependencies = [ - "hashbrown 0.14.5", + "hashbrown 0.17.1", ] [[package]] @@ -2779,9 +2747,9 @@ dependencies = [ [[package]] name = "libsqlite3-sys" -version = "0.30.1" +version = "0.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" +checksum = "b06e69070b7e12262b0cd90832f0a7396f142a2b330181255562decf9f0c6215" dependencies = [ "cc", "pkg-config", @@ -4258,11 +4226,20 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rsqlite-vfs" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "353b74f9810d5b085b6c42c9d86a5f0fdb795553235cdd48753b32e3809d18a7" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "rusqlite" -version = "0.32.1" +version = "0.43.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e" +checksum = "f172c4812a19b26d1c23cdb0070e69db335503dfa378b910eb857386e5b8beba" dependencies = [ "bitflags 2.13.2", "fallible-iterator", @@ -4270,6 +4247,7 @@ dependencies = [ "hashlink", "libsqlite3-sys", "smallvec", + "sqlite-wasm-rs", ] [[package]] @@ -4780,6 +4758,16 @@ dependencies = [ "system-deps", ] +[[package]] +name = "sqlite-wasm-rs" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cd504cecbfe7eef649988840f9a6cb768d11085efec138f8e99f160632d867" +dependencies = [ + "cc", + "rsqlite-vfs", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -5791,6 +5779,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "typed-path" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce898823d8cd0f028662d2f349694b1af0a5cc9be83f1d213a00d3147445e1e" + [[package]] name = "typeid" version = "1.0.3" @@ -6849,6 +6843,20 @@ name = "zeroize" version = "1.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] [[package]] name = "zerotrie" @@ -6885,16 +6893,21 @@ dependencies = [ [[package]] name = "zip" -version = "2.6.1" +version = "9.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dcb24d0152526ae49b9b96c1dcf71850ca1e0b882e4e28ed898a93c41334744" +checksum = "2522d7da11cd3f2fe692a06a3bf9cecdbaa3720ede120d5ae9cffe42c56c6029" dependencies = [ - "arbitrary", + "constant_time_eq", "crc32fast", - "crossbeam-utils", "flate2", + "getrandom 0.4.3", "indexmap 2.14.2", + "libc", "memchr", + "nix 0.31.3", + "typed-path", + "windows-sys 0.61.2", + "zeroize", "zopfli", ] diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..d534c94 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,33 @@ +# Security policy + +## Supported versions + +BeaconSync is in alpha. Only the latest `0.1.0-alpha` release gets security fixes; update to it +before reporting. + +## Reporting a vulnerability + +Please report privately, not in a public issue: open the repo's **Security** tab and choose +**Report a vulnerability** (GitHub private vulnerability reporting). + +Include what is affected, the version, and how to reproduce it. + +## What to expect + +- An acknowledgement within 7 days. +- An assessment and, if confirmed, a fix plan shortly after. Serious issues are fixed in a new + release first and disclosed in a GitHub security advisory once it is out. +- Credit in the advisory, unless you'd rather stay anonymous. + +## Scope + +- The `beacon` CLI and server agent (`crates/cli`, `crates/core`) +- The desktop app (`apps/desktop`) +- The companion mods (`mods/`) +- The hub server (`crates/hub`) and the public hub at `beaconsync.realdoc06.it` + +Out of scope: denial of service by flooding the public hub, issues in Minecraft, mod loaders, +Prism Launcher, Modrinth or CurseForge themselves, and mods a pack owner chose to ship (a pack +runs its mods' code by design). + +Please test against your own hub (`beacon-hub`), not the public one. diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 822de17..a24c06d 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -23,7 +23,7 @@ indicatif = "0.17" console = "0.15" tar = "0.4" flate2 = "1" -zip = { version = "2", default-features = false, features = ["deflate"] } +zip = { version = "9", default-features = false, features = ["deflate"] } walkdir = "2" libc = "0.2" ratatui = "0.30.2" diff --git a/crates/cli/src/backups.rs b/crates/cli/src/backups.rs index e6099d1..576706d 100644 --- a/crates/cli/src/backups.rs +++ b/crates/cli/src/backups.rs @@ -10,7 +10,8 @@ use beacon_core::{ }; use std::{ fs, - path::{Path, PathBuf}, + io::Read, + path::{Component, Path, PathBuf}, time::UNIX_EPOCH, }; @@ -147,9 +148,10 @@ fn find(server: &Path, id: &str) -> Result { /// Left out of BeaconSync backups: caches, logs, other backups (and their retention rule) and /// files being written. -/// `world` is the world folder, always kept even if its name contains "backup". +/// `world` is the world's top folder, always kept even if its name contains "backup". fn skip(server: &Path, world: &str, rel: &Path) -> bool { - let s = rel.to_string_lossy(); + // Forward slashes on every OS, so the prefixes below also match Windows paths. + let s = rel.to_string_lossy().replace('\\', "/"); let top = rel.components().next().map(|c| c.as_os_str().to_string_lossy().into_owned()).unwrap_or_default(); s.starts_with(".beacon/backup") || s.starts_with(".beacon/objects") @@ -170,22 +172,48 @@ pub fn create(server: &Path, note: &str) -> Result { fs::create_dir_all(&dir)?; let slug: String = note.chars().map(|c| if c.is_ascii_alphanumeric() || c == '.' { c } else { '-' }).collect(); let name = format!("{}-{}.tar.gz", beacon_core::now(), slug.trim_matches('-')); - let tmp = dir.join(format!("{name}.part")); + // Unique per call, so concurrent backups never share (or delete) each other's temp file. + static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let tmp = dir.join(format!("{name}.{}-{seq}.part", std::process::id())); { - let enc = flate2::write::GzEncoder::new(fs::File::create(&tmp)?, flate2::Compression::fast()); - let mut tar = tar::Builder::new(enc); - tar.follow_symlinks(false); - let world = level_name(server); - let walk = walkdir::WalkDir::new(server).min_depth(1).into_iter(); - for e in walk.filter_entry(|e| !skip(server, &world, e.path().strip_prefix(server).unwrap_or(e.path()))) { - let e = e?; - if e.file_type().is_file() { - tar.append_path_with_name(e.path(), e.path().strip_prefix(server)?)?; + // Owner-only on Unix: the archive holds `.beacon/server.json` and its hub token. Created + // exclusively, so nothing already at that path is ever reused. + let mut options = fs::OpenOptions::new(); + options.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + options.mode(0o600); + } + let enc = flate2::write::GzEncoder::new(options.open(&tmp)?, flate2::Compression::fast()); + let written = (|| -> Result<()> { + let mut tar = tar::Builder::new(enc); + tar.follow_symlinks(false); + // The world's top folder, normalized like restore's `world_dir` (`world/` is `world`), + // so the world restore clears is always one the backup kept, even when its name + // contains "backup". + let world = level_name(server); + let world = world_rel(&world) + .and_then(|w| w.components().next().map(|c| c.as_os_str().to_string_lossy().into_owned())) + .unwrap_or(world); + let walk = walkdir::WalkDir::new(server).min_depth(1).into_iter(); + for e in walk.filter_entry(|e| !skip(server, &world, e.path().strip_prefix(server).unwrap_or(e.path()))) { + let e = e?; + if e.file_type().is_file() { + tar.append_path_with_name(e.path(), e.path().strip_prefix(server)?)?; + } } + tar.into_inner()?.finish()?; + fs::rename(&tmp, dir.join(&name))?; + Ok(()) + })(); + // A failed backup (disk full, say) must not leave a partial archive outside retention. + if written.is_err() { + fs::remove_file(&tmp).ok(); } - tar.into_inner()?.finish()?; + written?; } - fs::rename(&tmp, dir.join(&name))?; find(server, &rel(server, &dir.join(name))) } @@ -234,52 +262,216 @@ fn level_name(server: &Path) -> String { .unwrap_or_else(|| "world".into()) } -/// Restores a backup. Minecraft must be stopped. Whole server backups replace the pack files -/// and worlds; mod backups replace only the world they contain. +/// `name` as one plain folder name (not `..`, `.`, an absolute path or a nested one): the single +/// top folder of a mod-made backup. Callers join the returned part, not `name`, so a trailing `/` +/// can't make deletion follow a symlinked folder. +fn one_folder(name: &str) -> Option<&std::ffi::OsStr> { + let mut parts = Path::new(name).components(); + match (parts.next(), parts.next()) { + (Some(Component::Normal(part)), None) => Some(part), + _ => None, + } +} + +/// `level-name` as a relative path of plain folder names (`world`, `./worlds/survival/`), +/// rebuilt from its parts. `None` for `..`, a root or a drive (on Windows also a drive inside a +/// later part, `worlds/D:x`, which would join as a drive): the world must sit inside the server. +fn world_rel(name: &str) -> Option { + let mut out = PathBuf::new(); + for part in Path::new(name).components() { + match part { + Component::CurDir => {} + Component::Normal(p) if !(cfg!(windows) && p.to_string_lossy().contains(':')) => out.push(p), + _ => return None, + } + } + (!out.as_os_str().is_empty()).then_some(out) +} + +/// The world folder a restore clears, refused unless it sits inside the server. +fn world_dir(server: &Path) -> Result { + let name = level_name(server); + let rel = + world_rel(&name).with_context(|| format!("level-name {name:?} in server.properties isn't a folder inside the server"))?; + Ok(server.join(rel)) +} + +/// The archive path, without `./` parts, when it names `.beacon/server.json` (the server's config, +/// hub token included) in any letter case: on a case-insensitive disk every spelling is the live +/// token. Callers write it under this spelling, so case-distinct files on other disks stay separate +/// files, each owner-only. +fn server_config_entry(path: &Path) -> Option { + let parts: Vec<_> = path.components().filter(|c| !matches!(c, Component::CurDir)).collect(); + match parts.as_slice() { + [Component::Normal(dir), Component::Normal(file)] + if dir.eq_ignore_ascii_case(".beacon") && file.eq_ignore_ascii_case("server.json") => + { + Some(Path::new(dir).join(file)) + } + _ => None, + } +} + +/// Removes a world folder before a restore. Missing is fine; anything else stops the restore. +fn clear(dir: &Path) -> Result<()> { + match fs::remove_dir_all(dir) { + Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e).with_context(|| format!("clearing {}", dir.display())), + _ => Ok(()), + } +} + +/// A restore worked out by [`plan`], every check passed, before anything is deleted. +pub struct Plan { + id: String, + /// Opened before the checks and extracted from in [`apply`], so the archive that was checked + /// is the one restored even if a backup mod deletes or replaces the file in between. + archive: fs::File, + kind: PlanKind, +} + +enum PlanKind { + /// A whole server backup: `dest` is the canonical server folder. + Full { dest: PathBuf, world: PathBuf }, + /// A mod-made world backup, unpacked into `dest` after clearing `old`. + World { zip: bool, dest: PathBuf, old: PathBuf }, +} + +impl Plan { + pub fn id(&self) -> &str { + &self.id + } +} + +/// Restores a backup: [`plan`], then [`apply`]. Minecraft must be stopped. Whole server backups +/// replace the pack files and worlds; mod backups replace only the world they contain. pub fn restore(server: &Path, id: &str) -> Result<()> { + apply(server, plan(server, id)?) +} + +/// The server token's folder, refused if it is a link: the token is only ever written into a real +/// folder inside the server. +fn refuse_link(dest: &Path, token: &Path) -> Result<()> { + let dir = dest.join(token.parent().unwrap_or(token)); + if fs::symlink_metadata(&dir).is_ok_and(|m| m.file_type().is_symlink()) { + bail!("{} is a link; not restoring the server token through it", dir.display()); + } + Ok(()) +} + +/// Every check a restore makes, without changing anything: safe to run while Minecraft is up, so +/// a bad backup is refused before the server is stopped or a safety backup is taken. +pub fn plan(server: &Path, id: &str) -> Result { let b = find(server, id)?; let file = server.join(&b.id); + let archive = fs::File::open(&file)?; if b.full { - if let Some(tree) = srv::applied_tree(server) { - for p in tree.files.keys() { - fs::remove_file(server.join(p)).ok(); - } + // Canonical like `Archive::unpack`, which also gives Windows its long path prefix. + let dest = server.canonicalize()?; + let world = world_dir(server)?; + let tokens = check_full_archive(archive.try_clone()?)?; + for t in &tokens { + refuse_link(&dest, t)?; } - // Chunks generated after the backup would otherwise survive the restore. - fs::remove_dir_all(server.join(level_name(server))).ok(); - tar::Archive::new(flate2::read::GzDecoder::new(fs::File::open(file)?)).unpack(server)?; - return Ok(()); + return Ok(Plan { id: b.id, archive, kind: PlanKind::Full { dest, world } }); } let name = b.id.to_ascii_lowercase(); if name.contains("incremental") || name.contains("differential") { bail!("{} only holds changes since an earlier backup; restore it from {} itself", b.note, b.source); } - let entries = archive_entries(&file)?; + let zip = file.to_string_lossy().to_ascii_lowercase().ends_with(".zip"); + // Read through the kept handle, so the layout checked is the layout extracted. + let entries = archive_entries(archive.try_clone()?, zip)?; // Either the world's files at the root, or one folder holding them. - let (dest, clear) = if entries.iter().any(|e| e == "level.dat") { - let world = server.join(level_name(server)); + let (dest, old) = if entries.iter().any(|e| e == "level.dat") { + let world = world_dir(server)?; (world.clone(), world) - } else if let Some(top) = entries.iter().find_map(|e| e.strip_suffix("/level.dat").filter(|t| !t.contains('/'))) { + } else if let Some(top) = entries.iter().find_map(|e| e.strip_suffix("/level.dat").and_then(one_folder)) { (server.to_path_buf(), server.join(top)) } else { bail!("{} doesn't look like a world backup (no level.dat)", b.note); }; - fs::remove_dir_all(&clear).ok(); + Ok(Plan { id: b.id, archive, kind: PlanKind::World { zip, dest, old } }) +} + +/// Carries out a [`plan`]. Minecraft must be stopped. +pub fn apply(server: &Path, plan: Plan) -> Result<()> { + use std::io::{Seek, SeekFrom}; + let mut archive = plan.archive; + // The checks read through a clone of this handle, which shares its position. + archive.seek(SeekFrom::Start(0))?; + let (zip, dest, old) = match plan.kind { + PlanKind::World { zip, dest, old } => (zip, dest, old), + PlanKind::Full { dest, world } => { + // Chunks generated after the backup would otherwise survive, so a world that can't be + // cleared stops the restore. + clear(&world)?; + if let Some(tree) = srv::applied_tree(server) { + for p in tree.files.keys() { + fs::remove_file(server.join(p)).ok(); + } + } + let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(archive)); + for entry in archive.entries()? { + let mut entry = entry?; + // The server token goes back owner-only from the start, never through a default + // mode, and only into the server's own `.beacon` folder. + if let Some(rel) = server_config_entry(&entry.path()?) { + // Checked again right before writing, in case the archive created the link. + refuse_link(&dest, &rel)?; + let mut bytes = vec![]; + entry.read_to_end(&mut bytes)?; + beacon_core::store::write_private(&dest.join(rel), &bytes)?; + } else { + entry.unpack_in(&dest)?; + } + } + return Ok(()); + } + }; + clear(&old)?; fs::create_dir_all(&dest)?; - if b.id.to_ascii_lowercase().ends_with(".zip") { - zip::ZipArchive::new(fs::File::open(&file)?)?.extract(&dest)?; + if zip { + zip::ZipArchive::new(archive)?.extract(&dest)?; } else { - tar::Archive::new(flate2::read::GzDecoder::new(fs::File::open(&file)?)).unpack(&dest)?; + tar::Archive::new(flate2::read::GzDecoder::new(archive)).unpack(&dest)?; } Ok(()) } -fn archive_entries(file: &Path) -> Result> { - if file.to_string_lossy().to_ascii_lowercase().ends_with(".zip") { - let z = zip::ZipArchive::new(fs::File::open(file)?)?; - return Ok(z.file_names().map(|n| n.trim_start_matches("./").to_string()).collect()); +/// Reads a whole server archive before a restore touches anything, returning the spellings of its +/// token entries. Refuses a truncated or corrupted backup (every entry, then the rest of the gzip +/// stream so its checksum is checked: tar stops at its end marker) and a link entry where the +/// token's folder would be. +fn check_full_archive(file: fs::File) -> Result> { + let mut tar = tar::Archive::new(flate2::read::GzDecoder::new(file)); + let mut tokens = vec![]; + for entry in tar.entries()? { + let entry = entry?; + let path = entry.path()?; + // BeaconSync writes relative paths only; tar would strip a root or drive and land these + // somewhere the checks below don't look. + if path.components().any(|c| matches!(c, Component::RootDir | Component::Prefix(_) | Component::ParentDir)) { + bail!("the backup has an unexpected path {}", path.display()); + } + let kind = entry.header().entry_type(); + if (kind.is_symlink() || kind.is_hard_link()) && server_config_entry(&path.join("server.json")).is_some() { + bail!("the backup links {}; not restoring the server token through it", path.display()); + } + tokens.extend(server_config_entry(&path)); } - let mut tar = tar::Archive::new(flate2::read::GzDecoder::new(fs::File::open(file)?)); + std::io::copy(&mut tar.into_inner(), &mut std::io::sink()).context("the backup is damaged")?; + Ok(tokens) +} + +fn archive_entries(file: fs::File, zip: bool) -> Result> { + if zip { + let z = zip::ZipArchive::new(file)?; + return Ok(z + .file_names() + .map(|n| n.map(|n| n.trim_start_matches("./").to_string())) + .collect::>()?); + } + let mut tar = tar::Archive::new(flate2::read::GzDecoder::new(file)); let mut out = vec![]; for e in tar.entries()? { out.push(e?.path()?.to_string_lossy().trim_start_matches("./").to_string()); @@ -290,6 +482,12 @@ fn archive_entries(file: &Path) -> Result> { #[cfg(test)] mod tests { use super::*; + + fn entries(tar_gz: &Path) -> Vec { + archive_entries(fs::File::open(tar_gz).unwrap(), false).unwrap() + } + + const SERVER_CONFIG: &str = ".beacon/server.json"; use std::io::Write; fn server() -> PathBuf { @@ -309,6 +507,8 @@ mod tests { #[test] fn full_backup_round_trips_and_leaves_newer_chunks_out() { let d = server(); + fs::create_dir_all(d.join(".beacon")).unwrap(); + fs::write(d.join(SERVER_CONFIG), "{\"token\":\"secret\"}").unwrap(); let b = create(&d, "before 1.4").unwrap(); assert_eq!((b.source.as_str(), b.note.as_str(), b.full), ("BeaconSync", "before 1.4", true)); fs::write(d.join("world/level.dat"), "new").unwrap(); @@ -316,6 +516,91 @@ mod tests { restore(&d, &b.id).unwrap(); assert_eq!(fs::read_to_string(d.join("world/level.dat")).unwrap(), "old"); assert!(!d.join("world/region/r.0.0.mca").exists()); + // The archive and the restored token are owner-only. + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let mode = |p: &Path| fs::metadata(p).unwrap().permissions().mode() & 0o777; + assert_eq!(mode(&d.join(&b.id)), 0o600); + assert_eq!(mode(&d.join(SERVER_CONFIG)), 0o600); + } + assert_eq!(fs::read_to_string(d.join(SERVER_CONFIG)).unwrap(), "{\"token\":\"secret\"}"); + fs::remove_dir_all(d).ok(); + } + + #[test] + fn a_world_named_like_a_backup_folder_is_still_backed_up() { + let d = server(); + fs::create_dir_all(d.join("backup-world")).unwrap(); + fs::write(d.join("backup-world/level.dat"), "mine").unwrap(); + fs::write(d.join("server.properties"), "level-name=backup-world/\n").unwrap(); + let b = create(&d, "x").unwrap(); + assert!(entries(&d.join(&b.id)).iter().any(|e| e == "backup-world/level.dat")); + fs::write(d.join("backup-world/level.dat"), "newer").unwrap(); + restore(&d, &b.id).unwrap(); + assert_eq!(fs::read_to_string(d.join("backup-world/level.dat")).unwrap(), "mine"); + fs::remove_dir_all(d).ok(); + } + + #[test] + fn broken_or_linking_full_backups_are_refused() { + let d = server(); + let good = create(&d, "good").unwrap(); + let own = own_dir(&d); + // Truncated: refused before the world is touched. + let bytes = fs::read(d.join(&good.id)).unwrap(); + fs::write(own.join("1-truncated.tar.gz"), &bytes[..bytes.len() / 2]).unwrap(); + assert!(find(&d, ".beacon/backups/1-truncated.tar.gz").unwrap().full); + assert!(plan(&d, ".beacon/backups/1-truncated.tar.gz").is_err(), "refused by the checks alone"); + // Only the gzip footer missing, or one flipped byte: tar alone would read these fine. + fs::write(own.join("1-footerless.tar.gz"), &bytes[..bytes.len() - 8]).unwrap(); + assert!(restore(&d, ".beacon/backups/1-footerless.tar.gz").is_err()); + let mut flipped = bytes.clone(); + let len = flipped.len(); + flipped[len - 5] ^= 0xff; + fs::write(own.join("1-flipped.tar.gz"), &flipped).unwrap(); + assert!(restore(&d, ".beacon/backups/1-flipped.tar.gz").is_err()); + assert_eq!(fs::read_to_string(d.join("world/level.dat")).unwrap(), "old"); + // A `.Beacon` link made by the archive itself is never written through. + #[cfg(unix)] + { + let outside = std::env::temp_dir().join(format!("beacon-outside-{}", rand_suffix())); + fs::create_dir_all(&outside).unwrap(); + let mut tar = tar::Builder::new(flate2::write::GzEncoder::new( + fs::File::create(own.join("2-linking.tar.gz")).unwrap(), + flate2::Compression::fast(), + )); + let mut link = tar::Header::new_gnu(); + link.set_entry_type(tar::EntryType::Symlink); + link.set_size(0); + tar.append_link(&mut link, ".Beacon", &outside).unwrap(); + let mut file = tar::Header::new_gnu(); + file.set_size(2); + tar.append_data(&mut file, ".Beacon/server.json", &b"{}"[..]).unwrap(); + tar.into_inner().unwrap().finish().unwrap(); + assert!(find(&d, ".beacon/backups/2-linking.tar.gz").unwrap().full); + let err = restore(&d, ".beacon/backups/2-linking.tar.gz").unwrap_err().to_string(); + assert!(err.contains("links"), "{err}"); + assert!(!outside.join("server.json").exists()); + assert_eq!(fs::read_to_string(d.join("world/level.dat")).unwrap(), "old", "refused before deleting"); + // The same link spelled with a root (`/.beacon`), which tar would strip on unpack. + let mut tar = tar::Builder::new(flate2::write::GzEncoder::new( + fs::File::create(own.join("3-rooted.tar.gz")).unwrap(), + flate2::Compression::fast(), + )); + let mut link = tar::Header::new_gnu(); + link.as_gnu_mut().unwrap().name[..8].copy_from_slice(b"/.beacon"); + link.set_entry_type(tar::EntryType::Symlink); + link.set_link_name(&outside).unwrap(); + link.set_size(0); + link.set_cksum(); + tar.append(&link, std::io::empty()).unwrap(); + tar.into_inner().unwrap().finish().unwrap(); + let err = plan(&d, ".beacon/backups/3-rooted.tar.gz").err().unwrap().to_string(); + assert!(err.contains("unexpected path"), "{err}"); + assert_eq!(fs::read_to_string(d.join("world/level.dat")).unwrap(), "old"); + fs::remove_dir_all(outside).ok(); + } fs::remove_dir_all(d).ok(); } @@ -336,10 +621,32 @@ mod tests { ); // Our own backups leave mod backups out. let own = create(&d, "manual").unwrap(); - assert!(!archive_entries(&d.join(own.id)).unwrap().iter().any(|e| e.starts_with("backups/"))); + assert!(!entries(&d.join(&own.id)).iter().any(|e| e.starts_with("backups/"))); restore(&d, &b.id).unwrap(); assert_eq!(fs::read_to_string(d.join("world/level.dat")).unwrap(), "from mod"); assert!(restore(&d, "../etc/passwd").is_err()); + // An archive naming `..` as its world is refused before anything is cleared. + let mut z = zip::ZipWriter::new(fs::File::create(d.join("backups/escape.zip")).unwrap()); + z.start_file("../level.dat", zip::write::SimpleFileOptions::default()).unwrap(); + z.finish().unwrap(); + assert!(restore(&d, "backups/escape.zip").is_err()); + assert!(d.join("world/level.dat").exists()); + // So is a server.properties pointing the world outside the server, and pack files survive. + let mut tree = beacon_core::model::Tree::default(); + tree.files.insert("mods/pack.jar".into(), serde_json::from_value(serde_json::json!({ "hash": "x", "size": 0 })).unwrap()); + fs::write(d.join(".beacon/server-tree.json"), serde_json::to_vec(&tree).unwrap()).unwrap(); + fs::write(d.join("mods/pack.jar"), "").unwrap(); + fs::write(d.join("server.properties"), "level-name=../elsewhere\n").unwrap(); + assert!(restore(&d, &own.id).is_err()); + assert!(d.join("world/level.dat").exists() && d.join("mods/pack.jar").exists(), "a refused restore deletes nothing"); + assert_eq!(one_folder("world/"), Some(std::ffi::OsStr::new("world"))); + assert_eq!(world_rel("worlds/survival/"), Some(PathBuf::from("worlds/survival"))); + assert_eq!(world_rel("../x").or(world_rel("/abs")).or(world_rel(".")), None); + assert_eq!(world_rel("./worlds/survival"), Some(PathBuf::from("worlds/survival"))); + assert_eq!(world_rel("worlds/D:x").is_none(), cfg!(windows)); + assert_eq!(server_config_entry(Path::new("./.Beacon/Server.json")), Some(PathBuf::from(".Beacon/Server.json"))); + assert_eq!(server_config_entry(Path::new("x/.beacon/server.json")), None); + fs::write(d.join("server.properties"), "level-name=world\n").unwrap(); // Partial archives from incremental backups are refused rather than half restored. fs::copy(d.join("backups/2026-11-05.zip"), d.join("backups/incremental.zip")).unwrap(); assert!(restore(&d, "backups/incremental.zip").is_err()); diff --git a/crates/cli/src/server.rs b/crates/cli/src/server.rs index d28c703..960d2a0 100644 --- a/crates/cli/src/server.rs +++ b/crates/cli/src/server.rs @@ -1320,7 +1320,11 @@ async fn act(dir: &Path, cfg: &mut ServerConfig, agent: &Option, mc: &mut O say(format!("backup done: {} ({})", b.id, size(b.size))); } ServerAction::Restore(id) => { - say(format!("restoring {id}")); + // Checked while Minecraft still runs: a refused restore stops nothing, takes no safety + // backup and changes nothing. + let d = dir.to_path_buf(); + let plan = tokio::task::spawn_blocking(move || backups::plan(&d, &id)).await??; + say(format!("restoring {}", plan.id())); if let Some(mut m) = mc.take() { m.send("say [BeaconSync] Restoring a backup, back in a minute").await; tokio::time::sleep(Duration::from_secs(3)).await; @@ -1328,17 +1332,23 @@ async fn act(dir: &Path, cfg: &mut ServerConfig, agent: &Option, mc: &mut O } let before = cfg.commit.clone(); let d = dir.to_path_buf(); - let restored = tokio::task::spawn_blocking(move || -> Result<()> { + // Outer error: the restore failed and so did undoing it. Minecraft stays stopped then, + // so it can't start a fresh world over the cleared one. Inner error: the server is as + // it was before the restore, so Minecraft starts again. + let restored = tokio::task::spawn_blocking(move || -> Result> { say("taking a safety backup first"); - let safety = backups::create(&d, "before restore")?; - if let Err(e) = backups::restore(&d, &id) { - backups::restore(&d, &safety.id)?; - return Err(e); + let safety = match backups::create(&d, "before restore") { + Ok(s) => s, + Err(e) => return Ok(Err(e)), + }; + if let Err(e) = backups::apply(&d, plan) { + backups::restore(&d, &safety.id).with_context(|| format!("{e:#}; undoing it failed too"))?; + return Ok(Err(e)); } backups::prune(&d); - Ok(()) + Ok(Ok(())) }) - .await?; + .await??; *cfg = ServerConfig::load(dir)?; *mc = Some(Mc::start(dir, cfg)?); restored?; diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 4d6df6f..24b579b 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -18,7 +18,7 @@ futures.workspace = true ignore = "0.4" walkdir = "2" rayon = "1" -zip = { version = "2", default-features = false, features = ["deflate"] } +zip = { version = "9", default-features = false, features = ["deflate"] } dirs = "6" tar = "0.4" flate2 = "1" diff --git a/crates/core/src/import.rs b/crates/core/src/import.rs index 680d9b4..1ed9323 100644 --- a/crates/core/src/import.rs +++ b/crates/core/src/import.rs @@ -65,7 +65,7 @@ pub enum ImportKind { pub fn detect(path: &Path) -> Result { let mut zip = zip::ZipArchive::new(fs::File::open(path)?).context("not a zip file")?; - let names: Vec = zip.file_names().map(str::to_string).collect(); + let names: Vec = zip.file_names().map(|n| n.map(|n| n.into_owned())).collect::>()?; if names.iter().any(|n| n == "modrinth.index.json") { return Ok(ImportKind::Mrpack); } @@ -151,7 +151,7 @@ pub async fn mrpack_tree(path: &Path, store: &ObjectStore) -> Result<(MrIndex, T if !file.is_file() { continue; } - let Some(rel) = file.name().strip_prefix(prefix).and_then(safe_rel) else { continue }; + let Some(rel) = file.name()?.strip_prefix(prefix).and_then(safe_rel) else { continue }; let mut bytes = Vec::with_capacity(file.size() as usize); file.read_to_end(&mut bytes)?; let id = store.put(&bytes)?; @@ -335,7 +335,7 @@ pub async fn import_curseforge(file: &Path, root: &Path, cf: &CurseForge, author let overrides = format!("{}/", manifest.overrides.as_deref().unwrap_or("overrides")); for i in 0..zip.len() { let mut f = zip.by_index(i)?; - let Some(rel) = f.name().strip_prefix(&overrides).and_then(safe_rel) else { continue }; + let Some(rel) = f.name()?.strip_prefix(&overrides).and_then(safe_rel) else { continue }; if f.is_file() { let mut bytes = vec![]; f.read_to_end(&mut bytes)?; @@ -381,13 +381,17 @@ pub async fn import_curseforge(file: &Path, root: &Path, cf: &CurseForge, author /// Extracts a Prism/MultiMC instance export (.zip) and starts tracking it. pub async fn import_prism_zip(file: &Path, root: &Path, author: &str) -> Result { let mut zip = zip::ZipArchive::new(fs::File::open(file)?)?; - let cfg_name = zip.file_names().find(|n| n.ends_with("instance.cfg")).context("no instance.cfg")?.to_string(); + let cfg_name = zip + .file_names() + .find(|n| n.as_ref().is_ok_and(|n| n.ends_with("instance.cfg"))) + .context("no instance.cfg")?? + .into_owned(); let prefix = cfg_name.trim_end_matches("instance.cfg").to_string(); let stem = file.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "pack".into()); let dir = instance::unique_dir(root, &stem); for i in 0..zip.len() { let mut f = zip.by_index(i)?; - let Some(rel) = f.name().strip_prefix(&prefix).and_then(safe_rel) else { continue }; + let Some(rel) = f.name()?.strip_prefix(&prefix).and_then(safe_rel) else { continue }; if f.is_file() { let mut bytes = vec![]; f.read_to_end(&mut bytes)?; diff --git a/crates/core/src/release.rs b/crates/core/src/release.rs index c83f495..8b6a753 100644 --- a/crates/core/src/release.rs +++ b/crates/core/src/release.rs @@ -104,8 +104,10 @@ pub async fn latest() -> Result { } } -/// A release by version ("0.1.0-alpha.2") or tag ("v0.1.0-alpha.2"). +/// A release by version ("0.1.0-alpha.2") or tag ("v0.1.0-alpha.2"). The version can come from +/// the hub, so it must look like one: anything else could steer the URL to another repo. pub async fn by_version(version: &str) -> Result { + ensure!(is_version(version), "{version:?} is not a BeaconSync version"); let tag = format!("v{}", version.trim_start_matches('v')); match manifest(&format!("download/{tag}")).await { Some(r) => Ok(r), @@ -143,6 +145,17 @@ pub async fn download(asset: &Asset, dest: &Path, on: impl Fn(u64)) -> Result<() Ok(()) } +/// Version characters only (`0-9a-zA-Z.-+`), starting with a digit and with no empty dot parts, so +/// it is safe inside a URL path (`..` and `.` can't appear as segments). +fn is_version(v: &str) -> bool { + let core = v.strip_prefix('v').unwrap_or(v); + (1..=64).contains(&v.len()) + && core.starts_with(|c: char| c.is_ascii_digit()) + && !core.contains("..") + && !core.ends_with('.') + && core.bytes().all(|b| b.is_ascii_alphanumeric() || b"-.+".contains(&b)) +} + /// Semver order for `MAJOR.MINOR.PATCH[-PRE]`: a prerelease sorts before its release and /// numeric prerelease parts compare as numbers ("alpha.10" > "alpha.9"). pub fn newer(a: &str, b: &str) -> bool { @@ -177,7 +190,29 @@ pub fn cli_asset() -> String { #[cfg(test)] mod tests { - use super::newer; + use super::{is_version, newer}; + + #[test] + fn only_version_strings_reach_release_urls() { + assert!(is_version("0.1.0-alpha.14") && is_version("v1.2.3+build.5")); + for bad in [ + "", + "v", + ".", + "..", + "v..", + "1..2", + "1.", + "x1.0", + "9.9.9/../../../other/repo", + "1.0%2f..", + "1.0?x", + "1.0#x", + "1.0\\..", + ] { + assert!(!is_version(bad), "{bad}"); + } + } #[test] fn orders_versions_like_semver() { diff --git a/crates/core/src/server/mod.rs b/crates/core/src/server/mod.rs index fe7df26..c37f6fe 100644 --- a/crates/core/src/server/mod.rs +++ b/crates/core/src/server/mod.rs @@ -56,15 +56,9 @@ impl ServerConfig { Ok(serde_json::from_slice(&bytes)?) } + /// Owner-only from the start: it holds the server's hub token. pub fn save(&self, dir: &Path) -> Result<()> { - let p = Self::path(dir); - write_atomic(&p, &serde_json::to_vec_pretty(self)?)?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - fs::set_permissions(&p, fs::Permissions::from_mode(0o600))?; - } - Ok(()) + crate::store::write_private(&Self::path(dir), &serde_json::to_vec_pretty(self)?) } /// Full command line used to start Minecraft. diff --git a/crates/core/src/settings.rs b/crates/core/src/settings.rs index 95fbe6b..c43f67c 100644 --- a/crates/core/src/settings.rs +++ b/crates/core/src/settings.rs @@ -38,14 +38,7 @@ impl Settings { /// Saves with owner-only permissions since it holds the auth token. pub fn save(&self) -> Result<()> { - let path = Self::path(); - crate::store::write_atomic(&path, &serde_json::to_vec_pretty(self)?)?; - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600))?; - } - Ok(()) + crate::store::write_private(&Self::path(), &serde_json::to_vec_pretty(self)?) } pub fn hub_url(&self) -> String { diff --git a/crates/core/src/store.rs b/crates/core/src/store.rs index 36f0876..402f3f8 100644 --- a/crates/core/src/store.rs +++ b/crates/core/src/store.rs @@ -76,20 +76,28 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> Result<()> { write_through_temp(path, bytes, false) } -/// [`write_atomic`] for files holding secrets: owner-only permissions from the start. +/// [`write_atomic`] for files holding secrets: owner-only permissions from the start on Unix. +/// On Windows the file gets its folder's permissions. pub fn write_private(path: &Path, bytes: &[u8]) -> Result<()> { write_through_temp(path, bytes, true) } fn write_through_temp(path: &Path, bytes: &[u8], private: bool) -> Result<()> { + static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); let dir = path.parent().context("path has no parent")?; fs::create_dir_all(dir)?; - let tmp = dir.join(format!(".{}.tmp{}", path.file_name().and_then(|n| n.to_str()).unwrap_or("obj"), std::process::id())); + let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.subsec_nanos()); + // Short and independent of the target's name, so long file names still fit the 255 byte limit. + let tmp = dir.join(format!( + ".beacon-{}-{}-{nanos}.tmp", + std::process::id(), + SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed), + )); { - // The mode only applies to new files, so a leftover temp file must go first. - fs::remove_file(&tmp).ok(); + // A fresh name created exclusively: concurrent saves never share a temp file, and nothing + // planted at that path (a symlink, a file with looser permissions) is ever written through. let mut options = fs::OpenOptions::new(); - options.write(true).create(true).truncate(true); + options.write(true).create_new(true); #[cfg(unix)] if private { use std::os::unix::fs::OpenOptionsExt; @@ -97,10 +105,17 @@ fn write_through_temp(path: &Path, bytes: &[u8], private: bool) -> Result<()> { } #[cfg(not(unix))] let _ = private; + // Only cleaned up once this call created it: a failed exclusive open names someone else's file. let mut f = options.open(&tmp)?; - f.write_all(bytes)?; - f.sync_all().ok(); + let written = f.write_all(bytes).map(|()| { + f.sync_all().ok(); + }); + drop(f); + // Unique names would otherwise pile up (secrets included) after failed writes. + if let Err(e) = written.and_then(|()| fs::rename(&tmp, path)) { + fs::remove_file(&tmp).ok(); + return Err(e.into()); + } } - fs::rename(&tmp, path)?; Ok(()) } diff --git a/crates/hub/Cargo.toml b/crates/hub/Cargo.toml index 9c47572..5531303 100644 --- a/crates/hub/Cargo.toml +++ b/crates/hub/Cargo.toml @@ -17,7 +17,7 @@ rand.workspace = true tracing.workspace = true tracing-subscriber.workspace = true axum = "0.8" -rusqlite = { version = "0.32", features = ["bundled"] } +rusqlite = { version = "0.43", features = ["bundled"] } argon2 = "0.5" clap = { version = "4", features = ["derive", "env"] } tower-http = { version = "0.6", features = ["trace"] } diff --git a/crates/hub/src/pages.rs b/crates/hub/src/pages.rs index 4ef6f73..f09b322 100644 --- a/crates/hub/src/pages.rs +++ b/crates/hub/src/pages.rs @@ -5,6 +5,7 @@ use crate::routes::{AppState, approve_device, check_credentials, public_info}; use axum::{ Form, extract::{Path, State}, + http::StatusCode, response::Html, }; use beacon_core::api::Credentials; @@ -79,11 +80,16 @@ pub struct DeviceForm { password: String, } -pub async fn device_submit(State(s): State, Form(f): Form) -> Html { - let result = check_credentials(&s, &Credentials { username: f.username, password: f.password }) +/// Errors keep their API status (401, 429...) so scripts and proxies see the failure. +pub async fn device_submit(State(s): State, Form(f): Form) -> (StatusCode, Html) { + let result = check_credentials(&s, Credentials { username: f.username, password: f.password }) + .await .and_then(|u| approve_device(&s, &u.id, &f.user_code)); match result { - Ok(()) => page("Approved", "

Approved

You can close this page and go back to the terminal.

"), - Err(e) => device_html(&format!("

{}

", esc(&e.1))), + Ok(()) => ( + StatusCode::OK, + page("Approved", "

Approved

You can close this page and go back to the terminal.

"), + ), + Err(e) => (e.0, device_html(&format!("

{}

", esc(&e.1)))), } } diff --git a/crates/hub/src/routes.rs b/crates/hub/src/routes.rs index ffcd341..d2bdfbd 100644 --- a/crates/hub/src/routes.rs +++ b/crates/hub/src/routes.rs @@ -21,10 +21,13 @@ use serde::Deserialize; use serde_json::{Value, json}; use std::{ collections::HashMap, - sync::{Arc, Mutex, MutexGuard}, + sync::{ + Arc, LazyLock, Mutex, MutexGuard, + atomic::{AtomicI64, AtomicUsize, Ordering}, + }, time::Duration, }; -use tokio::sync::Notify; +use tokio::sync::{Notify, Semaphore}; #[derive(Clone)] pub struct AppState(Arc); @@ -37,8 +40,19 @@ pub struct Inner { waiters: Mutex>>, /// Live console and backups per server, kept in memory only. consoles: Mutex>, + /// Failed sign ins per lowercased username, existing or not: (count, start of the window). + failures: Mutex>, + /// When `failures` last dropped expired entries. + failures_swept: AtomicI64, } +/// Wrong passwords allowed per username within [`FAILURE_WINDOW`] seconds before sign ins with a +/// password are refused for the rest of it. Existing tokens keep working. The limit is per +/// account, not per client, so someone who knows a username can keep its password sign in locked; +/// the hub sits behind a proxy and has no client address it can trust. +const MAX_FAILURES: u32 = 10; +const FAILURE_WINDOW: i64 = 15 * 60; + /// Lines kept per server for viewers that open the console later. const CONSOLE_LINES: usize = 2000; @@ -70,13 +84,38 @@ impl AppState { std::fs::create_dir_all(&config.data)?; let db = db::open(&config.data.join("hub.db"))?; let store = ObjectStore::new(config.data.join("objects")); - Ok(Self(Arc::new(Inner { db: Mutex::new(db), store, config, waiters: Mutex::default(), consoles: Mutex::default() }))) + Ok(Self(Arc::new(Inner { + db: Mutex::new(db), + store, + config, + waiters: Mutex::default(), + consoles: Mutex::default(), + failures: Mutex::default(), + failures_swept: AtomicI64::default(), + }))) } pub fn db(&self) -> MutexGuard<'_, Connection> { self.0.db.lock().unwrap_or_else(|e| e.into_inner()) } + /// Runs `f` on a username's failed sign in count, restarting it once its window is over. + /// Expired entries are swept at most once a minute, not on every sign in. + fn failures(&self, user: &str, f: impl FnOnce(&mut (u32, i64)) -> T) -> T { + let mut all = self.failures.lock().unwrap_or_else(|e| e.into_inner()); + let now = now(); + // Read and written only under the `failures` lock. + if now - self.failures_swept.load(Ordering::Relaxed) >= 60 { + all.retain(|_, (_, since)| now - *since < FAILURE_WINDOW); + self.failures_swept.store(now, Ordering::Relaxed); + } + let entry = all.entry(user.to_string()).or_insert((0, now)); + if now - entry.1 >= FAILURE_WINDOW { + *entry = (0, now); + } + f(entry) + } + fn waiter(&self, server: &str) -> Arc { self.waiters.lock().unwrap_or_else(|e| e.into_inner()).entry(server.to_string()).or_default().clone() } @@ -152,11 +191,17 @@ async fn register(State(s): State, Json(c): Json) -> Resu if c.password.len() < 8 { return Err(Error::bad("password must be at least 8 characters")); } - let pw = hash_password(&c.password)?; + let taken = |db: &Connection| -> Result { + Ok(db.query_row("select 1 from users where username = ?1", params![username], |_| Ok(())).optional()?.is_some()) + }; + // Checked before hashing too, so repeat requests for a taken name cost no Argon2 work. + if taken(&s.db())? { + return Err(Error::conflict("that username is taken")); + } + let pw = argon(ArgonSlot::take()?, move || hash_password(&c.password)).await??; let db = s.db(); let id = new_id(); - let taken = db.query_row("select 1 from users where username = ?1", params![username], |_| Ok(())).optional()?.is_some(); - if taken { + if taken(&db)? { return Err(Error::conflict("that username is taken")); } db.execute("insert into users (id, username, pw_hash, created) values (?1, ?2, ?3, ?4)", params![id, username, pw, now()])?; @@ -164,22 +209,105 @@ async fn register(State(s): State, Json(c): Json) -> Resu Ok(Json(AuthResponse { token, user: User { id, username: username.into() } })) } +/// One Argon2 run per CPU at a time: each takes tens of milliseconds and ~19 MiB, so a flood of +/// sign ins or registrations queues here instead of exhausting the host. +static ARGON: LazyLock> = + LazyLock::new(|| Arc::new(Semaphore::new(std::thread::available_parallelism().map_or(2, |n| n.get())))); + +/// Sign ins and registrations allowed to wait for [`ARGON`] at once; past that the hub answers +/// 503 right away instead of holding more requests (and their bodies) in memory. +const MAX_ARGON_WAITING: usize = 32; +static ARGON_WAITING: AtomicUsize = AtomicUsize::new(0); + +/// A place in the queue for [`ARGON`], taken before anything else is recorded so a busy hub +/// turns requests away without counting them (or remembering their usernames). +struct ArgonSlot; + +impl ArgonSlot { + fn take() -> Result { + if ARGON_WAITING.fetch_add(1, Ordering::Relaxed) >= MAX_ARGON_WAITING { + ARGON_WAITING.fetch_sub(1, Ordering::Relaxed); + return Err(Error(StatusCode::SERVICE_UNAVAILABLE, "the hub is busy, try again shortly".into())); + } + Ok(Self) + } +} + +impl Drop for ArgonSlot { + fn drop(&mut self) { + ARGON_WAITING.fetch_sub(1, Ordering::Relaxed); + } +} + +/// Runs Argon2 on the blocking pool, so it never stalls an async worker (and, under the DB lock, +/// every request). The permit lives as long as the work. If the request is dropped before the job +/// starts, the job is cancelled, so it never runs (or counts an attempt) for nobody. +async fn argon(slot: ArgonSlot, f: impl FnOnce() -> T + Send + 'static) -> Result { + struct AbortOnDrop(tokio::task::JoinHandle); + impl Drop for AbortOnDrop { + fn drop(&mut self) { + self.0.abort(); + } + } + let permit = ARGON.clone().acquire_owned().await.map_err(anyhow::Error::from)?; + drop(slot); + let mut job = AbortOnDrop(tokio::task::spawn_blocking(move || { + let out = f(); + drop(permit); + out + })); + (&mut job.0).await.map_err(|e| anyhow::Error::from(e).into()) +} + +/// Checked against when the username doesn't exist, so unknown names cost the same time as real +/// ones and don't reveal which accounts exist. +static DUMMY_HASH: LazyLock = LazyLock::new(|| hash_password("not a real password").unwrap_or_default()); + /// Checks a username/password pair (also used by the /device web page). -pub fn check_credentials(s: &AppState, c: &Credentials) -> Result { - let db = s.db(); - let row: Option<(String, String, String)> = db - .query_row("select id, username, pw_hash from users where username = ?1", params![c.username.trim()], |r| { +pub async fn check_credentials(s: &AppState, c: Credentials) -> Result { + let wrong = || Error(StatusCode::UNAUTHORIZED, "wrong username or password".into()); + let name = c.username.trim().to_string(); + // No account can have an invalid name, so this reveals nothing and keeps junk out of `failures`. + if !valid_username(&name) { + return Err(wrong()); + } + // Usernames match case-insensitively (`collate nocase`), so the limit must too. + let key = name.to_ascii_lowercase(); + let row: Option<(String, String, String)> = s + .db() + .query_row("select id, username, pw_hash from users where username = ?1", params![name], |r| { Ok((r.get(0)?, r.get(1)?, r.get(2)?)) }) .optional()?; - match row { - Some((id, username, hash)) if verify_password(&c.password, &hash) => Ok(User { id, username }), - _ => Err(Error(StatusCode::UNAUTHORIZED, "wrong username or password".into())), + let slot = ArgonSlot::take()?; + let hash = row.as_ref().map(|(_, _, h)| h.clone()); + let state = s.clone(); + // Counted inside the job, right before the check: parallel guesses can't all slip in under + // the limit, and a request turned away or dropped while queued never costs an attempt. + let verdict = argon(slot, move || { + let allowed = state.failures(&key, |(count, _)| { + *count += 1; + *count <= MAX_FAILURES + }); + if !allowed { + return None; + } + let ok = verify_password(&c.password, hash.as_deref().unwrap_or(&DUMMY_HASH)) && hash.is_some(); + if ok { + state.failures.lock().unwrap_or_else(|e| e.into_inner()).remove(&key); + } + Some(ok) + }) + .await?; + match (verdict, row) { + (None, _) => Err(Error(StatusCode::TOO_MANY_REQUESTS, "too many wrong passwords, try again in 15 minutes".into())), + (Some(true), Some((id, username, _))) => Ok(User { id, username }), + _ => Err(wrong()), } } async fn login(State(s): State, Json(c): Json) -> Result> { - let user = check_credentials(&s, &c)?; + let user = check_credentials(&s, c).await?; let token = issue_token(&s.db(), &user.id, "login")?; Ok(Json(AuthResponse { token, user })) } diff --git a/crates/hub/src/tests.rs b/crates/hub/src/tests.rs index dbc3b4d..aeb0e82 100644 --- a/crates/hub/src/tests.rs +++ b/crates/hub/src/tests.rs @@ -98,6 +98,39 @@ async fn publish_and_deploy_flow() { assert!(servers[0].online); } +#[tokio::test] +async fn wrong_passwords_lock_password_sign_in_for_a_while() { + let (_tmp, base) = start().await; + let anon = Hub::new(&base, None); + let good = Credentials { username: "doc".into(), password: "longpassword".into() }; + let auth = anon.register(&good).await.unwrap(); + let wrong = Credentials { password: "wrongpassword".into(), ..good.clone() }; + // Fired at once: the limit holds for parallel guesses too. + let guesses: Vec<_> = (0..20) + .map(|_| { + let (a, w) = (Hub::new(&base, None), wrong.clone()); + tokio::spawn(async move { a.login(&w).await.unwrap_err().to_string() }) + }) + .collect(); + let mut errors = vec![]; + for g in guesses { + errors.push(g.await.unwrap()); + } + assert_eq!(errors.iter().filter(|e| e.contains("too many")).count(), 10, "{errors:?}"); + assert!(anon.login(&good).await.unwrap_err().to_string().contains("too many")); + // Signed in clients are unaffected. + assert!(Hub::new(&base, Some(auth.token)).library().await.is_ok()); + // Other casings of the name are the same account and share the limit. + let shouted = Credentials { username: "DOC".into(), ..wrong.clone() }; + assert!(anon.login(&shouted).await.unwrap_err().to_string().contains("too many")); + // Unknown usernames are limited the same way, so guessing names gets no free attempts. + let ghost = Credentials { username: "ghost".into(), password: "whatever1".into() }; + for _ in 0..10 { + assert!(!anon.login(&ghost).await.unwrap_err().to_string().contains("too many")); + } + assert!(anon.login(&ghost).await.unwrap_err().to_string().contains("too many")); +} + #[tokio::test] async fn device_login() { let (_tmp, base) = start().await; diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 555f8f4..601e16f 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -134,7 +134,8 @@ GET /api/objects/:hash -> raw object bytes (blobs, trees, commits) Authenticated: ``` -POST /api/auth/register|login { username, password } -> { token, user } +POST /api/auth/register { username, password } -> { token, user } (503 when too many sign ins are already waiting) +POST /api/auth/login { username, password } -> { token, user } (429 after 10 wrong passwords for a username, existing or not, within 15 minutes; per account, so it can keep a known username's password sign in locked, but tokens keep working; 503 when too many sign ins are already waiting) POST /api/auth/device/start -> { device_code, user_code, expires_in } POST /api/auth/device/poll { device_code } -> { token, user } | 428 pending POST /api/auth/device/approve { user_code }