@matheusbsbs / matheus-bagliano / commits / aab4043851
Fork 0

git_connect: clear GIT_* environment for ssh

When we "switch" to another local repository to run the server side of a fetch or push, we must clear the variables in local_repo_env so that our local $GIT_DIR, etc, do not pollute the upload-pack or receive-pack that is executing in the "remote" repository. We have never done so for ssh connections. For the most part, nobody has noticed because ssh will not pass unknown environment variables by default. However, it is not out of the question for a user to configure ssh to pass along GIT_* variables using SendEnv/AcceptEnv. We can demonstrate the problem by using "git -c" on a local command and seeing its impact on a remote repository. This config ends up in $GIT_CONFIG_PARAMETERS. In the local case, the config has no impact, but in the ssh transport, it does (our test script has a fake ssh that passes through all environment variables; this isn't normal, but does simulate one possible setup). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 4, 2015 at 18:40 UTC aab40438511dd2e4505c0850478e79773aa6721c
2 files changed +36 -2
connect.c
+2 -2
@@ -721,6 +721,8 @@ struct child_process *git_connect(int fd[2], const char *url,
721 strbuf_addch(&cmd, ' ');
722 sq_quote_buf(&cmd, path);
723
724 + /* remove repo-local variables from the environment */
725 + conn->env = local_repo_env;
726 conn->in = conn->out = -1;
727 if (protocol == PROTO_SSH) {
728 const char *ssh;
@@ -778,8 +780,6 @@ struct child_process *git_connect(int fd[2], const char *url,
780 }
781 argv_array_push(&conn->args, ssh_host);
782 } else {
781 - /* remove repo-local variables from the environment */
782 - conn->env = local_repo_env;
783 conn->use_shell = 1;
784 }
785 argv_array_push(&conn->args, cmd.buf);
t/t5507-remote-environment.sh new
+34
@@ -0,0 +1,34 @@
1 +#!/bin/sh
2 +
3 +test_description='check environment showed to remote side of transports'
4 +. ./test-lib.sh
5 +
6 +test_expect_success 'set up "remote" push situation' '
7 + test_commit one &&
8 + git config push.default current &&
9 + git init remote
10 +'
11 +
12 +test_expect_success 'set up fake ssh' '
13 + GIT_SSH_COMMAND="f() {
14 + cd \"\$TRASH_DIRECTORY\" &&
15 + eval \"\$2\"
16 + }; f" &&
17 + export GIT_SSH_COMMAND &&
18 + export TRASH_DIRECTORY
19 +'
20 +
21 +# due to receive.denyCurrentBranch=true
22 +test_expect_success 'confirm default push fails' '
23 + test_must_fail git push remote
24 +'
25 +
26 +test_expect_success 'config does not travel over same-machine push' '
27 + test_must_fail git -c receive.denyCurrentBranch=false push remote
28 +'
29 +
30 +test_expect_success 'config does not travel over ssh push' '
31 + test_must_fail git -c receive.denyCurrentBranch=false push host:remote
32 +'
33 +
34 +test_done