| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,8 @@ | |||
| 5 | 5 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php | |
| 6 | 6 | ||
| 7 | 7 | import contextlib | |
| 8 | + import re | ||
| 9 | + | ||
| 8 | 10 | import io | |
| 9 | 11 | import logging | |
| 10 | 12 | import os | |
@@ -130,6 +132,59 @@ def pump_stream(cmdline, name, stream, is_decode, handler): | |||
| 130 | 132 | return finalizer(process) | |
| 131 | 133 | ||
| 132 | 134 | ||
| 135 | + def _safer_popen_windows(command, shell, env=None, **kwargs): | ||
| 136 | + """Call :class:`subprocess.Popen` on Windows but don't include a CWD in the search. | ||
| 137 | + This avoids an untrusted search path condition where a file like ``git.exe`` in a | ||
| 138 | + malicious repository would be run when GitPython operates on the repository. The | ||
| 139 | + process using GitPython may have an untrusted repository's working tree as its | ||
| 140 | + current working directory. Some operations may temporarily change to that directory | ||
| 141 | + before running a subprocess. In addition, while by default GitPython does not run | ||
| 142 | + external commands with a shell, it can be made to do so, in which case the CWD of | ||
| 143 | + the subprocess, which GitPython usually sets to a repository working tree, can | ||
| 144 | + itself be searched automatically by the shell. This wrapper covers all those cases. | ||
| 145 | + :note: This currently works by setting the ``NoDefaultCurrentDirectoryInExePath`` | ||
| 146 | + environment variable during subprocess creation. It also takes care of passing | ||
| 147 | + Windows-specific process creation flags, but that is unrelated to path search. | ||
| 148 | + :note: The current implementation contains a race condition on :attr:`os.environ`. | ||
| 149 | + GitPython isn't thread-safe, but a program using it on one thread should ideally | ||
| 150 | + be able to mutate :attr:`os.environ` on another, without unpredictable results. | ||
| 151 | + See comments in https://github.com/gitpython-developers/GitPython/pull/1650. | ||
| 152 | + """ | ||
| 153 | + # CREATE_NEW_PROCESS_GROUP is needed for some ways of killing it afterwards. See: | ||
| 154 | + # https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal | ||
| 155 | + # https://docs.python.org/3/library/subprocess.html#subprocess.CREATE_NEW_PROCESS_GROUP | ||
| 156 | + creationflags = subprocess.CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP | ||
| 157 | + | ||
| 158 | + # When using a shell, the shell is the direct subprocess, so the variable must be | ||
| 159 | + # set in its environment, to affect its search behavior. (The "1" can be any value.) | ||
| 160 | + if shell: | ||
| 161 | + safer_env = {} if env is None else dict(env) | ||
| 162 | + safer_env["NoDefaultCurrentDirectoryInExePath"] = "1" | ||
| 163 | + else: | ||
| 164 | + safer_env = env | ||
| 165 | + | ||
| 166 | + # When not using a shell, the current process does the search in a CreateProcessW | ||
| 167 | + # API call, so the variable must be set in our environment. With a shell, this is | ||
| 168 | + # unnecessary, in versions where https://github.com/python/cpython/issues/101283 is | ||
| 169 | + # patched. If not, in the rare case the ComSpec environment variable is unset, the | ||
| 170 | + # shell is searched for unsafely. Setting NoDefaultCurrentDirectoryInExePath in all | ||
| 171 | + # cases, as here, is simpler and protects against that. (The "1" can be any value.) | ||
| 172 | + with patch_env("NoDefaultCurrentDirectoryInExePath", "1"): | ||
| 173 | + return Popen( | ||
| 174 | + command, | ||
| 175 | + shell=shell, | ||
| 176 | + env=safer_env, | ||
| 177 | + creationflags=creationflags, | ||
| 178 | + **kwargs | ||
| 179 | + ) | ||
| 180 | + | ||
| 181 | + | ||
| 182 | + if os.name == "nt": | ||
| 183 | + safer_popen = _safer_popen_windows | ||
| 184 | + else: | ||
| 185 | + safer_popen = Popen | ||
| 186 | + | ||
| 187 | + | ||
| 133 | 188 | def dashify(string): | |
| 134 | 189 | return string.replace('_', '-') | |
| 135 | 190 | ||
@@ -150,11 +205,6 @@ def dict_to_slots_and__excluded_are_none(self, d, excluded=()): | |||
| 150 | 205 | # value of Windows process creation flag taken from MSDN | |
| 151 | 206 | CREATE_NO_WINDOW = 0x08000000 | |
| 152 | 207 | ||
| 153 | - ## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards, | ||
| 154 | - # see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal | ||
| 155 | - PROC_CREATIONFLAGS = (CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP | ||
| 156 | - if is_win else 0) | ||
| 157 | - | ||
| 158 | 208 | ||
| 159 | 209 | class Git(LazyMixin): | |
| 160 | 210 | ||
@@ -771,21 +821,18 @@ def execute(self, command, | |||
| 771 | 821 | log.debug("Popen(%s, cwd=%s, universal_newlines=%s, shell=%s, istream=%s)", | |
| 772 | 822 | command, cwd, universal_newlines, shell, istream_ok) | |
| 773 | 823 | try: | |
| 774 | - with patch_caller_env: | ||
| 775 | - proc = Popen( | ||
| 776 | - command, | ||
| 777 | - env=env, | ||
| 778 | - cwd=cwd, | ||
| 779 | - bufsize=-1, | ||
| 780 | - stdin=istream, | ||
| 781 | - stderr=PIPE, | ||
| 782 | - stdout=stdout_sink, | ||
| 783 | - shell=shell is not None and shell or self.USE_SHELL, | ||
| 784 | - close_fds=is_posix, # unsupported on windows | ||
| 785 | - universal_newlines=universal_newlines, | ||
| 786 | - creationflags=PROC_CREATIONFLAGS, | ||
| 787 | - **subprocess_kwargs | ||
| 788 | - ) | ||
| 824 | + proc = safer_popen( | ||
| 825 | + command, | ||
| 826 | + env=env, | ||
| 827 | + cwd=cwd, | ||
| 828 | + bufsize=-1, | ||
| 829 | + stdin=istream, | ||
| 830 | + stderr=PIPE, | ||
| 831 | + stdout=stdout_sink, | ||
| 832 | + shell=shell is not None and shell or self.USE_SHELL, | ||
| 833 | + universal_newlines=universal_newlines, | ||
| 834 | + **subprocess_kwargs | ||
| 835 | + ) | ||
| 789 | 836 | except cmd_not_found_exception as err: | |
| 790 | 837 | raise GitCommandNotFound(command, err) | |
| 791 | 838 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ | |||
| 13 | 13 | ) | |
| 14 | 14 | import subprocess | |
| 15 | 15 | ||
| 16 | - from git.cmd import PROC_CREATIONFLAGS, handle_process_output | ||
| 16 | + from git.cmd import handle_process_output, safer_popen | ||
| 17 | 17 | from git.compat import ( | |
| 18 | 18 | PY3, | |
| 19 | 19 | defenc, | |
@@ -73,15 +73,12 @@ def run_commit_hook(name, index, *args): | |||
| 73 | 73 | env["GIT_INDEX_FILE"] = safe_decode(index.path) if PY3 else safe_encode(index.path) | |
| 74 | 74 | env["GIT_EDITOR"] = ":" | |
| 75 | 75 | try: | |
| 76 | - cmd = subprocess.Popen( | ||
| 77 | - [hp] + list(args), | ||
| 78 | - env=env, | ||
| 79 | - stdout=subprocess.PIPE, | ||
| 80 | - stderr=subprocess.PIPE, | ||
| 81 | - cwd=index.repo.working_dir, | ||
| 82 | - close_fds=is_posix, | ||
| 83 | - creationflags=PROC_CREATIONFLAGS, | ||
| 84 | - ) | ||
| 76 | + cmd = safer_popen([hp] + list(args), | ||
| 77 | + env=env, | ||
| 78 | + stdout=subprocess.PIPE, | ||
| 79 | + stderr=subprocess.PIPE, | ||
| 80 | + cwd=index.repo.working_dir, | ||
| 81 | + close_fds=is_posix) | ||
| 85 | 82 | except Exception as ex: | |
| 86 | 83 | raise HookExecutionError(hp, ex) | |
| 87 | 84 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -911,7 +911,7 @@ def push( | |||
| 911 | 911 | be null.""" | |
| 912 | 912 | kwargs = add_progress(kwargs, self.repo.git, progress) | |
| 913 | 913 | ||
| 914 | - refspec = Git._unpack_args(refspec or []) | ||
| 914 | + refspec = Git._Git__unpack_args(refspec or []) | ||
| 915 | 915 | if not allow_unsafe_protocols: | |
| 916 | 916 | for ref in refspec: | |
| 917 | 917 | Git.check_unsafe_protocols(ref) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ | |||
| 16 | 16 | import textwrap | |
| 17 | 17 | import time | |
| 18 | 18 | import unittest | |
| 19 | + import virtualenv | ||
| 19 | 20 | ||
| 20 | 21 | from git.compat import string_types, is_win | |
| 21 | 22 | from git.util import rmtree, cwd | |
@@ -36,7 +37,8 @@ | |||
| 36 | 37 | __all__ = ( | |
| 37 | 38 | 'fixture_path', 'fixture', 'StringProcessAdapter', | |
| 38 | 39 | 'with_rw_directory', 'with_rw_repo', 'with_rw_and_rw_remote_repo', | |
| 39 | - 'TestBase', 'TestCase', | ||
| 40 | + 'TestBase', 'VirtualEnvironment', | ||
| 41 | + 'TestCase', | ||
| 40 | 42 | 'SkipTest', 'skipIf', | |
| 41 | 43 | 'GIT_REPO', 'GIT_DAEMON_PORT' | |
| 42 | 44 | ) | |
@@ -83,13 +85,13 @@ def with_rw_directory(func): | |||
| 83 | 85 | test succeeds, but leave it otherwise to aid additional debugging""" | |
| 84 | 86 | ||
| 85 | 87 | @wraps(func) | |
| 86 | - def wrapper(self): | ||
| 88 | + def wrapper(self, *args, **kwargs): | ||
| 87 | 89 | path = tempfile.mktemp(prefix=func.__name__) | |
| 88 | 90 | os.mkdir(path) | |
| 89 | 91 | keep = False | |
| 90 | 92 | try: | |
| 91 | 93 | try: | |
| 92 | - return func(self, path) | ||
| 94 | + return func(self, path, *args, **kwargs) | ||
| 93 | 95 | except Exception: | |
| 94 | 96 | log.info("Test %s.%s failed, output is at %r\n", | |
| 95 | 97 | type(self).__name__, func.__name__, path) | |
@@ -379,3 +381,49 @@ def _make_file(self, rela_path, data, repo=None): | |||
| 379 | 381 | with open(abs_path, "w") as fp: | |
| 380 | 382 | fp.write(data) | |
| 381 | 383 | return abs_path | |
| 384 | + | ||
| 385 | + | ||
| 386 | + class VirtualEnvironment: | ||
| 387 | + """A newly created Python virtual environment for use in a test.""" | ||
| 388 | + | ||
| 389 | + __slots__ = ("_env_dir",) | ||
| 390 | + | ||
| 391 | + def __init__(self, env_dir, with_pip): | ||
| 392 | + # On Python2 virtualenv the pip option and symlinks options aren't available | ||
| 393 | + if os.name == "nt": | ||
| 394 | + self._env_dir = osp.realpath(env_dir) | ||
| 395 | + # venv.create(self.env_dir, symlinks=False, with_pip=with_pip) | ||
| 396 | + virtualenv.cli_run([self.env_dir]) | ||
| 397 | + else: | ||
| 398 | + self._env_dir = env_dir | ||
| 399 | + # venv.create(self.env_dir, symlinks=True, with_pip=with_pip) | ||
| 400 | + virtualenv.cli_run([self.env_dir]) | ||
| 401 | + | ||
| 402 | + @property | ||
| 403 | + def env_dir(self): | ||
| 404 | + """The top-level directory of the environment.""" | ||
| 405 | + return self._env_dir | ||
| 406 | + | ||
| 407 | + @property | ||
| 408 | + def python(self): | ||
| 409 | + """Path to the Python executable in the environment.""" | ||
| 410 | + return self._executable("python") | ||
| 411 | + | ||
| 412 | + @property | ||
| 413 | + def pip(self): | ||
| 414 | + """Path to the pip executable in the environment, or RuntimeError if absent.""" | ||
| 415 | + return self._executable("pip") | ||
| 416 | + | ||
| 417 | + @property | ||
| 418 | + def sources(self): | ||
| 419 | + """Path to a src directory in the environment, which may not exist yet.""" | ||
| 420 | + return os.path.join(self.env_dir, "src") | ||
| 421 | + | ||
| 422 | + def _executable(self, basename): | ||
| 423 | + if os.name == "nt": | ||
| 424 | + path = osp.join(self.env_dir, "Scripts", basename + ".exe") | ||
| 425 | + else: | ||
| 426 | + path = osp.join(self.env_dir, "bin", basename) | ||
| 427 | + if osp.isfile(path) or osp.islink(path): | ||
| 428 | + return path | ||
| 429 | + raise RuntimeError("no regular file or symlink " + str(path)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,6 +44,11 @@ | |||
| 44 | 44 | except ImportError: | |
| 45 | 45 | import mock | |
| 46 | 46 | ||
| 47 | + try: | ||
| 48 | + from pathlib import Path | ||
| 49 | + except ImportError: | ||
| 50 | + from pathlib2 import Path | ||
| 51 | + | ||
| 47 | 52 | from git.compat import is_win | |
| 48 | 53 | ||
| 49 | 54 | @contextlib.contextmanager | |
@@ -57,6 +62,22 @@ def _chdir(new_dir): | |||
| 57 | 62 | os.chdir(old_dir) | |
| 58 | 63 | ||
| 59 | 64 | ||
| 65 | + @contextlib.contextmanager | ||
| 66 | + def _patch_out_env(name): | ||
| 67 | + try: | ||
| 68 | + old_value = os.environ[name] | ||
| 69 | + except KeyError: | ||
| 70 | + old_value = None | ||
| 71 | + else: | ||
| 72 | + del os.environ[name] | ||
| 73 | + try: | ||
| 74 | + yield | ||
| 75 | + finally: | ||
| 76 | + if old_value is not None: | ||
| 77 | + os.environ[name] = old_value | ||
| 78 | + | ||
| 79 | + | ||
| 80 | + | ||
| 60 | 81 | class TestGit(TestBase): | |
| 61 | 82 | ||
| 62 | 83 | @classmethod | |
@@ -321,7 +342,7 @@ def counter_stderr(line): | |||
| 321 | 342 | stdout=subprocess.PIPE, | |
| 322 | 343 | stderr=subprocess.PIPE, | |
| 323 | 344 | shell=False, | |
| 324 | - creationflags=cmd.PROC_CREATIONFLAGS, | ||
| 345 | + # creationflags=cmd.PROC_CREATIONFLAGS, | ||
| 325 | 346 | ) | |
| 326 | 347 | ||
| 327 | 348 | handle_process_output(proc, counter_stdout, counter_stderr, finalize_process) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -262,6 +262,9 @@ def test_clone_from_pathlib_withConfig(self, rw_dir): | |||
| 262 | 262 | ||
| 263 | 263 | @with_rw_repo("HEAD") | |
| 264 | 264 | def test_clone_unsafe_options(self, rw_repo): | |
| 265 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 266 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 267 | + | ||
| 265 | 268 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 266 | 269 | tmp_file = tmp_dir / "pwn" | |
| 267 | 270 | unsafe_options = [ | |
@@ -321,6 +324,9 @@ def test_clone_unsafe_options(self, rw_repo): | |||
| 321 | 324 | ||
| 322 | 325 | @with_rw_repo("HEAD") | |
| 323 | 326 | def test_clone_safe_options(self, rw_repo): | |
| 327 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 328 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 329 | + | ||
| 324 | 330 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 325 | 331 | options = [ | |
| 326 | 332 | "--depth=1", | |
@@ -335,6 +341,9 @@ def test_clone_safe_options(self, rw_repo): | |||
| 335 | 341 | ||
| 336 | 342 | @with_rw_repo("HEAD") | |
| 337 | 343 | def test_clone_from_unsafe_options(self, rw_repo): | |
| 344 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 345 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 346 | + | ||
| 338 | 347 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 339 | 348 | tmp_file = tmp_dir / "pwn" | |
| 340 | 349 | unsafe_options = [ | |
@@ -400,6 +409,9 @@ def test_clone_from_unsafe_options(self, rw_repo): | |||
| 400 | 409 | ||
| 401 | 410 | @with_rw_repo("HEAD") | |
| 402 | 411 | def test_clone_from_safe_options(self, rw_repo): | |
| 412 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 413 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 414 | + | ||
| 403 | 415 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 404 | 416 | options = [ | |
| 405 | 417 | "--depth=1", | |
@@ -413,6 +425,9 @@ def test_clone_from_safe_options(self, rw_repo): | |||
| 413 | 425 | assert destination.exists() | |
| 414 | 426 | ||
| 415 | 427 | def test_clone_from_unsafe_procol(self): | |
| 428 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 429 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 430 | + | ||
| 416 | 431 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 417 | 432 | tmp_file = tmp_dir / "pwn" | |
| 418 | 433 | urls = [ | |
@@ -425,6 +440,9 @@ def test_clone_from_unsafe_procol(self): | |||
| 425 | 440 | assert not tmp_file.exists() | |
| 426 | 441 | ||
| 427 | 442 | def test_clone_from_unsafe_procol_allowed(self): | |
| 443 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 444 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 445 | + | ||
| 428 | 446 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 429 | 447 | tmp_file = tmp_dir / "pwn" | |
| 430 | 448 | urls = [ | |
@@ -1286,6 +1304,9 @@ def test_git_work_tree_env(self, rw_dir): | |||
| 1286 | 1304 | ||
| 1287 | 1305 | @with_rw_repo("HEAD") | |
| 1288 | 1306 | def test_clone_command_injection(self, rw_repo): | |
| 1307 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 1308 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 1309 | + | ||
| 1289 | 1310 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 1290 | 1311 | unexpected_file = tmp_dir / "pwn" | |
| 1291 | 1312 | assert not unexpected_file.exists() | |
@@ -1297,6 +1318,9 @@ def test_clone_command_injection(self, rw_repo): | |||
| 1297 | 1318 | ||
| 1298 | 1319 | @with_rw_repo("HEAD") | |
| 1299 | 1320 | def test_clone_from_command_injection(self, rw_repo): | |
| 1321 | + if pathlib is None: # pythons bellow 3.4 don't have pathlib | ||
| 1322 | + raise SkipTest("pathlib was introduced in 3.4") | ||
| 1323 | + | ||
| 1300 | 1324 | tmp_dir = pathlib.Path(tempfile.mkdtemp()) | |
| 1301 | 1325 | temp_repo = Repo.init(tmp_dir / "repo") | |
| 1302 | 1326 | unexpected_file = tmp_dir / "pwn" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,6 +192,17 @@ def _get_exe_extensions(): | |||
| 192 | 192 | ||
| 193 | 193 | ||
| 194 | 194 | def py_where(program, path=None): | |
| 195 | + """Perform a path search to assist :func:`is_cygwin_git`. | ||
| 196 | + This is not robust for general use. It is an implementation detail of | ||
| 197 | + :func:`is_cygwin_git`. When a search following all shell rules is needed, | ||
| 198 | + :func:`shutil.which` can be used instead. | ||
| 199 | + :note: Neither this function nor :func:`shutil.which` will predict the effect of an | ||
| 200 | + executable search on a native Windows system due to a :class:`subprocess.Popen` | ||
| 201 | + call without ``shell=True``, because shell and non-shell executable search on | ||
| 202 | + Windows differ considerably. | ||
| 203 | + """ | ||
| 204 | + | ||
| 205 | + | ||
| 195 | 206 | # From: http://stackoverflow.com/a/377028/548792 | |
| 196 | 207 | winprog_exts = _get_exe_extensions() | |
| 197 | 208 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,5 +4,9 @@ coverage | |||
| 4 | 4 | flake8 | |
| 5 | 5 | nose | |
| 6 | 6 | tox | |
| 7 | + mock; python_version=='2.7' | ||
| 8 | + pathlib2; python_version=='2.7' | ||
| 7 | 9 | backports.tempfile | |
| 8 | 10 | six | |
| 11 | + virtualenv | ||
| 12 | + | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments