Skip to content

core: add potentially dangerous command check#4211

Merged
iceweasel-oai merged 7 commits intomainfrom
iceweasel/git-reset-not-safe
Sep 26, 2025
Merged

core: add potentially dangerous command check#4211
iceweasel-oai merged 7 commits intomainfrom
iceweasel/git-reset-not-safe

Conversation

@iceweasel-oai
Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting

Comment thread codex-rs/core/src/command_safety/is_dangerous_command.rs Outdated
@bolinfest bolinfest self-requested a review September 25, 2025 05:05
Copy link
Copy Markdown
Collaborator

@bolinfest bolinfest left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will never be able to enumerate all dangerous commands: please focus on matching safe/trusted commands.

@mistial-dev
Copy link
Copy Markdown

We will never be able to enumerate all dangerous commands: please focus on matching safe/trusted commands.

Codex has certain tendencies for certain dangerous commands. While it's true that it's impossible to ban all dangerous commands, just approving on "rm -rf" and "git reset --hard" would go a long way towards blunting some of the most egregious patterns it gets into when frustrated.

Comment on lines +12 to +24
if let [bash, flag, script] = command
&& bash == "bash"
&& flag == "-lc"
&& let Some(tree) = try_parse_bash(script)
&& let Some(all_commands) = try_parse_word_only_commands_sequence(&tree, script)
&& all_commands
.iter()
.any(|cmd| contains_dangerous_command(cmd))
{
return true;
}

false
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this code exists elsewhere: please refactor it for reuse.

return false;
}

let command_string = command.join(" ");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regular expressions are not good ways to match commands. For example, you have "git reset" but not "git reset" (two spaces instead of one).

Use the same strategy we do for trusted commands.

Comment on lines +15 to +16
&& let Some(tree) = try_parse_bash(script)
&& let Some(all_commands) = try_parse_word_only_commands_sequence(&tree, script)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to "fail deadly". If we aren't able to parse the script, should we presume it to be dangerous, rather than assuming it to be safe?

&& let Some(all_commands) = try_parse_word_only_commands_sequence(&tree, script)
&& all_commands
.iter()
.any(|cmd| contains_dangerous_command(cmd))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't think that this would catch: bash -lc "bash -lc \"dangerous_command\"". Should this be recursive in any way? Are we okay with a bit of a leaky sieve here since we also have the sandbox as a backstop?

@fouad-openai fouad-openai changed the title check for potentially dangerous commands. core: add potentially dangerous command check Sep 26, 2025
@fouad-openai fouad-openai dismissed bolinfest’s stale review September 26, 2025 02:32

comments addressed

@iceweasel-oai iceweasel-oai merged commit eb2b739 into main Sep 26, 2025
19 checks passed
@iceweasel-oai iceweasel-oai deleted the iceweasel/git-reset-not-safe branch September 26, 2025 02:46
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 26, 2025
Comment on lines +93 to +95
if command_might_be_dangerous(command) && !approved.contains(command) {
return SafetyCheck::AskUser;
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If approval_policy is AskForApproval::Never, then I believe this should return SafetyCheck::Reject with an appropriate reason.

For example, Never might be selected because Codex is being used in a non-interactive way (like codex exec), so the agent loop would never get a response.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants