Skip to content

Commit 516381d

Browse files
Lea Wiemanngitster
authored andcommitted
gitweb: quote commands properly when calling the shell
This eliminates the function git_cmd_str, which was used for composing command lines, and adds a quote_command function, which quotes all of its arguments (as in quote.c). Signed-off-by: Lea Wiemann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 79c6dca commit 516381d

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

gitweb/gitweb.perl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,9 +1500,13 @@ sub git_cmd {
15001500
return $GIT, '--git-dir='.$git_dir;
15011501
}
15021502

1503-
# returns path to the core git executable and the --git-dir parameter as string
1504-
sub git_cmd_str {
1505-
return join(' ', git_cmd());
1503+
# quote the given arguments for passing them to the shell
1504+
# quote_command("command", "arg 1", "arg with ' and ! characters")
1505+
# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"
1506+
# Try to avoid using this function wherever possible.
1507+
sub quote_command {
1508+
return join(' ',
1509+
map( { my $a = $_; $a =~ s/(['!])/'\\$1'/g; "'$a'" } @_ ));
15061510
}
15071511

15081512
# get HEAD ref of given project as hash
@@ -4633,19 +4637,19 @@ sub git_snapshot {
46334637
$hash = git_get_head_hash($project);
46344638
}
46354639

4636-
my $git_command = git_cmd_str();
46374640
my $name = $project;
46384641
$name =~ s,([^/])/*\.git$,$1,;
46394642
$name = basename($name);
46404643
my $filename = to_utf8($name);
46414644
$name =~ s/\047/\047\\\047\047/g;
46424645
my $cmd;
46434646
$filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4644-
$cmd = "$git_command archive " .
4645-
"--format=$known_snapshot_formats{$format}{'format'} " .
4646-
"--prefix=\'$name\'/ $hash";
4647+
$cmd = quote_command(
4648+
git_cmd(), 'archive',
4649+
"--format=$known_snapshot_formats{$format}{'format'}",
4650+
"--prefix=$name/", $hash);
46474651
if (exists $known_snapshot_formats{$format}{'compressor'}) {
4648-
$cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4652+
$cmd .= ' | ' . quote_command(@{$known_snapshot_formats{$format}{'compressor'}});
46494653
}
46504654

46514655
print $cgi->header(
@@ -4858,8 +4862,8 @@ sub git_object {
48584862
if ($hash || ($hash_base && !defined $file_name)) {
48594863
my $object_id = $hash || $hash_base;
48604864

4861-
my $git_command = git_cmd_str();
4862-
open my $fd, "-|", "$git_command cat-file -t $object_id 2>/dev/null"
4865+
open my $fd, "-|", quote_command(
4866+
git_cmd(), 'cat-file', '-t', $object_id) . ' 2> /dev/null'
48634867
or die_error('404 Not Found', "Object does not exist");
48644868
$type = <$fd>;
48654869
chomp $type;

0 commit comments

Comments
 (0)