Skip to content

Commit

Permalink
Move jail utils to 'jail' module.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Mar 16, 2024
1 parent 3a063a2 commit 0ae4eaa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
23 changes: 22 additions & 1 deletion src/jail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,29 @@ pub struct Jail {
saved_cwd: PathBuf,
}

/// Convert a `T: Display` to a `String`.
fn as_string<S: Display>(s: S) -> String { s.to_string() }

/// Remove any dots from the path by popping as needed.
fn dedot(path: &Path) -> PathBuf {
use std::path::Component::*;

let mut comps = vec![];
for component in path.components() {
match component {
p@Prefix(_) => comps = vec![p],
r@RootDir if comps.iter().all(|c| matches!(c, Prefix(_))) => comps.push(r),
r@RootDir => comps = vec![r],
CurDir => { },
ParentDir if comps.iter().all(|c| matches!(c, Prefix(_) | RootDir)) => { },
ParentDir => { comps.pop(); },
c@Normal(_) => comps.push(c),
}
}

comps.iter().map(|c| c.as_os_str()).collect()
}

static LOCK: Mutex<()> = parking_lot::const_mutex(());

impl Jail {
Expand Down Expand Up @@ -138,7 +159,7 @@ impl Jail {
}

fn safe_jailed_path(&self, path: &Path) -> Result<PathBuf> {
let path = crate::util::dedot(path);
let path = dedot(path);
if path.is_absolute() && path.starts_with(self.directory()) {
return Ok(path);
}
Expand Down
24 changes: 0 additions & 24 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,30 +239,6 @@ pub mod vec_tuple_map {
}
}

fn dedot_components<'c>(components: impl Iterator<Item = Component<'c>>) -> PathBuf {
use std::path::Component::*;

let mut comps = vec![];
for component in components {
match component {
p@Prefix(_) => comps = vec![p],
r@RootDir if comps.iter().all(|c| matches!(c, Prefix(_))) => comps.push(r),
r@RootDir => comps = vec![r],
CurDir => { },
ParentDir if comps.iter().all(|c| matches!(c, Prefix(_) | RootDir)) => { },
ParentDir => { comps.pop(); },
c@Normal(_) => comps.push(c),
}
}

comps.iter().map(|c| c.as_os_str()).collect()
}

/// Remove any dots from the path by popping as needed.
pub(crate) fn dedot(path: &Path) -> PathBuf {
dedot_components(path.components())
}

use crate::value::{Value, Dict};

/// Given a key path `key` of the form `a.b.c`, creates nested dictionaries for
Expand Down

0 comments on commit 0ae4eaa

Please sign in to comment.