Skip to content

Commit 05c1fb8

Browse files
committed
http: avoid generating double slashes in url
Prior to this change, given a remote url with a trailing slash, such as http://localhost/a/, service requests would contain a double slash: http://localhost/a//info/refs?service=git-receive-pack. Detect and prevent that. Updates libgit2#5321
1 parent 2f6f10b commit 05c1fb8

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/transports/http.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,22 @@ static int gen_request(
183183
{
184184
http_subtransport *t = OWNING_SUBTRANSPORT(s);
185185
const char *path = t->server.url.path ? t->server.url.path : "/";
186+
const char *service_url = s->service_url;
186187
size_t i;
188+
/* If path already ends in /, remove the leading slash from service_url */
189+
if ((git__suffixcmp(path, "/") == 0) && (git__prefixcmp(service_url, "/") == 0))
190+
service_url++;
187191

188192
if (t->proxy_opts.type == GIT_PROXY_SPECIFIED)
189193
git_buf_printf(buf, "%s %s://%s:%s%s%s HTTP/1.1\r\n",
190194
s->verb,
191195
t->server.url.scheme,
192196
t->server.url.host,
193197
t->server.url.port,
194-
path, s->service_url);
198+
path, service_url);
195199
else
196200
git_buf_printf(buf, "%s %s%s HTTP/1.1\r\n",
197-
s->verb, path, s->service_url);
201+
s->verb, path, service_url);
198202

199203
git_buf_puts(buf, "User-Agent: ");
200204
git_http__user_agent(buf);

src/transports/winhttp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,15 @@ static int winhttp_stream_connect(winhttp_stream *s)
373373
int default_connect_timeout = DEFAULT_CONNECT_TIMEOUT;
374374
DWORD autologon_policy = WINHTTP_AUTOLOGON_SECURITY_LEVEL_HIGH;
375375

376+
const char *service_url = s->service_url;
376377
size_t i;
377378
const git_proxy_options *proxy_opts;
378379

380+
/* If path already ends in /, remove the leading slash from service_url */
381+
if ((git__suffixcmp(t->server.url.path, "/") == 0) && (git__prefixcmp(service_url, "/") == 0))
382+
service_url++;
379383
/* Prepare URL */
380-
git_buf_printf(&buf, "%s%s", t->server.url.path, s->service_url);
384+
git_buf_printf(&buf, "%s%s", t->server.url.path, service_url);
381385

382386
if (git_buf_oom(&buf))
383387
return -1;

0 commit comments

Comments
 (0)