fix: prevent environment expansion in remote URLs · gitpython-developers/GitPython@8634174 · GitHub
Skip to content

Commit 8634174

Browse files
codexByron
andcommitted
fix: prevent environment expansion in remote URLs
Remote and submodule creation expanded environment-variable references in caller-supplied URLs before storing them in Git configuration. This could expose process environment values through a later network request or a tracked .gitmodules file. Preserve variables literally in the remaining untrusted URL callers while retaining Git.polish_url() expansion for intentional local-path normalization. Add regression tests covering remote configuration and submodule configuration. Git baseline: git remote add and git submodule add accept URL arguments literally; Git does not perform shell-style environment expansion on those arguments. Co-authored-by: Sebastian Thiel <sebastian.thiel@icloud.com>
1 parent e59d9ba commit 8634174

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

git/objects/submodule/base.py

Lines changed: 1 addition & 1 deletion

git/remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ def create(cls, repo: "Repo", name: str, url: str, allow_unsafe_protocols: bool
808808
"""
809809
scmd = "add"
810810
kwargs["insert_kwargs_after"] = scmd
811-
url = Git.polish_url(url)
811+
url = Git.polish_url(url, expand_vars=False)
812812
if not allow_unsafe_protocols:
813813
Git.check_unsafe_protocols(url)
814814
repo.git.remote(scmd, "--", name, url, **kwargs)

test/test_remote.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
55

66
import gc
7+
import os
78
import os.path as osp
89
from pathlib import Path
910
import random
@@ -685,6 +686,14 @@ def test_multiple_urls(self, rw_repo):
685686
# Will raise fatal: Will not delete all non-push URLs.
686687
self.assertRaises(GitCommandError, remote.delete_url, test3)
687688

689+
@with_rw_repo("HEAD", bare=False)
690+
def test_create_does_not_expand_environment_variables_in_url(self, rw_repo):
691+
url = "https://example.com/${GITPYTHON_TEST_SECRET}/repo.git"
692+
with mock.patch.dict(os.environ, {"GITPYTHON_TEST_SECRET": "sensitive-value"}):
693+
remote = rw_repo.create_remote("untrusted", url)
694+
695+
assert remote.url == url
696+
688697
def test_fetch_error(self):
689698
rem = self.rorepo.remote("origin")
690699
msg = (

test/test_submodule.py

Lines changed: 14 additions & 0 deletions

0 commit comments

Comments
 (0)