FlawOpen

flawopen.com/SQL Injection in Ruby/ActiveRecord where()

Is Rails where() string interpolation safe?

Reference page
Short answer

Think of SQL as a form with fixed instructions and empty value boxes. A bound parameter fills a box; Ruby interpolation writes request text into the instructions before Rails can separate the two. Keep SQL structure fixed and pass untrusted values as parameters.

UNSAFE
User.where(
  "id = #{params[:id]}"
)
SAFE
User.where(id: params[:id])
# or, for more complex conditions:
User.where("id = ?", params[:id])

Why interpolation changes the query

Ruby evaluates #{...} before Active Record receives the condition. For example, where("id = #{params[:id]}") gives Rails an already-combined SQL string; request input may then change the predicate. Active Record documents this string-building pattern as unsafe. Use a hash for supported equality, range, or subset conditions, or use ? placeholders for other predicates. Placeholders bind values, not identifiers or SQL keywords; keep query structure fixed or choose identifiers from a strict allowlist.

How to check your codebase

rg -n 'where\s*\(' app/
Review matches for interpolated or concatenated request data inside condition strings. Brakeman reports SQL injection risks when its analysis identifies unsafe data flow; a clean scan does not prove every query safe.

FAQ

Can I use hash conditions for every query?

No. Rails hash conditions support equality, range, and subset (IN) checks. For other predicates, use a fixed SQL condition with bound values, such as where("price > ?", amount). Binding protects values; it does not make dynamic table names, column names, or SQL fragments safe.

References

See also SQL Injection (CWE-89): How It Works and How to Prevent It in Every Language — flawopen.com Is Sequelize query() Safe from SQL Injection? Is SQLAlchemy text() Safe from SQL Injection? Does sqlx's Compile-Time Checking Prevent SQL Injection? Are Stored Procedures Immune to SQL Injection? Is TypeORM QueryBuilder Safe from SQL Injection? Django ORM SQL Injection: Safe Patterns and Pitfalls