-
-
Notifications
You must be signed in to change notification settings - Fork 942
safe mode to disable executing any external programs except git #2029
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
Open
eighthave
wants to merge
1
commit into
gitpython-developers:main
Choose a base branch
from
eighthave:safe-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
CommandError, | ||
GitCommandError, | ||
GitCommandNotFound, | ||
UnsafeExecutionError, | ||
UnsafeOptionError, | ||
UnsafeProtocolError, | ||
) | ||
|
@@ -627,6 +628,7 @@ class Git(metaclass=_GitMeta): | |
|
||
__slots__ = ( | ||
"_working_dir", | ||
"_safe", | ||
"cat_file_all", | ||
"cat_file_header", | ||
"_version_info", | ||
|
@@ -961,17 +963,56 @@ def check_unsafe_options(cls, options: List[str], unsafe_options: List[str]) -> | |
|
||
CatFileContentStream: TypeAlias = _CatFileContentStream | ||
|
||
def __init__(self, working_dir: Union[None, PathLike] = None) -> None: | ||
def __init__(self, working_dir: Union[None, PathLike] = None, safe: bool = False) -> None: | ||
"""Initialize this instance with: | ||
|
||
:param working_dir: | ||
Git directory we should work in. If ``None``, we always work in the current | ||
directory as returned by :func:`os.getcwd`. | ||
This is meant to be the working tree directory if available, or the | ||
``.git`` directory in case of bare repositories. | ||
|
||
:param safe: | ||
Lock down the configuration to make it as safe as possible | ||
when working with publicly accessible, untrusted | ||
repositories. This disables all known options that can run | ||
external programs and limits networking to the HTTP protocol | ||
via ``https://`` URLs. This might not cover Git config | ||
options that were added since this was implemented, or | ||
options that have unknown exploit vectors. It is a best | ||
effort defense rather than an exhaustive protection measure. | ||
|
||
In order to make this more likely to work with submodules, | ||
some attempts are made to rewrite remote URLs to ``https://`` | ||
using `insteadOf` in the config. This might not work on all | ||
projects, so submodules should always use ``https://`` URLs. | ||
|
||
:envvar:`GIT_TERMINAL_PROMPT` is set to `false` and these | ||
environment variables are forced to `/bin/true`: | ||
:envvar:`GIT_ASKPASS`, :envvar:`GIT_EDITOR`, | ||
:envvar:`GIT_PAGER`, :envvar:`GIT_SSH`, | ||
:envvar:`GIT_SSH_COMMAND`, and :envvar:`SSH_ASKPASS`. | ||
|
||
Git config options are supplied via the command line to set | ||
up key parts of safe mode. | ||
|
||
- Direct options for executing external commands are set to ``/bin/true``: | ||
``core.askpass``, ``core.sshCommand`` and ``credential.helper``. | ||
|
||
- External password prompts are disabled by skipping authentication using | ||
``http.emptyAuth=true``. | ||
|
||
- Any use of an fsmonitor daemon is disabled using ``core.fsmonitor=false``. | ||
|
||
- Hook scripts are disabled using ``core.hooksPath=/dev/null``. | ||
|
||
It was not possible to cover all config items that might execute an external | ||
command, for example, ``receive.procReceiveRefs``, | ||
``uploadpack.packObjectsHook`` and ``remote.<name>.vcs``. | ||
""" | ||
super().__init__() | ||
self._working_dir = expand_path(working_dir) | ||
self._safe = safe | ||
self._git_options: Union[List[str], Tuple[str, ...]] = () | ||
self._persistent_git_options: List[str] = [] | ||
|
||
|
@@ -1218,6 +1259,8 @@ def execute( | |
|
||
:raise git.exc.GitCommandError: | ||
|
||
:raise git.exc.UnsafeExecutionError: | ||
|
||
:note: | ||
If you add additional keyword arguments to the signature of this method, you | ||
must update the ``execute_kwargs`` variable housed in this module. | ||
|
@@ -1227,6 +1270,64 @@ def execute( | |
if self.GIT_PYTHON_TRACE and (self.GIT_PYTHON_TRACE != "full" or as_process): | ||
_logger.info(" ".join(redacted_command)) | ||
|
||
if shell is None: | ||
# Get the value of USE_SHELL with no deprecation warning. Do this without | ||
# warnings.catch_warnings, to avoid a race condition with application code | ||
# configuring warnings. The value could be looked up in type(self).__dict__ | ||
# or Git.__dict__, but those can break under some circumstances. This works | ||
# the same as self.USE_SHELL in more situations; see Git.__getattribute__. | ||
shell = super().__getattribute__("USE_SHELL") | ||
|
||
if self._safe: | ||
if shell: | ||
raise UnsafeExecutionError( | ||
redacted_command, | ||
"Command cannot be executed in a shell when in safe mode.", | ||
) | ||
if not isinstance(command, Sequence): | ||
raise UnsafeExecutionError( | ||
redacted_command, | ||
"Command must be a Sequence to be executed in safe mode.", | ||
) | ||
if command[0] != self.GIT_PYTHON_GIT_EXECUTABLE: | ||
raise UnsafeExecutionError( | ||
redacted_command, | ||
f'Only "{self.GIT_PYTHON_GIT_EXECUTABLE}" can be executed when in safe mode.', | ||
) | ||
config_args = [ | ||
"-c", | ||
"core.askpass=/bin/true", | ||
"-c", | ||
"core.fsmonitor=false", | ||
"-c", | ||
"core.hooksPath=/dev/null", | ||
"-c", | ||
"core.sshCommand=/bin/true", | ||
"-c", | ||
"credential.helper=/bin/true", | ||
"-c", | ||
"http.emptyAuth=true", | ||
"-c", | ||
"protocol.allow=never", | ||
"-c", | ||
"protocol.https.allow=always", | ||
"-c", | ||
"url.https://bitbucket.org/[email protected]:", | ||
"-c", | ||
"url.https://codeberg.org/[email protected]:", | ||
"-c", | ||
"url.https://github.com/[email protected]:", | ||
"-c", | ||
"url.https://gitlab.com/[email protected]:", | ||
"-c", | ||
"url.https://.insteadOf=git://", | ||
"-c", | ||
"url.https://.insteadOf=http://", | ||
"-c", | ||
"url.https://.insteadOf=ssh://", | ||
] | ||
command = [command.pop(0)] + config_args + command | ||
|
||
# Allow the user to have the command executed in their working dir. | ||
try: | ||
cwd = self._working_dir or os.getcwd() # type: Union[None, str] | ||
|
@@ -1244,6 +1345,15 @@ def execute( | |
# just to be sure. | ||
env["LANGUAGE"] = "C" | ||
env["LC_ALL"] = "C" | ||
# Globally disable things that can execute commands, including password prompts. | ||
if self._safe: | ||
env["GIT_ASKPASS"] = "/bin/true" | ||
env["GIT_EDITOR"] = "/bin/true" | ||
env["GIT_PAGER"] = "/bin/true" | ||
env["GIT_SSH"] = "/bin/true" | ||
env["GIT_SSH_COMMAND"] = "/bin/true" | ||
env["GIT_TERMINAL_PROMPT"] = "false" | ||
env["SSH_ASKPASS"] = "/bin/true" | ||
env.update(self._environment) | ||
if inline_env is not None: | ||
env.update(inline_env) | ||
|
@@ -1260,13 +1370,6 @@ def execute( | |
# END handle | ||
|
||
stdout_sink = PIPE if with_stdout else getattr(subprocess, "DEVNULL", None) or open(os.devnull, "wb") | ||
if shell is None: | ||
# Get the value of USE_SHELL with no deprecation warning. Do this without | ||
# warnings.catch_warnings, to avoid a race condition with application code | ||
# configuring warnings. The value could be looked up in type(self).__dict__ | ||
# or Git.__dict__, but those can break under some circumstances. This works | ||
# the same as self.USE_SHELL in more situations; see Git.__getattribute__. | ||
shell = super().__getattribute__("USE_SHELL") | ||
_logger.debug( | ||
"Popen(%s, cwd=%s, stdin=%s, shell=%s, universal_newlines=%s)", | ||
redacted_command, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.