From eb2b739d6a0da69c6f5bdc1885db4d2cc9e2bd36 Mon Sep 17 00:00:00 2001 From: iceweasel-oai Date: Thu, 25 Sep 2025 19:46:20 -0700 Subject: [PATCH] core: add potentially dangerous command check (#4211) Certain shell commands are potentially dangerous, and we want to check for them. Unless the user has explicitly approved a command, we will *always* ask them for approval when one of these commands is encountered, regardless of whether they are in a sandbox, or what their approval policy is. The first (of probably many) such examples is `git reset --hard`. We will be conservative and check for any `git reset` --- codex-rs/core/src/bash.rs | 15 +++ .../command_safety/is_dangerous_command.rs | 99 +++++++++++++++++++ .../src/command_safety/is_safe_command.rs | 9 +- codex-rs/core/src/command_safety/mod.rs | 1 + codex-rs/core/src/safety.rs | 56 ++++++++++- 5 files changed, 172 insertions(+), 8 deletions(-) create mode 100644 codex-rs/core/src/command_safety/is_dangerous_command.rs diff --git a/codex-rs/core/src/bash.rs b/codex-rs/core/src/bash.rs index f25b4f7f..c87f2764 100644 --- a/codex-rs/core/src/bash.rs +++ b/codex-rs/core/src/bash.rs @@ -88,6 +88,21 @@ pub fn try_parse_word_only_commands_sequence(tree: &Tree, src: &str) -> Option Option>> { + let [bash, flag, script] = command else { + return None; + }; + + if bash != "bash" || flag != "-lc" { + return None; + } + + let tree = try_parse_bash(script)?; + try_parse_word_only_commands_sequence(&tree, script) +} + fn parse_plain_command_from_node(cmd: tree_sitter::Node, src: &str) -> Option> { if cmd.kind() != "command" { return None; diff --git a/codex-rs/core/src/command_safety/is_dangerous_command.rs b/codex-rs/core/src/command_safety/is_dangerous_command.rs new file mode 100644 index 00000000..852af93e --- /dev/null +++ b/codex-rs/core/src/command_safety/is_dangerous_command.rs @@ -0,0 +1,99 @@ +use crate::bash::parse_bash_lc_plain_commands; + +pub fn command_might_be_dangerous(command: &[String]) -> bool { + if is_dangerous_to_call_with_exec(command) { + return true; + } + + // Support `bash -lc "