Skip to content

Commit 4161ebd

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 fa36692 commit 4161ebd

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
@@ -225,7 +225,9 @@ typedef enum {
225225
GIT_OPT_SET_ODB_PACKED_PRIORITY,
226226
GIT_OPT_SET_ODB_LOOSE_PRIORITY,
227227
GIT_OPT_GET_EXTENSIONS,
228-
GIT_OPT_SET_EXTENSIONS
228+
GIT_OPT_SET_EXTENSIONS,
229+
GIT_OPT_GET_OWNER_VALIDATION,
230+
GIT_OPT_SET_OWNER_VALIDATION
229231
} git_libgit2_opt_t;
230232

231233
/**
@@ -463,6 +465,14 @@ typedef enum {
463465
* > to support repositories with the `noop` extension but does want
464466
* > to support repositories with the `newext` extension.
465467
*
468+
* opts(GIT_OPT_GET_OWNER_VALIDATION, int *enabled)
469+
* > Gets the owner validation setting for repository
470+
* > directories.
471+
*
472+
* opts(GIT_OPT_SET_OWNER_VALIDATION, int enabled)
473+
* > Set that repository directories should be owned by the current
474+
* > user. The default is to validate ownership.
475+
*
466476
* @param option Option key
467477
* @param ... value to set the option
468478
* @return 0 on success, <0 on failure

src/libgit2/libgit2.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,14 @@ int git_libgit2_opts(int key, ...)
406406
}
407407
break;
408408

409+
case GIT_OPT_GET_OWNER_VALIDATION:
410+
*(va_arg(ap, int *)) = git_repository__validate_ownership;
411+
break;
412+
413+
case GIT_OPT_SET_OWNER_VALIDATION:
414+
git_repository__validate_ownership = (va_arg(ap, int) != 0);
415+
break;
416+
409417
default:
410418
git_error_set(GIT_ERROR_INVALID, "invalid option key");
411419
error = -1;

src/libgit2/repository.c

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

42+
bool git_repository__validate_ownership = true;
4243
bool git_repository__fsync_gitdir = false;
4344

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

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

983985
cleanup:

src/libgit2/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/clar_libgit2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,11 @@ void cl_sandbox_set_search_path_defaults(void)
599599
git_str_dispose(&path);
600600
}
601601

602+
void cl_sandbox_disable_ownership_validation(void)
603+
{
604+
git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
605+
}
606+
602607
#ifdef GIT_WIN32
603608
bool cl_sandbox_supports_8dot3(void)
604609
{

tests/clar/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/clar/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/libgit2/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_STR_INIT,
495501
config_data = GIT_STR_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_STR_INIT,
538546
config_data = GIT_STR_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)