@@ -131,6 +131,9 @@ class Repo:
131131git_dir :PathLike
132132"""The ``.git`` repository directory."""
133133
134+ safe :None
135+ """Whether this is operating using restricted protocol and execution access."""
136+
134137_common_dir :PathLike = ""
135138
136139# Precompiled regex
@@ -175,6 +178,7 @@ def __init__(
175178odbt :Type [LooseObjectDB ]= GitCmdObjectDB ,
176179search_parent_directories :bool = False ,
177180expand_vars :bool = True ,
181+ safe :bool = False ,
178182 )-> None :
179183R"""Create a new :class:`Repo` instance.
180184
@@ -204,6 +208,11 @@ def __init__(
204208 Please note that this was the default behaviour in older versions of
205209 GitPython, which is considered a bug though.
206210
211+ :param safe:
212+ Lock down the configuration to make it as safe as possible
213+ when working with publicly accessible, untrusted
214+ repositories.
215+
207216 :raise git.exc.InvalidGitRepositoryError:
208217
209218 :raise git.exc.NoSuchPathError:
@@ -235,6 +244,8 @@ def __init__(
235244if not os .path .exists (epath ):
236245raise NoSuchPathError (epath )
237246
247+ self .safe = safe
248+
238249# Walk up the path to find the `.git` dir.
239250curpath = epath
240251git_dir = None
@@ -289,6 +300,8 @@ def __init__(
289300raise InvalidGitRepositoryError (epath )
290301self .git_dir = git_dir
291302
303+ self .safe = safe
304+
292305self ._bare = False
293306try :
294307self ._bare = self .config_reader ("repository" ).getboolean ("core" ,"bare" )
@@ -309,7 +322,7 @@ def __init__(
309322# END working dir handling
310323
311324self .working_dir :PathLike = self ._working_tree_dir or self .common_dir
312- self .git = self .GitCommandWrapperType (self .working_dir )
325+ self .git = self .GitCommandWrapperType (self .working_dir , safe )
313326
314327# Special handling, in special times.
315328rootpath = osp .join (self .common_dir ,"objects" )
@@ -1305,6 +1318,7 @@ def init(
13051318mkdir :bool = True ,
13061319odbt :Type [GitCmdObjectDB ]= GitCmdObjectDB ,
13071320expand_vars :bool = True ,
1321+ safe :bool = False ,
13081322** kwargs :Any ,
13091323 )-> "Repo" :
13101324"""Initialize a git repository at the given path if specified.
@@ -1329,6 +1343,8 @@ def init(
13291343 information disclosure, allowing attackers to access the contents of
13301344 environment variables.
13311345
1346+ TODO :param safe:
1347+
13321348 :param kwargs:
13331349 Keyword arguments serving as additional options to the
13341350 :manpage:`git-init(1)` command.
@@ -1342,9 +1358,9 @@ def init(
13421358os .makedirs (path ,0o755 )
13431359
13441360# git command automatically chdir into the directory
1345- git = cls .GitCommandWrapperType (path )
1361+ git = cls .GitCommandWrapperType (path , safe )
13461362git .init (** kwargs )
1347- return cls (path ,odbt = odbt )
1363+ return cls (path ,odbt = odbt , safe = safe )
13481364
13491365@classmethod
13501366def _clone (
@@ -1357,6 +1373,7 @@ def _clone(
13571373multi_options :Optional [List [str ]]= None ,
13581374allow_unsafe_protocols :bool = False ,
13591375allow_unsafe_options :bool = False ,
1376+ safe :bool = False ,
13601377** kwargs :Any ,
13611378 )-> "Repo" :
13621379odbt = kwargs .pop ("odbt" ,odb_default_type )
@@ -1418,7 +1435,7 @@ def _clone(
14181435if not osp .isabs (path ):
14191436path = osp .join (git ._working_dir ,path )if git ._working_dir is not None else path
14201437
1421- repo = cls (path ,odbt = odbt )
1438+ repo = cls (path ,odbt = odbt , safe = safe )
14221439
14231440# Retain env values that were passed to _clone().
14241441repo .git .update_environment (** git .environment ())
@@ -1501,6 +1518,7 @@ def clone_from(
15011518multi_options :Optional [List [str ]]= None ,
15021519allow_unsafe_protocols :bool = False ,
15031520allow_unsafe_options :bool = False ,
1521+ safe :bool = False ,
15041522** kwargs :Any ,
15051523 )-> "Repo" :
15061524"""Create a clone from the given URL.
@@ -1537,7 +1555,7 @@ def clone_from(
15371555 :return:
15381556 :class:`Repo` instance pointing to the cloned directory.
15391557 """
1540- git = cls .GitCommandWrapperType (os .getcwd ())
1558+ git = cls .GitCommandWrapperType (os .getcwd (), safe )
15411559if env is not None :
15421560git .update_environment (** env )
15431561return cls ._clone (
@@ -1549,6 +1567,7 @@ def clone_from(
15491567multi_options ,
15501568allow_unsafe_protocols = allow_unsafe_protocols ,
15511569allow_unsafe_options = allow_unsafe_options ,
1570+ safe = safe ,
15521571** kwargs ,
15531572 )
15541573