by nathan_phoenix on 11/22/24, 10:43 AM with 46 comments
by mmsc on 11/22/24, 4:30 PM
Basically, the -v case was by design, so for `-v 'hash[$key]'`, "$key is expanded before the array subscript evaluation, and then the whole array plus expanded index is evaluated in a second pass". "Newer versions of bash (5.0 and higher) have a assoc_expand_once option which will suppress the multiple evaluations"
Note that the `-v` case doesn't really work the way one may infer from reading the OP:
> $ key='$(cat /etc/passwd > /tmp/pwned)'
> $ [[ -v 'x[$key]' ]]
> bash: $(cat /etc/passwd > /tmp/pwned): syntax error: operand expected (error token is "$(cat /etc/passwd > /tmp/pwned)") *
> [[ -v "${x[$key]}" ]]
> bash: $(cat /etc/passwd > /tmp/pwned): syntax error: operand expected (error token is "$(cat /etc/passwd > /tmp/pwned)")
by PhilipRoman on 11/22/24, 3:23 PM
IMO safe shell scripting is kind of dead. I can do it if I really have to, but too many external programs have tricky "convenience" features like interpreting flags after positional parameters, etc.
by voidfunc on 11/22/24, 4:24 PM
by spiffytech on 11/22/24, 4:12 PM
Shellcheck currently gives Sample 1 a pass. I hope this is something it can be modified to catch.
by webstrand on 11/22/24, 3:40 PM
by tpoacher on 11/22/24, 4:38 PM
function guess () { declare -i num="${1}" ; }
(unless I'm missing something?)by zettabomb on 11/22/24, 3:51 PM
by IYasha on 11/24/24, 8:00 AM
by tpoacher on 11/22/24, 4:45 PM
if you try to evaluate this kind of things as an arithmetic expression directly, it will fail with an error of a bad subscript (mind you, the attack will still work though).
by alganet on 11/22/24, 3:17 PM
https://gist.github.com/alganet/a4198158651f3b2dc43ce658052e...
Then, if we run it:
"line 3: test: a[$(cat /etc/passwd > /tmp/pwned)] + 42: integer expression expected"