Skip to content

Commit db22c91

Browse files
iceweasel-oaicodex
andauthored
Share Git safe-command logic on Windows (#21275)
## Why BUGB-15601 showed that the Windows safe-command path had drifted from the generic Git classifier. The Windows-specific Git parser could classify a PowerShell-wrapped `git` command as safe as soon as it found a safelisted subcommand, without applying the generic checks for unsafe subcommand options such as `--output`, `--ext-diff`, `--textconv`, `--paginate`, or `cat-file --filters`. The generic classifier already models the Git command boundary and the read-only argument checks more carefully, so Windows should reuse that logic instead of maintaining a smaller parallel parser. ## What Changed - Extracted the existing generic Git classification logic into `is_safe_git_command`. - Updated `windows_safe_commands.rs` to call that shared helper for parsed PowerShell `git` commands. - Removed the Windows-only Git subcommand safelist, including the `cat-file` allowance that was part of the reported bypass. - Added a Windows regression test that keeps PowerShell-wrapped Git commands with side-effecting options classified unsafe. - Made the full-path PowerShell test discover the installed PowerShell executable instead of depending on one hard-coded `pwsh.exe` path. ## Verification - `cargo test -p codex-shell-command rejects_git_subcommand_options_with_side_effects` - `cargo test -p codex-shell-command git_global_override_flags_are_not_safe` - `cargo test -p codex-shell-command windows_powershell_full_path_is_safe -- --nocapture` Co-authored-by: Codex <[email protected]>
1 parent 794c240 commit db22c91

2 files changed

Lines changed: 78 additions & 60 deletions

File tree

‎codex-rs/shell-command/src/command_safety/is_safe_command.rs‎

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -151,36 +151,7 @@ fn is_safe_to_call_with_exec(command: &[String]) -> bool {
151151
}
152152

153153
// Git
154-
Some("git") => {
155-
// Global options that redirect config, repository, or helper
156-
// lookup can make otherwise read-only git commands execute
157-
// attacker-controlled code, so they must never be auto-approved.
158-
if git_has_unsafe_global_option(command) {
159-
return false;
160-
}
161-
162-
let Some((subcommand_idx, subcommand)) =
163-
find_git_subcommand(command, &["status", "log", "diff", "show", "branch"])
164-
else {
165-
return false;
166-
};
167-
168-
let subcommand_args = &command[subcommand_idx + 1..];
169-
170-
match subcommand {
171-
"status" | "log" | "diff" | "show" => {
172-
git_subcommand_args_are_read_only(subcommand_args)
173-
}
174-
"branch" => {
175-
git_subcommand_args_are_read_only(subcommand_args)
176-
&& git_branch_is_read_only(subcommand_args)
177-
}
178-
other => {
179-
debug_assert!(false, "unexpected git subcommand from matcher: {other}");
180-
false
181-
}
182-
}
183-
}
154+
Some("git") => is_safe_git_command(command),
184155

185156
// Special-case `sed -n {N|M,N}p`
186157
Some("sed")
@@ -198,6 +169,35 @@ fn is_safe_to_call_with_exec(command: &[String]) -> bool {
198169
}
199170
}
200171

172+
pub(crate) fn is_safe_git_command(command: &[String]) -> bool {
173+
// Global options that redirect config, repository, or helper lookup can make
174+
// otherwise read-only git commands execute attacker-controlled code, so they
175+
// must never be auto-approved.
176+
if git_has_unsafe_global_option(command) {
177+
return false;
178+
}
179+
180+
let Some((subcommand_idx, subcommand)) =
181+
find_git_subcommand(command, &["status", "log", "diff", "show", "branch"])
182+
else {
183+
return false;
184+
};
185+
186+
let subcommand_args = &command[subcommand_idx + 1..];
187+
188+
match subcommand {
189+
"status" | "log" | "diff" | "show" => git_subcommand_args_are_read_only(subcommand_args),
190+
"branch" => {
191+
git_subcommand_args_are_read_only(subcommand_args)
192+
&& git_branch_is_read_only(subcommand_args)
193+
}
194+
other => {
195+
debug_assert!(false, "unexpected git subcommand from matcher: {other}");
196+
false
197+
}
198+
}
199+
}
200+
201201
// Treat `git branch` as safe only when the arguments clearly indicate
202202
// a read-only query, not a branch mutation (create/rename/delete).
203203
fn git_branch_is_read_only(branch_args: &[String]) -> bool {
@@ -542,8 +542,15 @@ mod tests {
542542
return;
543543
}
544544

545+
let Some(powershell) = crate::powershell::try_find_pwsh_executable_blocking()
546+
.or_else(crate::powershell::try_find_powershell_executable_blocking)
547+
else {
548+
return;
549+
};
550+
let powershell = powershell.as_path().to_str().unwrap();
551+
545552
assert!(is_known_safe_command(&vec_str(&[
546-
r"C:\Program Files\PowerShell\7\pwsh.exe",
553+
powershell,
547554
"-Command",
548555
"Get-Location",
549556
])));

‎codex-rs/shell-command/src/command_safety/windows_safe_commands.rs‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::command_safety::is_dangerous_command::git_global_option_requires_prompt;
1+
use crate::command_safety::is_safe_command::is_safe_git_command;
22
use crate::command_safety::powershell_parser::PowershellParseOutcome;
33
use crate::command_safety::powershell_parser::parse_with_powershell_ast;
44
use std::path::Path;
@@ -221,37 +221,11 @@ fn is_safe_ripgrep(words: &[String]) -> bool {
221221
})
222222
}
223223

