Prevent execution of remote files to maintain application integrity and confidentiality of data.
defmodule VulnerableApp do
use Plug.Router
plug :match
plug :dispatch
get '/' do
filename = get_param(conn, 'filename')
file_content = File.read!(filename)
send_resp(conn, 200, file_content)
end
end
This code includes a file specified by user input in the server's execution context, which could lead to Remote File Inclusion.
defmodule SecureApp do
use Plug.Router
plug :match
plug :dispatch
get '/' do
file_id = get_param(conn, 'file_id')
filename = lookup_filename(file_id)
file_content = File.read!(filename)
send_resp(conn, 200, file_content)
end
defp lookup_filename(file_id) do
# Query database to retrieve file path by ID
end
end
This code retrieves files in a safe manner by using a database of file references rather than directly including files from user inputs.