submodule foreach: skip eval for more than one argument

'eval "$@"' creates an extra layer of shell interpretation, which is probably not expected by a user who passes multiple arguments to git submodule foreach: $ git grep "'" [searches for single quotes] $ git submodule foreach git grep "'" Entering '[submodule]' /usr/lib/git-core/git-submodule: 1: eval: Syntax error: Unterminated quoted string Stopping at '[submodule]'; script returned non-zero status. To fix this, if the user passes more than one argument, execute "$@" directly instead of passing it to eval. Examples: * Typical usage when adding an extra level of quoting is to pass a single argument representing the entire command to be passed to the shell. This doesn't change that. * One can imagine someone feeding untrusted input as an argument: git submodule foreach git grep "$variable" That currently results in a nonobvious shell code injection vulnerability. Executing the command named by the arguments directly, as in this patch, fixes it. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Acked-by: Johan Herland <johan@herland.net> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Anders Kaseorg committed Sep 27, 2013 at 06:23 UTC 1c4fb136dbad762c9c4350ee79c3474ae8037587
2 files changed +15 -1
git-submodule.sh
+6 -1
@@ -545,7 +545,12 @@ cmd_foreach()
545 sm_path=$(relative_path "$sm_path") &&
546 # we make $path available to scripts ...
547 path=$sm_path &&
548 - eval "$@" &&
548 + if test $# -eq 1
549 + then
550 + eval "$1"
551 + else
552 + "$@"
553 + fi &&
554 if test -n "$recursive"
555 then
556 cmd_foreach "--recursive" "$@"
t/t7407-submodule-foreach.sh
+9
@@ -329,4 +329,13 @@ test_expect_success 'command passed to foreach --recursive retains notion of std
329 test_cmp expected actual
330 '
331
332 +test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
333 + (
334 + cd super &&
335 + git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
336 + git submodule foreach echo \"quoted\" > ../actual
337 + ) &&
338 + test_cmp expected actual
339 +'
340 +
341 test_done