Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat(solc): allow providing --allow-args
Browse files Browse the repository at this point in the history
  • Loading branch information
gakonst committed Nov 3, 2021
1 parent 24af9ed commit b32f681
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
43 changes: 43 additions & 0 deletions ethers-solc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,46 @@ impl fmt::Debug for ArtifactOutput {
}
}
}

use std::convert::TryFrom;

#[derive(Clone, Debug)]
/// Helper struct for serializing `--allow-paths` arguments to Solc
///
/// From the [Solc docs](https://docs.soliditylang.org/en/v0.8.9/using-the-compiler.html#base-path-and-import-remapping):
/// For security reasons the compiler has restrictions on what directories it can access.
/// Directories of source files specified on the command line and target paths of
/// remappings are automatically allowed to be accessed by the file reader,
/// but everything else is rejected by default. Additional paths (and their subdirectories)
/// can be allowed via the --allow-paths /sample/path,/another/sample/path switch.
/// Everything inside the path specified via --base-path is always allowed.
pub struct AllowedLibPaths(Vec<PathBuf>);

impl fmt::Display for AllowedLibPaths {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let lib_paths = self
.0
.iter()
.filter(|path| PathBuf::from(path).exists())
.map(|path| path.into_os_string().into_string().unwrap())
.collect::<Vec<_>>()
.join(",");
write!(f, "{}", lib_paths)
}
}

impl<T: Into<PathBuf>> TryFrom<Vec<T>> for AllowedLibPaths {
type Error = std::io::Error;

fn try_from(libs: Vec<T>) -> std::result::Result<Self, Self::Error> {
let libs = libs
.into_iter()
.map(|lib| {
let path: PathBuf = lib.into();
let lib = std::fs::canonicalize(path)?;
Ok(lib)
})
.collect::<std::result::Result<Vec<_>, std::io::Error>>()?;
Ok(AllowedLibPaths(libs))
}
}
10 changes: 8 additions & 2 deletions ethers-solc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod compile;
pub use compile::*;

mod config;
pub use config::{ArtifactOutput, ProjectPathsConfig, SolcConfig};
pub use config::{AllowedLibPaths, ArtifactOutput, ProjectPathsConfig, SolcConfig};

use crate::{artifacts::Source, cache::SolFilesCache};

Expand Down Expand Up @@ -40,6 +40,8 @@ pub struct Project {
pub artifacts: ArtifactOutput,
/// Errors/Warnings which match these error codes are not going to be logged
pub ignored_error_codes: Vec<u64>,
/// The paths which will be allowed for library inclusion
pub allowed_lib_paths: AllowedLibPaths,
}

impl Project {
Expand Down Expand Up @@ -116,8 +118,12 @@ impl Project {
let version = Solc::detect_version(&source)?;
// gets the solc binary for that version, it is expected tha this will succeed
// AND find the solc since it was installed right above
let solc = Solc::find_svm_installed_version(version.to_string())?
let mut solc = Solc::find_svm_installed_version(version.to_string())?
.expect("solc should have been installed");
// configure solc
solc.args.push("--allow-paths".to_string());
solc.args.push(self.allowed_lib_paths.to_string());

let entry = sources_by_version.entry(solc).or_insert_with(BTreeMap::new);
entry.insert(path, source);
}
Expand Down

0 comments on commit b32f681

Please sign in to comment.