mirror of
https://github.com/openai/codex.git
synced 2026-11-22 23:00:42 +00:00
Avoid executing PATH helpers before workspace trust (#42324)
## Why Automatic startup work and `codex doctor` can run before a workspace is trusted. A repository-controlled `PATH` must not be able to make those flows execute workspace-provided helpers. ## What changed - Resolve helpers used by automatic startup from trusted system installation directories, and give plugin-sync Git subprocesses a sanitized environment. - Make terminal detection environment-only and have doctor inspect executable locations without running them. Fetch update metadata with the HTTP client instead of `curl`. - Fall back conservatively when trusted terminal helpers are unavailable, including for tmux keyboard enhancement flags. ## Testing Add black-box coverage with hostile workspace `PATH` entries for startup, interactive tmux startup, support log collection, doctor, and curated plugin sync. Add unit coverage for trusted executable resolution and bounded update HTTP responses. GitOrigin-RevId: 3b8995eb422b60ed53b0386951de59e8f9bfc542
This commit is contained in:
@@ -78,14 +78,12 @@ pub fn sync_openai_plugins_repo(
|
||||
codex_home: &Path,
|
||||
http_client_factory: HttpClientFactory,
|
||||
) -> Result<String, String> {
|
||||
// Keep Git-only egress working without trusting workspace PATH entries.
|
||||
let git_binary = codex_utils_path::system_executable("git");
|
||||
// Apple's /usr/bin/git is an installer shim when developer tools are absent.
|
||||
// The resolver prefers the real CLT/Xcode executable when installed.
|
||||
#[cfg(target_os = "macos")]
|
||||
let git_binary = match which::which("git") {
|
||||
Ok(git_path) => macos_git_binary_from_path(git_path, apple_developer_tools_available()),
|
||||
Err(_) => None,
|
||||
};
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let git_binary = Some(PathBuf::from("git"));
|
||||
|
||||
let git_binary = git_binary.filter(|path| path != Path::new("/usr/bin/git"));
|
||||
sync_openai_plugins_repo_with_transport_overrides(
|
||||
codex_home,
|
||||
git_binary.as_deref(),
|
||||
@@ -106,7 +104,7 @@ fn sync_openai_plugins_repo_with_transport_overrides(
|
||||
|
||||
let git_sync_result = match git_binary {
|
||||
Some(git_binary) => sync_openai_plugins_repo_via_git(codex_home, git_binary),
|
||||
None => Err("git executable is unavailable".to_string()),
|
||||
None => Err("no Git executable found in trusted installation directories".to_string()),
|
||||
};
|
||||
|
||||
match git_sync_result {
|
||||
@@ -116,11 +114,13 @@ fn sync_openai_plugins_repo_with_transport_overrides(
|
||||
Ok(remote_sha)
|
||||
}
|
||||
Err(err) => {
|
||||
emit_curated_plugins_startup_sync_metric("git", "failure");
|
||||
warn!(
|
||||
error = %err,
|
||||
"git sync failed for curated plugin sync; falling back to GitHub HTTP"
|
||||
);
|
||||
if git_binary.is_some() {
|
||||
emit_curated_plugins_startup_sync_metric("git", "failure");
|
||||
warn!(
|
||||
error = %err,
|
||||
"git sync failed for curated plugin sync; falling back to GitHub HTTP"
|
||||
);
|
||||
}
|
||||
match sync_openai_plugins_repo_via_http(codex_home, api_base_url, http_client_factory) {
|
||||
Ok(remote_sha) => {
|
||||
emit_curated_plugins_startup_sync_metric("http", "success");
|
||||
@@ -266,7 +266,7 @@ fn fetch_curated_plugins_commit_from(
|
||||
context: &str,
|
||||
) -> Result<(), String> {
|
||||
let fetch_refspec = format!("+{source_revision}:{CURATED_PLUGINS_FETCH_REF}");
|
||||
let mut command = git_command(git_binary);
|
||||
let mut command = git_command(git_binary)?;
|
||||
command
|
||||
.arg("-C")
|
||||
.arg(repo_path)
|
||||
@@ -298,7 +298,7 @@ fn run_git_in_repo(
|
||||
args: &[&str],
|
||||
context: &str,
|
||||
) -> Result<(), String> {
|
||||
let mut command = git_command(git_binary);
|
||||
let mut command = git_command(git_binary)?;
|
||||
command.arg("-C").arg(repo_path).args(args);
|
||||
let output = run_git_command_with_timeout(&mut command, context, CURATED_PLUGINS_GIT_TIMEOUT)?;
|
||||
ensure_git_success(&output, context)
|
||||
@@ -608,9 +608,10 @@ fn read_local_git_or_sha_file(
|
||||
}
|
||||
|
||||
fn git_ls_remote_head_sha(codex_home: &Path, git_binary: &Path) -> Result<String, String> {
|
||||
let mut command = git_command(git_binary);
|
||||
let mut command = git_command(git_binary)?;
|
||||
let _trusted_repository = crate::configure_trusted_git_repository(&mut command, codex_home)?;
|
||||
command
|
||||
.current_dir(codex_home)
|
||||
.arg("ls-remote")
|
||||
.arg(OPENAI_PLUGINS_GIT_URL)
|
||||
.arg("HEAD");
|
||||
@@ -637,7 +638,7 @@ fn git_ls_remote_head_sha(codex_home: &Path, git_binary: &Path) -> Result<String
|
||||
}
|
||||
|
||||
fn git_head_sha(repo_path: &Path, git_binary: &Path) -> Result<String, String> {
|
||||
let output = git_command(git_binary)
|
||||
let output = git_command(git_binary)?
|
||||
.arg("-C")
|
||||
.arg(repo_path)
|
||||
.arg("rev-parse")
|
||||
@@ -661,31 +662,22 @@ fn git_head_sha(repo_path: &Path, git_binary: &Path) -> Result<String, String> {
|
||||
Ok(sha)
|
||||
}
|
||||
|
||||
fn git_command(git_binary: &Path) -> Command {
|
||||
crate::PluginGitMode::Automatic.command(git_binary)
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "macos", test))]
|
||||
fn macos_git_binary_from_path(
|
||||
git_path: PathBuf,
|
||||
apple_developer_tools_available: bool,
|
||||
) -> Option<PathBuf> {
|
||||
if git_path == Path::new("/usr/bin/git") && !apple_developer_tools_available {
|
||||
None
|
||||
} else {
|
||||
Some(git_path)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn apple_developer_tools_available() -> bool {
|
||||
Command::new("/usr/bin/xcode-select")
|
||||
.arg("-p")
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.status()
|
||||
.is_ok_and(|status| status.success())
|
||||
fn git_command(git_binary: &Path) -> Result<Command, String> {
|
||||
let mut command = crate::PluginGitMode::Automatic.command(git_binary);
|
||||
// Git launches transports and credential helpers too. Selecting a trusted
|
||||
// main executable alone does not prevent a workspace PATH helper from running.
|
||||
command
|
||||
.env(
|
||||
"PATH",
|
||||
codex_utils_path::system_path()
|
||||
.map_err(|err| format!("failed to construct trusted Git PATH: {err}"))?,
|
||||
)
|
||||
.env_remove("GIT_EXEC_PATH")
|
||||
.env_remove("GIT_TEMPLATE_DIR")
|
||||
.env_remove("DEVELOPER_DIR")
|
||||
.env("GIT_TERMINAL_PROMPT", "0")
|
||||
.env("NoDefaultCurrentDirectoryInExePath", "1");
|
||||
Ok(command)
|
||||
}
|
||||
|
||||
fn run_git_command_with_timeout(
|
||||
|
||||
@@ -21,6 +21,106 @@ use zip::write::SimpleFileOptions;
|
||||
|
||||
const TEST_CURATED_PLUGIN_SHA: &str = "0123456789abcdef0123456789abcdef01234567";
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn pretrust_startup_sync_uses_installed_git_with_hostile_path() {
|
||||
const CHILD_HOME: &str = "CODEX_TEST_CURATED_SYNC_HOME";
|
||||
if let Some(home) = std::env::var_os(CHILD_HOME) {
|
||||
// Call the public entry point, synchronously. The parent joins this
|
||||
// process before checking the marker, so no detached work can escape it.
|
||||
sync_openai_plugins_repo(
|
||||
Path::new(&home),
|
||||
crate::test_support::test_http_client_factory(),
|
||||
)
|
||||
.expect("Git-only startup sync should succeed");
|
||||
return;
|
||||
}
|
||||
let fixture = tempdir().expect("fixture");
|
||||
let source = fixture.path().join("remote");
|
||||
let home = fixture.path().join("home");
|
||||
let workspace = fixture.path().join("workspace");
|
||||
let bin = workspace.join("node_modules/.bin");
|
||||
let marker = fixture.path().join("helper-ran");
|
||||
std::fs::create_dir_all(&source).unwrap();
|
||||
std::fs::create_dir_all(&bin).unwrap();
|
||||
std::fs::create_dir(&home).unwrap();
|
||||
run_git(&source, &["init", "--quiet"]);
|
||||
for name in ["git", "git-upload-pack", "git-remote-https"] {
|
||||
write_executable_script(
|
||||
&bin.join(name),
|
||||
&format!("#!/bin/sh\nprintf ran > '{}'\nexit 1\n", marker.display()),
|
||||
);
|
||||
}
|
||||
let config = fixture.path().join("gitconfig");
|
||||
std::fs::write(
|
||||
&config,
|
||||
format!(
|
||||
"[url \"file://{}\"]\n insteadOf = {OPENAI_PLUGINS_GIT_URL}\n",
|
||||
source.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
for plugins in [vec!["gmail"], vec!["gmail", "linear"]] {
|
||||
write_openai_curated_marketplace(&source, &plugins);
|
||||
run_git(&source, &["add", "."]);
|
||||
run_git(
|
||||
&source,
|
||||
&[
|
||||
"-c",
|
||||
"user.name=Test",
|
||||
"-c",
|
||||
"user.email=test@example.com",
|
||||
"commit",
|
||||
"-qm",
|
||||
"update",
|
||||
],
|
||||
);
|
||||
let expected_sha = run_git(&source, &["rev-parse", "HEAD"]);
|
||||
let output = Command::new(std::env::current_exe().unwrap())
|
||||
.args([
|
||||
"--exact",
|
||||
"startup_sync::tests::pretrust_startup_sync_uses_installed_git_with_hostile_path",
|
||||
"--nocapture",
|
||||
])
|
||||
.current_dir(&workspace)
|
||||
.env(CHILD_HOME, &home)
|
||||
.env("PATH", &bin)
|
||||
.env("GIT_EXEC_PATH", &bin)
|
||||
.env("GIT_CONFIG_GLOBAL", &config)
|
||||
.env("GIT_CONFIG_SYSTEM", "/dev/null")
|
||||
.env("HTTP_PROXY", "http://127.0.0.1:9")
|
||||
.env("HTTPS_PROXY", "http://127.0.0.1:9")
|
||||
.env("http_proxy", "http://127.0.0.1:9")
|
||||
.env("https_proxy", "http://127.0.0.1:9")
|
||||
.output()
|
||||
.expect("join sync subprocess");
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{}\n{}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr)
|
||||
);
|
||||
assert_eq!(
|
||||
read_curated_plugins_sha(&home),
|
||||
Some(
|
||||
String::from_utf8(expected_sha.stdout)
|
||||
.unwrap()
|
||||
.trim()
|
||||
.to_string()
|
||||
)
|
||||
);
|
||||
assert!(!marker.exists(), "sync executed a workspace helper");
|
||||
for plugin in plugins {
|
||||
assert!(
|
||||
curated_plugins_repo_path(&home)
|
||||
.join(format!("plugins/{plugin}/.codex-plugin/plugin.json"))
|
||||
.is_file()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn github_http_routes_repository_ref_and_zipball_urls() {
|
||||
let server = MockServer::start().await;
|
||||
@@ -93,7 +193,7 @@ async fn backup_archive_routes_metadata_and_backend_supplied_download_urls() {
|
||||
|
||||
#[test]
|
||||
fn git_command_sanitizes_ambient_repository_environment() {
|
||||
let command = git_command(Path::new("git"));
|
||||
let command = git_command(Path::new("git")).expect("trusted Git command");
|
||||
|
||||
assert_eq!(
|
||||
command.get_args().collect::<Vec<_>>(),
|
||||
@@ -260,6 +360,7 @@ async fn ordinary_clone_rejects_tracked_embedded_bare_repository() {
|
||||
std::fs::remove_file(&marker).expect("remove unguarded execution marker");
|
||||
|
||||
let guarded = git_command(Path::new("git"))
|
||||
.expect("trusted Git command")
|
||||
.args(["status", "--short"])
|
||||
.current_dir(&nested)
|
||||
.output()
|
||||
@@ -289,6 +390,7 @@ async fn ordinary_clone_rejects_tracked_embedded_bare_repository() {
|
||||
);
|
||||
|
||||
let explicit_git_dir = git_command(Path::new("git"))
|
||||
.expect("trusted Git command")
|
||||
.arg("--git-dir")
|
||||
.arg(&nested)
|
||||
.args(["rev-parse", "--git-dir"])
|
||||
@@ -936,31 +1038,6 @@ async fn sync_openai_plugins_repo_falls_back_to_http_when_git_is_unavailable() {
|
||||
assert_eq!(read_curated_plugins_sha(tmp.path()).as_deref(), Some(sha));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apple_git_without_developer_tools_is_unavailable() {
|
||||
assert_eq!(
|
||||
macos_git_binary_from_path(
|
||||
PathBuf::from("/usr/bin/git"),
|
||||
/*apple_developer_tools_available*/ false,
|
||||
),
|
||||
None
|
||||
);
|
||||
assert_eq!(
|
||||
macos_git_binary_from_path(
|
||||
PathBuf::from("/usr/bin/git"),
|
||||
/*apple_developer_tools_available*/ true,
|
||||
),
|
||||
Some(PathBuf::from("/usr/bin/git"))
|
||||
);
|
||||
assert_eq!(
|
||||
macos_git_binary_from_path(
|
||||
PathBuf::from("/opt/homebrew/bin/git"),
|
||||
/*apple_developer_tools_available*/ false,
|
||||
),
|
||||
Some(PathBuf::from("/opt/homebrew/bin/git"))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sync_openai_plugins_repo_uses_http_without_git_transport() {
|
||||
let tmp = tempdir().expect("tempdir");
|
||||
|
||||
Reference in New Issue
Block a user