224-
/// Ensures a Git command sticks to whitelisted read-only subcommands and flags.
225-
fn is_safe_git_command(words: &[String]) -> bool {
226-
const SAFE_SUBCOMMANDS: &[&str] = &["status", "log", "show", "diff", "cat-file"];
227-
228-
for arg in words.iter().skip(1) {
229-
let arg_lc = arg.to_ascii_lowercase();
230-
231-
if arg.starts_with('-') {
232-
if git_global_option_requires_prompt(&arg_lc)
233-
|| arg.eq_ignore_ascii_case("--config")
234-
|| arg_lc.starts_with("--config=")
235-
{
236-
// Examples rejected here: "pwsh -Command 'git --git-dir=.evil-git diff'" and
237-
// "pwsh -Command 'git -c core.pager=cat show HEAD:foo.rs'".
238-
return false;
239-
}
240-
241-
continue;
242-
}
243-
244-
return SAFE_SUBCOMMANDS.contains(&arg_lc.as_str());
245-
}
246-
247-
// Examples rejected here: "pwsh -Command 'git'" and "pwsh -Command 'git status --short | Remove-Item foo'".
248-
false
249-
}
250-
251224
#[cfg(all(test, windows))]
252225
mod tests {
253226
use super::*;
254227
use crate::powershell::try_find_pwsh_executable_blocking;
228+
use pretty_assertions::assert_eq;
255229
use std::string::ToString;
256230

257231
/// Converts a slice of string literals into owned `String`s for the tests.
@@ -342,7 +316,7 @@ mod tests {
342316
assert!(is_safe_command_windows(&[
343317
pwsh.clone(),
344318
"-Command".to_string(),
345-
"-git cat-file -p HEAD:foo.rs".to_string()
319+
"git show HEAD:foo.rs".to_string()
346320
]));
347321

348322
assert!(is_safe_command_windows(&[
@@ -393,6 +367,43 @@ mod tests {
393367
}
394368
}
395369

370+
#[test]
371+
fn rejects_git_subcommand_options_with_side_effects() {
372+
let results: Vec<(&str, bool)> = [
373+
"git diff --output codex_poc.txt",
374+
"git diff --ext-diff HEAD",
375+
"git log --textconv -1",
376+
"git log --paginate -1",
377+
"git show --output=codex_poc.txt HEAD",
378+
"git cat-file --filters HEAD:a.txt",
379+
]
380+
.into_iter()
381+
.map(|script| {
382+
(
383+
script,
384+
is_safe_command_windows(&[
385+
"powershell.exe".to_string(),
386+
"-NoProfile".to_string(),
387+
"-Command".to_string(),
388+
script.to_string(),
389+
]),
390+
)
391+
})
392+
.collect();
393+
394+
assert_eq!(
395+
vec![
396+
("git diff --output codex_poc.txt", false),
397+
("git diff --ext-diff HEAD", false),
398+
("git log --textconv -1", false),
399+
("git log --paginate -1", false),
400+
("git show --output=codex_poc.txt HEAD", false),
401+
("git cat-file --filters HEAD:a.txt", false),
402+
],
403+
results
404+
);
405+
}
406+
396407
#[test]
397408
fn rejects_powershell_commands_with_side_effects() {
398409
assert!(!is_safe_command_windows(&vec_str(&[

0 commit comments

Comments
 (0)