Skip to content

Commit 46da295

Browse files
dschogitster
authored andcommitted
clone/fetch: anonymize URLs in the reflog
Even if we strongly discourage putting credentials into the URLs passed via the command-line, there _is_ support for that, and users _do_ do that. Let's scrub them before writing them to the reflog. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 274b9cc commit 46da295

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

builtin/clone.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
939939
{
940940
int is_bundle = 0, is_local;
941941
const char *repo_name, *repo, *work_tree, *git_dir;
942-
char *path, *dir;
942+
char *path, *dir, *display_repo = NULL;
943943
int dest_exists;
944944
const struct ref *refs, *remote_head;
945945
const struct ref *remote_head_points_at;
@@ -994,10 +994,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
994994
path = get_repo_path(repo_name, &is_bundle);
995995
if (path)
996996
repo = absolute_pathdup(repo_name);
997-
else if (!strchr(repo_name, ':'))
998-
die(_("repository '%s' does not exist"), repo_name);
999-
else
997+
else if (strchr(repo_name, ':')) {
1000998
repo = repo_name;
999+
display_repo = transport_anonymize_url(repo);
1000+
} else
1001+
die(_("repository '%s' does not exist"), repo_name);
10011002

10021003
/* no need to be strict, transport_set_option() will validate it again */
10031004
if (option_depth && atoi(option_depth) < 1)
@@ -1014,7 +1015,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
10141015
die(_("destination path '%s' already exists and is not "
10151016
"an empty directory."), dir);
10161017

1017-
strbuf_addf(&reflog_msg, "clone: from %s", repo);
1018+
strbuf_addf(&reflog_msg, "clone: from %s",
1019+
display_repo ? display_repo : repo);
1020+
free(display_repo);
10181021

10191022
if (option_bare)
10201023
work_tree = NULL;

builtin/fetch.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,8 +1765,13 @@ int cmd_fetch(int argc, const char **argv, const char *prefix)
17651765

17661766
/* Record the command line for the reflog */
17671767
strbuf_addstr(&default_rla, "fetch");
1768-
for (i = 1; i < argc; i++)
1769-
strbuf_addf(&default_rla, " %s", argv[i]);
1768+
for (i = 1; i < argc; i++) {
1769+
/* This handles non-URLs gracefully */
1770+
char *anon = transport_anonymize_url(argv[i]);
1771+
1772+
strbuf_addf(&default_rla, " %s", anon);
1773+
free(anon);
1774+
}
17701775

17711776
fetch_config_from_gitmodules(&submodule_fetch_jobs_config,
17721777
&recurse_submodules);

t/t5541-http-push-smart.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,21 @@ test_expect_success 'push status output scrubs password' '
456456
grep "^To $HTTPD_URL/smart/test_repo.git" status
457457
'
458458

459+
test_expect_success 'clone/fetch scrubs password from reflogs' '
460+
cd "$ROOT_PATH" &&
461+
git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
462+
reflog-test &&
463+
cd reflog-test &&
464+
test_commit prepare-for-force-fetch &&
465+
git switch -c away &&
466+
git fetch "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
467+
+master:master &&
468+
# should have been scrubbed down to vanilla URL
469+
git log -g master >reflog &&
470+
grep "$HTTPD_URL" reflog &&
471+
! grep "$HTTPD_URL_USER_PASS" reflog
472+
'
473+
459474
test_expect_success 'colorize errors/hints' '
460475
cd "$ROOT_PATH"/test_repo_clone &&
461476
test_must_fail git -c color.transport=always -c color.advice=always \

0 commit comments

Comments
 (0)