Skip to content

Commit f52b0cb

Browse files
dschogitster
authored andcommitted
credential: optionally allow partial URLs in credential_from_url_gently()
Prior to the fixes for CVE-2020-11008, we were _very_ lenient in what we required from a URL in order to parse it into a `struct credential`. That led to serious vulnerabilities. There was one call site, though, that really needed that leniency: when parsing config settings a la `credential.dev.azure.com.useHTTPPath`. Settings like this might be desired when users want to use, say, a given user name on a given host, regardless of the protocol to be used. In preparation for fixing that bug, let's refactor the code to optionally allow for partial URLs. For the moment, this functionality is only exposed via the now-renamed function `credential_from_url_1()`, but it is not used. The intention is to make it easier to verify that this commit does not change the existing behavior unless explicitly allowing for partial URLs. Please note that this patch does more than just reinstating a way to imitate the behavior before those CVE-2020-11008 fixes: Before that, we would simply ignore URLs without a protocol. In other words, misleadingly, the following setting would be applied to _all_ URLs: [credential "example.com"] username = that-me The obvious intention is to match the host name only. With this patch, we allow precisely that: when parsing the URL with non-zero `allow_partial_url`, we do not simply return success if there was no protocol, but we simply leave the protocol unset and continue parsing the URL. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af6b65d commit f52b0cb

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

credential.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,31 @@ static int check_url_component(const char *url, int quiet,
377377
return -1;
378378
}
379379

380-
int credential_from_url_gently(struct credential *c, const char *url,
381-
int quiet)
380+
/*
381+
* Potentially-partial URLs can, but do not have to, contain
382+
*
383+
* - a protocol (or scheme) of the form "<protocol>://"
384+
*
385+
* - a host name (the part after the protocol and before the first slash after
386+
* that, if any)
387+
*
388+
* - a user name and potentially a password (as "<user>[:<password>]@" part of
389+
* the host name)
390+
*
391+
* - a path (the part after the host name, if any, starting with the slash)
392+
*
393+
* Missing parts will be left unset in `struct credential`. Thus, `https://`
394+
* will have only the `protocol` set, `example.com` only the host name, and
395+
* `/git` only the path.
396+
*
397+
* Note that an empty host name in an otherwise fully-qualified URL (e.g.
398+
* `cert:///path/to/cert.pem`) will be treated as unset if we expect the URL to
399+
* be potentially partial, and only then (otherwise, the empty string is used).
400+
*
401+
* The credential_from_url() function does not allow partial URLs.
402+
*/
403+
static int credential_from_url_1(struct credential *c, const char *url,
404+
int allow_partial_url, int quiet)
382405
{
383406
const char *at, *colon, *cp, *slash, *host, *proto_end;
384407

@@ -391,12 +414,12 @@ int credential_from_url_gently(struct credential *c, const char *url,
391414
* (3) proto://<user>:<pass>@<host>/...
392415
*/
393416
proto_end = strstr(url, "://");
394-
if (!proto_end || proto_end == url) {
417+
if (!allow_partial_url && (!proto_end || proto_end == url)) {
395418
if (!quiet)
396419
warning(_("url has no scheme: %s"), url);
397420
return -1;
398421
}
399-
cp = proto_end + 3;
422+
cp = proto_end ? proto_end + 3 : url;
400423
at = strchr(cp, '@');
401424
colon = strchr(cp, ':');
402425
slash = strchrnul(cp, '/');
@@ -420,8 +443,10 @@ int credential_from_url_gently(struct credential *c, const char *url,
420443
host = at + 1;
421444
}
422445

423-
c->protocol = xmemdupz(url, proto_end - url);
424-
c->host = url_decode_mem(host, slash - host);
446+
if (proto_end && proto_end - url > 0)
447+
c->protocol = xmemdupz(url, proto_end - url);
448+
if (!allow_partial_url || slash - host > 0)
449+
c->host = url_decode_mem(host, slash - host);
425450
/* Trim leading and trailing slashes from path */
426451
while (*slash == '/')
427452
slash++;
@@ -443,6 +468,11 @@ int credential_from_url_gently(struct credential *c, const char *url,
443468
return 0;
444469
}
445470

471+
int credential_from_url_gently(struct credential *c, const char *url, int quiet)
472+
{
473+
return credential_from_url_1(c, url, 0, quiet);
474+
}
475+
446476
void credential_from_url(struct credential *c, const char *url)
447477
{
448478
if (credential_from_url_gently(c, url, 0) < 0)

0 commit comments

Comments
 (0)