Skip to content

Commit 9f66e42

Browse files
committed
fix: resolve core.excludesFile only from user-owned global git configs
Drop the repo-local .git/config pass in resolve_global_excludes_path() so an untrusted repo cannot point core.excludesFile at an arbitrary file (e.g. ~/.ssh/id_rsa) and have cbm open and read it merely by being indexed. git is safe here because it only trusts a repo's .git/config after a deliberate clone/init; the indexer has no such trust step. Global excludes now resolve only from the user-owned ~/.gitconfig and $XDG_CONFIG_HOME/git/config. A repo's own ignores remain handled by .gitignore / .cbmignore. Add a guard test asserting a repo-local .git/config whose excludesFile points outside the repo is ignored, not read. Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
1 parent 3975d79 commit 9f66e42

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

‎src/discover/discover.c‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,9 @@ static bool resolve_xdg_git_config_dir(char *out, size_t out_sz) {
310310
return out[0] != '\0';
311311
}
312312

313-
static bool resolve_global_excludes_path(const char *repo_path, char *out, size_t out_sz) {
313+
static bool resolve_global_excludes_path(char *out, size_t out_sz) {
314314
char config_path[CBM_SZ_4K];
315315

316-
path_join(config_path, sizeof(config_path), repo_path, ".git/config");
317-
if (read_core_excludes_file(config_path, out, out_sz)) {
318-
return true;
319-
}
320-
321316
const char *home = cbm_get_home_dir();
322317
if (home && home[0] != '\0') {
323318
path_join(config_path, sizeof(config_path), home, ".gitconfig");
@@ -783,7 +778,7 @@ int cbm_discover_ex(const char *repo_path, const cbm_discover_opts_t *opts, cbm_
783778
}
784779

785780
cbm_gitignore_t *global_gi = NULL;
786-
if (has_git_config && resolve_global_excludes_path(repo_path, gi_path, sizeof(gi_path))) {
781+
if (has_git_config && resolve_global_excludes_path(gi_path, sizeof(gi_path))) {
787782
global_gi = cbm_gitignore_load(gi_path);
788783
}
789784

‎tests/test_discover.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,43 @@ TEST(discover_global_excludesfile_from_gitconfig_tilde) {
446446
PASS();
447447
}
448448

449+
TEST(discover_repo_local_excludesfile_is_ignored) {
450+
git_env_snapshot_t env = save_git_env();
451+
char *tmp = th_mktempdir("cbm_disc_repo_cfg_ignored");
452+
ASSERT(tmp != NULL);
453+
454+
char base[512], repo[512], home[512], secret[512], config[1024];
455+
snprintf(base, sizeof(base), "%s", tmp);
456+
snprintf(repo, sizeof(repo), "%s/repo", base);
457+
snprintf(home, sizeof(home), "%s/home", base);
458+
snprintf(secret, sizeof(secret), "%s/secret-ignore", home);
459+
snprintf(config, sizeof(config), "[core]\n excludesFile = %s\n", secret);
460+
461+
cbm_setenv("HOME", home, 1);
462+
cbm_unsetenv("XDG_CONFIG_HOME");
463+
464+
th_mkdir_p(TH_PATH(repo, ".git"));
465+
th_write_file(TH_PATH(repo, ".git/config"), config);
466+
th_write_file(secret, "skip-me.go\n");
467+
th_write_file(TH_PATH(repo, "keep.go"), "package keep\n");
468+
th_write_file(TH_PATH(repo, "skip-me.go"), "package skip\n");
469+
470+
cbm_discover_opts_t opts = {0};
471+
cbm_file_info_t *files = NULL;
472+
int count = 0;
473+
474+
int rc = cbm_discover(repo, &opts, &files, &count);
475+
ASSERT_EQ(rc, 0);
476+
ASSERT_EQ(count, 2);
477+
ASSERT_TRUE(discover_has_rel_path(files, count, "keep.go"));
478+
ASSERT_TRUE(discover_has_rel_path(files, count, "skip-me.go"));
479+
480+
cbm_discover_free(files, count);
481+
restore_git_env(&env);
482+
th_cleanup(base);
483+
PASS();
484+
}
485+
449486
TEST(discover_missing_global_excludes_is_noop) {
450487
git_env_snapshot_t env = save_git_env();
451488
char *tmp = th_mktempdir("cbm_disc_global_missing");
@@ -967,6 +1004,7 @@ SUITE(discover) {
9671004
RUN_TEST(discover_with_gitignore);
9681005
RUN_TEST(discover_with_global_xdg_ignore);
9691006
RUN_TEST(discover_global_excludesfile_from_gitconfig_tilde);
1007+
RUN_TEST(discover_repo_local_excludesfile_is_ignored);
9701008
RUN_TEST(discover_missing_global_excludes_is_noop);
9711009
RUN_TEST(discover_cbmignore_negates_global_ignore);
9721010
RUN_TEST(discover_gitignore_dir_excluded_issue234);

0 commit comments

Comments
 (0)