| 1 |
// path_auditor.rs |
| 2 |
// |
| 3 |
// Copyright 2020 |
| 4 |
// Raphaël Gomès <[email protected]>, |
| 5 |
// |
| 6 |
// This software may be used and distributed according to the terms of the |
| 7 |
// GNU General Public License version 2 or any later version. |
| 8 |
|
| 9 |
use std::collections::HashSet; |
| 10 |
use std::path::Path; |
| 11 |
use std::path::PathBuf; |
| 12 |
use std::sync::Mutex; |
| 13 |
use std::sync::RwLock; |
| 14 |
|
| 15 |
use crate::utils::files::lower_clean; |
| 16 |
use crate::utils::hg_path::hg_path_to_path_buf; |
| 17 |
use crate::utils::hg_path::HgPath; |
| 18 |
use crate::utils::hg_path::HgPathBuf; |
| 19 |
use crate::utils::hg_path::HgPathError; |
| 20 |
use crate::utils::hg_path::HgPathErrorKind; |
| 21 |
use crate::utils::strings::find_slice_in_slice; |
| 22 |
|
| 23 |
/// Ensures that a path is valid for use in the repository i.e. does not use |
| 24 |
/// any banned components, does not traverse a symlink, etc. |
| 25 |
#[derive(Debug, Default)] |
| 26 |
pub struct PathAuditor { |
| 27 |
audited: Mutex<HashSet<HgPathBuf>>, |
| 28 |
audited_dirs: RwLock<HashSet<HgPathBuf>>, |
| 29 |
root: PathBuf, |
| 30 |
} |
| 31 |
|
| 32 |
impl PathAuditor { |
| 33 |
pub fn new(root: impl AsRef<Path>) -> Self { |
| 34 |
Self { root: root.as_ref().to_owned(), ..Default::default() } |
| 35 |
} |
| 36 |
pub fn audit_path( |
| 37 |
&self, |
| 38 |
path: impl AsRef<HgPath>, |
| 39 |
) -> Result<(), HgPathError> { |
| 40 |
// TODO windows "localpath" normalization |
| 41 |
let path = path.as_ref(); |
| 42 |
if path.is_empty() { |
| 43 |
return Ok(()); |
| 44 |
} |
| 45 |
// TODO case normalization |
| 46 |
if self.audited.lock().unwrap().contains(path) { |
| 47 |
return Ok(()); |
| 48 |
} |
| 49 |
// AIX ignores "/" at end of path, others raise EISDIR. |
| 50 |
let last_byte = path.as_bytes()[path.len() - 1]; |
| 51 |
if last_byte == b'/' || last_byte == b'\\' { |
| 52 |
return Err(HgPathErrorKind::EndsWithSlash(path.to_owned()).into()); |
| 53 |
} |
| 54 |
let parts: Vec<_> = path |
| 55 |
.as_bytes() |
| 56 |
.split(|b| std::path::is_separator(*b as char)) |
| 57 |
.collect(); |
| 58 |
|
| 59 |
let first_component = lower_clean(parts[0]); |
| 60 |
let first_component = first_component.as_slice(); |
| 61 |
if !path.split_drive().0.is_empty() |
| 62 |
|| (first_component == b".hg" |
| 63 |
|| first_component == b".hg." |
| 64 |
|| first_component == b"") |
| 65 |
|| parts.iter().any(|c| c == b"..") |
| 66 |
{ |
| 67 |
return Err(HgPathErrorKind::InsideDotHg(path.to_owned()).into()); |
| 68 |
} |
| 69 |
|
| 70 |
// Windows shortname aliases |
| 71 |
for part in parts.iter() { |
| 72 |
if part.contains(&b'~') { |
| 73 |
let mut split = part.splitn(2, |b| *b == b'~'); |
| 74 |
let first = |
| 75 |
split.next().unwrap().to_owned().to_ascii_uppercase(); |
| 76 |
let last = split.next().unwrap(); |
| 77 |
if last.iter().all(u8::is_ascii_digit) |
| 78 |
&& (first == b"HG" || first == b"HG8B6C") |
| 79 |
{ |
| 80 |
return Err(HgPathErrorKind::ContainsIllegalComponent( |
| 81 |
path.to_owned(), |
| 82 |
) |
| 83 |
.into()); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
let lower_path = lower_clean(path.as_bytes()); |
| 88 |
if find_slice_in_slice(&lower_path, b".hg").is_some() { |
| 89 |
let lower_parts: Vec<_> = path |
| 90 |
.as_bytes() |
| 91 |
.split(|b| std::path::is_separator(*b as char)) |
| 92 |
.collect(); |
| 93 |
for pattern in [b".hg".to_vec(), b".hg.".to_vec()].iter() { |
| 94 |
if let Some(pos) = lower_parts[1..] |
| 95 |
.iter() |
| 96 |
.position(|part| part == &pattern.as_slice()) |
| 97 |
{ |
| 98 |
let base = lower_parts[..=pos] |
| 99 |
.iter() |
| 100 |
.fold(HgPathBuf::new(), |acc, p| { |
| 101 |
acc.join(HgPath::new(p)) |
| 102 |
}); |
| 103 |
return Err(HgPathErrorKind::IsInsideNestedRepo { |
| 104 |
path: path.to_owned(), |
| 105 |
nested_repo: base, |
| 106 |
} |
| 107 |
.into()); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
let parts = &parts[..parts.len().saturating_sub(1)]; |
| 113 |
|
| 114 |
// We don't want to add "foo/bar/baz" to `audited_dirs` before checking |
| 115 |
// if there's a "foo/.hg" directory. This also means we won't |
| 116 |
// accidentally traverse a symlink into some other filesystem (which |
| 117 |
// is potentially expensive to access). |
| 118 |
for index in 0..parts.len() { |
| 119 |
let prefix = &parts[..=index].join(&b'/'); |
| 120 |
let prefix = HgPath::new(prefix); |
| 121 |
if self.audited_dirs.read().unwrap().contains(prefix) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
self.check_filesystem(prefix, path)?; |
| 125 |
self.audited_dirs.write().unwrap().insert(prefix.to_owned()); |
| 126 |
} |
| 127 |
|
| 128 |
self.audited.lock().unwrap().insert(path.to_owned()); |
| 129 |
|
| 130 |
Ok(()) |
| 131 |
} |
| 132 |
|
| 133 |
pub fn check_filesystem( |
| 134 |
&self, |
| 135 |
prefix: impl AsRef<HgPath>, |
| 136 |
path: impl AsRef<HgPath>, |
| 137 |
) -> Result<(), HgPathError> { |
| 138 |
let prefix = prefix.as_ref(); |
| 139 |
let path = path.as_ref(); |
| 140 |
let current_path = |
| 141 |
self.root.join(hg_path_to_path_buf(prefix).map_err(|_| { |
| 142 |
HgPathErrorKind::NotFsCompliant(path.to_owned()) |
| 143 |
})?); |
| 144 |
check_filesystem_single(current_path, prefix, path)?; |
| 145 |
|
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
|
| 149 |
pub fn check(&self, path: impl AsRef<HgPath>) -> bool { |
| 150 |
self.audit_path(path).is_ok() |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/// Check a single path for filesystem rules: |
| 155 |
/// - Is not a valid representation on this filesystem |
| 156 |
/// - Traverses a symlink |
| 157 |
/// - Is inside a nested repository |
| 158 |
/// |
| 159 |
/// This is only useful in the context of checking ancestor directories of |
| 160 |
/// `full_path`. |
| 161 |
/// |
| 162 |
/// # Arguments |
| 163 |
/// |
| 164 |
/// Both `path` and `hg_path` are passed in for performance reasons. |
| 165 |
/// |
| 166 |
/// * `ancestor`: The current ancestor of the filesystem path to check |
| 167 |
/// * `hg_ancestor`: The `HgPath` that corresponds to `ancestor` |
| 168 |
/// * `full_path`: The hg path that the overall logic wants to check |
| 169 |
pub fn check_filesystem_single( |
| 170 |
ancestor: impl AsRef<Path>, |
| 171 |
hg_ancestor: &HgPath, |
| 172 |
full_path: &HgPath, |
| 173 |
) -> Result<(), HgPathError> { |
| 174 |
let ancestor = ancestor.as_ref(); |
| 175 |
match std::fs::symlink_metadata(ancestor) { |
| 176 |
Err(e) => { |
| 177 |
// EINVAL can be raised as invalid path syntax under win32. |
| 178 |
if e.kind() != std::io::ErrorKind::NotFound |
| 179 |
&& e.kind() != std::io::ErrorKind::InvalidInput |
| 180 |
&& e.raw_os_error() != Some(20) |
| 181 |
{ |
| 182 |
// Rust does not yet have an `ErrorKind` for |
| 183 |
// `NotADirectory` (errno 20) |
| 184 |
// It happens if the dirstate contains `foo/bar` and |
| 185 |
// foo is not a directory |
| 186 |
return Err(HgPathErrorKind::NotFsCompliant( |
| 187 |
full_path.to_owned(), |
| 188 |
) |
| 189 |
.into()); |
| 190 |
} |
| 191 |
} |
| 192 |
Ok(meta) => { |
| 193 |
if meta.file_type().is_symlink() { |
| 194 |
return Err(HgPathErrorKind::TraversesSymbolicLink { |
| 195 |
path: full_path.to_owned(), |
| 196 |
symlink: hg_ancestor.to_owned(), |
| 197 |
} |
| 198 |
.into()); |
| 199 |
} |
| 200 |
if meta.file_type().is_dir() && ancestor.join(".hg").is_dir() { |
| 201 |
return Err(HgPathErrorKind::IsInsideNestedRepo { |
| 202 |
path: full_path.to_owned(), |
| 203 |
nested_repo: hg_ancestor.to_owned(), |
| 204 |
} |
| 205 |
.into()); |
| 206 |
} |
| 207 |
} |
| 208 |
}; |
| 209 |
Ok(()) |
| 210 |
} |
| 211 |
|
| 212 |
#[cfg(test)] |
| 213 |
mod tests { |
| 214 |
use std::fs::create_dir; |
| 215 |
use std::fs::create_dir_all; |
| 216 |
use std::fs::File; |
| 217 |
|
| 218 |
use tempfile::tempdir; |
| 219 |
|
| 220 |
use super::*; |
| 221 |
|
| 222 |
#[test] |
| 223 |
fn test_path_auditor() { |
| 224 |
let base_dir = tempdir().unwrap(); |
| 225 |
let base_dir_path = base_dir.path(); |
| 226 |
let auditor = PathAuditor::new(base_dir_path); |
| 227 |
|
| 228 |
let path = HgPath::new(b".hg/00changelog.i"); |
| 229 |
assert_eq!( |
| 230 |
auditor.audit_path(path), |
| 231 |
Err(HgPathErrorKind::InsideDotHg(path.to_owned()).into()) |
| 232 |
); |
| 233 |
let path = HgPath::new(b"this/is/nested/.hg/thing.txt"); |
| 234 |
assert_eq!( |
| 235 |
auditor.audit_path(path), |
| 236 |
Err(HgPathErrorKind::IsInsideNestedRepo { |
| 237 |
path: path.to_owned(), |
| 238 |
nested_repo: HgPathBuf::from_bytes(b"this/is/nested") |
| 239 |
} |
| 240 |
.into()) |
| 241 |
); |
| 242 |
|
| 243 |
create_dir_all(base_dir_path.join("this/is/nested/.hg")).unwrap(); |
| 244 |
let path = HgPath::new(b"this/is/nested/repo"); |
| 245 |
assert_eq!( |
| 246 |
auditor.audit_path(path), |
| 247 |
Err(HgPathErrorKind::IsInsideNestedRepo { |
| 248 |
path: path.to_owned(), |
| 249 |
nested_repo: HgPathBuf::from_bytes(b"this/is/nested") |
| 250 |
} |
| 251 |
.into()) |
| 252 |
); |
| 253 |
|
| 254 |
create_dir(base_dir_path.join("realdir")).unwrap(); |
| 255 |
File::create(base_dir_path.join("realdir/realfile")).unwrap(); |
| 256 |
// TODO make portable |
| 257 |
std::os::unix::fs::symlink( |
| 258 |
base_dir_path.join("realdir"), |
| 259 |
base_dir_path.join("symlink"), |
| 260 |
) |
| 261 |
.unwrap(); |
| 262 |
let path = HgPath::new(b"symlink/realfile"); |
| 263 |
assert_eq!( |
| 264 |
auditor.audit_path(path), |
| 265 |
Err(HgPathErrorKind::TraversesSymbolicLink { |
| 266 |
path: path.to_owned(), |
| 267 |
symlink: HgPathBuf::from_bytes(b"symlink"), |
| 268 |
} |
| 269 |
.into()) |
| 270 |
); |
| 271 |
} |
| 272 |
} |