Skip to content

Commit aab4043

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a17c56c commit aab4043

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

connect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,8 @@ struct child_process *git_connect(int fd[2], const char *url,
721721
strbuf_addch(&cmd, ' ');
722722
sq_quote_buf(&cmd, path);
723723

724+
/* remove repo-local variables from the environment */
725+
conn->env = local_repo_env;
724726
conn->in = conn->out = -1;
725727
if (protocol == PROTO_SSH) {
726728
const char *ssh;
@@ -778,8 +780,6 @@ struct child_process *git_connect(int fd[2], const char *url,
778780
}
779781
argv_array_push(&conn->args, ssh_host);
780782
} else {
781-
/* remove repo-local variables from the environment */
782-
conn->env = local_repo_env;
783783
conn->use_shell = 1;
784784
}
785785
argv_array_push(&conn->args, cmd.buf);

t/t5507-remote-environment.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)