@matheusbsbs / matheus-bagliano / commits / 24036686c4
Fork 0

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 <peff@peff.net> Reviewed-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jeff King committed Apr 18, 2020 at 20:48 UTC 24036686c4af84c9e84e486ef3debab6e6d8e6b5
3 files changed +19 -2
credential.c
+1 -2
@@ -373,8 +373,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
373
374 if (proto_end - url > 0)
375 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);
377 /* Trim leading and trailing slashes from path */
378 while (*slash == '/')
379 slash++;
http.c
+1
@@ -524,6 +524,7 @@ static int has_cert_password(void)
524 return 0;
525 if (!cert_auth.password) {
526 cert_auth.protocol = xstrdup("cert");
527 + cert_auth.host = xstrdup("");
528 cert_auth.username = xstrdup("");
529 cert_auth.path = xstrdup(ssl_cert);
530 credential_fill(&cert_auth);
t/t0300-credentials.sh
+17
@@ -414,4 +414,21 @@ test_expect_success 'url parser ignores embedded newlines' '
414 EOF
415 '
416
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 +
434 test_done