foundry_cheatcodes/
config.rs

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/// Additional, configurable context the `Cheatcodes` inspector has access to
17///
18/// This is essentially a subset of various `Config` settings `Cheatcodes` needs to know.
19#[derive(Clone, Debug)]
20pub struct CheatsConfig {
21    /// Whether the FFI cheatcode is enabled.
22    pub ffi: bool,
23    /// Use the create 2 factory in all cases including tests and non-broadcasting scripts.
24    pub always_use_create_2_factory: bool,
25    /// Sets a timeout for vm.prompt cheatcodes
26    pub prompt_timeout: Duration,
27    /// RPC storage caching settings determines what chains and endpoints to cache
28    pub rpc_storage_caching: StorageCachingConfig,
29    /// Disables storage caching entirely.
30    pub no_storage_caching: bool,
31    /// All known endpoints and their aliases
32    pub rpc_endpoints: ResolvedRpcEndpoints,
33    /// Project's paths as configured
34    pub paths: ProjectPathsConfig,
35    /// Path to the directory that contains the bindings generated by `forge bind-json`.
36    pub bind_json_path: PathBuf,
37    /// Filesystem permissions for cheatcodes like `writeFile`, `readFile`
38    pub fs_permissions: FsPermissions,
39    /// Project root
40    pub root: PathBuf,
41    /// Absolute Path to broadcast dir i.e project_root/broadcast
42    pub broadcast: PathBuf,
43    /// How the evm was configured by the user
44    pub evm_opts: EvmOpts,
45    /// Address labels from config
46    pub labels: AddressHashMap<String>,
47    /// Artifacts which are guaranteed to be fresh (either recompiled or cached).
48    /// If Some, `vm.getDeployedCode` invocations are validated to be in scope of this list.
49    /// If None, no validation is performed.
50    pub available_artifacts: Option<ContractsByArtifact>,
51    /// Currently running artifact.
52    pub running_artifact: Option<ArtifactId>,
53    /// Whether to enable legacy (non-reverting) assertions.
54    pub assertions_revert: bool,
55    /// Optional seed for the RNG algorithm.
56    pub seed: Option<U256>,
57    /// Whether to allow `expectRevert` to work for internal calls.
58    pub internal_expect_revert: bool,
59}
60
61impl CheatsConfig {
62    /// Extracts the necessary settings from the Config
63    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        // If user explicitly disabled safety checks, do not set available_artifacts
73        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    /// Returns a new `CheatsConfig` configured with the given `Config` and `EvmOpts`.
99    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    /// Attempts to canonicalize (see [std::fs::canonicalize]) the path.
104    ///
105    /// Canonicalization fails for non-existing paths, in which case we just normalize the path.
106    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    /// Returns true if the given path is allowed, if any path `allowed_paths` is an ancestor of the
112    /// path
113    ///
114    /// We only allow paths that are inside  allowed paths. To prevent path traversal
115    /// ("../../etc/passwd") we canonicalize/normalize the path first. We always join with the
116    /// configured root directory.
117    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    /// Returns an error if no access is granted to access `path`, See also [Self::is_path_allowed]
126    ///
127    /// Returns the normalized version of `path`, see [`CheatsConfig::normalized_path`]
128    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    /// Returns true if the given `path` is the project's foundry.toml file
144    ///
145    /// Note: this should be called with normalized path
146    pub fn is_foundry_toml(&self, path: impl AsRef<Path>) -> bool {
147        // path methods that do not access the filesystem are such as [`Path::starts_with`], are
148        // case-sensitive no matter the platform or filesystem. to make this case-sensitive
149        // we convert the underlying `OssStr` to lowercase checking that `path` and
150        // `foundry.toml` are the same file by comparing the FD, because it may not exist
151        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    /// Same as [`Self::is_foundry_toml`] but returns an `Err` if [`Self::is_foundry_toml`] returns
157    /// true
158    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    /// Returns the RPC to use
164    ///
165    /// If `url_or_alias` is a known alias in the `ResolvedRpcEndpoints` then it returns the
166    /// corresponding URL of that alias. otherwise this assumes `url_or_alias` is itself a URL
167    /// if it starts with a `http` or `ws` scheme.
168    ///
169    /// If the url is a path to an existing file, it is also considered a valid RPC URL, IPC path.
170    ///
171    /// # Errors
172    ///
173    ///  - Returns an error if `url_or_alias` is a known alias but references an unresolved env var.
174    ///  - Returns an error if `url_or_alias` is not an alias but does not start with a `http` or
175    ///    `ws` `scheme` and is not a path to an existing file
176    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            // check if it's a URL or a path to an existing file to an ipc socket
181            if url_or_alias.starts_with("http") ||
182                url_or_alias.starts_with("ws") ||
183                // check for existing ipc file
184                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    /// Returns all the RPC urls and their alias.
194    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}