//! Path mappings: which remote path a project file goes to, and which files //! are excluded. //! //! Local paths are project-relative with `/`. A file maps through the //! mapping with the longest local folder that contains it; without any //! mapping the project root maps to the server's root. Remote paths are //! joined with `/`, never contain `..`, and stay inside the mapping's //! remote folder. use crate::config::Server; /// `path` with `\` turned into `/`, empty and `.` segments dropped, and no /// leading or trailing `/`. `None` if it contains `..` or a control /// character. pub fn normalize_relative(path: &str) -> Option { if path.chars().any(char::is_control) { return None; } let mut parts = Vec::new(); for part in path.split(['/', '\\']) { match part { "" | "." => {} ".." => return None, other => parts.push(other), } } Some(parts.join("/")) } /// Whether `pattern` is a usable excluded path: not empty, no `..`, no /// control characters. pub fn valid_pattern(pattern: &str) -> bool { !pattern.is_empty() && normalize_relative(pattern).is_some_and(|p| !p.is_empty()) } /// Whether `name` matches `pattern`, where `*` matches any run of /// characters and `?` exactly one. fn glob(pattern: &[char], name: &[char]) -> bool { let (mut p, mut n) = (0usize, 0usize); let mut star: Option<(usize, usize)> = None; while n < name.len() { match pattern.get(p) { Some('*') => { star = Some((p, n)); p += 1; } Some(c) if *c == '?' || Some(c) == name.get(n) => { p += 1; n += 1; } _ => match star { Some((sp, sn)) => { p = sp + 1; n = sn + 1; star = Some((sp, sn + 1)); } None => return false, }, } } pattern .get(p..) .is_some_and(|rest| rest.iter().all(|c| *c == '*')) } /// Whether the project-relative `path` (normalized) is excluded by /// `server`: a pattern without `*` or `?` excludes that path and /// everything below it; a pattern with them is matched against every /// segment's name. pub fn is_excluded(server: &Server, path: &str) -> bool { server.excluded.iter().any(|pattern| { if pattern.contains(['*', '?']) { let pattern: Vec = pattern.chars().collect(); path.split('/').any(|segment| { let name: Vec = segment.chars().collect(); glob(&pattern, &name) }) } else { path == pattern || path .strip_prefix(pattern.as_str()) .is_some_and(|rest| rest.starts_with('/')) } }) } /// Joins remote path pieces with `/`, skipping empty ones. Keeps a leading /// `/` of the first non-empty piece. fn join(pieces: &[&str]) -> String { let mut out = String::new(); for piece in pieces.iter().filter(|p| !p.is_empty()) { if out.is_empty() { out.push_str(piece.trim_end_matches('/')); if out.is_empty() { out.push('/'); } } else { if !out.ends_with('/') { out.push('/'); } out.push_str(piece.trim_matches('/')); } } out } /// The remote path of the project-relative `path` on `server`, or `None` /// when it is excluded, escapes the project, or no mapping covers it. /// For a folder server the result is relative to its root folder. pub fn remote_path(server: &Server, path: &str) -> Option { let path = normalize_relative(path)?; if is_excluded(server, &path) { return None; } let (local, remote) = if server.mappings.is_empty() { ("", "") } else { server .mappings .iter() .filter(|m| { m.local.is_empty() || path == m.local || path .strip_prefix(m.local.as_str()) .is_some_and(|rest| rest.starts_with('/')) }) .max_by_key(|m| m.local.len()) .map(|m| (m.local.as_str(), m.remote.as_str()))? }; let rest = path .strip_prefix(local) .unwrap_or(&path) .trim_start_matches('/'); let root = if remote.starts_with('/') || server.kind == crate::ServerKind::Folder { "" } else { server.root.as_str() }; let joined = join(&[root, remote, rest]); Some(joined) } #[cfg(test)] mod tests { use super::*; use crate::config::{AuthKind, Mapping, ServerKind}; use proptest::prelude::*; fn server(mappings: &[(&str, &str)], excluded: &[&str]) -> Server { Server { id: "s".into(), name: "S".into(), kind: ServerKind::Sftp, host: "h".into(), port: None, user: "u".into(), root: "/var/www".into(), auth: AuthKind::Password, key_path: None, mappings: mappings .iter() .map(|(l, r)| Mapping { local: (*l).into(), remote: (*r).into(), }) .collect(), excluded: excluded.iter().map(|e| (*e).into()).collect(), upload_on_save: false, } } #[test] fn normalizes() { assert_eq!(normalize_relative("./a//b\\c/").unwrap(), "a/b/c"); assert_eq!(normalize_relative("").unwrap(), ""); assert!(normalize_relative("a/../b").is_none()); assert!(normalize_relative("a\nb").is_none()); } #[test] fn default_mapping_uses_the_root() { let s = server(&[], &[]); assert_eq!(remote_path(&s, "src/a.php").unwrap(), "/var/www/src/a.php"); let mut s = s; s.root = String::new(); assert_eq!(remote_path(&s, "src/a.php").unwrap(), "src/a.php"); s.root = "/".into(); assert_eq!(remote_path(&s, "a.php").unwrap(), "/a.php"); } #[test] fn longest_mapping_wins() { let s = server(&[("", "app"), ("public", "/srv/html"), ("pub", "x")], &[]); assert_eq!( remote_path(&s, "public/index.php").unwrap(), "/srv/html/index.php" ); assert_eq!(remote_path(&s, "public").unwrap(), "/srv/html"); assert_eq!( remote_path(&s, "publicity.txt").unwrap(), "/var/www/app/publicity.txt" ); assert_eq!(remote_path(&s, "pub/a").unwrap(), "/var/www/x/a"); } #[test] fn unmapped_and_excluded_paths_are_none() { let s = server( &[("public", "html")], &[".env", "var/cache", "*.log", "tmp?"], ); assert!(remote_path(&s, "src/a.php").is_none()); let s = server(&[], &[".env", "var/cache", "*.log", "tmp?"]); assert!(remote_path(&s, ".env").is_none()); assert!(remote_path(&s, "var/cache/x").is_none()); assert!(remote_path(&s, "var/cache").is_none()); assert!(remote_path(&s, "var/cached").is_some()); assert!(remote_path(&s, "logs/app.log").is_none()); assert!(remote_path(&s, "tmp1/a").is_none()); assert!(remote_path(&s, "tmp12/a").is_some()); assert!(remote_path(&s, "../x").is_none()); } #[test] fn globs() { let g = |p: &str, n: &str| { glob( &p.chars().collect::>(), &n.chars().collect::>(), ) }; assert!(g("*.log", "a.log")); assert!(g("*", "")); assert!(g("a*b*c", "aXbYbZc")); assert!(!g("a*b", "aXc")); assert!(!g("?", "")); } proptest! { #[test] fn mapped_paths_stay_under_the_mapping( parts in proptest::collection::vec("[a-zA-Z0-9._ -]{1,8}", 1..6), remote in "[a-z]{0,6}", ) { let s = server(&[("", &remote)], &[]); let path = parts.join("/"); if let Some(r) = remote_path(&s, &path) { let base = join(&["/var/www", &remote]); prop_assert!(r.starts_with(&base)); prop_assert!(!r.split('/').any(|seg| seg == "..")); } else { prop_assert!(normalize_relative(&path).is_none()); } } #[test] fn glob_never_panics(p in ".{0,12}", n in ".{0,12}") { let _ = glob(&p.chars().collect::>(), &n.chars().collect::>()); } } }