Prevent compromising log integrity or system safety by log injection
def handle_post(%{params: %{"user_input" => input}} = conn, _opts) do
Logger.info("User input: #{input}")
# further code
end
This code is vulnerable because it directly logs user-provided input without sanitizing it. An attacker could inject malicious content into the logs via this input, potentially compromising the integrity of the logs or even the system of the person viewing the logs.
def handle_post(%{params: %{"user_input" => input}} = conn, _opts) do
sanitized_input = sanitize_input(input)
Logger.info("User input: #{sanitized_input}")
# further code
end
def sanitize_input(input) do
# Sanitization logic, e.g., escaping special characters
end
The secure code example sanitizes the user input before logging it, helping prevent log injection attacks. The sanitize_input function would contain the appropriate logic for sanitizing the input, such as stripping control characters or escaping special characters.