mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
[release/0.144] fix(core) expand is_dangerous_command (#33455)
## Summary Cherry-picks the seven commits from [openai/codex-internal#1942](https://github.com/openai/codex-internal/pull/1942) onto `release/0.144`. This backport: 1. enables dangerous-command detection in danger-full-access mode 2. expands literal Bash parsing so additional forced `rm` forms are detected 3. returns a specific rejection reason to the model when a dangerous command is denied The commits applied without conflicts or release-only changes. `git range-diff` confirms every cherry-picked patch matches the source PR exactly. ## Validation - `just test -p codex-shell-command` (141 passed) - `just test -p codex-core exec_policy` (107 passed) - `just test -p codex-core` outside the sandbox (2,947 passed; 4 unrelated environment/setup failures) - 3 RMCP tests could not locate the test-only `test_stdio_server` binary - 1 user-shell environment test observed the tool runner's mandated `CODEX_SANDBOX_NETWORK_DISABLED=1` - `just fix -p codex-core` - `just fix -p codex-shell-command` - `just fmt` - `git diff --check`
This commit is contained in:
@@ -123,6 +123,39 @@ pub fn parse_shell_lc_plain_commands(command: &[String]) -> Option<Vec<Vec<Strin
|
||||
parse_shell_script_into_commands(script)
|
||||
}
|
||||
|
||||
/// Extracts the literal portions of command invocations from a shell script.
|
||||
///
|
||||
/// Unlike [`parse_shell_lc_plain_commands`], this accepts complex shell syntax
|
||||
/// and returns the statically known words from every command node in a valid
|
||||
/// syntax tree. Dynamic words and redirections are omitted. This is suitable
|
||||
/// for identifying dangerous literal commands, but must not be used to prove
|
||||
/// that a command is safe.
|
||||
pub(crate) fn parse_shell_lc_literal_commands(command: &[String]) -> Option<Vec<Vec<String>>> {
|
||||
let (_, script) = extract_bash_command(command)?;
|
||||
let tree = try_parse_shell(script)?;
|
||||
let root = tree.root_node();
|
||||
if root.has_error() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut commands = Vec::new();
|
||||
let mut stack = vec![root];
|
||||
while let Some(node) = stack.pop() {
|
||||
if node.kind() == "command"
|
||||
&& let Some(command) = parse_literal_command_from_node(node, script)
|
||||
{
|
||||
commands.push(command);
|
||||
}
|
||||
|
||||
let mut cursor = node.walk();
|
||||
for child in node.named_children(&mut cursor) {
|
||||
stack.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
Some(commands)
|
||||
}
|
||||
|
||||
/// Returns the parsed argv for a single shell command in a here-doc style
|
||||
/// script (`<<`), as long as the script contains exactly one command node.
|
||||
pub fn parse_shell_lc_single_command_prefix(command: &[String]) -> Option<Vec<String>> {
|
||||
@@ -201,6 +234,46 @@ fn parse_plain_command_from_node(cmd: tree_sitter::Node, src: &str) -> Option<Ve
|
||||
Some(words)
|
||||
}
|
||||
|
||||
fn parse_literal_command_from_node(cmd: Node<'_>, src: &str) -> Option<Vec<String>> {
|
||||
if cmd.kind() != "command" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut words = Vec::new();
|
||||
let mut found_command_name = false;
|
||||
let mut cursor = cmd.walk();
|
||||
for child in cmd.named_children(&mut cursor) {
|
||||
if child.kind() == "command_name" {
|
||||
let command_name = parse_literal_shell_word(child.named_child(0)?, src)?;
|
||||
words.push(command_name);
|
||||
found_command_name = true;
|
||||
} else if found_command_name && let Some(word) = parse_literal_shell_word(child, src) {
|
||||
words.push(word);
|
||||
}
|
||||
}
|
||||
|
||||
found_command_name.then_some(words)
|
||||
}
|
||||
|
||||
fn parse_literal_shell_word(node: Node<'_>, src: &str) -> Option<String> {
|
||||
match node.kind() {
|
||||
"word" | "number" if is_literal_word_or_number(node) => {
|
||||
Some(node.utf8_text(src.as_bytes()).ok()?.to_owned())
|
||||
}
|
||||
"string" => parse_double_quoted_string(node, src),
|
||||
"raw_string" => parse_raw_string(node, src),
|
||||
"concatenation" => {
|
||||
let mut concatenated = String::new();
|
||||
let mut cursor = node.walk();
|
||||
for part in node.named_children(&mut cursor) {
|
||||
concatenated.push_str(&parse_literal_shell_word(part, src)?);
|
||||
}
|
||||
(!concatenated.is_empty()).then_some(concatenated)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_heredoc_command_words(cmd: Node<'_>, src: &str) -> Option<Vec<String>> {
|
||||
if cmd.kind() != "command" {
|
||||
return None;
|
||||
|
||||
@@ -1,45 +1,69 @@
|
||||
use crate::bash::parse_shell_lc_plain_commands;
|
||||
use crate::bash::parse_shell_lc_literal_commands;
|
||||
use std::path::Path;
|
||||
#[cfg(windows)]
|
||||
#[path = "windows_dangerous_commands.rs"]
|
||||
mod windows_dangerous_commands;
|
||||
|
||||
pub fn command_might_be_dangerous(command: &[String]) -> bool {
|
||||
/// Identifies the dangerous-command rule matched by a command invocation.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum DangerousCommandMatch {
|
||||
/// An `rm` invocation includes the force option.
|
||||
ForcedRm,
|
||||
/// Another dangerous-command rule matched.
|
||||
Other,
|
||||
}
|
||||
|
||||
const MAX_DANGEROUS_COMMAND_WRAPPER_DEPTH: usize = 8;
|
||||
|
||||
/// Returns the dangerous-command rule matched by an already-tokenized command.
|
||||
pub fn dangerous_command_match(command: &[String]) -> Option<DangerousCommandMatch> {
|
||||
dangerous_command_match_with_depth(command, /*wrapper_depth*/ 0)
|
||||
}
|
||||
|
||||
fn dangerous_command_match_with_depth(
|
||||
command: &[String],
|
||||
wrapper_depth: usize,
|
||||
) -> Option<DangerousCommandMatch> {
|
||||
if wrapper_depth > MAX_DANGEROUS_COMMAND_WRAPPER_DEPTH {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Some(dangerous_match) = dangerous_command_match_for_exec(command, wrapper_depth) {
|
||||
return Some(dangerous_match);
|
||||
}
|
||||
|
||||
// Support shell scripts where any literal command might be dangerous,
|
||||
// including commands nested in control flow or substitutions.
|
||||
if let Some(dangerous_match) = parse_shell_lc_literal_commands(command).and_then(|commands| {
|
||||
commands
|
||||
.iter()
|
||||
.find_map(|command| dangerous_command_match_with_depth(command, wrapper_depth + 1))
|
||||
}) {
|
||||
return Some(dangerous_match);
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if windows_dangerous_commands::is_dangerous_command_windows(command) {
|
||||
return true;
|
||||
return Some(DangerousCommandMatch::Other);
|
||||
}
|
||||
}
|
||||
|
||||
if is_dangerous_to_call_with_exec(command) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Support `bash -lc "<script>"` where the any part of the script might contain a dangerous command.
|
||||
if let Some(all_commands) = parse_shell_lc_plain_commands(command)
|
||||
&& all_commands
|
||||
.iter()
|
||||
.any(|cmd| is_dangerous_to_call_with_exec(cmd))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
false
|
||||
None
|
||||
}
|
||||
|
||||
/// Returns whether already-tokenized PowerShell words should be treated as
|
||||
/// dangerous by the Windows unmatched-command heuristics.
|
||||
pub fn is_dangerous_powershell_words(command: &[String]) -> bool {
|
||||
/// Returns the dangerous-command rule matched by tokenized PowerShell words.
|
||||
pub fn dangerous_powershell_words_match(command: &[String]) -> Option<DangerousCommandMatch> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
windows_dangerous_commands::is_dangerous_powershell_words(command)
|
||||
.then_some(DangerousCommandMatch::Other)
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
let _ = command;
|
||||
false
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,23 +166,89 @@ pub(crate) fn find_git_subcommand<'a>(
|
||||
None
|
||||
}
|
||||
|
||||
fn is_dangerous_to_call_with_exec(command: &[String]) -> bool {
|
||||
let cmd0 = command.first().map(String::as_str);
|
||||
fn dangerous_command_match_for_exec(
|
||||
command: &[String],
|
||||
wrapper_depth: usize,
|
||||
) -> Option<DangerousCommandMatch> {
|
||||
let cmd0 = command
|
||||
.first()
|
||||
.and_then(|command| executable_name_lookup_key(command));
|
||||
|
||||
match cmd0 {
|
||||
Some("rm") => matches!(command.get(1).map(String::as_str), Some("-f" | "-rf")),
|
||||
match cmd0.as_deref() {
|
||||
Some("rm") if rm_args_include_force_option(&command[1..]) => {
|
||||
Some(DangerousCommandMatch::ForcedRm)
|
||||
}
|
||||
|
||||
// for sudo <cmd> simply do the check for <cmd>
|
||||
Some("sudo") => is_dangerous_to_call_with_exec(&command[1..]),
|
||||
// For sudo <cmd>, simply check <cmd>.
|
||||
Some("sudo") => dangerous_command_match_with_depth(&command[1..], wrapper_depth + 1),
|
||||
|
||||
// Skip environment assignments before checking the command run by env.
|
||||
Some("env") => dangerous_command_match_for_env(command, wrapper_depth),
|
||||
|
||||
// A trap action is shell source stored in the first operand.
|
||||
Some("trap") => dangerous_command_match_for_trap(command, wrapper_depth),
|
||||
|
||||
// ── anything else ─────────────────────────────────────────────────
|
||||
_ => false,
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn dangerous_command_match_for_env(
|
||||
command: &[String],
|
||||
wrapper_depth: usize,
|
||||
) -> Option<DangerousCommandMatch> {
|
||||
let mut command_index = 1;
|
||||
while let Some(argument) = command.get(command_index) {
|
||||
if argument == "--" {
|
||||
command_index += 1;
|
||||
break;
|
||||
}
|
||||
if matches!(argument.as_str(), "-i" | "--ignore-environment")
|
||||
|| argument
|
||||
.split_once('=')
|
||||
.is_some_and(|(name, _)| !name.is_empty() && !name.starts_with('-'))
|
||||
{
|
||||
command_index += 1;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
dangerous_command_match_with_depth(&command[command_index..], wrapper_depth + 1)
|
||||
}
|
||||
|
||||
fn dangerous_command_match_for_trap(
|
||||
command: &[String],
|
||||
wrapper_depth: usize,
|
||||
) -> Option<DangerousCommandMatch> {
|
||||
let mut action_index = 1;
|
||||
if command
|
||||
.get(action_index)
|
||||
.is_some_and(|argument| argument == "--")
|
||||
{
|
||||
action_index += 1;
|
||||
}
|
||||
let action = command
|
||||
.get(action_index)
|
||||
.filter(|action| !action.starts_with('-'))?;
|
||||
let shell_command = vec!["sh".to_string(), "-c".to_string(), action.clone()];
|
||||
dangerous_command_match_with_depth(&shell_command, wrapper_depth + 1)
|
||||
}
|
||||
|
||||
fn rm_args_include_force_option(args: &[String]) -> bool {
|
||||
args.iter()
|
||||
.take_while(|arg| arg.as_str() != "--")
|
||||
.any(|arg| {
|
||||
arg == "--force"
|
||||
|| arg
|
||||
.strip_prefix('-')
|
||||
.is_some_and(|flags| !flags.starts_with('-') && flags.contains('f'))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn vec_str(items: &[&str]) -> Vec<String> {
|
||||
items.iter().map(std::string::ToString::to_string).collect()
|
||||
@@ -166,22 +256,85 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn rm_rf_is_dangerous() {
|
||||
assert!(command_might_be_dangerous(&vec_str(&["rm", "-rf", "/"])));
|
||||
assert_eq!(
|
||||
dangerous_command_match(&vec_str(&["rm", "-rf", "/"])),
|
||||
Some(DangerousCommandMatch::ForcedRm)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rm_f_is_dangerous() {
|
||||
assert!(command_might_be_dangerous(&vec_str(&["rm", "-f", "/"])));
|
||||
assert_eq!(
|
||||
dangerous_command_match(&vec_str(&["rm", "-f", "/"])),
|
||||
Some(DangerousCommandMatch::ForcedRm)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn direct_powershell_words_reuse_windows_dangerous_detection() {
|
||||
fn forced_rm_variants_are_dangerous() {
|
||||
for command in [
|
||||
vec_str(&["/bin/rm", "-fr", "/tmp/example"]),
|
||||
vec_str(&["rm", "-r", "-f", "/tmp/example"]),
|
||||
vec_str(&["rm", "--force", "/tmp/example"]),
|
||||
vec_str(&["rm", "/tmp/example", "-f"]),
|
||||
vec_str(&["sudo", "rm", "-rf", "/tmp/example"]),
|
||||
vec_str(&["env", "TARGET=/tmp/example", "rm", "-rf", "/tmp/example"]),
|
||||
] {
|
||||
assert_eq!(
|
||||
dangerous_command_match(&command),
|
||||
Some(DangerousCommandMatch::ForcedRm),
|
||||
"{command:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forced_rm_in_complex_shell_syntax_is_dangerous() {
|
||||
for script in [
|
||||
"printf x | rm -rf /tmp/example",
|
||||
"if test -d /tmp/example; then rm --force /tmp/example; fi",
|
||||
"rm -rf \"$TARGET\" >/dev/null",
|
||||
"for target in /tmp/a /tmp/b; do rm -r -f \"$target\"; done",
|
||||
"echo \"$(rm -rf /tmp/example)\"",
|
||||
"bash -c 'rm -rf /tmp/example'",
|
||||
"trap 'rm -rf /tmp/example' EXIT",
|
||||
"for a in '-C5a25KeRr' '--' '--json' '--bogus'; do HOME=$(mktemp -d) MDE_URL=http://127.0.0.1:1 MDE_TOKEN=x node cli/mde.cjs ls \"$a\" >/tmp/mde-review-out 2>/tmp/mde-review-err; code=$?; printf '%s\\t%s\\t%s\\n' \"$a\" \"$code\" \"$(tr '\\n' ' ' </tmp/mde-review-err)\"; rm -rf \"$HOME\"; done",
|
||||
] {
|
||||
let command = vec_str(&["bash", "-lc", script]);
|
||||
assert_eq!(
|
||||
dangerous_command_match(&command),
|
||||
Some(DangerousCommandMatch::ForcedRm),
|
||||
"{script}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_forced_or_non_literal_rm_is_not_dangerous() {
|
||||
for command in [
|
||||
vec_str(&["rm", "-r", "/tmp/example"]),
|
||||
vec_str(&["rm", "--", "-f"]),
|
||||
vec_str(&["bash", "-lc", "echo 'rm -rf /tmp/example'"]),
|
||||
vec_str(&["bash", "-lc", "cmd=rm; $cmd -rf /tmp/example"]),
|
||||
vec_str(&["bash", "-lc", "if then rm -rf /tmp/example"]),
|
||||
vec_str(&["env", "TARGET=/tmp/example", "rm", "-r", "/tmp/example"]),
|
||||
vec_str(&["bash", "-lc", "trap 'echo rm -rf /tmp/example' EXIT"]),
|
||||
] {
|
||||
assert_eq!(dangerous_command_match(&command), None, "{command:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn direct_powershell_words_return_other_match_on_windows() {
|
||||
let command = vec_str(&["Remove-Item", "test", "-Force"]);
|
||||
|
||||
if cfg!(windows) {
|
||||
assert!(is_dangerous_powershell_words(&command));
|
||||
assert_eq!(
|
||||
dangerous_powershell_words_match(&command),
|
||||
Some(DangerousCommandMatch::Other)
|
||||
);
|
||||
} else {
|
||||
assert!(!is_dangerous_powershell_words(&command));
|
||||
assert_eq!(dangerous_powershell_words_match(&command), None);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user