@matheusbsbs / matheus-bagliano / commits / 9a8606465e
Fork 0

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 <peff@peff.net> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Sep 21, 2020 at 12:40 UTC 9a8606465e81a04d58c20324c402ba3464bc706a
1 file changed +8 -16
contrib/mw-to-git/git-remote-mediawiki.perl
+8 -16
@@ -371,8 +371,8 @@ sub get_mw_pages {
371
372 # usage: $out = run_git_quoted(["command", "args", ...]);
373 # $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
376 sub _run_git {
377 my $args = shift;
378 my $encoding = (shift || 'encoding(UTF-8)');
@@ -391,8 +391,8 @@ sub run_git_quoted {
391 _run_git(["git", @{$_[0]}], $_[1]);
392 }
393
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]);
396 }
397
398 sub get_all_mediafiles {
@@ -521,10 +521,8 @@ sub download_mw_mediafile {
521
522 sub get_last_local_revision {
523 # 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"]);
526 my @note_info = split(/ /, $note);
527
528 my $lastrevision_number;
@@ -1189,16 +1187,10 @@ sub mw_push_revision {
1187 my $mw_revision = $last_remote_revid;
1188
1189 # 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]);
1191 chomp($HEAD_sha1);
1192 # 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"]);
1194 chomp($remoteorigin_sha1);
1195
1196 if ($last_local_revid > 0 &&