credential: treat "?" and "#" in URLs as end of host

It's unusual to see:

without an intervening slash, like:

or even:

but it is a valid end to the hostname (actually "authority component")
according to RFC 3986. Likewise for "#".

And curl will parse the URL according to the standard, meaning it will
contact, but our credential code would ask about a bogus
hostname with a "?" in it. Let's make sure we follow the standard, and
more importantly ask about the same hosts that curl will be talking to.

It would be nice if we could just ask curl to parse the URL for us. But
it didn't grow a URL-parsing API until 7.62, so we'd be stuck with
fallback code either way. Plus we'd need this code in the main Git
binary, where we've tried to avoid having a link dependency on libcurl.

But let's at least fix our parser. Moving to curl's parser would prevent
other potential discrepancies, but this gives us immediate relief for
the known problem, and would help our fallback code if we eventually use

Signed-off-by: Jeff King <>
Signed-off-by: Junio C Hamano <>
Jeff King 2020-04-14 17:43:04 -04:00 committed by Junio C Hamano
parent de49261b05
commit 4c5971e18a
2 changed files with 43 additions and 2 deletions

View File

@ -388,7 +388,14 @@ int credential_from_url_gently(struct credential *c, const char *url,
cp = proto_end + 3;
at = strchr(cp, '@');
colon = strchr(cp, ':');
slash = strchrnul(cp, '/');
* A query or fragment marker before the slash ends the host portion.
* We'll just continue to call this "slash" for simplicity. Notably our
* "trim leading slashes" part won't skip over this part of the path,
* but that's what we'd want.
slash = cp + strcspn(cp, "/?#");
if (!at || slash <= at) {
/* Case (1) */

View File

@ -443,11 +443,45 @@ test_expect_success 'url parser ignores embedded newlines' '
warning: url contains a newline in its host component:
warning: url contains a newline in its path component:
warning: skipping credential lookup for url:
askpass: Username:
askpass: Password:
# usage: check_host_and_path <url> <expected-host> <expected-path>
check_host_and_path () {
# we always parse the path component, but we need this to make sure it
# is passed to the helper
test_config credential.useHTTPPath true &&
check fill "verbatim user pass" <<-EOF
verbatim: get
verbatim: protocol=https
verbatim: host=$2
verbatim: path=$3
test_expect_success 'url parser handles bare query marker' '
check_host_and_path ?foo.git
test_expect_success 'url parser handles bare fragment marker' '
check_host_and_path "#foo.git"
test_expect_success 'url parser not confused by encoded markers' '
check_host_and_path \
"" foo.git