Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Fix: `wt beta run-hook` now requires approval

Maximilian Roos55456a7fd941
diff --git a/src/commands/merge.rs b/src/commands/merge.rs
index 7778337b6..874fa8792 100644
--- a/src/commands/merge.rs
+++ b/src/commands/merge.rs
@@ -156,7 +156,7 @@ pub fn handle_merge(
// Do this after commit/squash/rebase to validate the final state that will be pushed
if !no_verify && let Some(project_config) = repo.load_project_config()? {
let ctx = env.context(force);
- run_pre_merge_commands(&project_config, &ctx, &target_branch)?;
+ run_pre_merge_commands(&project_config, &ctx, &target_branch, true)?;
}
// Fast-forward push to target branch with commit/squash/rebase info for consolidated message
@@ -229,7 +229,7 @@ pub fn handle_merge(
&primary_repo_root,
force,
);
- execute_post_merge_commands(&ctx, &target_branch)?;
+ execute_post_merge_commands(&ctx, &target_branch, true)?;
}
Ok(())
@@ -263,10 +263,15 @@ fn handle_merge_summary_output(primary_path: Option<&std::path::Path>) -> Result
Ok(())
}
/// Run pre-merge commands sequentially (blocking, fail-fast)
+///
+/// # Security Note
+/// `auto_trust`: When true, skip approval prompts (commands already approved in `wt merge`'s batch).
+/// When false, require approval (standalone `wt beta run-hook` execution).
pub fn run_pre_merge_commands(
project_config: &ProjectConfig,
ctx: &CommandContext,
target_branch: &str,
+ auto_trust: bool,
) -> Result<(), GitError> {
let Some(pre_merge_config) = &project_config.pre_merge_command else {
return Ok(());
@@ -277,7 +282,7 @@ pub fn run_pre_merge_commands(
pipeline.run_sequential(
pre_merge_config,
CommandPhase::PreMerge,
- true, // auto_trust: commands already approved in batch
+ auto_trust,
&[("target", target_branch)],
"pre-merge",
HookType::PreMerge,
@@ -286,9 +291,14 @@ pub fn run_pre_merge_commands(
}
/// Execute post-merge commands sequentially in the main worktree (blocking)
+///
+/// # Security Note
+/// `auto_trust`: When true, skip approval prompts (commands already approved in `wt merge`'s batch).
+/// When false, require approval (standalone `wt beta run-hook` execution).
pub fn execute_post_merge_commands(
ctx: &CommandContext,
target_branch: &str,
+ auto_trust: bool,
) -> Result<(), GitError> {
// Load project config from the main worktree path directly
let project_config = match ctx.repo.load_project_config()? {
@@ -305,7 +315,7 @@ pub fn execute_post_merge_commands(
pipeline.run_sequential(
post_merge_config,
CommandPhase::PostMerge,
- true, // auto_trust: commands already approved in batch
+ auto_trust,
&[("target", target_branch)],
"post-merge",
HookType::PostMerge,
diff --git a/src/commands/standalone.rs b/src/commands/standalone.rs
index 7b391f6dc..245566d9b 100644
--- a/src/commands/standalone.rs
+++ b/src/commands/standalone.rs
@@ -43,12 +43,12 @@ pub fn handle_standalone_run_hook(hook_type: HookType, force: bool) -> Result<()
HookType::PreMerge => {
check_hook_configured(&project_config.pre_merge_command, hook_type)?;
let target_branch = repo.default_branch().unwrap_or_else(|_| "main".to_string());
- run_pre_merge_commands(&project_config, &ctx, &target_branch)
+ run_pre_merge_commands(&project_config, &ctx, &target_branch, false)
}
HookType::PostMerge => {
check_hook_configured(&project_config.post_merge_command, hook_type)?;
let target_branch = repo.default_branch().unwrap_or_else(|_| "main".to_string());
- execute_post_merge_commands(&ctx, &target_branch)
+ execute_post_merge_commands(&ctx, &target_branch, false)
}
}
}
diff --git a/tests/integration_tests/approval_ui.rs b/tests/integration_tests/approval_ui.rs
index 5c2a216c2..8d7e5950c 100644
--- a/tests/integration_tests/approval_ui.rs
+++ b/tests/integration_tests/approval_ui.rs
@@ -299,3 +299,87 @@ fn test_permission_error_user_output() {
insta::assert_snapshot!("permission_error_user_output", combined);
});
}
+
+/// Helper for run-hook snapshot tests with approval prompt
+fn snapshot_run_hook(test_name: &str, repo: &TestRepo, hook_type: &str, approve: bool) {
+ let settings = setup_snapshot_settings(repo);
+ settings.bind(|| {
+ let mut cmd = make_snapshot_cmd(repo, "beta", &["run-hook", hook_type], None);
+ cmd.stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped());
+
+ let mut child = cmd.spawn().expect("Failed to spawn command");
+
+ // Write approval response
+ {
+ let stdin = child.stdin.as_mut().expect("Failed to get stdin");
+ let response = if approve { b"y\n" } else { b"n\n" };
+ stdin.write_all(response).expect("Failed to write to stdin");
+ }
+
+ let output = child
+ .wait_with_output()
+ .expect("Failed to wait for command");
+
+ // Use insta snapshot for combined output
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ let combined = format!(
+ "exit_code: {}\n----- stdout -----\n{}\n----- stderr -----\n{}",
+ output.status.code().unwrap_or(-1),
+ stdout,
+ stderr
+ );
+
+ insta::assert_snapshot!(test_name, combined);
+ });
+}
+
+/// Test that `wt beta run-hook pre-merge` requires approval (security boundary test)
+///
+/// This verifies the fix for the security issue where run-hook was bypassing approval.
+/// Before the fix, pre-merge hooks ran with auto_trust=true, skipping approval prompts.
+#[test]
+fn test_run_hook_pre_merge_requires_approval() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ repo.write_project_config(
+ r#"pre-merge-command = "echo 'Running pre-merge checks on {{ branch }}'""#,
+ );
+
+ repo.commit("Add pre-merge hook");
+
+ // Decline approval to verify the prompt appears
+ snapshot_run_hook(
+ "run_hook_pre_merge_requires_approval",
+ &repo,
+ "pre-merge",
+ false,
+ );
+}
+
+/// Test that `wt beta run-hook post-merge` requires approval (security boundary test)
+///
+/// This verifies the fix for the security issue where run-hook was bypassing approval.
+/// Before the fix, post-merge hooks ran with auto_trust=true, skipping approval prompts.
+#[test]
+fn test_run_hook_post_merge_requires_approval() {
+ let repo = TestRepo::new();
+ repo.commit("Initial commit");
+
+ repo.write_project_config(
+ r#"post-merge-command = "echo 'Post-merge cleanup for {{ branch }}'""#,
+ );
+
+ repo.commit("Add post-merge hook");
+
+ // Decline approval to verify the prompt appears
+ snapshot_run_hook(
+ "run_hook_post_merge_requires_approval",
+ &repo,
+ "post-merge",
+ false,
+ );
+}
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__run_hook_post_merge_requires_approval.snap b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_post_merge_requires_approval.snap
new file mode 100644
index 000000000..c4003fc61
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_post_merge_requires_approval.snap
@@ -0,0 +1,16 @@
+---
+source: tests/integration_tests/approval_ui.rs
+expression: combined
+---
+exit_code: 1
+----- stdout -----
+
+
+----- stderr -----
+
+🟑 test-repo wants to execute 1 command:
+
+πŸ”„ post-merge:
+  echo 'Post-merge cleanup for main'
+
+πŸ’‘ Allow and remember? [y/N]
diff --git a/tests/snapshots/integration__integration_tests__approval_ui__run_hook_pre_merge_requires_approval.snap b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_pre_merge_requires_approval.snap
new file mode 100644
index 000000000..bb58104aa
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__approval_ui__run_hook_pre_merge_requires_approval.snap
@@ -0,0 +1,16 @@
+---
+source: tests/integration_tests/approval_ui.rs
+expression: combined
+---
+exit_code: 1
+----- stdout -----
+
+
+----- stderr -----
+
+🟑 test-repo wants to execute 1 command:
+
+πŸ”„ pre-merge:
+  echo 'Running pre-merge checks on main'
+
+πŸ’‘ Allow and remember? [y/N]