Yes, there is an injection vulnerability here. The problem is that -v
test evaluates its argument as a variable, and in bash v4.3 and later that can include an array element (e.g. arrayVar[5]
), and since array indexes (for non-associative arrays) are numbers, the index part gets evaluated as an arithmetic context, which can include command substitutions.
So if $3
is something like this:
x[$(touch /tmp/pwned)]
...or, if you're worried about sensitive variables:
x[$(echo "$SensitiveVar" >/tmp/pwned)]
...it'll wind up executing the part inside $( )
, with privilege and access to internal shell variables.
Note that since this occurs because of how the -v
test is evaluated, quoting $3
doesn't help, and neither does using [[ ]]
instead of [ ]
.