]> git.ipfire.org Git - thirdparty/git.git/commitdiff
credential: parse URL without host as empty host, not unset
authorJeff King <peff@peff.net>
Sun, 19 Apr 2020 03:48:05 +0000 (20:48 -0700)
committerJonathan Nieder <jrnieder@gmail.com>
Sun, 19 Apr 2020 23:10:57 +0000 (16:10 -0700)
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>
credential.c
http.c
t/t0300-credentials.sh

index eeeac3242e71caf3ab0740c0d5262f2fc4cc0c42..d1bb71b41adfc35a8f25fe8525814f9d56323d74 100644 (file)
@@ -373,8 +373,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
 
        if (proto_end - url > 0)
                c->protocol = xmemdupz(url, proto_end - url);
-       if (slash - host > 0)
-               c->host = url_decode_mem(host, slash - host);
+       c->host = url_decode_mem(host, slash - host);
        /* Trim leading and trailing slashes from path */
        while (*slash == '/')
                slash++;
diff --git a/http.c b/http.c
index a5bd5d62c22c054f82b9971fc1f320c643f1d6fb..29de6bbb23abfe3be6d4863909e4df3d31a35f95 100644 (file)
--- a/http.c
+++ b/http.c
@@ -524,6 +524,7 @@ static int has_cert_password(void)
                return 0;
        if (!cert_auth.password) {
                cert_auth.protocol = xstrdup("cert");
+               cert_auth.host = xstrdup("");
                cert_auth.username = xstrdup("");
                cert_auth.path = xstrdup(ssl_cert);
                credential_fill(&cert_auth);
index f4c5d7ff91b1fb965aca5d76cadede1c468f6c07..1c1010bc54cd567411d10521b5e1878239a4ffb0 100755 (executable)
@@ -414,4 +414,21 @@ test_expect_success 'url parser ignores embedded newlines' '
        EOF
 '
 
+test_expect_success 'host-less URLs are parsed as empty host' '
+       check fill "verbatim foo bar" <<-\EOF
+       url=cert:///path/to/cert.pem
+       --
+       protocol=cert
+       host=
+       path=path/to/cert.pem
+       username=foo
+       password=bar
+       --
+       verbatim: get
+       verbatim: protocol=cert
+       verbatim: host=
+       verbatim: path=path/to/cert.pem
+       EOF
+'
+
 test_done