Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
12 changes: 12 additions & 0 deletions git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class IndexFile(LazyMixin, git_diff.Diffable, Serializable):
index directly before operating on it using the git command.
"""

unsafe_git_checkout_index_options = ["--prefix"]

__slots__ = ("repo", "version", "entries", "_extension_data", "_file_path")

_VERSION = 2
Expand Down Expand Up @@ -1212,6 +1214,7 @@ def checkout(
paths: Union[None, Iterable[PathLike]] = None,
force: bool = False,
fprogress: Callable = lambda *args: None,
allow_unsafe_options: bool = False,
**kwargs: Any,
) -> Union[None, Iterator[PathLike], Sequence[PathLike]]:
"""Check out the given paths or all files from the version known to the index
Expand All @@ -1238,6 +1241,9 @@ def checkout(
no explicit paths are given. Otherwise progress information will be send
prior and after a file has been checked out.

:param allow_unsafe_options:
Allow unsafe options, such as ``--prefix``.

:param kwargs:
Additional arguments to be passed to :manpage:`git-checkout-index(1)`.

Expand All @@ -1261,6 +1267,12 @@ def checkout(
i.e. if you want :manpage:`git-checkout(1)`-like behaviour, use
``head.checkout`` instead of ``index.checkout``.
"""
if not allow_unsafe_options:
Git.check_unsafe_options(
options=Git._option_candidates([], kwargs),
unsafe_options=self.unsafe_git_checkout_index_options,
)

args = ["--index"]
if force:
args.append("--force")
Expand Down
13 changes: 13 additions & 0 deletions git/refs/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from typing import Any, TYPE_CHECKING, Type, Union

from git.cmd import Git
from git.types import AnyGitObject, PathLike

if TYPE_CHECKING:
Expand All @@ -42,6 +43,8 @@ class TagReference(Reference):

__slots__ = ()

unsafe_git_tag_options = ["--file", "-F"]

_common_default = "tags"
_common_path_default = Reference._common_path_default + "/" + _common_default

Expand Down Expand Up @@ -92,6 +95,7 @@ def create(
reference: Union[str, "SymbolicReference"] = "HEAD",
logmsg: Union[str, None] = None,
force: bool = False,
allow_unsafe_options: bool = False,
**kwargs: Any,
) -> "TagReference":
"""Create a new tag reference.
Expand Down Expand Up @@ -121,12 +125,21 @@ def create(
:param force:
If ``True``, force creation of a tag even though that tag already exists.

:param allow_unsafe_options:
Allow unsafe options, such as ``--file``.

:param kwargs:
Additional keyword arguments to be passed to :manpage:`git-tag(1)`.

:return:
A new :class:`TagReference`.
"""
if not allow_unsafe_options:
Git.check_unsafe_options(
options=Git._option_candidates([], kwargs),
unsafe_options=cls.unsafe_git_tag_options,
)

if "ref" in kwargs and kwargs["ref"]:
reference = kwargs["ref"]

Expand Down
6 changes: 6 additions & 0 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class Repo:
"-c",
# Can install hooks that execute during clone:
"--template",
# Fetches from a caller-controlled URL:
"--bundle-uri",
]
"""Options to :manpage:`git-clone(1)` that allow arbitrary commands to be executed.
Comment thread
Byron marked this conversation as resolved.
Outdated

Expand All @@ -172,6 +174,10 @@ class Repo:
# Writes output to a caller-controlled filesystem path.
"--output",
"-o",
# Reads from a caller-controlled filesystem path:
"--add-file",
# Injects a caller-controlled path and contents:
"--add-virtual-file",
]

unsafe_git_revision_options = [
Expand Down
2 changes: 2 additions & 0 deletions test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_clone_unsafe_options(self, rw_repo):
"-cprotocol.ext.allow=always",
"-vcprotocol.ext.allow=always",
f"--template={tmp_dir}",
f"--bundle-uri=file://{tmp_dir}",
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand All @@ -147,6 +148,7 @@ def test_clone_unsafe_options(self, rw_repo):
{"conf": "protocol.ext.allow=always"},
{"c": "protocol.ext.allow=always"},
{"template": tmp_dir},
{"bundle_uri": f"file://{tmp_dir}"},
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand Down
10 changes: 10 additions & 0 deletions test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
HookExecutionError,
InvalidGitRepositoryError,
UnmergedEntriesError,
UnsafeOptionError,
)
from git.index.fun import hook_path, run_commit_hook
from git.index.typ import BaseIndexEntry, IndexEntry
Expand Down Expand Up @@ -202,6 +203,15 @@ def _make_hook(git_dir, name, content, make_exec=True):

@ddt.ddt
class TestIndex(TestBase):
@with_rw_repo("HEAD")
def test_checkout_rejects_unsafe_prefix(self, rw_repo):
with tempfile.TemporaryDirectory() as target:
with self.assertRaises(UnsafeOptionError):
rw_repo.index.checkout(prefix=f"{target}/")

rw_repo.index.checkout(prefix=f"{target}/", allow_unsafe_options=True)
self.assertTrue(osp.isfile(osp.join(target, "CHANGES")))

def __init__(self, *args):
super().__init__(*args)
self._reset_progress()
Expand Down
13 changes: 13 additions & 0 deletions test/test_refs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
SymbolicReference,
TagReference,
)
from git.exc import UnsafeOptionError
from git.objects.tag import TagObject
import git.refs as refs
from git.util import Actor
Expand Down Expand Up @@ -60,6 +61,18 @@ def test_from_path(self):
# Check remoteness
assert Reference(self.rorepo, "refs/remotes/origin").is_remote()

@with_rw_repo("HEAD")
def test_tag_create_rejects_unsafe_file_options(self, rw_repo):
with tempfile.NamedTemporaryFile("w", encoding="utf-8") as message:
message.write("private tag message")
message.flush()
for index, option in enumerate(({"F": message.name}, {"file": message.name})):
with self.assertRaises(UnsafeOptionError):
TagReference.create(rw_repo, f"unsafe-{index}", **option)

tag = TagReference.create(rw_repo, "allowed-file", F=message.name, allow_unsafe_options=True)
self.assertEqual(tag.tag.message, "private tag message")

Comment thread
Byron marked this conversation as resolved.
def test_from_pathlike(self):
# Should be able to create any reference directly.
for ref_type in (Reference, Head, TagReference, RemoteReference):
Expand Down
4 changes: 4 additions & 0 deletions test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ def test_archive_rejects_unsafe_options(self):
with self.assertRaises(UnsafeOptionError):
self.rorepo.archive(io.BytesIO(), "0.1.6", output=output_marker)
assert not osp.exists(output_marker)
with self.assertRaises(UnsafeOptionError):
self.rorepo.archive(io.BytesIO(), "0.1.6", add_file=output_marker)
with self.assertRaises(UnsafeOptionError):
self.rorepo.archive(io.BytesIO(), "0.1.6", add_virtual_file="file:content")

def test_archive_rejects_unsafe_remote_protocol(self):
with tempfile.TemporaryDirectory() as tdir:
Expand Down
Loading