@matheusbsbs / matheus-bagliano / commits / e7fab62b73
Fork 0

credential: treat URL with empty scheme as invalid

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>

Jonathan Nieder committed Apr 18, 2020 at 20:54 UTC e7fab62b736cca3416660636e46f0be8386a5030
3 files changed +43 -3
credential.c
+2 -3
@@ -357,7 +357,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
357 * (3) proto://<user>:<pass>@<host>/...
358 */
359 proto_end = strstr(url, "://");
360 - if (!proto_end) {
360 + if (!proto_end || proto_end == url) {
361 if (!quiet)
362 warning(_("url has no scheme: %s"), url);
363 return -1;
@@ -382,8 +382,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
382 host = at + 1;
383 }
384
385 - if (proto_end - url > 0)
386 - c->protocol = xmemdupz(url, proto_end - url);
385 + c->protocol = xmemdupz(url, proto_end - url);
386 c->host = url_decode_mem(host, slash - host);
387 /* Trim leading and trailing slashes from path */
388 while (*slash == '/')
t/t5550-http-fetch-dumb.sh
+9
@@ -314,6 +314,15 @@ test_expect_success 'remote-http complains cleanly about malformed urls' '
314 test_i18ngrep "url has no scheme" stderr
315 '
316
317 +# NEEDSWORK: Writing commands to git-remote-curl can race against the latter
318 +# erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
319 +# learned to handle early remote helper failures more cleanly.
320 +test_expect_success 'remote-http complains cleanly about empty scheme' '
321 + test_must_fail ok=sigpipe git ls-remote \
322 + http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
323 + test_i18ngrep "url has no scheme" stderr
324 +'
325 +
326 test_expect_success 'redirects can be forbidden/allowed' '
327 test_must_fail git -c http.followRedirects=false \
328 clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
t/t7416-submodule-dash-url.sh
+32
@@ -92,6 +92,38 @@ test_expect_success 'fsck rejects relative URL resolving to missing scheme' '
92 grep gitmodulesUrl err
93 '
94
95 +test_expect_success 'fsck rejects empty URL scheme' '
96 + git checkout --orphan empty-scheme &&
97 + cat >.gitmodules <<-\EOF &&
98 + [submodule "foo"]
99 + url = http::://one.example.com/foo.git
100 + EOF
101 + git add .gitmodules &&
102 + test_tick &&
103 + git commit -m "gitmodules with empty URL scheme" &&
104 + test_when_finished "rm -rf dst" &&
105 + git init --bare dst &&
106 + git -C dst config transfer.fsckObjects true &&
107 + test_must_fail git push dst HEAD 2>err &&
108 + grep gitmodulesUrl err
109 +'
110 +
111 +test_expect_success 'fsck rejects relative URL resolving to empty scheme' '
112 + git checkout --orphan relative-empty-scheme &&
113 + cat >.gitmodules <<-\EOF &&
114 + [submodule "foo"]
115 + url = ../../../:://one.example.com/foo.git
116 + EOF
117 + git add .gitmodules &&
118 + test_tick &&
119 + git commit -m "relative gitmodules URL resolving to empty scheme" &&
120 + test_when_finished "rm -rf dst" &&
121 + git init --bare dst &&
122 + git -C dst config transfer.fsckObjects true &&
123 + test_must_fail git push dst HEAD 2>err &&
124 + grep gitmodulesUrl err
125 +'
126 +
127 test_expect_success 'fsck permits embedded newline with unrecognized scheme' '
128 git checkout --orphan newscheme &&
129 cat >.gitmodules <<-\EOF &&