Skip to content

Commit 9a86064

Browse files
avargitster
authored andcommitted
remote-mediawiki: use "sh" to eliminate unquoted commands
Remove the use of run_git_unquoted() completely with a use of "sh -c" suggested by Jeff King, i.e.: sh -c '"$@" 2>/dev/null' -- echo sneaky 'argument;id' I don't think this is needed now for any potential RCE issue. The $remotename argument is ultimately picked by the local user (and similarly, the $local variable comes from a user-supplied refspec). But completely eliminating the use of unquoted shell arguments has a value in and of itself, by making the code easier to review. As noted in an earlier commit I think the use of IPC::Open3 would be too verbose here, but this "sh -c" trick strikes the right balance between readability and semantic sanity. Suggested-by: Jeff King <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 878d150 commit 9a86064

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

contrib/mw-to-git/git-remote-mediawiki.perl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ sub get_mw_pages {
371371

372372
# usage: $out = run_git_quoted(["command", "args", ...]);
373373
# $out = run_git_quoted(["command", "args", ...], "raw"); # don't interpret output as UTF-8.
374-
# $out = run_git_unquoted(["command args"); # don't quote arguments
375-
# $out = run_git_unquoted(["command args", "raw"); # ditto but raw instead of UTF-8 as above
374+
# $out = run_git_quoted_nostderr(["command", "args", ...]); # discard stderr
375+
# $out = run_git_quoted_nostderr(["command", "args", ...], "raw"); # ditto but raw instead of UTF-8 as above
376376
sub _run_git {
377377
my $args = shift;
378378
my $encoding = (shift || 'encoding(UTF-8)');
@@ -391,8 +391,8 @@ sub run_git_quoted {
391391
_run_git(["git", @{$_[0]}], $_[1]);
392392
}
393393

394-
sub run_git_unquoted {
395-
_run_git(["git $_[0]"], $_[1]);
394+
sub run_git_quoted_nostderr {
395+
_run_git(['sh', '-c', 'git "$@" 2>/dev/null', '--', @{$_[0]}], $_[1]);
396396
}
397397

398398
sub get_all_mediafiles {
@@ -521,10 +521,8 @@ sub download_mw_mediafile {
521521

522522
sub get_last_local_revision {
523523
# Get note regarding last mediawiki revision.
524-
#
525-
# It's OK to use run_git_unquoted() here because $remotename is
526-
# supplied by the local git itself.
527-
my $note = run_git_unquoted("notes --ref=${remotename}/mediawiki show refs/mediawiki/${remotename}/master 2>/dev/null");
524+
my $note = run_git_quoted_nostderr(["notes", "--ref=${remotename}/mediawiki",
525+
"show", "refs/mediawiki/${remotename}/master"]);
528526
my @note_info = split(/ /, $note);
529527

530528
my $lastrevision_number;
@@ -1189,16 +1187,10 @@ sub mw_push_revision {
11891187
my $mw_revision = $last_remote_revid;
11901188

11911189
# Get sha1 of commit pointed by local HEAD
1192-
#
1193-
# It's OK to use run_git_unquoted() because $local is supplied
1194-
# by the local git itself.
1195-
my $HEAD_sha1 = run_git_unquoted("rev-parse ${local} 2>/dev/null");
1190+
my $HEAD_sha1 = run_git_quoted_nostderr(["rev-parse", $local]);
11961191
chomp($HEAD_sha1);
11971192
# Get sha1 of commit pointed by remotes/$remotename/master
1198-
#
1199-
# It's OK to use run_git_unquoted() here because $remotename is
1200-
# supplied by the local git itself.
1201-
my $remoteorigin_sha1 = run_git_unquoted("rev-parse refs/remotes/${remotename}/master 2>/dev/null");
1193+
my $remoteorigin_sha1 = run_git_quoted_nostderr(["rev-parse", "refs/remotes/${remotename}/master"]);
12021194
chomp($remoteorigin_sha1);
12031195

12041196
if ($last_local_revid > 0 &&

0 commit comments

Comments
 (0)