Skip to content

Commit 9a121b0

Browse files
dschogitster
authored andcommitted
credential: handle credential.<partial-URL>.<key> again
In the patches for CVE-2020-11008, the ability to specify credential settings in the config for partial URLs got lost. For example, it used to be possible to specify a credential helper for a specific protocol: [credential "https://"] helper = my-https-helper Likewise, it used to be possible to configure settings for a specific host, e.g.: [credential "dev.azure.com"] useHTTPPath = true Let's reinstate this behavior. While at it, increase the test coverage to document and verify the behavior with a couple other categories of partial URLs. Signed-off-by: Johannes Schindelin <[email protected]> Reviewed-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6828e59 commit 9a121b0

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

credential.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ int credential_match(const struct credential *want,
3535
#undef CHECK
3636
}
3737

38+
39+
static int credential_from_potentially_partial_url(struct credential *c,
40+
const char *url);
41+
3842
static int credential_config_callback(const char *var, const char *value,
3943
void *data)
4044
{
@@ -53,7 +57,13 @@ static int credential_config_callback(const char *var, const char *value,
5357
char *url = xmemdupz(key, dot - key);
5458
int matched;
5559

56-
credential_from_url(&want, url);
60+
if (credential_from_potentially_partial_url(&want, url) < 0) {
61+
warning(_("skipping credential lookup for key: %s"),
62+
var);
63+
credential_clear(&want);
64+
free(url);
65+
return 0;
66+
}
5767
matched = credential_match(&want, c);
5868

5969
credential_clear(&want);
@@ -430,6 +440,12 @@ static int credential_from_url_1(struct credential *c, const char *url,
430440
return 0;
431441
}
432442

443+
static int credential_from_potentially_partial_url(struct credential *c,
444+
const char *url)
445+
{
446+
return credential_from_url_1(c, url, 1, 0);
447+
}
448+
433449
int credential_from_url_gently(struct credential *c, const char *url, int quiet)
434450
{
435451
return credential_from_url_1(c, url, 0, quiet);

t/t0300-credentials.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,4 +448,42 @@ test_expect_success 'credential system refuses to work with missing protocol' '
448448
test_i18ncmp expect stderr
449449
'
450450

451+
test_expect_success 'credential config with partial URLs' '
452+
echo "echo password=yep" | write_script git-credential-yep &&
453+
test_write_lines url=https://[email protected]/repo.git >stdin &&
454+
for partial in \
455+
example.com \
456+
457+
https:// \
458+
https://example.com \
459+
https://example.com/ \
460+
461+
https://[email protected]/ \
462+
https://example.com/repo.git \
463+
https://[email protected]/repo.git \
464+
/repo.git
465+
do
466+
git -c credential.$partial.helper=yep \
467+
credential fill <stdin >stdout &&
468+
grep yep stdout ||
469+
return 1
470+
done &&
471+
472+
for partial in \
473+
dont.use.this \
474+
http:// \
475+
/repo
476+
do
477+
git -c credential.$partial.helper=yep \
478+
credential fill <stdin >stdout &&
479+
! grep yep stdout ||
480+
return 1
481+
done &&
482+
483+
git -c credential.$partial.helper=yep \
484+
-c credential.with%0anewline.username=uh-oh \
485+
credential fill <stdin >stdout 2>stderr &&
486+
test_i18ngrep "skipping credential lookup for key" stderr
487+
'
488+
451489
test_done

0 commit comments

Comments
 (0)