From 74c76bb5cea8ff70153a8f9ebe02df6ffb0205f7 Mon Sep 17 00:00:00 2001 From: qvkap Date: Sat, 5 Sep 2026 08:29:46 +0300 Subject: fix: dependency resolution, update file removal and sync TLS validation --- src/sync.rs | 161 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 103 insertions(+), 58 deletions(-) (limited to 'src/sync.rs') diff --git a/src/sync.rs b/src/sync.rs index ad36aea..2d1b6b7 100755 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,21 +1,75 @@ use colored::*; -use std::path::{Path}; -use git2::{Repository, RemoteCallbacks, FetchOptions, build::RepoBuilder}; +use std::path::Path; +use std::process::Command; use crate::config::Config; const REPOS_DIR: &str = "/var/ror/packages"; +fn enforce_repo_https(url: &str) -> Result<(), String> { + if url.starts_with("http://") { + return Err(format!( + "Insecure HTTP URL rejected: {}. Only HTTPS is allowed for repositories.", + url + )); + } + Ok(()) +} + +fn check_repo_permissions() { + let repo_path = Path::new(REPOS_DIR); + if !repo_path.exists() { + return; + } + match std::fs::metadata(repo_path) { + Ok(meta) => { + use std::os::unix::fs::MetadataExt; + let uid = meta.uid(); + if uid != 0 { + eprintln!( + "{} WARNING: Repository directory '{}' is owned by UID {} (not root). \ + This is a security risk — any user can modify packages.", + "[ror]".yellow().bold(), + REPOS_DIR, + uid + ); + } + let mode = meta.mode() & 0o7777; + if mode & 0o002 != 0 { + eprintln!( + "{} WARNING: Repository directory '{}' is world-writable (mode {:o}). \ + This is a security risk — any user can inject malicious packages.", + "[ror]".yellow().bold(), + REPOS_DIR, + mode + ); + } + } + Err(_) => {} + } +} + pub fn run_sync(cfg: &Config) { if cfg.repositories.is_empty() { eprintln!("{} No repositories configured.", "[ror]".red().bold()); return; } + check_repo_permissions(); for (name, repo) in &cfg.repositories { + if let Err(e) = enforce_repo_https(&repo.url) { + eprintln!("{} {}: {}", "[ror]".red().bold(), name, e); + continue; + } + if let Some(ref mirror) = repo.mirror { + if let Err(e) = enforce_repo_https(mirror) { + eprintln!("{} {}: {}", "[ror]".red().bold(), name, e); + continue; + } + } println!("{} Syncing repository '{}'...", "[ror]".blue().bold(), name); let repo_path = Path::new(REPOS_DIR).join(name); if repo_path.exists() { println!("{} Updating {}...", ">>>".green(), name); - if let Err(e) = pull_or_clone(&repo_path, &repo.url) { + if let Err(e) = pull_repo(&repo_path, repo.mirror.as_deref()) { eprintln!("{} {}", "[ror]".red(), e); } else { println!("{} Repository updated.", ">>>".green()); @@ -33,66 +87,57 @@ pub fn run_sync(cfg: &Config) { fn clone_repo(name: &str, primary: &str, mirror: Option<&str>) -> Result<(), String> { let path = Path::new(REPOS_DIR).join(name); - let cb = RemoteCallbacks::new(); - let mut fo = FetchOptions::new(); - fo.remote_callbacks(cb); - let result = RepoBuilder::new() - .fetch_options(fo) - .clone(primary, &path); - if result.is_err() { - if let Some(mirror_url) = mirror { - let cb2 = RemoteCallbacks::new(); - let mut fo2 = FetchOptions::new(); - fo2.remote_callbacks(cb2); - RepoBuilder::new() - .fetch_options(fo2) - .clone(mirror_url, &path) - .map_err(|e| format!("Failed to clone from primary and mirror: {}", e))?; - } else { - return Err(format!("Clone failed: {}", result.err().unwrap())); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|e| format!("Failed to create repos dir: {}", e))?; + } + + let status = Command::new("git") + .args(["clone", primary, path.to_str().unwrap()]) + .status() + .map_err(|e| format!("Failed to run git: {}", e))?; + + if status.success() { + return Ok(()); + } + + if let Some(mirror_url) = mirror { + eprintln!("{} Primary clone failed, trying mirror...", "[ror]".yellow()); + let mirror_status = Command::new("git") + .args(["clone", mirror_url, path.to_str().unwrap()]) + .status() + .map_err(|e| format!("Failed to run git clone for mirror: {}", e))?; + + if mirror_status.success() { + return Ok(()); } } - Ok(()) + + Err(format!("Failed to clone repository '{}'", name)) } -fn pull_or_clone(repo_path: &Path, primary: &str) -> Result<(), String> { - let repo = Repository::open(repo_path) - .map_err(|e| format!("Cannot open repository: {}", e))?; - let mut remote = repo.find_remote("origin") - .or_else(|_| { - repo.remote_set_url("origin", primary)?; - repo.find_remote("origin") - }) - .map_err(|e| format!("Remote error: {}", e))?; - let cb = RemoteCallbacks::new(); - let mut fo = FetchOptions::new(); - fo.remote_callbacks(cb); - remote.fetch(&["refs/heads/*:refs/heads/*"], Some(&mut fo), None) - .map_err(|e| format!("Fetch failed: {}", e))?; - let fetch_head = repo.find_reference("FETCH_HEAD") - .map_err(|e| format!("FETCH_HEAD missing: {}", e))?; - let fetch_commit = repo.reference_to_annotated_commit(&fetch_head) - .map_err(|e| format!("Annotated commit error: {}", e))?; - let (analysis, _) = repo.merge_analysis(&[&fetch_commit]) - .map_err(|e| format!("Merge analysis failed: {}", e))?; - if analysis.is_fast_forward() { - let mut refname = "refs/heads/master".to_string(); - if let Ok(head) = repo.head() { - if let Some(name) = head.name() { - refname = name.to_string(); - } +fn pull_repo(repo_path: &Path, mirror: Option<&str>) -> Result<(), String> { + let path_str = repo_path.to_str().ok_or("Invalid repo path")?; + let status = Command::new("git") + .args(["-C", path_str, "pull"]) + .status() + .map_err(|e| format!("Failed to run git pull: {}", e))?; + + if status.success() { + return Ok(()); + } + + if let Some(mirror_url) = mirror { + eprintln!("{} Pull from primary failed, trying mirror...", "[ror]".yellow()); + let mirror_status = Command::new("git") + .args(["-C", path_str, "pull", mirror_url]) + .status() + .map_err(|e| format!("Failed to run git pull from mirror: {}", e))?; + + if mirror_status.success() { + return Ok(()); } - let mut reference = repo.find_reference(&refname) - .map_err(|e| format!("Reference not found: {}", e))?; - reference.set_target(fetch_commit.id(), "fast-forward merge") - .map_err(|e| format!("Set target failed: {}", e))?; - repo.set_head(&refname) - .map_err(|e| format!("Set head failed: {}", e))?; - repo.checkout_head(Some(git2::build::CheckoutBuilder::new().force())) - .map_err(|e| format!("Checkout failed: {}", e))?; - } else if analysis.is_normal() { - return Err("Non-fast-forward update requires manual merge".to_string()); } - Ok(()) + + Err(format!("Failed to pull repository at '{}'", path_str)) } -- cgit v1.2.3