Skip to content

Commit a397e9c

Browse files
committed
Merge branch 'jk/credential-parsing-end-of-host-in-URL'
Parsing of URL for the credential helper has been corrected. * jk/credential-parsing-end-of-host-in-URL: credential: treat "?" and "#" in URLs as end of host
2 parents d6d561d + 4c5971e commit a397e9c

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

credential.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,14 @@ int credential_from_url_gently(struct credential *c, const char *url,
399399
cp = proto_end + 3;
400400
at = strchr(cp, '@');
401401
colon = strchr(cp, ':');
402-
slash = strchrnul(cp, '/');
402+
403+
/*
404+
* A query or fragment marker before the slash ends the host portion.
405+
* We'll just continue to call this "slash" for simplicity. Notably our
406+
* "trim leading slashes" part won't skip over this part of the path,
407+
* but that's what we'd want.
408+
*/
409+
slash = cp + strcspn(cp, "/?#");
403410

404411
if (!at || slash <= at) {
405412
/* Case (1) */

t/t0300-credentials.sh

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ test_expect_success 'url parser rejects embedded newlines' '
532532
url=https://one.example.com?%0ahost=two.example.com/
533533
EOF
534534
cat >expect <<-\EOF &&
535-
warning: url contains a newline in its host component: https://one.example.com?%0ahost=two.example.com/
535+
warning: url contains a newline in its path component: https://one.example.com?%0ahost=two.example.com/
536536
fatal: credential url cannot be parsed: https://one.example.com?%0ahost=two.example.com/
537537
EOF
538538
test_i18ncmp expect stderr
@@ -575,4 +575,38 @@ test_expect_success 'credential system refuses to work with missing protocol' '
575575
test_i18ncmp expect stderr
576576
'
577577

578+
# usage: check_host_and_path <url> <expected-host> <expected-path>
579+
check_host_and_path () {
580+
# we always parse the path component, but we need this to make sure it
581+
# is passed to the helper
582+
test_config credential.useHTTPPath true &&
583+
check fill "verbatim user pass" <<-EOF
584+
url=$1
585+
--
586+
protocol=https
587+
host=$2
588+
path=$3
589+
username=user
590+
password=pass
591+
--
592+
verbatim: get
593+
verbatim: protocol=https
594+
verbatim: host=$2
595+
verbatim: path=$3
596+
EOF
597+
}
598+
599+
test_expect_success 'url parser handles bare query marker' '
600+
check_host_and_path https://example.com?foo.git example.com ?foo.git
601+
'
602+
603+
test_expect_success 'url parser handles bare fragment marker' '
604+
check_host_and_path https://example.com#foo.git example.com "#foo.git"
605+
'
606+
607+
test_expect_success 'url parser not confused by encoded markers' '
608+
check_host_and_path https://example.com%23%3f%2f/foo.git \
609+
"example.com#?/" foo.git
610+
'
611+
578612
test_done

0 commit comments

Comments
 (0)