8000 Merge pull request #6429 from csware/safe.directory-wildcard · libgit2/libgit2@d0203b6 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0203b6

Browse files
authored
Merge pull request #6429 from csware/safe.directory-wildcard
Add support for "safe.directory *"
2 parents 33e1c98 + 594bd70 commit d0203b6

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/libgit2/repository.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,9 @@ static int validate_ownership_cb(const git_config_entry *entry, void *payload)
498498
if (strcmp(entry->value, "") == 0)
499499
*data->is_safe = false;
500500

501-
if (git_fs_path_prettify_dir(&data->tmp, entry->value, NULL) == 0 &&
501+
if (strcmp(entry->value, "*") == 0)
502+
*data->is_safe = true;
503+
else if (git_fs_path_prettify_dir(&data->tmp, entry->value, NULL) == 0 &&
502504
strcmp(data->tmp.ptr, data->repo_path) == 0)
503505
*data->is_safe = true;
504506

tests/libgit2/repo/open.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,45 @@ void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
575575
git_str_dispose(&config_data);
576576
}
577577

578+
void test_repo_open__can_wildcard_allowlist_with_problematic_ownership(void)
579+
{
580+
git_repository < 10000 span class=pl-c1>*repo;
581+
git_str config_path = GIT_STR_INIT, config_filename = GIT_STR_INIT;
582+
583+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
584+
585+
cl_fixture_sandbox("empty_standard_repo");
586+
cl_git_pass(cl_rename(
587+
"empty_standard_repo/.gitted", "empty_standard_repo/.git"));
588+
589+
git_fs_path__set_owner(GIT_FS_PATH_OWNER_OTHER);
590+
cl_git_fail_with(
591+
GIT_EOWNER, git_repository_open(&repo, "empty_standard_repo"));
592+
593+
/* Add safe.directory options to the global configuration */
594+
git_str_joinpath(&config_path, clar_sandbox_path(), "__global_config");
595+
cl_must_pass(p_mkdir(config_path.ptr, 0777));
596+
git_libgit2_opts(
597+
GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
598+
config_path.ptr);
599+
600+
git_str_joinpath(&config_filename, config_path.ptr, ".gitconfig");
601+
602+
cl_git_rewritefile(config_filename.ptr, "[foo]\n"
603+
"\tbar = Foobar\n"
604+
"\tbaz = Baz!\n"
605+
"[safe]\n"
606+
"\tdirectory = *\n"
607+
"[bar]\n"
608+
"\tfoo = barfoo\n");
609+
610+
cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
611+
git_repository_free(repo);
612+
613+
git_str_dispose(&config_path);
614+
git_str_dispose(&config_filename);
615+
}
616+
578617
void test_repo_open__can_allowlist_bare_gitdir(void)
579618
{
580619
git_repository *repo;
@@ -619,6 +658,43 @@ void test_repo_open__can_allowlist_bare_gitdir(void)
619658
git_str_dispose(&config_data);
620659
}
621660

661+
void test_repo_open__can_wildcard_allowlist_bare_gitdir(void)
662+
{
663+
git_repository *repo;
664+
git_str config_path = GIT_STR_INIT, config_filename = GIT_STR_INIT;
665+
666+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
667+
668+
cl_fixture_sandbox("testrepo.git");
669+
670+
git_fs_path__set_owner(GIT_FS_PATH_OWNER_OTHER);
671+
cl_git_fail_with(
672+
GIT_EOWNER, git_repository_open(&repo, "testrepo.git"));
673+
674+
/* Add safe.directory options to the global configuration */
675+
git_str_joinpath(&config_path, clar_sandbox_path(), "__global_config");
676+
cl_must_pass(p_mkdir(config_path.ptr, 0777));
677+
git_libgit2_opts(
678+
GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
679+
config_path.ptr);
680+
681+
git_str_joinpath(&config_filename, config_path.ptr, ".gitconfig");
682+
683+
cl_git_rewritefile(config_filename.ptr, "[foo]\n"
684+
"\tbar = Foobar\n"
685+
"\tbaz = Baz!\n"
686+
"[safe]\n"
687+
"\tdirectory = *\n"
688+
"[bar]\n"
689+
"\tfoo = barfoo\n");
690+
691+
cl_git_pass(git_repository_open(&repo, "testrepo.git"));
692+
git_repository_free(repo);
693+
694+
git_str_dispose(&config_path);
695+
git_str_dispose(&config_filename);
696+
}
697+
622698
void test_repo_open__can_reset_safe_directory_list(void)
623699
{
624700
git_repository *repo;

0 commit comments

Comments
 (0)
0