Skip to content

Commit 497b6ee

Browse files
grandizzyDaniPopes
andauthored
feat(forge): add script execution protection config (#10408)
* feat(forge): add script execution protection config * Update crates/config/src/lib.rs Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --------- Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com>
1 parent 378ded1 commit 497b6ee

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

crates/config/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ pub struct Config {
523523
#[serde(default)]
524524
pub compilation_restrictions: Vec<CompilationRestrictions>,
525525

526+
/// Whether to enable script execution protection.
527+
pub script_execution_protection: bool,
528+
526529
/// PRIVATE: This structure may grow, As such, constructing this structure should
527530
/// _always_ be done using a public constructor or update syntax:
528531
///
@@ -2412,6 +2415,7 @@ impl Default for Config {
24122415
additional_compiler_profiles: Default::default(),
24132416
compilation_restrictions: Default::default(),
24142417
eof: false,
2418+
script_execution_protection: true,
24152419
_non_exhaustive: (),
24162420
}
24172421
}

crates/evm/evm/src/executors/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl Executor {
258258
}
259259

260260
#[inline]
261-
pub fn set_script(&mut self, script_address: Address) {
261+
pub fn set_script_execution(&mut self, script_address: Address) {
262262
self.inspector_mut().script(script_address);
263263
}
264264

crates/forge/tests/cli/config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ forgetest!(can_extract_config_values, |prj, cmd| {
169169
additional_compiler_profiles: Default::default(),
170170
compilation_restrictions: Default::default(),
171171
eof: false,
172+
script_execution_protection: true,
172173
_non_exhaustive: (),
173174
};
174175
prj.write_config(input.clone());
@@ -1041,6 +1042,7 @@ transaction_timeout = 120
10411042
eof = false
10421043
additional_compiler_profiles = []
10431044
compilation_restrictions = []
1045+
script_execution_protection = true
10441046
10451047
[profile.default.rpc_storage_caching]
10461048
chains = "all"
@@ -1298,7 +1300,8 @@ exclude = []
12981300
"transaction_timeout": 120,
12991301
"eof": false,
13001302
"additional_compiler_profiles": [],
1301-
"compilation_restrictions": []
1303+
"compilation_restrictions": [],
1304+
"script_execution_protection": true
13021305
}
13031306
13041307
"#]]);

crates/forge/tests/cli/script.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,6 +2668,17 @@ Error: Usage of `address(this)` detected in script contract. Script contracts ar
26682668
Error: script failed: <empty revert data>
26692669
...
26702670
2671+
"#]]);
2672+
2673+
// Disable script protection.
2674+
prj.update_config(|config| {
2675+
config.script_execution_protection = false;
2676+
});
2677+
cmd.assert_success().stdout_eq(str![[r#"
2678+
...
2679+
Script ran successfully.
2680+
...
2681+
26712682
"#]]);
26722683
});
26732684

crates/script/src/execute.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ impl PreExecutionState {
144144
&self.build_data.predeploy_libraries,
145145
self.execution_data.bytecode.clone(),
146146
needs_setup(&self.execution_data.abi),
147-
self.script_config.sender_nonce,
147+
&self.script_config,
148148
self.args.broadcast,
149-
self.script_config.evm_opts.fork_url.is_none(),
150149
)?;
151150

152151
if setup_result.success {

crates/script/src/runner.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::ScriptResult;
1+
use super::{ScriptConfig, ScriptResult};
22
use crate::build::ScriptPredeployLibraries;
33
use alloy_eips::eip7702::SignedAuthorization;
44
use alloy_primitives::{Address, Bytes, TxKind, U256};
@@ -33,9 +33,8 @@ impl ScriptRunner {
3333
libraries: &ScriptPredeployLibraries,
3434
code: Bytes,
3535
setup: bool,
36-
sender_nonce: u64,
36+
script_config: &ScriptConfig,
3737
is_broadcast: bool,
38-
need_create2_deployer: bool,
3938
) -> Result<(Address, ScriptResult)> {
4039
trace!(target: "script", "executing setUP()");
4140

@@ -45,11 +44,12 @@ impl ScriptRunner {
4544
self.executor.set_balance(self.evm_opts.sender, U256::MAX)?;
4645
}
4746

48-
if need_create2_deployer {
47+
if script_config.evm_opts.fork_url.is_none() {
4948
self.executor.deploy_create2_deployer()?;
5049
}
5150
}
5251

52+
let sender_nonce = script_config.sender_nonce;
5353
self.executor.set_nonce(self.evm_opts.sender, sender_nonce)?;
5454

5555
// We max out their balance so that they can deploy and make calls.
@@ -158,7 +158,9 @@ impl ScriptRunner {
158158
}
159159

160160
// set script address to be used by execution inspector
161-
self.executor.set_script(address);
161+
if script_config.config.script_execution_protection {
162+
self.executor.set_script_execution(address);
163+
}
162164

163165
traces.extend(constructor_traces.map(|traces| (TraceKind::Deployment, traces)));
164166

0 commit comments

Comments
 (0)