Skip to content

Commit 1c4fb13

Browse files
anderskjrn
authored andcommitted
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 <[email protected]> Acked-by: Johan Herland <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]>
1 parent 02a110a commit 1c4fb13

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

git-submodule.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,12 @@ cmd_foreach()
545545
sm_path=$(relative_path "$sm_path") &&
546546
# we make $path available to scripts ...
547547
path=$sm_path &&
548-
eval "$@" &&
548+
if test $# -eq 1
549+
then
550+
eval "$1"
551+
else
552+
"$@"
553+
fi &&
549554
if test -n "$recursive"
550555
then
551556
cmd_foreach "--recursive" "$@"

t/t7407-submodule-foreach.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,13 @@ test_expect_success 'command passed to foreach --recursive retains notion of std
329329
test_cmp expected actual
330330
'
331331

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+
332341
test_done

0 commit comments

Comments
 (0)