Skip to content

Commit e6f11c1

Browse files
committed
Merge branch 'jk/connect-clear-env'
The ssh transport, just like any other transport over the network, did not clear GIT_* environment variables, but it is possible to use SendEnv and AcceptEnv to leak them to the remote invocation of Git, which is not a good idea at all. Explicitly clear them just like we do for the local transport. * jk/connect-clear-env: git_connect: clarify conn->use_shell flag git_connect: clear GIT_* environment for ssh
2 parents 7b09c45 + a48b409 commit e6f11c1

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

connect.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,13 @@ 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;
726+
conn->use_shell = 1;
724727
conn->in = conn->out = -1;
725728
if (protocol == PROTO_SSH) {
726729
const char *ssh;
727-
int putty, tortoiseplink = 0;
730+
int putty = 0, tortoiseplink = 0;
728731
char *ssh_host = hostandport;
729732
const char *port = NULL;
730733
get_host_and_port(&ssh_host, &port);
@@ -746,13 +749,17 @@ struct child_process *git_connect(int fd[2], const char *url,
746749
}
747750

748751
ssh = getenv("GIT_SSH_COMMAND");
749-
if (ssh) {
750-
conn->use_shell = 1;
751-
putty = 0;
752-
} else {
752+
if (!ssh) {
753753
const char *base;
754754
char *ssh_dup;
755755

756+
/*
757+
* GIT_SSH is the no-shell version of
758+
* GIT_SSH_COMMAND (and must remain so for
759+
* historical compatibility).
760+
*/
761+
conn->use_shell = 0;
762+
756763
ssh = getenv("GIT_SSH");
757764
if (!ssh)
758765
ssh = "ssh";
@@ -762,8 +769,9 @@ struct child_process *git_connect(int fd[2], const char *url,
762769

763770
tortoiseplink = !strcasecmp(base, "tortoiseplink") ||
764771
!strcasecmp(base, "tortoiseplink.exe");
765-
putty = !strcasecmp(base, "plink") ||
766-
!strcasecmp(base, "plink.exe") || tortoiseplink;
772+
putty = tortoiseplink ||
773+
!strcasecmp(base, "plink") ||
774+
!strcasecmp(base, "plink.exe");
767775

768776
free(ssh_dup);
769777
}
@@ -777,10 +785,6 @@ struct child_process *git_connect(int fd[2], const char *url,
777785
argv_array_push(&conn->args, port);
778786
}
779787
argv_array_push(&conn->args, ssh_host);
780-
} else {
781-
/* remove repo-local variables from the environment */
782-
conn->env = local_repo_env;
783-
conn->use_shell = 1;
784788
}
785789
argv_array_push(&conn->args, cmd.buf);
786790

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)