|
1 | 1 | use clap::{Parser, ValueHint};
|
2 |
| -use eyre::{Result, WrapErr}; |
3 |
| -use foundry_cli::utils::LoadConfig; |
4 |
| -use foundry_compilers::{Graph, resolver::parse::SolData}; |
5 |
| -use foundry_config::{Config, impl_figment_convert_basic}; |
6 |
| -use itertools::Itertools; |
7 |
| -use solar_parse::{ast, ast::visit::Visit, interface::Session}; |
8 |
| -use std::{ |
9 |
| - ops::ControlFlow, |
10 |
| - path::{Path, PathBuf}, |
11 |
| -}; |
| 2 | +use eyre::Result; |
| 3 | +use foundry_cli::opts::BuildOpts; |
| 4 | +use foundry_config::impl_figment_convert; |
| 5 | +use std::path::PathBuf; |
12 | 6 |
|
13 | 7 | /// CLI arguments for `forge geiger`.
|
| 8 | +/// |
| 9 | +/// This command is an alias for `forge lint --only-lint unsafe-cheatcode` |
| 10 | +/// and detects usage of unsafe cheat codes in a project and its dependencies. |
14 | 11 | #[derive(Clone, Debug, Parser)]
|
15 | 12 | pub struct GeigerArgs {
|
16 | 13 | /// Paths to files or directories to detect.
|
17 | 14 | #[arg(
|
18 |
| - conflicts_with = "root", |
19 | 15 | value_hint = ValueHint::FilePath,
|
20 | 16 | value_name = "PATH",
|
21 |
| - num_args(1..), |
| 17 | + num_args(0..) |
22 | 18 | )]
|
23 | 19 | paths: Vec<PathBuf>,
|
24 | 20 |
|
25 |
| - /// The project's root path. |
26 |
| - /// |
27 |
| - /// By default root of the Git repository, if in one, |
28 |
| - /// or the current working directory. |
29 |
| - #[arg(long, value_hint = ValueHint::DirPath, value_name = "PATH")] |
30 |
| - root: Option<PathBuf>, |
31 |
| - |
32 |
| - /// Globs to ignore. |
33 |
| - #[arg( |
34 |
| - long, |
35 |
| - value_hint = ValueHint::FilePath, |
36 |
| - value_name = "PATH", |
37 |
| - num_args(1..), |
38 |
| - )] |
39 |
| - ignore: Vec<PathBuf>, |
40 |
| - |
41 | 21 | #[arg(long, hide = true)]
|
42 | 22 | check: bool,
|
| 23 | + |
43 | 24 | #[arg(long, hide = true)]
|
44 | 25 | full: bool,
|
| 26 | + |
| 27 | + #[command(flatten)] |
| 28 | + build: BuildOpts, |
45 | 29 | }
|
46 | 30 |
|
47 |
| -impl_figment_convert_basic!(GeigerArgs); |
| 31 | +impl_figment_convert!(GeigerArgs, build); |
48 | 32 |
|
49 | 33 | impl GeigerArgs {
|
50 |
| - pub fn sources(&self, config: &Config) -> Result<Vec<PathBuf>> { |
51 |
| - let cwd = std::env::current_dir()?; |
52 |
| - |
53 |
| - let mut sources: Vec<PathBuf> = { |
54 |
| - if self.paths.is_empty() { |
55 |
| - let paths = config.project_paths(); |
56 |
| - Graph::<SolData>::resolve(&paths)? |
57 |
| - .files() |
58 |
| - .keys() |
59 |
| - .filter(|f| !paths.has_library_ancestor(f)) |
60 |
| - .cloned() |
61 |
| - .collect() |
62 |
| - } else { |
63 |
| - self.paths |
64 |
| - .iter() |
65 |
| - .flat_map(|path| foundry_common::fs::files_with_ext(path, "sol")) |
66 |
| - .unique() |
67 |
| - .collect() |
68 |
| - } |
69 |
| - }; |
70 |
| - |
71 |
| - sources.retain_mut(|path| { |
72 |
| - let abs_path = if path.is_absolute() { path.clone() } else { cwd.join(&path) }; |
73 |
| - *path = abs_path.strip_prefix(&cwd).unwrap_or(&abs_path).to_path_buf(); |
74 |
| - !self.ignore.iter().any(|ignore| { |
75 |
| - if ignore.is_absolute() { |
76 |
| - abs_path.starts_with(ignore) |
77 |
| - } else { |
78 |
| - abs_path.starts_with(cwd.join(ignore)) |
79 |
| - } |
80 |
| - }) |
81 |
| - }); |
82 |
| - |
83 |
| - Ok(sources) |
84 |
| - } |
85 |
| - |
86 |
| - pub fn run(self) -> Result<usize> { |
| 34 | + pub fn run(self) -> Result<()> { |
| 35 | + // Deprecated flags warnings |
87 | 36 | if self.check {
|
88 | 37 | sh_warn!("`--check` is deprecated as it's now the default behavior\n")?;
|
89 | 38 | }
|
90 | 39 | if self.full {
|
91 | 40 | sh_warn!("`--full` is deprecated as reports are not generated anymore\n")?;
|
92 | 41 | }
|
93 | 42 |
|
94 |
| - let config = self.load_config()?; |
95 |
| - let sources = self.sources(&config).wrap_err("Failed to resolve files")?; |
96 |
| - |
97 |
| - if config.ffi { |
98 |
| - sh_warn!("FFI enabled\n")?; |
99 |
| - } |
100 |
| - |
101 |
| - let mut sess = Session::builder().with_stderr_emitter().build(); |
102 |
| - sess.dcx = sess.dcx.set_flags(|flags| flags.track_diagnostics = false); |
103 |
| - let unsafe_cheatcodes = &[ |
104 |
| - "ffi".to_string(), |
105 |
| - "readFile".to_string(), |
106 |
| - "readLine".to_string(), |
107 |
| - "writeFile".to_string(), |
108 |
| - "writeLine".to_string(), |
109 |
| - "removeFile".to_string(), |
110 |
| - "closeFile".to_string(), |
111 |
| - "setEnv".to_string(), |
112 |
| - "deriveKey".to_string(), |
113 |
| - ]; |
114 |
| - Ok(sess |
115 |
| - .enter(|| sources.iter().map(|file| lint_file(&sess, unsafe_cheatcodes, file)).sum())) |
116 |
| - } |
117 |
| -} |
118 |
| - |
119 |
| -fn lint_file(sess: &Session, unsafe_cheatcodes: &[String], path: &Path) -> usize { |
120 |
| - try_lint_file(sess, unsafe_cheatcodes, path).unwrap_or(0) |
121 |
| -} |
122 |
| - |
123 |
| -fn try_lint_file( |
124 |
| - sess: &Session, |
125 |
| - unsafe_cheatcodes: &[String], |
126 |
| - path: &Path, |
127 |
| -) -> solar_parse::interface::Result<usize> { |
128 |
| - let arena = solar_parse::ast::Arena::new(); |
129 |
| - let mut parser = solar_parse::Parser::from_file(sess, &arena, path)?; |
130 |
| - let ast = parser.parse_file().map_err(|e| e.emit())?; |
131 |
| - let mut visitor = Visitor::new(sess, unsafe_cheatcodes); |
132 |
| - let _ = visitor.visit_source_unit(&ast); |
133 |
| - Ok(visitor.count) |
134 |
| -} |
135 |
| - |
136 |
| -struct Visitor<'a> { |
137 |
| - sess: &'a Session, |
138 |
| - count: usize, |
139 |
| - unsafe_cheatcodes: &'a [String], |
140 |
| -} |
141 |
| - |
142 |
| -impl<'a> Visitor<'a> { |
143 |
| - fn new(sess: &'a Session, unsafe_cheatcodes: &'a [String]) -> Self { |
144 |
| - Self { sess, count: 0, unsafe_cheatcodes } |
145 |
| - } |
146 |
| -} |
147 |
| - |
148 |
| -impl<'ast> Visit<'ast> for Visitor<'_> { |
149 |
| - type BreakValue = solar_parse::interface::data_structures::Never; |
| 43 | + sh_warn!( |
| 44 | + "`forge geiger` is just an alias for `forge lint --only-lint unsafe-cheatcode`\n" |
| 45 | + )?; |
| 46 | + |
| 47 | + // Convert geiger command to lint command with specific lint filter |
| 48 | + let lint_args = crate::cmd::lint::LintArgs { |
| 49 | + paths: self.paths, |
| 50 | + severity: None, |
| 51 | + lint: Some(vec!["unsafe-cheatcode".to_string()]), |
| 52 | + json: false, |
| 53 | + build: self.build, |
| 54 | + }; |
150 | 55 |
|
151 |
| - fn visit_expr(&mut self, expr: &'ast ast::Expr<'ast>) -> ControlFlow<Self::BreakValue> { |
152 |
| - if let ast::ExprKind::Call(lhs, _args) = &expr.kind |
153 |
| - && let ast::ExprKind::Member(_lhs, member) = &lhs.kind |
154 |
| - && self.unsafe_cheatcodes.iter().any(|c| c.as_str() == member.as_str()) |
155 |
| - { |
156 |
| - let msg = format!("usage of unsafe cheatcode `vm.{member}`"); |
157 |
| - self.sess.dcx.err(msg).span(member.span).emit(); |
158 |
| - self.count += 1; |
159 |
| - } |
160 |
| - self.walk_expr(expr) |
| 56 | + // Run the lint command with the geiger-specific configuration |
| 57 | + lint_args.run() |
161 | 58 | }
|
162 | 59 | }
|
0 commit comments