Blame SOURCES/git-cve-2017-1000117.patch

6526e6
diff --git a/cache.h b/cache.h
6526e6
index 1a2cec0b8..b9fc3a8e3 100644
6526e6
--- a/cache.h
6526e6
+++ b/cache.h
6526e6
@@ -991,6 +991,14 @@ char *strip_path_suffix(const char *path, const char *suffix);
6526e6
 int daemon_avoid_alias(const char *path);
6526e6
 extern int is_ntfs_dotgit(const char *name);
6526e6
 
6526e6
+/*
6526e6
+ * Returns true iff "str" could be confused as a command-line option when
6526e6
+ * passed to a sub-program like "ssh". Note that this has nothing to do with
6526e6
+ * shell-quoting, which should be handled separately; we're assuming here that
6526e6
+ * the string makes it verbatim to the sub-program.
6526e6
+ */
6526e6
+int looks_like_command_line_option(const char *str);
6526e6
+
6526e6
 /**
6526e6
  * Return a newly allocated string with the evaluation of
6526e6
  * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
6526e6
diff --git a/connect.c b/connect.c
6526e6
index fd7ffe184..d77d39771 100644
6526e6
--- a/connect.c
6526e6
+++ b/connect.c
6526e6
@@ -553,6 +553,11 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
6526e6
 
6526e6
 	get_host_and_port(&host, &port);
6526e6
 
6526e6
+	if (looks_like_command_line_option(host))
6526e6
+		die("strange hostname '%s' blocked", host);
6526e6
+	if (looks_like_command_line_option(port))
6526e6
+		die("strange port '%s' blocked", port);
6526e6
+
6526e6
 	proxy = xmalloc(sizeof(*proxy));
6526e6
 	child_process_init(proxy);
6526e6
 	argv_array_push(&proxy->args, git_proxy_command);
6526e6
@@ -722,6 +727,9 @@ struct child_process *git_connect(int fd[2], const char *url,
6526e6
 		conn = xmalloc(sizeof(*conn));
6526e6
 		child_process_init(conn);
6526e6
 
6526e6
+		if (looks_like_command_line_option(path))
6526e6
+			die("strange pathname '%s' blocked", path);
6526e6
+
6526e6
 		strbuf_addstr(&cmd, prog);
6526e6
 		strbuf_addch(&cmd, ' ');
6526e6
 		sq_quote_buf(&cmd, path);
6526e6
@@ -754,6 +762,9 @@ struct child_process *git_connect(int fd[2], const char *url,
6526e6
 				return NULL;
6526e6
 			}
6526e6
 
6526e6
+			if (looks_like_command_line_option(ssh_host))
6526e6
+				die("strange hostname '%s' blocked", ssh_host);
6526e6
+
6526e6
 			ssh = getenv("GIT_SSH_COMMAND");
6526e6
 			if (!ssh) {
6526e6
 				const char *base;
6526e6
diff --git a/path.c b/path.c
6526e6
index 8b7e16812..b214ac3fe 100644
6526e6
--- a/path.c
6526e6
+++ b/path.c
6526e6
@@ -1178,6 +1178,11 @@ int is_ntfs_dotgit(const char *name)
6526e6
 		}
6526e6
 }
6526e6
 
6526e6
+int looks_like_command_line_option(const char *str)
6526e6
+{
6526e6
+	return str && str[0] == '-';
6526e6
+}
6526e6
+
6526e6
 char *xdg_config_home(const char *filename)
6526e6
 {
6526e6
 	const char *home, *config_home;
6526e6
diff --git a/t/t5532-fetch-proxy.sh b/t/t5532-fetch-proxy.sh
6526e6
index 5531bd1af..d3b2651b6 100755
6526e6
--- a/t/t5532-fetch-proxy.sh
6526e6
+++ b/t/t5532-fetch-proxy.sh
6526e6
@@ -40,4 +40,9 @@ test_expect_success 'fetch through proxy works' '
6526e6
 	test_cmp expect actual
6526e6
 '
6526e6
 
6526e6
+test_expect_success 'funny hostnames are rejected before running proxy' '
6526e6
+	test_must_fail git fetch git://-remote/repo.git 2>stderr &&
6526e6
+	! grep "proxying for" stderr
6526e6
+'
6526e6
+
6526e6
 test_done
6526e6
diff --git a/t/t5810-proto-disable-local.sh b/t/t5810-proto-disable-local.sh
6526e6
index 563592d8a..c1ef99b85 100755
6526e6
--- a/t/t5810-proto-disable-local.sh
6526e6
+++ b/t/t5810-proto-disable-local.sh
6526e6
@@ -11,4 +11,27 @@ test_expect_success 'setup repository to clone' '
6526e6
 test_proto "file://" file "file://$PWD"
6526e6
 test_proto "path" file .
6526e6
 
6526e6
+test_expect_success 'setup repo with dash' '
6526e6
+	git init --bare repo.git &&
6526e6
+	git push repo.git HEAD &&
6526e6
+	mv repo.git "$PWD/-repo.git"
6526e6
+'
6526e6
+
6526e6
+# This will fail even without our rejection because upload-pack will
6526e6
+# complain about the bogus option. So let's make sure that GIT_TRACE
6526e6
+# doesn't show us even running upload-pack.
6526e6
+#
6526e6
+# We must also be sure to use "fetch" and not "clone" here, as the latter
6526e6
+# actually canonicalizes our input into an absolute path (which is fine
6526e6
+# to allow).
6526e6
+test_expect_success 'repo names starting with dash are rejected' '
6526e6
+	rm -f trace.out &&
6526e6
+	test_must_fail env GIT_TRACE="$PWD/trace.out" git fetch -- -repo.git &&
6526e6
+	! grep upload-pack trace.out
6526e6
+'
6526e6
+
6526e6
+test_expect_success 'full paths still work' '
6526e6
+	git fetch "$PWD/-repo.git"
6526e6
+'
6526e6
+
6526e6
 test_done
6526e6
diff --git a/t/t5813-proto-disable-ssh.sh b/t/t5813-proto-disable-ssh.sh
6526e6
index a954ead8a..3f084ee30 100755
6526e6
--- a/t/t5813-proto-disable-ssh.sh
6526e6
+++ b/t/t5813-proto-disable-ssh.sh
6526e6
@@ -17,4 +17,27 @@ test_proto "host:path" ssh "remote:repo.git"
6526e6
 test_proto "ssh://" ssh "ssh://remote$PWD/remote/repo.git"
6526e6
 test_proto "git+ssh://" ssh "git+ssh://remote$PWD/remote/repo.git"
6526e6
 
6526e6
+# Don't even bother setting up a "-remote" directory, as ssh would generally
6526e6
+# complain about the bogus option rather than completing our request. Our
6526e6
+# fake wrapper actually _can_ handle this case, but it's more robust to
6526e6
+# simply confirm from its output that it did not run at all.
6526e6
+test_expect_success 'hostnames starting with dash are rejected' '
6526e6
+	test_must_fail git clone ssh://-remote/repo.git dash-host 2>stderr &&
6526e6
+	! grep ^ssh: stderr
6526e6
+'
6526e6
+
6526e6
+test_expect_success 'setup repo with dash' '
6526e6
+	git init --bare remote/-repo.git &&
6526e6
+	git push remote/-repo.git HEAD
6526e6
+'
6526e6
+
6526e6
+test_expect_success 'repo names starting with dash are rejected' '
6526e6
+	test_must_fail git clone remote:-repo.git dash-path 2>stderr &&
6526e6
+	! grep ^ssh: stderr
6526e6
+'
6526e6
+
6526e6
+test_expect_success 'full paths still work' '
6526e6
+	git clone "remote:$PWD/remote/-repo.git" dash-path
6526e6
+'
6526e6
+
6526e6
 test_done