Skip to content

Commit 5597517

Browse files
authored
Merge pull request libgit2#5373 from pks-t/pks/fetchhead-strip-creds
fetchhead: strip credentials from remote URL
2 parents a1bff63 + 93a9044 commit 5597517

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/fetchhead.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "futils.h"
1515
#include "filebuf.h"
1616
#include "refs.h"
17+
#include "net.h"
1718
#include "repository.h"
1819

1920
int git_fetchhead_ref_cmp(const void *a, const void *b)
@@ -36,6 +37,33 @@ int git_fetchhead_ref_cmp(const void *a, const void *b)
3637
return 0;
3738
}
3839

40+
static char *sanitized_remote_url(const char *remote_url)
41+
{
42+
git_net_url url = GIT_NET_URL_INIT;
43+
char *sanitized = NULL;
44+
int error;
45+
46+
if (git_net_url_parse(&url, remote_url) == 0) {
47+
git_buf buf = GIT_BUF_INIT;
48+
49+
git__free(url.username);
50+
git__free(url.password);
51+
url.username = url.password = NULL;
52+
53+
if ((error = git_net_url_fmt(&buf, &url)) < 0)
54+
goto fallback;
55+
56+
sanitized = git_buf_detach(&buf);
57+
}
58+
59+
fallback:
60+
if (!sanitized)
61+
sanitized = git__strdup(remote_url);
62+
63+
git_net_url_dispose(&url);
64+
return sanitized;
65+
}
66+
3967
int git_fetchhead_ref_create(
4068
git_fetchhead_ref **out,
4169
git_oid *oid,
@@ -57,11 +85,15 @@ int git_fetchhead_ref_create(
5785
git_oid_cpy(&fetchhead_ref->oid, oid);
5886
fetchhead_ref->is_merge = is_merge;
5987

60-
if (ref_name)
88+
if (ref_name) {
6189
fetchhead_ref->ref_name = git__strdup(ref_name);
90+
GIT_ERROR_CHECK_ALLOC(fetchhead_ref->ref_name);
91+
}
6292

63-
if (remote_url)
64-
fetchhead_ref->remote_url = git__strdup(remote_url);
93+
if (remote_url) {
94+
fetchhead_ref->remote_url = sanitized_remote_url(remote_url);
95+
GIT_ERROR_CHECK_ALLOC(fetchhead_ref->remote_url);
96+
}
6597

6698
*out = fetchhead_ref;
6799

tests/fetchhead/nonetwork.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void test_fetchhead_nonetwork__write(void)
108108
typedef struct {
109109
git_vector *fetchhead_vector;
110110
size_t idx;
111-
} fetchhead_ref_cb_data;
111+
} fetchhead_ref_cb_data;
112112

113113
static int fetchhead_ref_cb(const char *name, const char *url,
114114
const git_oid *oid, unsigned int is_merge, void *payload)
@@ -493,3 +493,21 @@ void test_fetchhead_nonetwork__create_with_multiple_refspecs(void)
493493
git_remote_free(remote);
494494
git_buf_dispose(&path);
495495
}
496+
497+
void test_fetchhead_nonetwork__credentials_are_stripped(void)
498+
{
499+
git_fetchhead_ref *ref;
500+
git_oid oid;
501+
502+
cl_git_pass(git_oid_fromstr(&oid, "49322bb17d3acc9146f98c97d078513228bbf3c0"));
503+
cl_git_pass(git_fetchhead_ref_create(&ref, &oid, 0,
504+
"refs/tags/commit_tree", "http://foo:bar@github.com/libgit2/TestGitRepository"));
505+
cl_assert_equal_s(ref->remote_url, "http://github.com/libgit2/TestGitRepository");
506+
git_fetchhead_ref_free(ref);
507+
508+
cl_git_pass(git_oid_fromstr(&oid, "49322bb17d3acc9146f98c97d078513228bbf3c0"));
509+
cl_git_pass(git_fetchhead_ref_create(&ref, &oid, 0,
510+
"refs/tags/commit_tree", "https://foo:bar@github.com/libgit2/TestGitRepository"));
511+
cl_assert_equal_s(ref->remote_url, "https://github.com/libgit2/TestGitRepository");
512+
git_fetchhead_ref_free(ref);
513+
}

tests/online/fetchhead.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,20 @@ void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void)
154154

155155
cl_assert_equal_i(refs, count_references());
156156
}
157+
158+
void test_online_fetchhead__creds_get_stripped(void)
159+
{
160+
git_buf buf = GIT_BUF_INIT;
161+
git_remote *remote;
162+
163+
cl_git_pass(git_repository_init(&g_repo, "./foo", 0));
164+
cl_git_pass(git_remote_create_anonymous(&remote, g_repo, "https://foo:bar@github.com/libgit2/TestGitRepository"));
165+
cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
166+
167+
cl_git_pass(git_futils_readbuffer(&buf, "./foo/.git/FETCH_HEAD"));
168+
cl_assert_equal_s(buf.ptr,
169+
"49322bb17d3acc9146f98c97d078513228bbf3c0\t\thttps://github.com/libgit2/TestGitRepository\n");
170+
171+
git_remote_free(remote);
172+
git_buf_dispose(&buf);
173+
}

0 commit comments

Comments
 (0)