Skip to content

Fix two remaining Windows untrusted search path cases #1792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make Git.execute a bit simpler and very slightly more robust
This covers the rare unpatched python/cpython#101283 case the
previous commit added tests for, that only applies in the unusual
situation that the ComSpec environment variable is unset and an old
build (but this includes downloadable builds and current
actions/setup-python builds) of Python <=3.9 for Windows is in use.

The main benefit of this change is really to slightly simplify the
code under test. (It might even be justified to remove the
use_shell_impostor=True test cases at some point.)
  • Loading branch information
EliahKagan committed Jan 9, 2024
commit d2506c73b6ad59bff1f2f58c4edd15f717012591
19 changes: 8 additions & 11 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,25 +988,22 @@ def execute(
if shell is None:
shell = self.USE_SHELL

cmd_not_found_exception = FileNotFoundError
maybe_patch_caller_env = contextlib.nullcontext()

if os.name == "nt":
cmd_not_found_exception = OSError
if kill_after_timeout is not None:
raise GitCommandError(
redacted_command,
'"kill_after_timeout" feature is not supported on Windows.',
)

cmd_not_found_exception = OSError

# Search PATH, but do not search CWD. The "1" can be any value.
# Search PATH but not CWD. The "1" can be any value. We'll patch just before
# the Popen call and unpatch just after, or we get a worse race condition.
maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1")
if shell:
# If the direct subprocess is a shell, this must go in its environment.
# Modify the direct shell subprocess's own search behavior accordingly.
env["NoDefaultCurrentDirectoryInExePath"] = "1"
else:
# If we're not using a shell, the variable goes in our own environment.
maybe_patch_caller_env = patch_env("NoDefaultCurrentDirectoryInExePath", "1")
else:
cmd_not_found_exception = FileNotFoundError
maybe_patch_caller_env = contextlib.nullcontext()
# END handle

stdout_sink = PIPE if with_stdout else getattr(subprocess, "DEVNULL", None) or open(os.devnull, "wb")
Expand Down