Skip to content

feat(forge): add script execution protection config #10408

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

Merged
merged 4 commits into from
Apr 30, 2025
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
4 changes: 4 additions & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ pub struct Config {
#[serde(default)]
pub compilation_restrictions: Vec<CompilationRestrictions>,

/// Whether to enable script execution protection.
pub script_execution_protection: bool,

/// PRIVATE: This structure may grow, As such, constructing this structure should
/// _always_ be done using a public constructor or update syntax:
///
Expand Down Expand Up @@ -2412,6 +2415,7 @@ impl Default for Config {
additional_compiler_profiles: Default::default(),
compilation_restrictions: Default::default(),
eof: false,
script_execution_protection: true,
_non_exhaustive: (),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/src/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl Executor {
}

#[inline]
pub fn set_script(&mut self, script_address: Address) {
pub fn set_script_execution(&mut self, script_address: Address) {
self.inspector_mut().script(script_address);
}

Expand Down
5 changes: 4 additions & 1 deletion crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ forgetest!(can_extract_config_values, |prj, cmd| {
additional_compiler_profiles: Default::default(),
compilation_restrictions: Default::default(),
eof: false,
script_execution_protection: true,
_non_exhaustive: (),
};
prj.write_config(input.clone());
Expand Down Expand Up @@ -1041,6 +1042,7 @@ transaction_timeout = 120
eof = false
additional_compiler_profiles = []
compilation_restrictions = []
script_execution_protection = true

[profile.default.rpc_storage_caching]
chains = "all"
Expand Down Expand Up @@ -1298,7 +1300,8 @@ exclude = []
"transaction_timeout": 120,
"eof": false,
"additional_compiler_profiles": [],
"compilation_restrictions": []
"compilation_restrictions": [],
"script_execution_protection": true
}

"#]]);
Expand Down
11 changes: 11 additions & 0 deletions crates/forge/tests/cli/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,17 @@ Error: Usage of `address(this)` detected in script contract. Script contracts ar
Error: script failed: <empty revert data>
...

"#]]);

// Disable script protection.
prj.update_config(|config| {
config.script_execution_protection = false;
});
cmd.assert_success().stdout_eq(str![[r#"
...
Script ran successfully.
...

"#]]);
});

Expand Down
3 changes: 1 addition & 2 deletions crates/script/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ impl PreExecutionState {
&self.build_data.predeploy_libraries,
self.execution_data.bytecode.clone(),
needs_setup(&self.execution_data.abi),
self.script_config.sender_nonce,
&self.script_config,
self.args.broadcast,
self.script_config.evm_opts.fork_url.is_none(),
)?;

if setup_result.success {
Expand Down
12 changes: 7 additions & 5 deletions crates/script/src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::ScriptResult;
use super::{ScriptConfig, ScriptResult};
use crate::build::ScriptPredeployLibraries;
use alloy_eips::eip7702::SignedAuthorization;
use alloy_primitives::{Address, Bytes, TxKind, U256};
Expand Down Expand Up @@ -33,9 +33,8 @@ impl ScriptRunner {
libraries: &ScriptPredeployLibraries,
code: Bytes,
setup: bool,
sender_nonce: u64,
script_config: &ScriptConfig,
is_broadcast: bool,
need_create2_deployer: bool,
) -> Result<(Address, ScriptResult)> {
trace!(target: "script", "executing setUP()");

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

if need_create2_deployer {
if script_config.evm_opts.fork_url.is_none() {
self.executor.deploy_create2_deployer()?;
}
}

let sender_nonce = script_config.sender_nonce;
self.executor.set_nonce(self.evm_opts.sender, sender_nonce)?;

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

// set script address to be used by execution inspector
self.executor.set_script(address);
if script_config.config.script_execution_protection {
self.executor.set_script_execution(address);
}

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

Expand Down