Skip to content

Commit b58e905

Browse files
committed
repo: make ownership checks optional
Introduce the `GIT_OPT_SET_OWNER_VALIDATION` option, so that users can disable repository ownership validation.
1 parent eb8c3e5 commit b58e905

File tree

8 files changed

+40
-2
lines changed

8 files changed

+40
-2
lines changed

include/git2/common.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ typedef enum {
211211
GIT_OPT_SET_ODB_PACKED_PRIORITY,
212212
GIT_OPT_SET_ODB_LOOSE_PRIORITY,
213213
GIT_OPT_GET_EXTENSIONS,
214-
GIT_OPT_SET_EXTENSIONS
214+
GIT_OPT_SET_EXTENSIONS,
215+
GIT_OPT_GET_OWNER_VALIDATION,
216+
GIT_OPT_SET_OWNER_VALIDATION
215217
} git_libgit2_opt_t;
216218

217219
/**
@@ -449,6 +451,14 @@ typedef enum {
449451
* > to support repositories with the `noop` extension but does want
450452
* > to support repositories with the `newext` extension.
451453
*
454+
* opts(GIT_OPT_GET_OWNER_VALIDATION, int *enabled)
455+
* > Gets the owner validation setting for repository
456+
* > directories.
457+
*
458+
* opts(GIT_OPT_SET_OWNER_VALIDATION, int enabled)
459+
* > Set that repository directories should be owned by the current
460+
* > user. The default is to validate ownership.
461+
*
452462
* @param option Option key
453463
* @param ... value to set the option
454464
* @return 0 on success, <0 on failure

src/libgit2.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ int git_libgit2_opts(int key, ...)
390390
}
391391
break;
392392

393+
case GIT_OPT_GET_OWNER_VALIDATION:
394+
*(va_arg(ap, int *)) = git_repository__validate_ownership;
395+
break;
396+
397+
case GIT_OPT_SET_OWNER_VALIDATION:
398+
git_repository__validate_ownership = (va_arg(ap, int) != 0);
399+
break;
400+
393401
default:
394402
git_error_set(GIT_ERROR_INVALID, "invalid option key");
395403
error = -1;

src/repository.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
# include "win32/w32_util.h"
3939
#endif
4040

41+
bool git_repository__validate_ownership = true;
4142
bool git_repository__fsync_gitdir = false;
4243

4344
static const struct {
@@ -976,7 +977,8 @@ int git_repository_open_ext(
976977
*/
977978
validation_path = repo->is_bare ? repo->gitdir : repo->workdir;
978979

979-
if ((error = validate_ownership(validation_path)) < 0)
980+
if (git_repository__validate_ownership &&
981+
(error = validate_ownership(validation_path)) < 0)
980982
goto cleanup;
981983

982984
cleanup:

src/repository.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#define GIT_DIR_SHORTNAME "GIT~1"
3535

3636
extern bool git_repository__fsync_gitdir;
37+
extern bool git_repository__validate_ownership;
3738

3839
/** Cvar cache identifiers */
3940
typedef enum {

tests/clar_libgit2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ void cl_sandbox_set_search_path_defaults(void)
603603
git_buf_dispose(&path);
604604
}
605605

606+
void cl_sandbox_disable_ownership_validation(void)
607+
{
608+
git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
609+
}
610+
606611
#ifdef GIT_WIN32
607612
bool cl_sandbox_supports_8dot3(void)
608613
{

tests/clar_libgit2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ void cl_fake_home(void);
222222
void cl_fake_home_cleanup(void *);
223223

224224
void cl_sandbox_set_search_path_defaults(void);
225+
void cl_sandbox_disable_ownership_validation(void);
225226

226227
#ifdef GIT_WIN32
227228
# define cl_msleep(x) Sleep(x)

tests/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ int main(int argc, char *argv[])
2626

2727
cl_global_trace_register();
2828
cl_sandbox_set_search_path_defaults();
29+
cl_sandbox_disable_ownership_validation();
2930

3031
/* Run the test suite */
3132
res = clar_test_run();

tests/repo/open.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#include "sysdir.h"
44
#include <ctype.h>
55

6+
static int validate_ownership = 0;
67
static git_buf config_path = GIT_BUF_INIT;
78

89
void test_repo_open__initialize(void)
910
{
1011
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &config_path));
12+
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_OWNER_VALIDATION, &validate_ownership));
1113
}
1214

1315
void test_repo_open__cleanup(void)
@@ -23,6 +25,8 @@ void test_repo_open__cleanup(void)
2325

2426
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr));
2527
git_buf_dispose(&config_path);
28+
29+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, validate_ownership));
2630
}
2731

2832
void test_repo_open__bare_empty_repo(void)
@@ -470,6 +474,8 @@ void test_repo_open__validates_dir_ownership(void)
470474
{
471475
git_repository *repo;
472476

477+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
478+
473479
cl_fixture_sandbox("empty_standard_repo");
474480
cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
475481

@@ -494,6 +500,8 @@ void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
494500
config_filename = GIT_BUF_INIT,
495501
config_data = GIT_BUF_INIT;
496502

503+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
504+
497505
cl_fixture_sandbox("empty_standard_repo");
498506
cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
499507

@@ -537,6 +545,8 @@ void test_repo_open__can_reset_safe_directory_list(void)
537545
config_filename = GIT_BUF_INIT,
538546
config_data = GIT_BUF_INIT;
539547

548+
cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));
549+
540550
cl_fixture_sandbox("empty_standard_repo");
541551
cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));
542552

0 commit comments

Comments
 (0)