Skip to content

Commit a4f234b

Browse files
committed
Merge branch 'jk/ssh-funny-url' into maint-2.7
2 parents c8dd1e3 + aeeb2d4 commit a4f234b

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

cache.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,14 @@ char *strip_path_suffix(const char *path, const char *suffix);
991991
int daemon_avoid_alias(const char *path);
992992
extern int is_ntfs_dotgit(const char *name);
993993

994+
/*
995+
* Returns true iff "str" could be confused as a command-line option when
996+
* passed to a sub-program like "ssh". Note that this has nothing to do with
997+
* shell-quoting, which should be handled separately; we're assuming here that
998+
* the string makes it verbatim to the sub-program.
999+
*/
1000+
int looks_like_command_line_option(const char *str);
1001+
9941002
/**
9951003
* Return a newly allocated string with the evaluation of
9961004
* "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise

connect.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
553553

554554
get_host_and_port(&host, &port);
555555

556+
if (looks_like_command_line_option(host))
557+
die("strange hostname '%s' blocked", host);
558+
if (looks_like_command_line_option(port))
559+
die("strange port '%s' blocked", port);
560+
556561
proxy = xmalloc(sizeof(*proxy));
557562
child_process_init(proxy);
558563
argv_array_push(&proxy->args, git_proxy_command);
@@ -722,6 +727,9 @@ struct child_process *git_connect(int fd[2], const char *url,
722727
conn = xmalloc(sizeof(*conn));
723728
child_process_init(conn);
724729

730+
if (looks_like_command_line_option(path))
731+
die("strange pathname '%s' blocked", path);
732+
725733
strbuf_addstr(&cmd, prog);
726734
strbuf_addch(&cmd, ' ');
727735
sq_quote_buf(&cmd, path);
@@ -754,6 +762,9 @@ struct child_process *git_connect(int fd[2], const char *url,
754762
return NULL;
755763
}
756764

765+
if (looks_like_command_line_option(ssh_host))
766+
die("strange hostname '%s' blocked", ssh_host);
767+
757768
ssh = getenv("GIT_SSH_COMMAND");
758769
if (!ssh) {
759770
const char *base;

path.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,11 @@ int is_ntfs_dotgit(const char *name)
11781178
}
11791179
}
11801180

1181+
int looks_like_command_line_option(const char *str)
1182+
{
1183+
return str && str[0] == '-';
1184+
}
1185+
11811186
char *xdg_config_home(const char *filename)
11821187
{
11831188
const char *home, *config_home;

t/t5532-fetch-proxy.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ test_expect_success 'fetch through proxy works' '
4040
test_cmp expect actual
4141
'
4242

43+
test_expect_success 'funny hostnames are rejected before running proxy' '
44+
test_must_fail git fetch git://-remote/repo.git 2>stderr &&
45+
! grep "proxying for" stderr
46+
'
47+
4348
test_done

t/t5810-proto-disable-local.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,27 @@ test_expect_success 'setup repository to clone' '
1111
test_proto "file://" file "file://$PWD"
1212
test_proto "path" file .
1313

14+
test_expect_success 'setup repo with dash' '
15+
git init --bare repo.git &&
16+
git push repo.git HEAD &&
17+
mv repo.git "$PWD/-repo.git"
18+
'
19+
20+
# This will fail even without our rejection because upload-pack will
21+
# complain about the bogus option. So let's make sure that GIT_TRACE
22+
# doesn't show us even running upload-pack.
23+
#
24+
# We must also be sure to use "fetch" and not "clone" here, as the latter
25+
# actually canonicalizes our input into an absolute path (which is fine
26+
# to allow).
27+
test_expect_success 'repo names starting with dash are rejected' '
28+
rm -f trace.out &&
29+
test_must_fail env GIT_TRACE="$PWD/trace.out" git fetch -- -repo.git &&
30+
! grep upload-pack trace.out
31+
'
32+
33+
test_expect_success 'full paths still work' '
34+
git fetch "$PWD/-repo.git"
35+
'
36+
1437
test_done

t/t5813-proto-disable-ssh.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,27 @@ test_proto "host:path" ssh "remote:repo.git"
1717
test_proto "ssh://" ssh "ssh://remote$PWD/remote/repo.git"
1818
test_proto "git+ssh://" ssh "git+ssh://remote$PWD/remote/repo.git"
1919

20+
# Don't even bother setting up a "-remote" directory, as ssh would generally
21+
# complain about the bogus option rather than completing our request. Our
22+
# fake wrapper actually _can_ handle this case, but it's more robust to
23+
# simply confirm from its output that it did not run at all.
24+
test_expect_success 'hostnames starting with dash are rejected' '
25+
test_must_fail git clone ssh://-remote/repo.git dash-host 2>stderr &&
26+
! grep ^ssh: stderr
27+
'
28+
29+
test_expect_success 'setup repo with dash' '
30+
git init --bare remote/-repo.git &&
31+
git push remote/-repo.git HEAD
32+
'
33+
34+
test_expect_success 'repo names starting with dash are rejected' '
35+
test_must_fail git clone remote:-repo.git dash-path 2>stderr &&
36+
! grep ^ssh: stderr
37+
'
38+
39+
test_expect_success 'full paths still work' '
40+
git clone "remote:$PWD/remote/-repo.git" dash-path
41+
'
42+
2043
test_done

0 commit comments

Comments
 (0)