Unsanitized user input in SQL query

Description

Including unsanitized data, such as user input or request data, in raw SQL queries makes your application vulnerable to SQL injection attacks.

Remediations

❌ Avoid raw queries, especially those that contain unsanitized user input:

User.where("user.email = #{params[:email]}")

✅ Use the ActiveRecord API wherever possible:

User.where(email: params[:email])

✅ Use bind variables:

User.where("user.email = ?", [params[:email]])

✅ Santize the value manually:

User.where(sanitize_sql(["user.email = ?", params[:email]]))

Resources

Associated CWE

OWASP Top 10

Configuration

To skip this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --skip-rule=ruby_rails_sql_injection

To run only this rule during a scan, use the following flag

bearer scan /path/to/your-project/ --only-rule=ruby_rails_sql_injection