Skip to content

Commit 45bb916

Browse files
spectral54gitster
authored andcommitted
setup: allow cwd=.git w/ bareRepository=explicit
The safe.bareRepository setting can be set to 'explicit' to disallow implicit uses of bare repositories, preventing an attack [1] where an artificial and malicious bare repository is embedded in another git repository. Unfortunately, some tooling uses myrepo/.git/ as the cwd when executing commands, and this is blocked when safe.bareRepository=explicit. Blocking is unnecessary, as git already prevents nested .git directories. Teach git to not reject uses of git inside of the .git directory: check if cwd is .git (or a subdirectory of it) and allow it even if safe.bareRepository=explicit. [1] https://github.com/justinsteven/advisories/blob/main/2022_git_buried_bare_repos_and_fsmonitor_various_abuses.md Signed-off-by: Kyle Lippincott <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit 45bb916

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

setup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,8 @@ static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
13591359

13601360
if (is_git_directory(dir->buf)) {
13611361
trace2_data_string("setup", NULL, "implicit-bare-repository", dir->buf);
1362-
if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT)
1362+
if (get_allowed_bare_repo() == ALLOWED_BARE_REPO_EXPLICIT &&
1363+
!ends_with_path_components(dir->buf, ".git"))
13631364
return GIT_DIR_DISALLOWED_BARE;
13641365
if (!ensure_valid_ownership(NULL, NULL, dir->buf, report))
13651366
return GIT_DIR_INVALID_OWNERSHIP;

t/t0035-safe-bare-repository.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ test_expect_success 'no trace when GIT_DIR is explicitly provided' '
7878
expect_accepted_explicit "$pwd/outer-repo/bare-repo"
7979
'
8080

81+
test_expect_success 'no trace when "bare repository" is .git' '
82+
expect_accepted_implicit -C outer-repo/.git
83+
'
84+
85+
test_expect_success 'no trace when "bare repository" is a subdir of .git' '
86+
expect_accepted_implicit -C outer-repo/.git/objects
87+
'
88+
8189
test_done

0 commit comments

Comments
 (0)