Skip to contentSkip to conversation

http-backend: Protect GIT_PROJECT_ROOT from /../ requests

PATCH New Shawn O. Pearce 1 participant 1 message Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 http-backend: Protect GIT_PROJECT_ROOT from /../ requests

Eons ago HPA taught git-daemon how to protect itself from /../ attacks, which Junio brought back into service in d79374c7b58d ("daemon.c and path.enter_repo(): revamp path validation").

I did not carry this into git-http-backend as originally we relied only upon PATH_TRANSLATED, and assumed the HTTP server had done its access control checks to validate the resolved path was within a directory permitting access from the remote client. This would usually be sufficient to protect a server from requests for its /etc/passwd file by http://host/smart/../etc/passwd sorts of URLs.

However in 917adc036086 Mark Lodato added GIT_PROJECT_ROOT as an additional method of configuring the CGI. When this environment variable is used the web server does not generate the final access path and therefore may blindly pass through "/../etc/passwd" in PATH_INFO under the assumption that "/../" might have special meaning to the invoked CGI.

Instead of permitting these sorts of malformed path requests, we now reject them back at the client, with an error message for the server log. This matches git-daemon behavior.

---
 cache.h                 |    1 +
 daemon.c                |   49 +----------------------------------------------
 http-backend.c          |    6 +++++
 path.c                  |   47 +++++++++++++++++++++++++++++++++++++++++++++
 t/t5560-http-backend.sh |   31 +++++++++++++++++++++++++++++
 5 files changed, 86 insertions(+), 48 deletions(-)
cache.h +1 -0
index 4e283be..ecbd88a 100644
@@ -656,6 +656,7 @@ const char *make_relative_path(const char *abs, const char *base);
 int normalize_path_copy(char *dst, const char *src);
 int longest_ancestor_length(const char *path, const char *prefix_list);
 char *strip_path_suffix(const char *path, const char *suffix);
+int daemon_avoid_alias(const char *path);
 
 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
 extern int sha1_object_info(const unsigned char *, unsigned long *);
daemon.c +1 -48

61 lines not on this page · raw message

http-backend.c +6 -0
index 646e910..f8ea9d7 100644
@@ -559,7 +559,13 @@ static char* getdir(void)
 	if (root && *root) {
 		if (!pathinfo || !*pathinfo)
 			die("GIT_PROJECT_ROOT is set but PATH_INFO is not");
+		if (daemon_avoid_alias(pathinfo))
+			die("'%s': aliased", pathinfo);
 		strbuf_addstr(&buf, root);
+		if (buf.buf[buf.len - 1] != '/')
+			strbuf_addch(&buf, '/');
+		if (pathinfo[0] == '/')
+			pathinfo++;
 		strbuf_addstr(&buf, pathinfo);
 		return strbuf_detach(&buf, NULL);
 	} else if (path && *path) {
path.c +47 -0

50 lines not on this page · raw message

t/t5560-http-backend.sh +31 -0
index 908ba07..ed034bc 100755
@@ -146,6 +146,37 @@ test_expect_success 'http.receivepack false' '
 	POST git-receive-pack 0000 "403 Forbidden"
 '
 
+run_backend() {
+	REQUEST_METHOD=GET \
+	GIT_PROJECT_ROOT="$HTTPD_DOCUMENT_ROOT_PATH" \
+	PATH_INFO="$2" \
+	git http-backend >act.out 2>act.err
+}
+
+path_info() {
+	if test $1 = 0; then
+		run_backend "$2"
+	else
+		test_must_fail run_backend "$2" &&
+		echo "fatal: '$2': aliased" >exp.err &&
+		test_cmp exp.err act.err
+	fi
+}
+
+test_expect_success 'http-backend blocks bad PATH_INFO' '
+	config http.getanyfile true &&
+
+	run_backend 0 /repo.git/HEAD &&
+
+	run_backend 1 /repo.git/../HEAD &&
+	run_backend 1 /../etc/passwd &&
+	run_backend 1 ../etc/passwd &&
+	run_backend 1 /etc//passwd &&
+	run_backend 1 /etc/./passwd &&
+	run_backend 1 /etc/.../passwd &&
+	run_backend 1 //domain/data.txt
+'
+
 cat >exp <<EOF
 
 ###  refs/heads/master
Signature · 1 line
1.6.5.2.351.g09432