diff --git a/src/drivers.rs b/src/drivers.rs index b92311a..6aeca8e 100644 --- a/src/drivers.rs +++ b/src/drivers.rs @@ -5962,7 +5962,6 @@ struct FrozenSymlinkFrame { /// function. #[cfg(unix)] fn open_frozen_symlink_frame( - canonical_root: &Path, symlink_path: &Path, ceiling: u32, budget: &mut u32, @@ -6024,12 +6023,8 @@ fn resolve_frozen_source_symlink(canonical_root: &Path, path: &Path) -> Result

= std::collections::HashMap::new(); - let mut stack: Vec = vec![open_frozen_symlink_frame( - canonical_root, - path, - MAX_LINK_TRAVERSALS, - &mut budget, - )?]; + let mut stack: Vec = + vec![open_frozen_symlink_frame(path, MAX_LINK_TRAVERSALS, &mut budget)?]; loop { let top = stack.len() - 1; @@ -6066,7 +6061,6 @@ fn resolve_frozen_source_symlink(canonical_root: &Path, path: &Path) -> Result

/ + /// origin.git`. A clean revision must resolve; moving `refs/heads/main` + /// onto either hostile tree this suite already builds for + /// `freeze_exact` (duplicate-named trees, a `.GIT` look-alike) must be + /// refused by `resolve()`'s own `transfer.fsckObjects=true` fetch -- + /// the exact guard S1 added, and the only thing standing between an + /// admitted ref and every downstream consumer of `ResolvedSource` for + /// every caller `resolve()` has, HTTPS or not. + /// + /// Skips (does not fail) when `openssl` or `git-http-backend` is + /// unavailable -- both exist in `eureka-verify-rust`, but neither is + /// guaranteed on a workstation. + #[cfg(unix)] + #[test] + fn resolve_admits_a_clean_https_origin_and_refuses_hostile_trees_moved_onto_it() -> Result<()> + { + use std::os::unix::fs::PermissionsExt; + + let git_http_backend = Command::new("git") + .arg("--exec-path") + .output() + .ok() + .filter(|output| output.status.success()) + .map(|output| { + PathBuf::from(String::from_utf8_lossy(&output.stdout).trim().to_owned()) + .join("git-http-backend") + }) + .filter(|path| path.is_file()); + let openssl_present = Command::new("openssl") + .arg("version") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map(|status| status.success()) + .unwrap_or(false); + let Some(git_http_backend) = git_http_backend else { + eprintln!( + "SKIP resolve_admits_a_clean_https_origin_and_refuses_hostile_trees_moved_onto_it: \ + git-http-backend not found" + ); + return Ok(()); + }; + if !openssl_present { + eprintln!( + "SKIP resolve_admits_a_clean_https_origin_and_refuses_hostile_trees_moved_onto_it: \ + openssl not found" + ); + return Ok(()); + } + + let temp = tempfile::tempdir()?; + fs::set_permissions(temp.path(), fs::Permissions::from_mode(0o755))?; + + // A self-signed certificate for 127.0.0.1, since the origin is + // reached at a loopback address, not a real hostname. + let cert = temp.path().join("cert.pem"); + let key = temp.path().join("key.pem"); + let openssl_status = Command::new("openssl") + .args([ + "req", "-x509", "-newkey", "rsa:2048", "-sha256", "-days", "1", "-nodes", + "-keyout", + ]) + .arg(&key) + .arg("-out") + .arg(&cert) + .args(["-subj", "/CN=127.0.0.1", "-addext", "subjectAltName=IP:127.0.0.1"]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status()?; + ensure!(openssl_status.success(), "generating the self-signed test certificate"); + fs::set_permissions(&cert, fs::Permissions::from_mode(0o644))?; + fs::set_permissions(&key, fs::Permissions::from_mode(0o644))?; + + // The origin: a bare repository built directly with Git plumbing + // (no working tree, so an oversized or hostile blob never has to + // round-trip through a real file on disk to become a commit). + // Reached purely over HTTPS by every caller below, so unlike the + // `file://` hostile-tree fixtures elsewhere in this suite, nothing + // here needs `chown_to_source_identity` or `git_owned`'s dubious- + // ownership workaround: the bridge script below runs + // `git-http-backend` as this same process, against a repository + // this same process owns. + let repos = temp.path().join("repos"); + fs::create_dir(&repos)?; + let origin_repo = repos.join("origin.git"); + fs::create_dir(&origin_repo)?; + git_at(&origin_repo, &["init", "--bare", "--initial-branch=main"])?; + let recipe_blob = hash_object(&origin_repo, b"target = 'test'\n")?; + let root_tree = literal_tree(&origin_repo, &[("100644", "deployment.toml", &recipe_blob)])?; + let root_commit = git_owned(&origin_repo, &["commit-tree", &root_tree, "-m", "root"])?; + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &root_commit])?; + + // The bridge: `git-http-backend` has no HTTPS front end of its own, + // so a small CGI-over-TLS shim (see `GIT_HTTPS_BRIDGE_PY`) stands in + // for a real web server. + let bridge_script = temp.path().join("git_https_bridge.py"); + fs::write(&bridge_script, GIT_HTTPS_BRIDGE_PY)?; + let port_file = temp.path().join("port"); + let mut bridge = Command::new("python3") + .arg(&bridge_script) + .arg(&port_file) + .arg(&repos) + .arg(&cert) + .arg(&key) + .arg(&git_http_backend) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .context("starting the test HTTPS bridge")?; + let port: u16 = { + let mut attempts = 0; + loop { + if let Ok(text) = fs::read_to_string(&port_file) { + if let Ok(port) = text.trim().parse() { + break port; + } + } + if let Some(status) = bridge.try_wait()? { + let mut stderr = String::new(); + if let Some(mut pipe) = bridge.stderr.take() { + let _ = pipe.read_to_string(&mut stderr); + } + bail!("the test HTTPS bridge exited early with {status}: {stderr}"); + } + attempts += 1; + ensure!(attempts < 200, "the test HTTPS bridge never reported its port"); + std::thread::sleep(std::time::Duration::from_millis(25)); + } + }; + struct KillOnDrop(std::process::Child); + impl Drop for KillOnDrop { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } + } + let _bridge = KillOnDrop(bridge); + + let source_cache_root = temp.path().join("source-cache"); + let frozen_source_root = temp.path().join("frozen-source"); + fs::create_dir(&frozen_source_root)?; + fs::set_permissions(&frozen_source_root, fs::Permissions::from_mode(0o700))?; + let identity = ProcessIdentity { uid: 1000, gid: 1000 }; + + // Trust for the self-signed CA is scoped to this driver's own Git + // identity, not the container's real trust store: `prepare_source_ + // root` assigns `HOME` (`source_cache_root/.home`) to `identity` + // and `git_command` clears every other environment variable, so + // `$HOME/.gitconfig`'s `http.sslCAInfo` is the only channel left + // for this test to hand the driver's own `git` a CA it will trust. + // Pre-created here, with the same ownership and mode `ensure_ + // source_directory` would assign, so it passes that function's + // ownership check instead of being reassigned or rejected. + use std::os::unix::ffi::OsStrExt; + fs::create_dir(&source_cache_root)?; + unsafe { + ensure!( + libc::chown( + std::ffi::CString::new(source_cache_root.as_os_str().as_bytes())?.as_ptr(), + identity.uid, + identity.gid, + ) == 0, + "chowning the test source cache root" + ); + } + fs::set_permissions(&source_cache_root, fs::Permissions::from_mode(0o750))?; + let home = source_cache_root.join(".home"); + fs::create_dir(&home)?; + fs::write(home.join(".gitconfig"), format!("[http]\n\tsslCAInfo = {}\n", cert.display()))?; + unsafe { + ensure!( + libc::chown( + std::ffi::CString::new(home.as_os_str().as_bytes())?.as_ptr(), + identity.uid, + identity.gid, + ) == 0, + "chowning the test HOME directory" + ); + ensure!( + libc::chown( + std::ffi::CString::new(home.join(".gitconfig").as_os_str().as_bytes())?.as_ptr(), + identity.uid, + identity.gid, + ) == 0, + "chowning the test .gitconfig" + ); + } + fs::set_permissions(&home, fs::Permissions::from_mode(0o750))?; + fs::set_permissions(home.join(".gitconfig"), fs::Permissions::from_mode(0o640))?; + + let driver = GitSourceDriver::new( + source_cache_root.clone(), + frozen_source_root.clone(), + Some(identity), + ); + let origin_url = format!("https://127.0.0.1:{port}/origin.git"); + let checkout = source_cache_root.join("checkout"); + + let render_binding = |resolution_target: &str| -> String { + HTTPS_RIG_BINDING + .replace("{origin}", &origin_url) + .replace("{minimum_revision}", &root_commit) + .replace("{checkout}", &checkout.to_string_lossy()) + .replace("target = \"https-rig\"", &format!("target = \"{resolution_target}\"")) + }; + + // Clean: `main` still points at the root commit. + let binding = OperatorBinding::parse(&render_binding("https-rig-clean")) + .context("parsing the clean HTTPS rig binding")?; + driver + .resolve(&binding, "r1", 1) + .context("a clean HTTPS origin must resolve")?; + + // Hostile: reuse this suite's own dup-trees and `.GIT` fixtures + // (`s4_4_hostile_fixtures`), moving `main` onto each in turn. + for (label, build) in s4_4_hostile_fixtures() + .into_iter() + .filter(|(label, _)| *label == "dup-trees" || *label == ".GIT") + { + let entries = build(&origin_repo, &recipe_blob) + .with_context(|| format!("building the {label} fixture"))?; + let entry_refs: Vec<(&str, &str, &str)> = entries + .iter() + .map(|(mode, name, sha)| (mode.as_str(), name.as_str(), sha.as_str())) + .collect(); + let hostile_tree = literal_tree(&origin_repo, &entry_refs)?; + let hostile_commit = git_owned( + &origin_repo, + &["commit-tree", &hostile_tree, "-p", &root_commit, "-m", "hostile"], + )?; + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &hostile_commit])?; + + let binding = OperatorBinding::parse(&render_binding(&format!("https-rig-{label}"))) + .with_context(|| format!("parsing the {label} HTTPS rig binding"))?; + let result = driver.resolve(&binding, &format!("r-{label}"), 1); + assert!( + result.is_err(), + "{label}: a hostile tree moved onto main over a real HTTPS origin must be \ + refused by resolve()'s own guarded fetch: {result:?}" + ); + + // Restore `main` to the clean commit before the next fixture, + // so each one is judged against its own fresh fetch. + git_owned(&origin_repo, &["update-ref", "refs/heads/main", &root_commit])?; + } + + Ok(()) + } }