#[cfg(hookfs_backend)]
use crate::namespace::{Namespace, is_lexically_safe};
#[cfg(hookfs_backend)]
use crate::registry::Registry;
#[cfg(hookfs_backend)]
use crate::vfs::VirtualFs;
#[cfg(hookfs_backend)]
use std::path::{Path, PathBuf};
#[cfg(hookfs_backend)]
use std::sync::{Arc, Mutex};
#[cfg(hookfs_backend)]
#[derive(Debug)]
pub(crate) enum Route {
Virtual(PathBuf),
Rejected,
Real,
}
#[cfg(hookfs_backend)]
pub(crate) struct Engine {
vfs: Arc<dyn VirtualFs>,
namespace: Namespace,
registry: Registry,
cwd: Mutex<Option<String>>,
allow_writes: bool,
}
#[cfg(hookfs_backend)]
impl std::fmt::Debug for Engine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Engine")
.field("namespace", &self.namespace)
.field("registry", &self.registry)
.field("allow_writes", &self.allow_writes)
.finish_non_exhaustive()
}
}
#[cfg(hookfs_backend)]
impl Engine {
pub(crate) fn new(vfs: Arc<dyn VirtualFs>, namespace: Namespace, allow_writes: bool) -> Self {
Self {
vfs,
namespace,
registry: Registry::new(),
cwd: Mutex::new(None),
allow_writes,
}
}
pub(crate) fn vfs(&self) -> &Arc<dyn VirtualFs> {
&self.vfs
}
pub(crate) fn registry(&self) -> &Registry {
&self.registry
}
pub(crate) fn allow_writes(&self) -> bool {
self.allow_writes
}
pub(crate) fn virtual_cwd(&self) -> Option<String> {
self.cwd
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
}
#[cfg(any(test, unix))]
pub(crate) fn set_virtual_cwd(&self, cwd: Option<String>) {
*self
.cwd
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = cwd;
}
pub(crate) fn classify(&self, decoded: &str) -> Route {
let absolute: PathBuf = if is_absolute(decoded) {
PathBuf::from(decoded)
} else if let Some(cwd) = self.virtual_cwd() {
Path::new(&cwd).join(decoded)
} else {
return Route::Real;
};
if !self.namespace.is_virtual(&absolute) {
return Route::Real;
}
if is_lexically_safe(&absolute.to_string_lossy()) {
Route::Virtual(absolute)
} else {
Route::Rejected
}
}
}
#[cfg(windows)]
pub(crate) fn is_absolute(s: &str) -> bool {
if s.starts_with("\\\\") || s.starts_with("//") {
return true;
}
let bytes = s.as_bytes();
bytes.first().is_some_and(u8::is_ascii_alphabetic)
&& bytes.get(1) == Some(&b':')
&& matches!(bytes.get(2), Some(b'\\' | b'/'))
}
#[cfg(unix)]
pub(crate) fn is_absolute(s: &str) -> bool {
s.starts_with('/')
}
#[cfg(all(test, windows))]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::providers::MemoryFs;
fn engine() -> (Engine, PathBuf) {
let root = PathBuf::from("C:\\__hookfs__\\rtest");
(
Engine::new(MemoryFs::new(), Namespace::from_root(root.clone()), false),
root,
)
}
#[test]
fn absoluteness() {
assert!(is_absolute("C:\\a"));
assert!(is_absolute("c:/a"));
assert!(is_absolute("\\\\server\\share"));
assert!(is_absolute("\\\\?\\C:\\a"));
assert!(!is_absolute("a\\b"));
assert!(!is_absolute("relative"));
}
#[test]
fn classifies_virtual_real_and_rejected() {
let (e, root) = engine();
let clip = root.join("sample.braw");
assert!(matches!(
e.classify(&clip.to_string_lossy()),
Route::Virtual(_)
));
assert!(matches!(
e.classify("C:\\Windows\\notepad.exe"),
Route::Real
));
let escape = format!("{}\\..\\..\\secret", root.display());
assert!(matches!(e.classify(&escape), Route::Rejected));
assert!(matches!(e.classify("sample.braw"), Route::Real));
}
#[test]
fn relative_paths_resolve_against_virtual_cwd() {
let (e, root) = engine();
e.set_virtual_cwd(Some(root.to_string_lossy().into_owned()));
assert!(matches!(e.classify("sample.braw"), Route::Virtual(_)));
}
}
#[cfg(all(test, unix))]
#[allow(clippy::unwrap_used)]
mod posix_tests {
use super::*;
#[cfg(hookfs_backend)]
use crate::providers::MemoryFs;
#[cfg(hookfs_backend)]
fn engine() -> (Engine, PathBuf) {
let root = PathBuf::from("/__hookfs__/rtest");
(
Engine::new(MemoryFs::new(), Namespace::from_root(root.clone()), false),
root,
)
}
#[test]
fn absoluteness() {
assert!(is_absolute("/etc/passwd"));
assert!(!is_absolute("relative/path"));
assert!(!is_absolute("./a"));
}
#[cfg(hookfs_backend)]
#[test]
fn classifies_virtual_real_and_rejected() {
let (e, root) = engine();
let clip = root.join("sample.braw");
assert!(matches!(
e.classify(&clip.to_string_lossy()),
Route::Virtual(_)
));
assert!(matches!(e.classify("/usr/bin/ls"), Route::Real));
let escape = format!("{}/../../secret", root.display());
assert!(matches!(e.classify(&escape), Route::Rejected));
assert!(matches!(e.classify("sample.braw"), Route::Real));
}
#[cfg(hookfs_backend)]
#[test]
fn relative_paths_resolve_against_virtual_cwd() {
let (e, root) = engine();
e.set_virtual_cwd(Some(root.to_string_lossy().into_owned()));
assert!(matches!(e.classify("sample.braw"), Route::Virtual(_)));
}
}