Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanitize all source entries #19

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ impl Metadata {
self.sources()
.into_iter()
.map(|(path, entry)| {
let path = root.join(path);
// This is relevant because the etherscan [Metadata](crate::contract::Metadata) can
// contain absolute paths (supported by standard-json-input). See also: <https://github.com/foundry-rs/foundry/issues/6541>
// for example, we want to ensure "/contracts/SimpleToken.sol" is mapped to
// `<root_dir>/contracts/SimpleToken.sol`.
let sanitized_path = crate::source_tree::sanitize_path(path);
let path = root.join(sanitized_path);
SourceTreeEntry { path, contents: entry.content }
})
.collect()
Expand Down
5 changes: 3 additions & 2 deletions src/source_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ impl SourceTree {
}

/// Remove any components in a smart contract source path that could cause a directory traversal.
fn sanitize_path(path: &Path) -> PathBuf {
let sanitized = Path::new(path)
pub(crate) fn sanitize_path(path: impl AsRef<Path>) -> PathBuf {
let sanitized = path
.as_ref()
.components()
.filter(|x| x.as_os_str() != Component::ParentDir.as_os_str())
.collect::<PathBuf>();
Expand Down