Skip to content

Warn about Code Execution in Arithmetic Expressions for Bash [[ $1 -eq 42 ]]  #3088

Open
@clushie

Description

@clushie

Blog posts discussing this issue:

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

  • test.sh
#!/usr/bin/env bash
echo "integer comparison with quoted: ${1}"

if [[ "${1}" -eq 42 ]]
then
  echo "Correct"
else
  echo "Wrong"
fi
echo 'a[$(head -1 /etc/passwd)] + 42'
./test.sh 'a[$(head -1 /etc/passwd)] + 42'

The shell evaluates values in an arithmetic context in several syntax constructs where the shell expects an integer. This includes: $((here)), ((here)), ${var:here:here}, ${var[here]}, var[here]=.. and on either side of any [[ numerical comparator like -eq, -gt, -le and friends.

Also affected

  • foo="${untrusted_input}"; (( foo > 10 ))

Here's what shellcheck currently says:

  • "$?" = 0 aka no issues

Here's what I wanted or expected to see:

  • [[ ]] with Arithmetic Expressions is insecure and can yield to code execution, use [ ] instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions