Skip to content

Commit e7fab62

Browse files
committed
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 <[email protected]>
1 parent c44088e commit e7fab62

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

credential.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
357357
* (3) proto://<user>:<pass>@<host>/...
358358
*/
359359
proto_end = strstr(url, "://");
360-
if (!proto_end) {
360+
if (!proto_end || proto_end == url) {
361361
if (!quiet)
362362
warning(_("url has no scheme: %s"), url);
363363
return -1;
@@ -382,8 +382,7 @@ int credential_from_url_gently(struct credential *c, const char *url,
382382
host = at + 1;
383383
}
384384

385-
if (proto_end - url > 0)
386-
c->protocol = xmemdupz(url, proto_end - url);
385+
c->protocol = xmemdupz(url, proto_end - url);
387386
c->host = url_decode_mem(host, slash - host);
388387
/* Trim leading and trailing slashes from path */
389388
while (*slash == '/')

t/t5550-http-fetch-dumb.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,15 @@ test_expect_success 'remote-http complains cleanly about malformed urls' '
314314
test_i18ngrep "url has no scheme" stderr
315315
'
316316

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+
317326
test_expect_success 'redirects can be forbidden/allowed' '
318327
test_must_fail git -c http.followRedirects=false \
319328
clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&

t/t7416-submodule-dash-url.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ test_expect_success 'fsck rejects relative URL resolving to missing scheme' '
9292
grep gitmodulesUrl err
9393
'
9494

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+
95127
test_expect_success 'fsck permits embedded newline with unrecognized scheme' '
96128
git checkout --orphan newscheme &&
97129
cat >.gitmodules <<-\EOF &&

0 commit comments

Comments
 (0)