Skip to content

Commit 2403668

Browse files
peffjrn
authored andcommitted
credential: parse URL without host as empty host, not unset
We may feed a URL like "cert:///path/to/cert.pem" into the credential machinery to get the key for a client-side certificate. That credential has no hostname field, which is about to be disallowed (to avoid confusion with protocols where a helper _would_ expect a hostname). This means as of the next patch, credential helpers won't work for unlocking certs. Let's fix that by doing two things: - when we parse a url with an empty host, set the host field to the empty string (asking only to match stored entries with an empty host) rather than NULL (asking to match _any_ host). - when we build a cert:// credential by hand, similarly assign an empty string It's the latter that is more likely to impact real users in practice, since it's what's used for http connections. But we don't have good infrastructure to test it. The url-parsing version will help anybody using git-credential in a script, and is easy to test. Signed-off-by: Jeff King <[email protected]> Reviewed-by: Taylor Blau <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent 73aafe9 commit 2403668

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

credential.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
373373

374374
if (proto_end - url > 0)
375375
c->protocol = xmemdupz(url, proto_end - url);
376-
if (slash - host > 0)
377-
c->host = url_decode_mem(host, slash - host);
376+
c->host = url_decode_mem(host, slash - host);
378377
/* Trim leading and trailing slashes from path */
379378
while (*slash == '/')
380379
slash++;

http.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ static int has_cert_password(void)
524524
return 0;
525525
if (!cert_auth.password) {
526526
cert_auth.protocol = xstrdup("cert");
527+
cert_auth.host = xstrdup("");
527528
cert_auth.username = xstrdup("");
528529
cert_auth.path = xstrdup(ssl_cert);
529530
credential_fill(&cert_auth);

t/t0300-credentials.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,21 @@ test_expect_success 'url parser ignores embedded newlines' '
414414
EOF
415415
'
416416

417+
test_expect_success 'host-less URLs are parsed as empty host' '
418+
check fill "verbatim foo bar" <<-\EOF
419+
url=cert:///path/to/cert.pem
420+
--
421+
protocol=cert
422+
host=
423+
path=path/to/cert.pem
424+
username=foo
425+
password=bar
426+
--
427+
verbatim: get
428+
verbatim: protocol=cert
429+
verbatim: host=
430+
verbatim: path=path/to/cert.pem
431+
EOF
432+
'
433+
417434
test_done

0 commit comments

Comments
 (0)