Prevent misinterpretation of data types and code injection
defmodule Vulnerable do
def process(input) do
{:ok, number} = Integer.parse(input)
IO.puts(number)
end
end
This Elixir code is vulnerable because it directly uses the user input without validating its type. This can lead to type confusion and potentially code execution.
defmodule Safe do
def process(input) do
case Integer.parse(input) do
:error -> IO.puts('Invalid input')
{:ok, number} -> IO.puts(number)
end
end
end
This Elixir code is safe because it validates the type of the input data before using it, preventing type confusion and potential code execution.