Prevent unauthorized code or command execution
def run_command(input) do
:os.cmd("echo #{input}")
end
In this code, the function 'run_command' takes user input and directly interpolates it into an OS command to be executed. This is a vulnerability because an attacker could manipulate the input to execute arbitrary commands on the OS, leading to unauthorized code execution. The function is not validating or sanitizing the input before use, which makes it susceptible to the vulnerability.
def run_command(input) do
sanitized_input = String.replace(input, ~r/[\n\r\$()]/, "")
case sanitized_input do
^sanitized_input when is_binary(sanitized_input) -> :os.cmd('echo ' <> sanitized_input)
_ -> {:error, "Invalid input"}
end
end
This code mitigates the vulnerability by first sanitizing the user input, removing or substituting characters that could modify the OS command. Then it verifies if the sanitized_input still is a binary string. If it isn't, it returns an error. Only if the input passes these checks, it's then used in the OS command.