Unverified Commit a9eac6a6 by Edward Thomson Committed by GitHub

Merge pull request #6268 from libgit2/ethomson/ownership_13

Validate repository directory ownership (v1.3)
parents 37caa8dc b58e9053
......@@ -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;
/**
......@@ -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
......
......@@ -58,6 +58,7 @@ typedef enum {
GIT_EMISMATCH = -33, /**< Hashsum mismatch in object */
GIT_EINDEXDIRTY = -34, /**< Unsaved changes in the index would be overwritten */
GIT_EAPPLYFAIL = -35, /**< Patch application failed */
GIT_EOWNER = -36 /**< The object is not owned by the current user */
} git_error_code;
/**
......
......@@ -1118,16 +1118,20 @@ int git_config_find_system(git_buf *path)
int git_config_find_programdata(git_buf *path)
{
int ret;
bool is_safe;
if ((ret = git_buf_sanitize(path)) < 0)
if ((ret = git_buf_sanitize(path)) < 0 ||
(ret = git_sysdir_find_programdata_file(path,
GIT_CONFIG_FILENAME_PROGRAMDATA)) < 0 ||
(ret = git_path_owner_is_system_or_current_user(&is_safe, path->ptr)) < 0)
return ret;
ret = git_sysdir_find_programdata_file(path,
GIT_CONFIG_FILENAME_PROGRAMDATA);
if (ret != GIT_OK)
return ret;
if (!is_safe) {
git_error_set(GIT_ERROR_CONFIG, "programdata path has invalid ownership");
return -1;
}
return git_path_validate_system_file_ownership(path->ptr);
return 0;
}
int git_config__global_location(git_buf *buf)
......
......@@ -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;
......
......@@ -2024,78 +2024,237 @@ done:
return supported;
}
int git_path_validate_system_file_ownership(const char *path)
static git_path__mock_owner_t mock_owner = GIT_PATH_MOCK_OWNER_NONE;
void git_path__set_owner(git_path__mock_owner_t owner)
{
mock_owner = owner;
}
#ifdef GIT_WIN32
static PSID *sid_dup(PSID sid)
{
DWORD len;
PSID dup;
len = GetLengthSid(sid);
if ((dup = git__malloc(len)) == NULL)
return NULL;
if (!CopySid(len, dup, sid)) {
git_error_set(GIT_ERROR_OS, "could not duplicate sid");
git__free(dup);
return NULL;
}
return dup;
}
static int current_user_sid(PSID *out)
{
#ifndef GIT_WIN32
GIT_UNUSED(path);
return GIT_OK;
#else
git_win32_path buf;
PSID owner_sid;
PSECURITY_DESCRIPTOR descriptor = NULL;
HANDLE token;
TOKEN_USER *info = NULL;
DWORD err, len;
int ret;
HANDLE token = NULL;
DWORD len = 0;
int error = -1;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
git_error_set(GIT_ERROR_OS, "could not lookup process information");
goto done;
}
if (GetTokenInformation(token, TokenUser, NULL, 0, &len) ||
GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
git_error_set(GIT_ERROR_OS, "could not lookup token metadata");
goto done;
}
info = git__malloc(len);
GIT_ERROR_CHECK_ALLOC(info);
if (!GetTokenInformation(token, TokenUser, info, len, &len)) {
git_error_set(GIT_ERROR_OS, "could not lookup current user");
goto done;
}
if ((*out = sid_dup(info->User.Sid)))
error = 0;
done:
if (token)
CloseHandle(token);
git__free(info);
return error;
}
static int file_owner_sid(PSID *out, const char *path)
{
git_win32_path path_w32;
PSECURITY_DESCRIPTOR descriptor = NULL;
PSID owner_sid;
DWORD ret;
int error = -1;
if (git_win32_path_from_utf8(buf, path) < 0)
if (git_win32_path_from_utf8(path_w32, path) < 0)
return -1;
err = GetNamedSecurityInfoW(buf, SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION,
ret = GetNamedSecurityInfoW(path_w32, SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
&owner_sid, NULL, NULL, NULL, &descriptor);
if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND) {
ret = GIT_ENOTFOUND;
goto cleanup;
if (ret == ERROR_FILE_NOT_FOUND || ret == ERROR_PATH_NOT_FOUND)
error = GIT_ENOTFOUND;
else if (ret != ERROR_SUCCESS)
git_error_set(GIT_ERROR_OS, "failed to get security information");
else if (!IsValidSid(owner_sid))
git_error_set(GIT_ERROR_OS, "file owner is not valid");
else if ((*out = sid_dup(owner_sid)))
error = 0;
if (descriptor)
LocalFree(descriptor);
return error;
}
int git_path_owner_is_current_user(bool *out, const char *path)
{
PSID owner_sid = NULL, user_sid = NULL;
int error = -1;
if (mock_owner) {
*out = (mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
return 0;
}
if (err != ERROR_SUCCESS) {
git_error_set(GIT_ERROR_OS, "failed to get security information");
ret = GIT_ERROR;
goto cleanup;
if ((error = file_owner_sid(&owner_sid, path)) < 0 ||
(error = current_user_sid(&user_sid)) < 0)
goto done;
*out = EqualSid(owner_sid, user_sid);
error = 0;
done:
git__free(owner_sid);
git__free(user_sid);
return error;
}
int git_path_owner_is_system(bool *out, const char *path)
{
PSID owner_sid;
if (mock_owner) {
*out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM);
return 0;
}
if (!IsValidSid(owner_sid)) {
git_error_set(GIT_ERROR_INVALID, "programdata configuration file owner is unknown");
ret = GIT_ERROR;
goto cleanup;
if (file_owner_sid(&owner_sid, path) < 0)
return -1;
*out = IsWellKnownSid(owner_sid, WinBuiltinAdministratorsSid) ||
IsWellKnownSid(owner_sid, WinLocalSystemSid);
git__free(owner_sid);
return 0;
}
int git_path_owner_is_system_or_current_user(bool *out, const char *path)
{
PSID owner_sid = NULL, user_sid = NULL;
int error = -1;
if (mock_owner) {
*out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM ||
mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
return 0;
}
if (file_owner_sid(&owner_sid, path) < 0)
goto done;
if (IsWellKnownSid(owner_sid, WinBuiltinAdministratorsSid) ||
IsWellKnownSid(owner_sid, WinLocalSystemSid)) {
ret = GIT_OK;
goto cleanup;
*out = 1;
error = 0;
goto done;
}
/* Obtain current user's SID */
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token) &&
!GetTokenInformation(token, TokenUser, NULL, 0, &len)) {
info = git__malloc(len);
GIT_ERROR_CHECK_ALLOC(info);
if (!GetTokenInformation(token, TokenUser, info, len, &len)) {
git__free(info);
info = NULL;
if (current_user_sid(&user_sid) < 0)
goto done;
*out = EqualSid(owner_sid, user_sid);
error = 0;
done:
git__free(owner_sid);
git__free(user_sid);
return error;
}
#else
static int path_owner_is(bool *out, const char *path, uid_t *uids, size_t uids_len)
{
struct stat st;
size_t i;
*out = false;
if (p_lstat(path, &st) != 0) {
if (errno == ENOENT)
return GIT_ENOTFOUND;
git_error_set(GIT_ERROR_OS, "could not stat '%s'", path);
return -1;
}
for (i = 0; i < uids_len; i++) {
if (uids[i] == st.st_uid) {
*out = true;
break;
}
}
/*
* If the file is owned by the same account that is running the current
* process, it's okay to read from that file.
*/
if (info && EqualSid(owner_sid, info->User.Sid))
ret = GIT_OK;
else {
git_error_set(GIT_ERROR_INVALID, "programdata configuration file owner is not valid");
ret = GIT_ERROR;
return 0;
}
int git_path_owner_is_current_user(bool *out, const char *path)
{
uid_t userid = geteuid();
if (mock_owner) {
*out = (mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
return 0;
}
git__free(info);
cleanup:
if (descriptor)
LocalFree(descriptor);
return path_owner_is(out, path, &userid, 1);
}
return ret;
#endif
int git_path_owner_is_system(bool *out, const char *path)
{
uid_t userid = 0;
if (mock_owner) {
*out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM);
return 0;
}
return path_owner_is(out, path, &userid, 1);
}
int git_path_owner_is_system_or_current_user(bool *out, const char *path)
{
uid_t userids[2] = { geteuid(), 0 };
if (mock_owner) {
*out = (mock_owner == GIT_PATH_MOCK_OWNER_SYSTEM ||
mock_owner == GIT_PATH_MOCK_OWNER_CURRENT_USER);
return 0;
}
return path_owner_is(out, path, userids, 2);
}
#endif
......@@ -722,16 +722,36 @@ int git_path_normalize_slashes(git_buf *out, const char *path);
bool git_path_supports_symlinks(const char *dir);
typedef enum {
GIT_PATH_MOCK_OWNER_NONE = 0, /* do filesystem lookups as normal */
GIT_PATH_MOCK_OWNER_SYSTEM = 1,
GIT_PATH_MOCK_OWNER_CURRENT_USER = 2,
GIT_PATH_MOCK_OWNER_OTHER = 3
} git_path__mock_owner_t;
/**
* Sets the mock ownership for files; subsequent calls to
* `git_path_owner_is_*` functions will return this data until cleared
* with `GIT_PATH_MOCK_OWNER_NONE`.
*/
void git_path__set_owner(git_path__mock_owner_t owner);
/**
* Validate a system file's ownership
*
* Verify that the file in question is owned by an administrator or system
* account, or at least by the current user.
*
* This function returns 0 if successful. If the file is not owned by any of
* these, or any other if there have been problems determining the file
* ownership, it returns -1.
* account.
*/
int git_path_owner_is_system(bool *out, const char *path);
/**
* Verify that the file in question is owned by the current user;
*/
int git_path_owner_is_current_user(bool *out, const char *path);
/**
* Verify that the file in question is owned by an administrator or system
* account _or_ the current user;
*/
int git_path_validate_system_file_ownership(const char *path);
int git_path_owner_is_system_or_current_user(bool *out, const char *path);
#endif
......@@ -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 {
......@@ -64,6 +65,7 @@ static const struct {
static int check_repositoryformatversion(int *version, git_config *config);
static int check_extensions(git_config *config, int version);
static int load_global_config(git_config **config);
#define GIT_COMMONDIR_FILE "commondir"
#define GIT_GITDIR_FILE "gitdir"
......@@ -482,6 +484,63 @@ static int read_gitfile(git_buf *path_out, const char *file_path)
return error;
}
typedef struct {
const char *repo_path;
git_buf tmp;
bool is_safe;
} validate_ownership_data;
static int validate_ownership_cb(const git_config_entry *entry, void *payload)
{
validate_ownership_data *data = payload;
if (strcmp(entry->value, "") == 0)
data->is_safe = false;
if (git_path_prettify_dir(&data->tmp, entry->value, NULL) == 0 &&
strcmp(data->tmp.ptr, data->repo_path) == 0)
data->is_safe = true;
return 0;
}
static int validate_ownership(const char *repo_path)
{
git_config *config = NULL;
validate_ownership_data data = { repo_path, GIT_BUF_INIT, false };
bool is_safe;
int error;
if ((error = git_path_owner_is_current_user(&is_safe, repo_path)) < 0) {
if (error == GIT_ENOTFOUND)
error = 0;
goto done;
}
if (is_safe) {
error = 0;
goto done;
}
if (load_global_config(&config) == 0) {
error = git_config_get_multivar_foreach(config, "safe.directory", NULL, validate_ownership_cb, &data);
if (!error && data.is_safe)
goto done;
}
git_error_set(GIT_ERROR_CONFIG,
"repository path '%s' is not owned by current user",
repo_path);
error = GIT_EOWNER;
done:
git_config_free(config);
git_buf_dispose(&data.tmp);
return error;
}
static int find_repo(
git_buf *gitdir_path,
git_buf *workdir_path,
......@@ -855,6 +914,7 @@ int git_repository_open_ext(
gitlink = GIT_BUF_INIT, commondir = GIT_BUF_INIT;
git_repository *repo = NULL;
git_config *config = NULL;
const char *validation_path;
int version = 0;
if (flags & GIT_REPOSITORY_OPEN_FROM_ENV)
......@@ -903,16 +963,24 @@ int git_repository_open_ext(
if ((error = check_extensions(config, version)) < 0)
goto cleanup;
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0)
if ((flags & GIT_REPOSITORY_OPEN_BARE) != 0) {
repo->is_bare = 1;
else {
} else {
if (config &&
((error = load_config_data(repo, config)) < 0 ||
(error = load_workdir(repo, config, &workdir)) < 0))
goto cleanup;
}
/*
* Ensure that the git directory is owned by the current user.
*/
validation_path = repo->is_bare ? repo->gitdir : repo->workdir;
if (git_repository__validate_ownership &&
(error = validate_ownership(validation_path)) < 0)
goto cleanup;
cleanup:
git_buf_dispose(&gitdir);
git_buf_dispose(&workdir);
......@@ -1609,36 +1677,53 @@ static bool is_filesystem_case_insensitive(const char *gitdir_path)
return is_insensitive;
}
static bool are_symlinks_supported(const char *wd_path)
/*
* Return a configuration object with only the global and system
* configurations; no repository-level configuration.
*/
static int load_global_config(git_config **config)
{
git_config *config = NULL;
git_buf global_buf = GIT_BUF_INIT;
git_buf xdg_buf = GIT_BUF_INIT;
git_buf system_buf = GIT_BUF_INIT;
git_buf programdata_buf = GIT_BUF_INIT;
int symlinks = 0;
int error;
/*
* To emulate Git for Windows, symlinks on Windows must be explicitly
* opted-in. We examine the system configuration for a core.symlinks
* set to true. If found, we then examine the filesystem to see if
* symlinks are _actually_ supported by the current user. If that is
* _not_ set, then we do not test or enable symlink support.
*/
#ifdef GIT_WIN32
git_config_find_global(&global_buf);
git_config_find_xdg(&xdg_buf);
git_config_find_system(&system_buf);
git_config_find_programdata(&programdata_buf);
if (load_config(&config, NULL,
error = load_config(config, NULL,
path_unless_empty(&global_buf),
path_unless_empty(&xdg_buf),
path_unless_empty(&system_buf),
path_unless_empty(&programdata_buf)) < 0)
goto done;
path_unless_empty(&programdata_buf));
git_buf_dispose(&global_buf);
git_buf_dispose(&xdg_buf);
git_buf_dispose(&system_buf);
git_buf_dispose(&programdata_buf);
if (git_config_get_bool(&symlinks, config, "core.symlinks") < 0 || !symlinks)
return error;
}
static bool are_symlinks_supported(const char *wd_path)
{
git_config *config = NULL;
int symlinks = 0;
/*
* To emulate Git for Windows, symlinks on Windows must be explicitly
* opted-in. We examine the system configuration for a core.symlinks
* set to true. If found, we then examine the filesystem to see if
* symlinks are _actually_ supported by the current user. If that is
* _not_ set, then we do not test or enable symlink support.
*/
#ifdef GIT_WIN32
if (load_global_config(&config) < 0 ||
git_config_get_bool(&symlinks, config, "core.symlinks") < 0 ||
!symlinks)
goto done;
#endif
......@@ -1646,10 +1731,6 @@ static bool are_symlinks_supported(const char *wd_path)
goto done;
done:
git_buf_dispose(&global_buf);
git_buf_dispose(&xdg_buf);
git_buf_dispose(&system_buf);
git_buf_dispose(&programdata_buf);
git_config_free(config);
return symlinks != 0;
}
......
......@@ -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 {
......
......@@ -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)
{
......
......@@ -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)
......
......@@ -659,3 +659,28 @@ void test_core_path__git_path_is_file(void)
cl_git_pass(git_path_is_gitfile("blob", 4, GIT_PATH_GITFILE_GITATTRIBUTES, GIT_PATH_FS_HFS));
cl_git_fail(git_path_is_gitfile("blob", 4, 3, GIT_PATH_FS_HFS));
}
void test_core_path__validate_current_user_ownership(void)
{
bool is_cur;
cl_must_pass(p_mkdir("testdir", 0777));
cl_git_pass(git_path_owner_is_current_user(&is_cur, "testdir"));
cl_assert_equal_i(is_cur, 1);
cl_git_rewritefile("testfile", "This is a test file.");
cl_git_pass(git_path_owner_is_current_user(&is_cur, "testfile"));
cl_assert_equal_i(is_cur, 1);
#ifdef GIT_WIN32
cl_git_pass(git_path_owner_is_current_user(&is_cur, "C:\\"));
cl_assert_equal_i(is_cur, 0);
cl_git_fail(git_path_owner_is_current_user(&is_cur, "c:\\path\\does\\not\\exist"));
#else
cl_git_pass(git_path_owner_is_current_user(&is_cur, "/"));
cl_assert_equal_i(is_cur, 0);
cl_git_fail(git_path_owner_is_current_user(&is_cur, "/path/does/not/exist"));
#endif
}
#define FETCH_HEAD_WILDCARD_DATA_LOCAL \
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \
"d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of git://github.com/libgit2/TestGitRepository\n" \
"55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of git://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of git://github.com/libgit2/TestGitRepository\n"
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\tbranch 'master' of https://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of https://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of https://github.com/libgit2/TestGitRepository\n" \
"d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of https://github.com/libgit2/TestGitRepository\n" \
"55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of https://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of https://github.com/libgit2/TestGitRepository\n"
#define FETCH_HEAD_WILDCARD_DATA \
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \
"d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of git://github.com/libgit2/TestGitRepository\n" \
"55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of git://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of git://github.com/libgit2/TestGitRepository\n" \
"6e0c7bdb9b4ed93212491ee778ca1c65047cab4e\tnot-for-merge\ttag 'nearly-dangling' of git://github.com/libgit2/TestGitRepository\n"
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\tbranch 'master' of https://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of https://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of https://github.com/libgit2/TestGitRepository\n" \
"d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of https://github.com/libgit2/TestGitRepository\n" \
"55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of https://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of https://github.com/libgit2/TestGitRepository\n" \
"6e0c7bdb9b4ed93212491ee778ca1c65047cab4e\tnot-for-merge\ttag 'nearly-dangling' of https://github.com/libgit2/TestGitRepository\n"
#define FETCH_HEAD_WILDCARD_DATA2 \
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\tbranch 'master' of https://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of https://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of https://github.com/libgit2/TestGitRepository\n" \
#define FETCH_HEAD_NO_MERGE_DATA \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \
"49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \
"d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of git://github.com/libgit2/TestGitRepository\n" \
"55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of git://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of git://github.com/libgit2/TestGitRepository\n" \
"6e0c7bdb9b4ed93212491ee778ca1c65047cab4e\tnot-for-merge\ttag 'nearly-dangling' of git://github.com/libgit2/TestGitRepository\n"
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of https://github.com/libgit2/TestGitRepository\n" \
"49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tbranch 'master' of https://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of https://github.com/libgit2/TestGitRepository\n" \
"d96c4e80345534eccee5ac7b07fc7603b56124cb\tnot-for-merge\ttag 'annotated_tag' of https://github.com/libgit2/TestGitRepository\n" \
"55a1a760df4b86a02094a904dfa511deb5655905\tnot-for-merge\ttag 'blob' of https://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of https://github.com/libgit2/TestGitRepository\n" \
"6e0c7bdb9b4ed93212491ee778ca1c65047cab4e\tnot-for-merge\ttag 'nearly-dangling' of https://github.com/libgit2/TestGitRepository\n"
#define FETCH_HEAD_NO_MERGE_DATA2 \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \
"49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of https://github.com/libgit2/TestGitRepository\n" \
"49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tbranch 'master' of https://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of https://github.com/libgit2/TestGitRepository\n" \
#define FETCH_HEAD_NO_MERGE_DATA3 \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n" \
"49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tbranch 'master' of git://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of git://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of git://github.com/libgit2/TestGitRepository\n" \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\tnot-for-merge\tbranch 'first-merge' of https://github.com/libgit2/TestGitRepository\n" \
"49322bb17d3acc9146f98c97d078513228bbf3c0\tnot-for-merge\tbranch 'master' of https://github.com/libgit2/TestGitRepository\n" \
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1\tnot-for-merge\tbranch 'no-parent' of https://github.com/libgit2/TestGitRepository\n" \
"8f50ba15d49353813cc6e20298002c0d17b0a9ee\tnot-for-merge\ttag 'commit_tree' of https://github.com/libgit2/TestGitRepository\n" \
#define FETCH_HEAD_EXPLICIT_DATA \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\t\tbranch 'first-merge' of git://github.com/libgit2/TestGitRepository\n"
"0966a434eb1a025db6b71485ab63a3bfbea520b6\t\tbranch 'first-merge' of https://github.com/libgit2/TestGitRepository\n"
#define FETCH_HEAD_QUOTE_DATA \
"0966a434eb1a025db6b71485ab63a3bfbea520b6\t\tbranch 'first's-merge' of git://github.com/libgit2/TestGitRepository\n"
"0966a434eb1a025db6b71485ab63a3bfbea520b6\t\tbranch 'first's-merge' of https://github.com/libgit2/TestGitRepository\n"
......@@ -33,42 +33,42 @@ static void populate_fetchhead(git_vector *out, git_repository *repo)
"49322bb17d3acc9146f98c97d078513228bbf3c0"));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 1,
"refs/heads/master",
"git://github.com/libgit2/TestGitRepository"));
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid_fromstr(&oid,
"0966a434eb1a025db6b71485ab63a3bfbea520b6"));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/heads/first-merge",
"git://github.com/libgit2/TestGitRepository"));
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid_fromstr(&oid,
"42e4e7c5e507e113ebbb7801b16b52cf867b7ce1"));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/heads/no-parent",
"git://github.com/libgit2/TestGitRepository"));
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid_fromstr(&oid,
"d96c4e80345534eccee5ac7b07fc7603b56124cb"));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/tags/annotated_tag",
"git://github.com/libgit2/TestGitRepository"));
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid_fromstr(&oid,
"55a1a760df4b86a02094a904dfa511deb5655905"));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/tags/blob",
"git://github.com/libgit2/TestGitRepository"));
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_oid_fromstr(&oid,
"8f50ba15d49353813cc6e20298002c0d17b0a9ee"));
cl_git_pass(git_fetchhead_ref_create(&fetchhead_ref, &oid, 0,
"refs/tags/commit_tree",
"git://github.com/libgit2/TestGitRepository"));
"https://github.com/libgit2/TestGitRepository"));
cl_git_pass(git_vector_insert(out, fetchhead_ref));
cl_git_pass(git_fetchhead_write(repo, out));
......
......@@ -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();
......
......@@ -8,9 +8,9 @@
#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
#define BB_REPO_URL "https://libgit3@bitbucket.org/libgit2/testgitrepository.git"
#define BB_REPO_URL_WITH_PASS "https://libgit3:libgit3@bitbucket.org/libgit2/testgitrepository.git"
#define BB_REPO_URL_WITH_WRONG_PASS "https://libgit3:wrong@bitbucket.org/libgit2/testgitrepository.git"
#define BB_REPO_URL "https://libgit2-test@bitbucket.org/libgit2-test/testgitrepository.git"
#define BB_REPO_URL_WITH_PASS "https://libgit2-test:YT77Ppm2nq8w4TYjGS8U@bitbucket.org/libgit2-test/testgitrepository.git"
#define BB_REPO_URL_WITH_WRONG_PASS "https://libgit2-test:wrong@bitbucket.org/libgit2-test/testgitrepository.git"
#define GOOGLESOURCE_REPO_URL "https://chromium.googlesource.com/external/github.com/sergi/go-diff"
#define SSH_REPO_URL "ssh://github.com/libgit2/TestGitRepository"
......@@ -405,7 +405,7 @@ void test_online_clone__credentials(void)
void test_online_clone__credentials_via_custom_headers(void)
{
const char *creds = "libgit3:libgit3";
const char *creds = "libgit2-test:YT77Ppm2nq8w4TYjGS8U";
git_buf auth = GIT_BUF_INIT;
cl_git_pass(git_buf_puts(&auth, "Authorization: Basic "));
......@@ -413,7 +413,7 @@ void test_online_clone__credentials_via_custom_headers(void)
g_options.fetch_opts.custom_headers.count = 1;
g_options.fetch_opts.custom_headers.strings = &auth.ptr;
cl_git_pass(git_clone(&g_repo, "https://bitbucket.org/libgit2/testgitrepository.git", "./foo", &g_options));
cl_git_pass(git_clone(&g_repo, "https://bitbucket.org/libgit2-test/testgitrepository.git", "./foo", &g_options));
git_buf_dispose(&auth);
}
......@@ -421,7 +421,7 @@ void test_online_clone__credentials_via_custom_headers(void)
void test_online_clone__bitbucket_style(void)
{
git_credential_userpass_payload user_pass = {
"libgit3", "libgit3"
"libgit2-test", "YT77Ppm2nq8w4TYjGS8U"
};
g_options.fetch_opts.callbacks.credentials = git_credential_userpass;
......@@ -435,7 +435,7 @@ void test_online_clone__bitbucket_style(void)
void test_online_clone__bitbucket_uses_creds_in_url(void)
{
git_credential_userpass_payload user_pass = {
"libgit2", "wrong"
"libgit2-test", "wrong"
};
g_options.fetch_opts.callbacks.credentials = git_credential_userpass;
......@@ -453,7 +453,7 @@ void test_online_clone__bitbucket_uses_creds_in_url(void)
void test_online_clone__bitbucket_falls_back_to_specified_creds(void)
{
git_credential_userpass_payload user_pass = {
"libgit2", "libgit2"
"libgit2-test", "libgit2"
};
g_options.fetch_opts.callbacks.credentials = git_credential_userpass;
......
......@@ -67,11 +67,6 @@ static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
git_remote_free(remote);
}
void test_online_fetch__default_git(void)
{
do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
}
void test_online_fetch__default_http(void)
{
do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
......@@ -84,7 +79,7 @@ void test_online_fetch__default_https(void)
void test_online_fetch__no_tags_git(void)
{
do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}
void test_online_fetch__no_tags_http(void)
......@@ -95,7 +90,7 @@ void test_online_fetch__no_tags_http(void)
void test_online_fetch__fetch_twice(void)
{
git_remote *remote;
cl_git_pass(git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_create(&remote, _repo, "test", "https://github.com/libgit2/TestGitRepository.git"));
cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
cl_git_pass(git_remote_download(remote, NULL, NULL));
git_remote_disconnect(remote);
......
......@@ -5,7 +5,7 @@
#include "../fetchhead/fetchhead_data.h"
#include "git2/clone.h"
#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"
#define LIVE_REPO_URL "https://github.com/libgit2/TestGitRepository"
static git_repository *g_repo;
static git_clone_options g_options;
......@@ -53,7 +53,6 @@ static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fet
git_remote *remote;
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
git_buf fetchhead_buf = GIT_BUF_INIT;
int equals = 0;
git_strarray array, *active_refs = NULL;
cl_git_pass(git_remote_lookup(&remote, g_repo, "origin"));
......@@ -70,11 +69,8 @@ static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fet
cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD"));
equals = (strcmp(fetchhead_buf.ptr, expected_fetchhead) == 0);
cl_assert_equal_s(fetchhead_buf.ptr, expected_fetchhead);
git_buf_dispose(&fetchhead_buf);
cl_assert(equals);
}
void test_online_fetchhead__wildcard_spec(void)
......
#include "clar_libgit2.h"
#define URL "git://github.com/libgit2/TestGitRepository"
#define URL "https://github.com/libgit2/TestGitRepository"
#define REFSPEC "refs/heads/first-merge:refs/remotes/origin/first-merge"
static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
......
......@@ -28,7 +28,6 @@ void test_repo_config__cleanup(void)
cl_assert(!git_path_isdir("alternate"));
cl_fixture_cleanup("empty_standard_repo");
}
void test_repo_config__can_open_global_when_there_is_no_file(void)
......
......@@ -3,13 +3,30 @@
#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)
{
cl_git_sandbox_cleanup();
cl_fixture_cleanup("empty_standard_repo");
cl_fixture_cleanup("__global_config");
if (git_path_isdir("alternate"))
git_futils_rmdir_r("alternate", NULL, GIT_RMDIR_REMOVE_FILES);
git_path__set_owner(GIT_PATH_MOCK_OWNER_NONE);
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)
......@@ -453,3 +470,133 @@ void test_repo_open__force_bare(void)
git_repository_free(barerepo);
}
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"));
/* When the current user owns the repo config, that's acceptable */
git_path__set_owner(GIT_PATH_MOCK_OWNER_CURRENT_USER);
cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
git_repository_free(repo);
/* When the system user owns the repo config, fail */
git_path__set_owner(GIT_PATH_MOCK_OWNER_SYSTEM);
cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
/* When an unknown user owns the repo config, fail */
git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER);
cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
}
void test_repo_open__can_allowlist_dirs_with_problematic_ownership(void)
{
git_repository *repo;
git_buf config_path = GIT_BUF_INIT,
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"));
git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER);
cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
/* Add safe.directory options to the global configuration */
git_buf_joinpath(&config_path, clar_sandbox_path(), "__global_config");
cl_must_pass(p_mkdir(config_path.ptr, 0777));
git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr);
git_buf_joinpath(&config_filename, config_path.ptr, ".gitconfig");
git_buf_printf(&config_data,
"[foo]\n" \
"\tbar = Foobar\n" \
"\tbaz = Baz!\n" \
"[safe]\n" \
"\tdirectory = /non/existent/path\n" \
"\tdirectory = /\n" \
"\tdirectory = c:\\\\temp\n" \
"\tdirectory = %s/%s\n" \
"\tdirectory = /tmp\n" \
"[bar]\n" \
"\tfoo = barfoo\n",
clar_sandbox_path(), "empty_standard_repo");
cl_git_rewritefile(config_filename.ptr, config_data.ptr);
cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
git_repository_free(repo);
git_buf_dispose(&config_path);
git_buf_dispose(&config_filename);
git_buf_dispose(&config_data);
}
void test_repo_open__can_reset_safe_directory_list(void)
{
git_repository *repo;
git_buf config_path = GIT_BUF_INIT,
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"));
git_path__set_owner(GIT_PATH_MOCK_OWNER_OTHER);
cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
/* Add safe.directory options to the global configuration */
git_buf_joinpath(&config_path, clar_sandbox_path(), "__global_config");
cl_must_pass(p_mkdir(config_path.ptr, 0777));
git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, config_path.ptr);
git_buf_joinpath(&config_filename, config_path.ptr, ".gitconfig");
/* The blank resets our sandbox directory and opening fails */
git_buf_printf(&config_data,
"[foo]\n" \
"\tbar = Foobar\n" \
"\tbaz = Baz!\n" \
"[safe]\n" \
"\tdirectory = %s/%s\n" \
"\tdirectory = \n" \
"\tdirectory = /tmp\n" \
"[bar]\n" \
"\tfoo = barfoo\n",
clar_sandbox_path(), "empty_standard_repo");
cl_git_rewritefile(config_filename.ptr, config_data.ptr);
cl_git_fail(git_repository_open(&repo, "empty_standard_repo"));
/* The blank resets tmp and allows subsequent declarations to succeed */
git_buf_clear(&config_data);
git_buf_printf(&config_data,
"[foo]\n" \
"\tbar = Foobar\n" \
"\tbaz = Baz!\n" \
"[safe]\n" \
"\tdirectory = /tmp\n" \
"\tdirectory = \n" \
"\tdirectory = %s/%s\n" \
"[bar]\n" \
"\tfoo = barfoo\n",
clar_sandbox_path(), "empty_standard_repo");
cl_git_rewritefile(config_filename.ptr, config_data.ptr);
cl_git_pass(git_repository_open(&repo, "empty_standard_repo"));
git_repository_free(repo);
git_buf_dispose(&config_path);
git_buf_dispose(&config_filename);
git_buf_dispose(&config_data);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment