sql-injection

Untrusted input written into SQL.

Group
Code
Runs
Runs under ur scan: ur scan --rule sql-injection
Level
warning
Reads
Every file
Fix
None: the safe form depends on what the value is meant to be

Why

A value from outside the program, such as a request's parameter or a message another process sends, or a parameter of the method that a caller may give one, is written into an SQL query, so whoever chose it can change what the query does: read other rows, bypass a check, or change data. Pass values as bind parameters, as Rails' where("name = ?", name) or a prepared statement's ?, and choose column names from a fixed list.

The fix, as Ur suggests it: pass the value as a bind parameter.

For agents

Never interpolate or concatenate values into SQL; pass them as bind parameters or a hash of conditions.

Example

app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
    name = params[:name]
    @users = User.where("name = '#{name}'")
  end
end
What Ur prints
$ ur scan --rule sql-injection
warning[sql-injection]: `params`, a request parameter, reaches `where`
  --> ./app/controllers/users_controller.rb:4:14 method `index`
  = step 1: ./app/controllers/users_controller.rb:3:12 `params`, a request parameter,
  = step 2: ./app/controllers/users_controller.rb:4:25 interpolated into a string
  = hint: pass the value as a bind parameter
  = see: CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/89.html
scanned 1 file (no framework recognized)
  sql-injection: 1 high, 0 medium
1 finding and 0 of medium confidence

Try it in the playground →

Source

CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection'), MITRE (2024-11-19).