Skip to content

Commit b8cfad5

Browse files
authored
Fix: Secure temporary file creation in chisel edit_session (#11744)
* Update dispatcher.rs * Update Cargo.toml * Update dispatcher.rs * Update Cargo.toml
1 parent bb29f1c commit b8cfad5

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

crates/chisel/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ foundry-compilers.workspace = true
2626
foundry-config.workspace = true
2727
foundry-evm.workspace = true
2828

29+
tempfile.workspace = true
30+
2931
solar.workspace = true
3032

3133
alloy-dyn-abi = { workspace = true, features = ["arbitrary"] }

crates/chisel/src/dispatcher.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ use solar::{
2727
};
2828
use std::{
2929
borrow::Cow,
30+
io::Write,
3031
ops::ControlFlow,
3132
path::{Path, PathBuf},
3233
process::Command,
3334
};
35+
use tempfile::Builder;
3436
use tracing::debug;
3537
use yansi::Paint;
3638

@@ -488,20 +490,25 @@ impl ChiselDispatcher {
488490

489491
pub(crate) async fn edit_session(&mut self) -> Result<()> {
490492
// create a temp file with the content of the run code
491-
let tmp = std::env::temp_dir().join("chisel-tmp.sol");
492-
std::fs::write(&tmp, self.source().run_code.as_bytes())
493+
let mut tmp = Builder::new()
494+
.prefix("chisel-")
495+
.suffix(".sol")
496+
.tempfile()
497+
.wrap_err("Could not create temporary file")?;
498+
tmp.as_file_mut()
499+
.write_all(self.source().run_code.as_bytes())
493500
.wrap_err("Could not write to temporary file")?;
494501

495502
// open the temp file with the editor
496503
let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vim".to_string());
497504
let mut cmd = Command::new(editor);
498-
cmd.arg(&tmp);
505+
cmd.arg(tmp.path());
499506
let st = cmd.status()?;
500507
if !st.success() {
501508
eyre::bail!("Editor exited with {st}");
502509
}
503510

504-
let edited_code = std::fs::read_to_string(tmp)?;
511+
let edited_code = std::fs::read_to_string(tmp.path())?;
505512
let mut new_source = self.source().clone();
506513
new_source.clear_run();
507514
new_source.add_run_code(&edited_code);

0 commit comments

Comments
 (0)