I've got a python script that wraps a bash command line tool, that gets it's variables from external source (environment variables). is there any way to perform some soft of escaping to prevent malicious user from executing bad code in one of those parameters.
for example if the script looks like this
/bin/sh
/usr/bin/tool ${VAR1} ${VAR2}
and someone set VAR2 as follows
export VAR2=123 && \rm -rf /
so it may not treat VAR2 as pure input, and perform the rm
command.
Is there any way to make the variable non-executable and take the string as-is to the command line tool as input ?
The correct and safe way to pass the values of variables VAR1
and VAR2
as arguments to /usr/bin/tool
is:
/usr/bin/tool -- "$VAR1" "$VAR2"
--
should prevent the variable values being treated as options if they begin with -
characters. You might have to do something else if tool
is badly written and doesn't accept --
to terminate command line options./bin/sh
in the question was intended to be a #! /bin/sh
shebang. Since the question was tagged bash
, note that #! /bin/sh
should not be used with code that includes Bashisms. /bin/sh
may not be Bash, and even if it is Bash it behaves differently when invoked as /bin/sh
rather than /bin/bash
.rm -rf /
) embedded in the variable values to be run at that point. The danger is that badly-written code that uses the variables will create and run commands that include the variable values in unsafe ways. See should I avoid bash -c, sh -c, and other shells' equivalents in my shell scripts? for an explanation of (only) some of the dangers.