Escape installer PATH entries for shell metacharacters (#62055)

## Why

Installation directories containing shell metacharacters can be interpreted incorrectly in shell profiles and the printed launch command.

## What changed

Escape installation directories in the generated `PATH` export and reuse that export in the current-terminal instructions. Preserve the escape sequences when rewriting an existing profile block by passing the line through the `awk` environment.

## Testing

Add a regression test that installs into directories containing shell metacharacters, verifies `codex` lookup after adding and updating the profile, and executes the printed launch command.

GitOrigin-RevId: b6c6044382128c97dfb503516f5eeaa934c85bbb
This commit is contained in:
Eric Traut
2026-11-17 14:38:53 +00:00
committed by copyberry
parent 5a4aff8431
commit 640b86dc87
2 changed files with 50 additions and 39 deletions
+17 -38
View File
@@ -611,7 +611,9 @@ add_to_path() {
path_profile="$profile"
begin_marker="# >>> Codex installer >>>"
end_marker="# <<< Codex installer <<<"
path_line="export PATH=\"$BIN_DIR:\$PATH\""
# Escape the directory for double quotes. Quote ! separately for interactive shells.
path_prefix=$(printf '%s:\n' "$BIN_DIR" | LC_ALL=C sed 's/[\\"$`]/\\&/g; s/!/"\\!"/g')
path_line="export PATH=\"$path_prefix\$PATH\""
if [ -f "$profile" ] && grep -F "$begin_marker" "$profile" >/dev/null 2>&1; then
if grep -F "$path_line" "$profile" >/dev/null 2>&1; then
@@ -650,7 +652,7 @@ rewrite_path_block() {
path_line="$4"
tmp_profile="$tmp_dir/profile.$$.tmp"
awk -v begin="$begin_marker" -v end="$end_marker" -v line="$path_line" '
CODEX_PATH_LINE="$path_line" awk -v begin="$begin_marker" -v end="$end_marker" '
BEGIN {
in_block = 0
replaced = 0
@@ -658,7 +660,7 @@ rewrite_path_block() {
$0 == begin {
if (!replaced) {
print begin
print line
print ENVIRON["CODEX_PATH_LINE"]
print end
replaced = 1
}
@@ -1038,26 +1040,18 @@ prompt_yes_no() {
}
print_launch_instructions() {
if [ "$path_action" = "already" ]; then
step "$BIN_DIR is already on PATH"
step "Current terminal: codex"
else
step "Current terminal: $path_line && codex"
fi
step "Future terminals: open a new terminal and run: codex"
case "$path_action" in
added)
step "Current terminal: export PATH=\"$BIN_DIR:\$PATH\" && codex"
step "Future terminals: open a new terminal and run: codex"
step "PATH was added to $path_profile"
;;
updated)
step "Current terminal: export PATH=\"$BIN_DIR:\$PATH\" && codex"
step "Future terminals: open a new terminal and run: codex"
step "PATH was updated in $path_profile"
;;
configured)
step "Current terminal: export PATH=\"$BIN_DIR:\$PATH\" && codex"
step "Future terminals: open a new terminal and run: codex"
step "PATH is already configured in $path_profile"
;;
*)
step "Current terminal: codex"
step "Future terminals: open a new terminal and run: codex"
;;
added) step "PATH was added to $path_profile" ;;
updated) step "PATH was updated in $path_profile" ;;
configured) step "PATH is already configured in $path_profile" ;;
esac
}
@@ -1479,22 +1473,7 @@ if ! prune_standalone_releases; then
fi
release_install_lock
handle_conflicting_install
case "$path_action" in
added)
print_launch_instructions
;;
updated)
print_launch_instructions
;;
configured)
print_launch_instructions
;;
*)
step "$BIN_DIR is already on PATH"
print_launch_instructions
;;
esac
print_launch_instructions
printf 'Codex CLI %s installed successfully.\n' "$resolved_version"
maybe_launch_codex_now
+33 -1
View File
@@ -135,6 +135,37 @@ class InstallShTest(unittest.TestCase):
)
self.assertTrue(os.access(host_path, os.X_OK))
def test_shell_lookup_after_adding_and_updating_profile(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
archive, checksum, metadata = create_package_release(root)
for name in ["literal$HOME", r'quote" back\tslash and !tick`']:
install_dir = root / name
result, _ = run_installer_in(
root,
VERSION,
install_dir=install_dir,
metadata_json=metadata,
archive_path=archive,
checksum_path=checksum,
force_macos=True,
)
self.assertEqual(result.returncode, 0, result.stderr)
env = {"HOME": str(root / "home"), "PATH": "/usr/bin:/bin"}
lookup = subprocess.check_output(
["/bin/sh", "-c", '. "$HOME/.profile" && command -v codex'],
env=env,
text=True,
)
self.assertEqual(lookup.rstrip("\n"), str(install_dir / "codex"))
instructions = result.stdout.split("Current terminal: ", 1)[1]
launched = subprocess.check_output(
["/bin/sh", "-c", instructions.splitlines()[0]], env=env, text=True
)
self.assertEqual(launched.strip(), f"codex-cli {VERSION}")
def test_noninteractive_install_warns_about_existing_npm_codex(self) -> None:
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
@@ -1017,6 +1048,7 @@ def run_installer_in(
release: str,
*,
installer_path: Path = INSTALL_SCRIPT,
install_dir: Path | None = None,
metadata_failure: bool = False,
metadata_json: str | None = None,
releases_metadata_json: str | None = None,
@@ -1163,7 +1195,7 @@ def run_installer_in(
env.update(
{
"CODEX_HOME": str(root / "codex-home"),
"CODEX_INSTALL_DIR": str(root / "install-bin"),
"CODEX_INSTALL_DIR": str(install_dir or root / "install-bin"),
"CODEX_NON_INTERACTIVE": "1",
"CODEX_RELEASE": release,
"CODEX_INSTALL_DAEMON_ONLY": "1" if daemon_only else "0",