]> git.ipfire.org Git - thirdparty/git.git/commitdiff
credential: treat URL with empty scheme as invalid
authorJonathan Nieder <jrnieder@gmail.com>
Sun, 19 Apr 2020 03:54:57 +0000 (20:54 -0700)
committerJonathan Nieder <jrnieder@gmail.com>
Sun, 19 Apr 2020 23:10:58 +0000 (16:10 -0700)
Until "credential: refuse to operate when missing host or protocol",
Git's credential handling code interpreted URLs with empty scheme to
mean "give me credentials matching this host for any protocol".

Luckily libcurl does not recognize such URLs (it tries to look for a
protocol named "" and fails). Just in case that changes, let's reject
them within Git as well. This way, credential_from_url is guaranteed to
always produce a "struct credential" with protocol and host set.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
credential.c
t/t5550-http-fetch-dumb.sh
t/t7416-submodule-dash-url.sh

index aedb64574def1542f24d9e8a09322779464cc97d..64a841eddca9b48f16e649ead8338ad2e1076563 100644 (file)
@@ -357,7 +357,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
         *   (3) proto://<user>:<pass>@<host>/...
         */
        proto_end = strstr(url, "://");
-       if (!proto_end) {
+       if (!proto_end || proto_end == url) {
                if (!quiet)
                        warning(_("url has no scheme: %s"), url);
                return -1;
@@ -382,8 +382,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
                host = at + 1;
        }
 
-       if (proto_end - url > 0)
-               c->protocol = xmemdupz(url, proto_end - url);
+       c->protocol = xmemdupz(url, proto_end - url);
        c->host = url_decode_mem(host, slash - host);
        /* Trim leading and trailing slashes from path */
        while (*slash == '/')
index 517202e47726a90cb43811649bd7b483ea1030c6..86f6eeaf71d925e5fe97a6c2cdf8e6552a50ec91 100755 (executable)
@@ -314,6 +314,15 @@ test_expect_success 'remote-http complains cleanly about malformed urls' '
        test_i18ngrep "url has no scheme" stderr
 '
 
+# NEEDSWORK: Writing commands to git-remote-curl can race against the latter
+# erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
+# learned to handle early remote helper failures more cleanly.
+test_expect_success 'remote-http complains cleanly about empty scheme' '
+       test_must_fail ok=sigpipe git ls-remote \
+               http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
+       test_i18ngrep "url has no scheme" stderr
+'
+
 test_expect_success 'redirects can be forbidden/allowed' '
        test_must_fail git -c http.followRedirects=false \
                clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
index 249dc3d1d462f3bdb938313bf6f1e2a20063b1b4..9309040373c83211c4ee96da4202c40d5189fc89 100755 (executable)
@@ -92,6 +92,38 @@ test_expect_success 'fsck rejects relative URL resolving to missing scheme' '
        grep gitmodulesUrl err
 '
 
+test_expect_success 'fsck rejects empty URL scheme' '
+       git checkout --orphan empty-scheme &&
+       cat >.gitmodules <<-\EOF &&
+       [submodule "foo"]
+               url = http::://one.example.com/foo.git
+       EOF
+       git add .gitmodules &&
+       test_tick &&
+       git commit -m "gitmodules with empty URL scheme" &&
+       test_when_finished "rm -rf dst" &&
+       git init --bare dst &&
+       git -C dst config transfer.fsckObjects true &&
+       test_must_fail git push dst HEAD 2>err &&
+       grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects relative URL resolving to empty scheme' '
+       git checkout --orphan relative-empty-scheme &&
+       cat >.gitmodules <<-\EOF &&
+       [submodule "foo"]
+               url = ../../../:://one.example.com/foo.git
+       EOF
+       git add .gitmodules &&
+       test_tick &&
+       git commit -m "relative gitmodules URL resolving to empty scheme" &&
+       test_when_finished "rm -rf dst" &&
+       git init --bare dst &&
+       git -C dst config transfer.fsckObjects true &&
+       test_must_fail git push dst HEAD 2>err &&
+       grep gitmodulesUrl err
+'
+
 test_expect_success 'fsck permits embedded newline with unrecognized scheme' '
        git checkout --orphan newscheme &&
        cat >.gitmodules <<-\EOF &&