Skip to content

Commit 830471c

Browse files
authored
Merge pull request #2147 from EliahKagan/claude/execute-string-platform-docs
Rewrite Git.execute() command parameter docstring per #2146
2 parents 7b83f7a + 38d62c4 commit 830471c

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

git/cmd.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,16 +1131,28 @@ def execute(
11311131
information (stdout).
11321132
11331133
:param command:
1134-
The command to execute. A sequence of program arguments is the
1135-
recommended form when `shell` is ``False`` (the default), e.g.
1136-
``["git", "log", "-n", "1"]``.
1137-
1138-
A string is accepted, but with `shell` set to ``False`` it is passed
1139-
as a single executable name to :class:`subprocess.Popen`. For example,
1140-
``"git log -n 1"`` looks for an executable literally named
1141-
``git log -n 1`` and will fail with :class:`GitCommandNotFound`. To
1142-
split a command string into argv tokens, pass ``shlex.split(...)`` as
1143-
a sequence or set `shell` to ``True`` (see the warning below).
1134+
The command to execute. A sequence of program arguments is recommended.
1135+
A string is also accepted, but its meaning is strongly platform-dependent.
1136+
1137+
By default, a shell is not used. On Unix-like systems, a string is the whole
1138+
program name (so ``"git log -n 1"`` raises :class:`GitCommandNotFound`). On
1139+
Windows, the program parses the arguments itself, so multi-word strings can
1140+
work but are not portable.
1141+
1142+
Avoid ``shell=True`` (and :attr:`Git.USE_SHELL`): this runs the command in
1143+
a shell, which is generally unsafe. The shell interprets metacharacters
1144+
such as ``;``, ``|``, ``&``, ``$(...)``, ``$VAR``, ``%VAR%``, and ``^``
1145+
(depending on the platform) as syntax. Any untrusted text in the command
1146+
can then execute arbitrary OS commands. See :attr:`Git.USE_SHELL`.
1147+
1148+
Producing a sequence automatically by :func:`shlex.split` and passing it
1149+
as the command is far safer than ``shell=True``. But :func:`shlex.split`
1150+
parses POSIX shell syntax on all systems, and the result is still unsafe
1151+
for anything but *fixed, fully trusted* strings. Do not use it on strings
1152+
built by interpolating values: whitespace or quoting in an untrusted value
1153+
can still inject arguments. For input derived in any way from untrusted
1154+
data, build the argument sequence yourself, while ensuring each argument
1155+
is fully sanitized.
11441156
11451157
:param istream:
11461158
Standard input filehandle passed to :class:`subprocess.Popen`.
@@ -1208,6 +1220,11 @@ def execute(
12081220
needed (nor useful) to work around any known operating system specific
12091221
issues.
12101222
1223+
On Unix-like systems, when migrating away from passing string commands with
1224+
``shell=True``, :func:`shlex.split` may serve as a transitional step in rare
1225+
cases, with extreme care. (Drop ``shell=True`` and pass the resulting
1226+
sequence as the command.) See the `command` parameter above on the risks.
1227+
12111228
:param env:
12121229
A dictionary of environment variables to be passed to
12131230
:class:`subprocess.Popen`.

0 commit comments

Comments
 (0)