Skip to content

Commit

Permalink
submodule foreach: skip eval for more than one argument
Browse files Browse the repository at this point in the history
'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 <[email protected]>
Acked-by: Johan Herland <[email protected]>
Signed-off-by: Jonathan Nieder <[email protected]>
  • Loading branch information
andersk authored and jrn committed Sep 27, 2013
1 parent 02a110a commit 1c4fb13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion git-submodule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,12 @@ cmd_foreach()
sm_path=$(relative_path "$sm_path") &&
# we make $path available to scripts ...
path=$sm_path &&
eval "$@" &&
if test $# -eq 1
then
eval "$1"
else
"$@"
fi &&
if test -n "$recursive"
then
cmd_foreach "--recursive" "$@"
Expand Down
9 changes: 9 additions & 0 deletions t/t7407-submodule-foreach.sh
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,13 @@ test_expect_success 'command passed to foreach --recursive retains notion of std
test_cmp expected actual
'

test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
(
cd super &&
git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
git submodule foreach echo \"quoted\" > ../actual
) &&
test_cmp expected actual
'

test_done

0 comments on commit 1c4fb13

Please sign in to comment.