2022-03-06 15:21:19 +00:00
|
|
|
use crate::Result;
|
|
|
|
use std::{
|
|
|
|
fs::create_dir_all,
|
|
|
|
path::{Component, Path, PathBuf},
|
|
|
|
};
|
|
|
|
|
2022-08-24 19:50:29 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-03-06 15:21:19 +00:00
|
|
|
pub struct SourceTreeEntry {
|
|
|
|
pub path: PathBuf,
|
|
|
|
pub contents: String,
|
|
|
|
}
|
|
|
|
|
2022-08-24 19:50:29 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-03-06 15:21:19 +00:00
|
|
|
pub struct SourceTree {
|
|
|
|
pub entries: Vec<SourceTreeEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SourceTree {
|
|
|
|
/// Expand the source tree into the provided directory. This method sanitizes paths to ensure
|
|
|
|
/// that no directory traversal happens.
|
|
|
|
pub fn write_to(&self, dir: &Path) -> Result<()> {
|
2022-09-04 17:57:52 +00:00
|
|
|
create_dir_all(dir)?;
|
2022-03-06 15:21:19 +00:00
|
|
|
for entry in &self.entries {
|
2022-03-24 17:04:46 +00:00
|
|
|
let mut sanitized_path = sanitize_path(&entry.path);
|
|
|
|
if sanitized_path.extension().is_none() {
|
|
|
|
sanitized_path.set_extension("sol");
|
|
|
|
}
|
2022-03-06 15:21:19 +00:00
|
|
|
let joined = dir.join(sanitized_path);
|
|
|
|
if let Some(parent) = joined.parent() {
|
|
|
|
create_dir_all(parent)?;
|
|
|
|
std::fs::write(joined, &entry.contents)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove any components in a smart contract source path that could cause a directory traversal.
|
|
|
|
fn sanitize_path(path: &Path) -> PathBuf {
|
2022-08-17 21:15:00 +00:00
|
|
|
let sanitized = Path::new(path)
|
2022-03-06 15:21:19 +00:00
|
|
|
.components()
|
|
|
|
.filter(|x| x.as_os_str() != Component::ParentDir.as_os_str())
|
2022-08-17 21:15:00 +00:00
|
|
|
.collect::<PathBuf>();
|
|
|
|
|
|
|
|
// Force absolute paths to be relative
|
|
|
|
sanitized.strip_prefix("/").map(PathBuf::from).unwrap_or(sanitized)
|
2022-03-06 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::fs::read_dir;
|
|
|
|
|
2022-03-24 17:04:46 +00:00
|
|
|
/// Ensure that the source tree is written correctly and .sol extension is added to a path with
|
|
|
|
/// no extension.
|
2022-03-06 15:21:19 +00:00
|
|
|
#[test]
|
|
|
|
fn test_source_tree_write() {
|
|
|
|
let tempdir = tempfile::tempdir().unwrap();
|
|
|
|
let st = SourceTree {
|
|
|
|
entries: vec![
|
|
|
|
SourceTreeEntry { path: PathBuf::from("a/a.sol"), contents: String::from("Test") },
|
2022-03-24 17:04:46 +00:00
|
|
|
SourceTreeEntry { path: PathBuf::from("b/b"), contents: String::from("Test 2") },
|
2022-03-06 15:21:19 +00:00
|
|
|
],
|
|
|
|
};
|
2022-03-17 22:14:53 +00:00
|
|
|
st.write_to(tempdir.path()).unwrap();
|
2022-03-24 17:04:46 +00:00
|
|
|
let a_sol_path = PathBuf::new().join(&tempdir).join("a").join("a.sol");
|
|
|
|
let b_sol_path = PathBuf::new().join(&tempdir).join("b").join("b.sol");
|
|
|
|
assert!(a_sol_path.exists());
|
|
|
|
assert!(b_sol_path.exists());
|
2022-03-06 15:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensure that the .. are ignored when writing the source tree to disk because of
|
|
|
|
/// sanitization.
|
|
|
|
#[test]
|
|
|
|
fn test_malformed_source_tree_write() {
|
|
|
|
let tempdir = tempfile::tempdir().unwrap();
|
|
|
|
let st = SourceTree {
|
|
|
|
entries: vec![
|
|
|
|
SourceTreeEntry {
|
|
|
|
path: PathBuf::from("../a/a.sol"),
|
|
|
|
contents: String::from("Test"),
|
|
|
|
},
|
|
|
|
SourceTreeEntry {
|
|
|
|
path: PathBuf::from("../b/../b.sol"),
|
|
|
|
contents: String::from("Test 2"),
|
|
|
|
},
|
2022-08-17 21:15:00 +00:00
|
|
|
SourceTreeEntry {
|
|
|
|
path: PathBuf::from("/c/c.sol"),
|
|
|
|
contents: String::from("Test 3"),
|
|
|
|
},
|
2022-03-06 15:21:19 +00:00
|
|
|
],
|
|
|
|
};
|
2022-03-17 22:14:53 +00:00
|
|
|
st.write_to(tempdir.path()).unwrap();
|
2022-03-06 15:21:19 +00:00
|
|
|
let written_paths = read_dir(tempdir.path()).unwrap();
|
|
|
|
let paths: Vec<PathBuf> =
|
|
|
|
written_paths.into_iter().filter_map(|x| x.ok()).map(|x| x.path()).collect();
|
2022-08-17 21:15:00 +00:00
|
|
|
assert_eq!(paths.len(), 3);
|
2022-03-06 15:21:19 +00:00
|
|
|
assert!(paths.contains(&tempdir.path().join("a")));
|
|
|
|
assert!(paths.contains(&tempdir.path().join("b")));
|
2022-08-17 21:15:00 +00:00
|
|
|
assert!(paths.contains(&tempdir.path().join("c")));
|
2022-03-06 15:21:19 +00:00
|
|
|
}
|
|
|
|
}
|