Earlier quoted context omitted.
So -eq triggers evaluation? Sounds like typical bash magic. I would use [ an the problem goes away. Showing -eq is not the best example, it can just be replaced by = and the problem goes away. But if you need -gt or similar there is no replacement. So one should stick to [. If I follow correctly the dangerous combination is [[ and arithmetic comparisons?
`-eq` is for arithmetic comparison; `=` is for string comparison. They don't do the same thing, and it's unsound to uniformly replace either with the other. The dangerous thing here is that an undefined number of contexts exist where Bash treats strings as arithmetic expressions, which can contain arbitrary code despite not being quoted for expansion. `-eq` is just one example of that; others have linked other exampl…
TIL: Some surprising code execution sources in bash
41–50 of 51 posts
Re: TIL: Some surprising code execution sources in bash
#42Earlier quoted context omitted.
`-eq` is for arithmetic comparison; `=` is for string comparison. They don't do the same thing, and it's unsound to uniformly replace either with the other. The dangerous thing here is that an undefined number of contexts exist where Bash treats strings as arithmetic expressions, which can contain arbitrary code despite not being quoted for expansion. `-eq` is just one example of that; others have linked other exampl…
Can you give an example where = would be unsuitable for comparison of numbers?
$ [[ 0xFF -eq 255 ]] ; echo $?
0
$ [[ 0xFF = 255 ]] ; echo $?
1Re: TIL: Some surprising code execution sources in bash
#43Earlier quoted context omitted.
Curious what you use instead of bash? When you spin up a server somewhere, what's the first thing you like to install that replaces what we typically use bash for?
Do these apply to NuShell? I think something like that is the way forward. Something with real data types rather than implicitly doing weird array processing. I would be pretty happy with something similar to Python but with easier IO redirection and subprocess management. xonsh is neat in principle, but painful in actual usage ime. And I suspect vulnerable to similar issues around the Python-bash interop.
Let's say you need to install some third party software that is pretty standard `./configure && make && make install`, what would you do? Port `configure` to python?
Re: TIL: Some surprising code execution sources in bash
#44Earlier quoted context omitted.
> Double quotes never prevent variable expansion. I am not sure what the author is talking about. Shellcheck is correct to not complain. I stopped reading there. I think it would behoove you to read the rest of the post. The double quotes are not the operative part of example there; they're only there to demonstrate that the code execution doesn't come from splatting or word splitting. The actual code execution in Ca…
Ok, need to read it again with more time. Myself I typically don't script in bash. Most of the extras like [[ are not needed, you can do everything in dash. Arrays are the only feature that comes to my mind where bash would be handy.
Re: TIL: Some surprising code execution sources in bash
#45Re: TIL: Some surprising code execution sources in bash
#46What's the fix for those code samples? Shellcheck currently gives Sample 1 a pass. I hope this is something it can be modified to catch.
Honestly, the fix is to only allow alphanumeric input to shellscripts. Anything else invariably fails at some point.
Re: TIL: Some surprising code execution sources in bash
#47Earlier quoted context omitted.
Can you give an example where = would be unsuitable for comparison of numbers?
Here's a trivial one: $ [[ 0xFF -eq 255 ]] ; echo $? 0 $ [[ 0xFF = 255 ]] ; echo $? 1
Re: TIL: Some surprising code execution sources in bash
#48Earlier quoted context omitted.
Do these apply to NuShell? I think something like that is the way forward. Something with real data types rather than implicitly doing weird array processing. I would be pretty happy with something similar to Python but with easier IO redirection and subprocess management. xonsh is neat in principle, but painful in actual usage ime. And I suspect vulnerable to similar issues around the Python-bash interop.
What would you do with `configure` scripts? Let's say you need to install some third party software that is pretty standard `./configure && make && make install`, what would you do? Port `configure` to python?
Re: TIL: Some surprising code execution sources in bash
#49From what I understand, based on the premise that this results from switching into 'arithmetic' mode, you don't even need test. The following will also work with the proposed attack: function guess () { declare -i num="${1}" ; } (unless I'm missing something?)