1use super::Result;
2use crate::Vm::Rpc;
3use alloy_primitives::{U256, map::AddressHashMap};
4use foundry_common::{ContractsByArtifact, fs::normalize_path};
5use foundry_compilers::{ArtifactId, ProjectPathsConfig, utils::canonicalize};
6use foundry_config::{
7 Config, FsPermissions, ResolvedRpcEndpoint, ResolvedRpcEndpoints, RpcEndpoint, RpcEndpointUrl,
8 cache::StorageCachingConfig, fs_permissions::FsAccessKind,
9};
10use foundry_evm_core::opts::EvmOpts;
11use std::{
12 path::{Path, PathBuf},
13 time::Duration,
14};
15
16#[derive(Clone, Debug)]
20pub struct CheatsConfig {
21 pub ffi: bool,
23 pub always_use_create_2_factory: bool,
25 pub prompt_timeout: Duration,
27 pub rpc_storage_caching: StorageCachingConfig,
29 pub no_storage_caching: bool,
31 pub rpc_endpoints: ResolvedRpcEndpoints,
33 pub paths: ProjectPathsConfig,
35 pub bind_json_path: PathBuf,
37 pub fs_permissions: FsPermissions,
39 pub root: PathBuf,
41 pub broadcast: PathBuf,
43 pub evm_opts: EvmOpts,
45 pub labels: AddressHashMap<String>,
47 pub available_artifacts: Option<ContractsByArtifact>,
51 pub running_artifact: Option<ArtifactId>,
53 pub assertions_revert: bool,
55 pub seed: Option<U256>,
57 pub internal_expect_revert: bool,
59}
60
61impl CheatsConfig {
62 pub fn new(
64 config: &Config,
65 evm_opts: EvmOpts,
66 available_artifacts: Option<ContractsByArtifact>,
67 running_artifact: Option<ArtifactId>,
68 ) -> Self {
69 let rpc_endpoints = config.rpc_endpoints.clone().resolved();
70 trace!(?rpc_endpoints, "using resolved rpc endpoints");
71
72 let available_artifacts =
74 if config.unchecked_cheatcode_artifacts { None } else { available_artifacts };
75
76 Self {
77 ffi: evm_opts.ffi,
78 always_use_create_2_factory: evm_opts.always_use_create_2_factory,
79 prompt_timeout: Duration::from_secs(config.prompt_timeout),
80 rpc_storage_caching: config.rpc_storage_caching.clone(),
81 no_storage_caching: config.no_storage_caching,
82 rpc_endpoints,
83 paths: config.project_paths(),
84 bind_json_path: config.bind_json.out.clone(),
85 fs_permissions: config.fs_permissions.clone().joined(config.root.as_ref()),
86 root: config.root.clone(),
87 broadcast: config.root.clone().join(&config.broadcast),
88 evm_opts,
89 labels: config.labels.clone(),
90 available_artifacts,
91 running_artifact,
92 assertions_revert: config.assertions_revert,
93 seed: config.fuzz.seed,
94 internal_expect_revert: config.allow_internal_expect_revert,
95 }
96 }
97
98 pub fn clone_with(&self, config: &Config, evm_opts: EvmOpts) -> Self {
100 Self::new(config, evm_opts, self.available_artifacts.clone(), self.running_artifact.clone())
101 }
102
103 pub fn normalized_path(&self, path: impl AsRef<Path>) -> PathBuf {
107 let path = self.root.join(path);
108 canonicalize(&path).unwrap_or_else(|_| normalize_path(&path))
109 }
110
111 pub fn is_path_allowed(&self, path: impl AsRef<Path>, kind: FsAccessKind) -> bool {
118 self.is_normalized_path_allowed(&self.normalized_path(path), kind)
119 }
120
121 fn is_normalized_path_allowed(&self, path: &Path, kind: FsAccessKind) -> bool {
122 self.fs_permissions.is_path_allowed(path, kind)
123 }
124
125 pub fn ensure_path_allowed(
129 &self,
130 path: impl AsRef<Path>,
131 kind: FsAccessKind,
132 ) -> Result<PathBuf> {
133 let path = path.as_ref();
134 let normalized = self.normalized_path(path);
135 ensure!(
136 self.is_normalized_path_allowed(&normalized, kind),
137 "the path {} is not allowed to be accessed for {kind} operations",
138 normalized.strip_prefix(&self.root).unwrap_or(path).display()
139 );
140 Ok(normalized)
141 }
142
143 pub fn is_foundry_toml(&self, path: impl AsRef<Path>) -> bool {
147 let foundry_toml = self.root.join(Config::FILE_NAME);
152 Path::new(&foundry_toml.to_string_lossy().to_lowercase())
153 .starts_with(Path::new(&path.as_ref().to_string_lossy().to_lowercase()))
154 }
155
156 pub fn ensure_not_foundry_toml(&self, path: impl AsRef<Path>) -> Result<()> {
159 ensure!(!self.is_foundry_toml(path), "access to `foundry.toml` is not allowed");
160 Ok(())
161 }
162
163 pub fn rpc_endpoint(&self, url_or_alias: &str) -> Result<ResolvedRpcEndpoint> {
177 if let Some(endpoint) = self.rpc_endpoints.get(url_or_alias) {
178 Ok(endpoint.clone().try_resolve())
179 } else {
180 if url_or_alias.starts_with("http") ||
182 url_or_alias.starts_with("ws") ||
183 Path::new(url_or_alias).exists()
185 {
186 let url = RpcEndpointUrl::Env(url_or_alias.to_string());
187 Ok(RpcEndpoint::new(url).resolve())
188 } else {
189 Err(fmt_err!("invalid rpc url: {url_or_alias}"))
190 }
191 }
192 }
193 pub fn rpc_urls(&self) -> Result<Vec<Rpc>> {
195 let mut urls = Vec::with_capacity(self.rpc_endpoints.len());
196 for alias in self.rpc_endpoints.keys() {
197 let url = self.rpc_endpoint(alias)?.url()?;
198 urls.push(Rpc { key: alias.clone(), url });
199 }
200 Ok(urls)
201 }
202}
203
204impl Default for CheatsConfig {
205 fn default() -> Self {
206 Self {
207 ffi: false,
208 always_use_create_2_factory: false,
209 prompt_timeout: Duration::from_secs(120),
210 rpc_storage_caching: Default::default(),
211 no_storage_caching: false,
212 rpc_endpoints: Default::default(),
213 paths: ProjectPathsConfig::builder().build_with_root("./"),
214 fs_permissions: Default::default(),
215 root: Default::default(),
216 bind_json_path: PathBuf::default().join("utils").join("jsonBindings.sol"),
217 broadcast: Default::default(),
218 evm_opts: Default::default(),
219 labels: Default::default(),
220 available_artifacts: Default::default(),
221 running_artifact: Default::default(),
222 assertions_revert: true,
223 seed: None,
224 internal_expect_revert: false,
225 }
226 }
227}
228
229#[cfg(test)]
230mod tests {
231 use super::*;
232 use foundry_config::fs_permissions::PathPermission;
233
234 fn config(root: &str, fs_permissions: FsPermissions) -> CheatsConfig {
235 CheatsConfig::new(
236 &Config { root: root.into(), fs_permissions, ..Default::default() },
237 Default::default(),
238 None,
239 None,
240 )
241 }
242
243 #[test]
244 fn test_allowed_paths() {
245 let root = "/my/project/root/";
246 let config = config(root, FsPermissions::new(vec![PathPermission::read_write("./")]));
247
248 assert!(config.ensure_path_allowed("./t.txt", FsAccessKind::Read).is_ok());
249 assert!(config.ensure_path_allowed("./t.txt", FsAccessKind::Write).is_ok());
250 assert!(config.ensure_path_allowed("../root/t.txt", FsAccessKind::Read).is_ok());
251 assert!(config.ensure_path_allowed("../root/t.txt", FsAccessKind::Write).is_ok());
252 assert!(config.ensure_path_allowed("../../root/t.txt", FsAccessKind::Read).is_err());
253 assert!(config.ensure_path_allowed("../../root/t.txt", FsAccessKind::Write).is_err());
254 }
255
256 #[test]
257 fn test_is_foundry_toml() {
258 let root = "/my/project/root/";
259 let config = config(root, FsPermissions::new(vec![PathPermission::read_write("./")]));
260
261 let f = format!("{root}foundry.toml");
262 assert!(config.is_foundry_toml(f));
263
264 let f = format!("{root}Foundry.toml");
265 assert!(config.is_foundry_toml(f));
266
267 let f = format!("{root}lib/other/foundry.toml");
268 assert!(!config.is_foundry_toml(f));
269 }
270}