Skip to content

Commit 4c3dbbf

Browse files
committedMar 3, 2015
Merge branch 'jk/daemon-interpolate'
The "interpolated-path" option of "git daemon" inserted any string client declared on the "host=" capability request without checking. Sanitize and limit %H and %CH to a saner and a valid DNS name. * jk/daemon-interpolate: daemon: sanitize incoming virtual hostname t5570: test git-daemon's --interpolated-path option git_connect: let user override virtual-host we send to daemon
2 parents ef4cdb8 + b485373 commit 4c3dbbf

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed
 

‎connect.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -670,10 +670,20 @@ struct child_process *git_connect(int fd[2], const char *url,
670670
printf("Diag: path=%s\n", path ? path : "NULL");
671671
conn = NULL;
672672
} else if (protocol == PROTO_GIT) {
673+
/*
674+
* Set up virtual host information based on where we will
675+
* connect, unless the user has overridden us in
676+
* the environment.
677+
*/
678+
char *target_host = getenv("GIT_OVERRIDE_VIRTUAL_HOST");
679+
if (target_host)
680+
target_host = xstrdup(target_host);
681+
else
682+
target_host = xstrdup(hostandport);
683+
673684
/* These underlying connection commands die() if they
674685
* cannot connect.
675686
*/
676-
char *target_host = xstrdup(hostandport);
677687
if (git_use_proxy(hostandport))
678688
conn = git_proxy_connect(fd, hostandport);
679689
else

‎daemon.c

+45-5
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,45 @@ static void parse_host_and_port(char *hostport, char **host,
536536
}
537537
}
538538

539+
/*
540+
* Sanitize a string from the client so that it's OK to be inserted into a
541+
* filesystem path. Specifically, we disallow slashes, runs of "..", and
542+
* trailing and leading dots, which means that the client cannot escape
543+
* our base path via ".." traversal.
544+
*/
545+
static void sanitize_client_strbuf(struct strbuf *out, const char *in)
546+
{
547+
for (; *in; in++) {
548+
if (*in == '/')
549+
continue;
550+
if (*in == '.' && (!out->len || out->buf[out->len - 1] == '.'))
551+
continue;
552+
strbuf_addch(out, *in);
553+
}
554+
555+
while (out->len && out->buf[out->len - 1] == '.')
556+
strbuf_setlen(out, out->len - 1);
557+
}
558+
559+
static char *sanitize_client(const char *in)
560+
{
561+
struct strbuf out = STRBUF_INIT;
562+
sanitize_client_strbuf(&out, in);
563+
return strbuf_detach(&out, NULL);
564+
}
565+
566+
/*
567+
* Like sanitize_client, but we also perform any canonicalization
568+
* to make life easier on the admin.
569+
*/
570+
static char *canonicalize_client(const char *in)
571+
{
572+
struct strbuf out = STRBUF_INIT;
573+
sanitize_client_strbuf(&out, in);
574+
strbuf_tolower(&out);
575+
return strbuf_detach(&out, NULL);
576+
}
577+
539578
/*
540579
* Read the host as supplied by the client connection.
541580
*/
@@ -557,10 +596,10 @@ static void parse_host_arg(char *extra_args, int buflen)
557596
parse_host_and_port(val, &host, &port);
558597
if (port) {
559598
free(tcp_port);
560-
tcp_port = xstrdup(port);
599+
tcp_port = sanitize_client(port);
561600
}
562601
free(hostname);
563-
hostname = xstrdup_tolower(host);
602+
hostname = canonicalize_client(host);
564603
hostname_lookup_done = 0;
565604
}
566605

@@ -597,8 +636,9 @@ static void lookup_hostname(void)
597636
ip_address = xstrdup(addrbuf);
598637

599638
free(canon_hostname);
600-
canon_hostname = xstrdup(ai->ai_canonname ?
601-
ai->ai_canonname : ip_address);
639+
canon_hostname = ai->ai_canonname ?
640+
sanitize_client(ai->ai_canonname) :
641+
xstrdup(ip_address);
602642

603643
freeaddrinfo(ai);
604644
}
@@ -620,7 +660,7 @@ static void lookup_hostname(void)
620660
addrbuf, sizeof(addrbuf));
621661

622662
free(canon_hostname);
623-
canon_hostname = xstrdup(hent->h_name);
663+
canon_hostname = sanitize_client(hent->h_name);
624664
free(ip_address);
625665
ip_address = xstrdup(addrbuf);
626666
}

‎t/t5570-git-daemon.sh

+27
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,32 @@ test_expect_success 'push disabled' "test_remote_error 'service not enab
141141
test_expect_success 'read access denied' "test_remote_error -x 'no such repository' fetch repo.git "
142142
test_expect_success 'not exported' "test_remote_error -n 'repository not exported' fetch repo.git "
143143

144+
stop_git_daemon
145+
start_git_daemon --interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
146+
147+
test_expect_success 'access repo via interpolated hostname' '
148+
repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/localhost/interp.git" &&
149+
git init --bare "$repo" &&
150+
git push "$repo" HEAD &&
151+
>"$repo"/git-daemon-export-ok &&
152+
rm -rf tmp.git &&
153+
GIT_OVERRIDE_VIRTUAL_HOST=localhost \
154+
git clone --bare "$GIT_DAEMON_URL/interp.git" tmp.git &&
155+
rm -rf tmp.git &&
156+
GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
157+
git clone --bare "$GIT_DAEMON_URL/interp.git" tmp.git
158+
'
159+
160+
test_expect_success 'hostname cannot break out of directory' '
161+
rm -rf tmp.git &&
162+
repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/../escape.git" &&
163+
git init --bare "$repo" &&
164+
git push "$repo" HEAD &&
165+
>"$repo"/git-daemon-export-ok &&
166+
test_must_fail \
167+
env GIT_OVERRIDE_VIRTUAL_HOST=.. \
168+
git clone --bare "$GIT_DAEMON_URL/escape.git" tmp.git
169+
'
170+
144171
stop_git_daemon
145172
test_done

0 commit comments

Comments
 (0)
Please sign in to comment.