Skip to content

Commit

Permalink
repo: make ownership checks optional
Browse files Browse the repository at this point in the history
Introduce the `GIT_OPT_SET_OWNER_VALIDATION` option, so that users can
disable repository ownership validation.
  • Loading branch information
ethomson committed Apr 12, 2022
1 parent eb8c3e5 commit b58e905
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 2 deletions.
12 changes: 11 additions & 1 deletion include/git2/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ typedef enum {
GIT_OPT_SET_ODB_PACKED_PRIORITY,
GIT_OPT_SET_ODB_LOOSE_PRIORITY,
GIT_OPT_GET_EXTENSIONS,
GIT_OPT_SET_EXTENSIONS
GIT_OPT_SET_EXTENSIONS,
GIT_OPT_GET_OWNER_VALIDATION,
GIT_OPT_SET_OWNER_VALIDATION
} git_libgit2_opt_t;

/**
Expand Down Expand Up @@ -449,6 +451,14 @@ typedef enum {
* > to support repositories with the `noop` extension but does want
* > to support repositories with the `newext` extension.
*
* opts(GIT_OPT_GET_OWNER_VALIDATION, int *enabled)
* > Gets the owner validation setting for repository
* > directories.
*
* opts(GIT_OPT_SET_OWNER_VALIDATION, int enabled)
* > Set that repository directories should be owned by the current
* > user. The default is to validate ownership.
*
* @param option Option key
* @param ... value to set the option
* @return 0 on success, <0 on failure
Expand Down
8 changes: 8 additions & 0 deletions src/libgit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ int git_libgit2_opts(int key, ...)
}
break;

case GIT_OPT_GET_OWNER_VALIDATION:
*(va_arg(ap, int *)) = git_repository__validate_ownership;
break;

case GIT_OPT_SET_OWNER_VALIDATION:
git_repository__validate_ownership = (va_arg(ap, int) != 0);
break;

default:
git_error_set(GIT_ERROR_INVALID, "invalid option key");
error = -1;
Expand Down
4 changes: 3 additions & 1 deletion src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
# include "win32/w32_util.h"
#endif

bool git_repository__validate_ownership = true;
bool git_repository__fsync_gitdir = false;

static const struct {
Expand Down Expand Up @@ -976,7 +977,8 @@ int git_repository_open_ext(
*/
validation_path = repo->is_bare ? repo->gitdir : repo->workdir;

if ((error = validate_ownership(validation_path)) < 0)
if (git_repository__validate_ownership &&
(error = validate_ownership(validation_path)) < 0)
goto cleanup;

cleanup:
Expand Down
1 change: 1 addition & 0 deletions src/repository.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define GIT_DIR_SHORTNAME "GIT~1"

extern bool git_repository__fsync_gitdir;
extern bool git_repository__validate_ownership;

/** Cvar cache identifiers */
typedef enum {
Expand Down
5 changes: 5 additions & 0 deletions tests/clar_libgit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ void cl_sandbox_set_search_path_defaults(void)
git_buf_dispose(&path);
}

void cl_sandbox_disable_ownership_validation(void)
{
git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 0);
}

#ifdef GIT_WIN32
bool cl_sandbox_supports_8dot3(void)
{
Expand Down
1 change: 1 addition & 0 deletions tests/clar_libgit2.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ void cl_fake_home(void);
void cl_fake_home_cleanup(void *);

void cl_sandbox_set_search_path_defaults(void);
void cl_sandbox_disable_ownership_validation(void);

#ifdef GIT_WIN32
# define cl_msleep(x) Sleep(x)
Expand Down
1 change: 1 addition & 0 deletions tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ int main(int argc, char *argv[])

cl_global_trace_register();
cl_sandbox_set_search_path_defaults();
cl_sandbox_disable_ownership_validation();

/* Run the test suite */
res = clar_test_run();
Expand Down
10 changes: 10 additions & 0 deletions tests/repo/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include "sysdir.h"
#include <ctype.h>

static int validate_ownership = 0;
static git_buf config_path = GIT_BUF_INIT;

void test_repo_open__initialize(void)
{
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &config_path));
cl_git_pass(git_libgit2_opts(GIT_OPT_GET_OWNER_VALIDATION, &validate_ownership));
}

void test_repo_open__cleanup(void)
Expand All @@ -23,6 +25,8 @@ void test_repo_open__cleanup(void)

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr));
git_buf_dispose(&config_path);

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, validate_ownership));
}

void test_repo_open__bare_empty_repo(void)
Expand Down Expand Up @@ -470,6 +474,8 @@ void test_repo_open__validates_dir_ownership(void)
{
git_repository *repo;

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));

cl_fixture_sandbox("empty_standard_repo");
cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));

Expand All @@ -494,6 +500,8 @@ void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
config_filename = GIT_BUF_INIT,
config_data = GIT_BUF_INIT;

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));

cl_fixture_sandbox("empty_standard_repo");
cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));

Expand Down Expand Up @@ -537,6 +545,8 @@ void test_repo_open__can_reset_safe_directory_list(void)
config_filename = GIT_BUF_INIT,
config_data = GIT_BUF_INIT;

cl_git_pass(git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, 1));

cl_fixture_sandbox("empty_standard_repo");
cl_git_pass(cl_rename("empty_standard_repo/.gitted", "empty_standard_repo/.git"));

Expand Down

0 comments on commit b58e905

Please sign in to comment.