code-injection

Untrusted input run as code or used to choose a method or class.

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

Why

A value from a request is evaluated as code, or names the method send calls or the class constantize loads, so whoever sends the request can run code or call methods the program never meant to expose. Map the input to one of a fixed set of methods or classes instead.

The fix, as Ur suggests it: choose the method or class from a fixed set.

For agents

Never evaluate input, or call a method or load a class that input names; choose from a fixed set instead.

Example

app/controllers/reports_controller.rb
class ReportsController < ApplicationController
  def show
    model = params[:type].constantize
    render json: model.find(params[:id])
  end
end
What Ur prints
$ ur scan --rule code-injection
warning[code-injection]: `params`, a request parameter, reaches `constantize`
  --> ./app/controllers/reports_controller.rb:3:13 method `show`
  = step 1: ./app/controllers/reports_controller.rb:3:13 `params`, a request parameter,
  = hint: choose the method or class from a fixed set
  = see: CWE-94: Improper Control of Generation of Code ('Code Injection'), MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/94.html
scanned 1 file (no framework recognized)
  code-injection: 1 high, 0 medium
1 finding and 0 of medium confidence

Try it in the playground →

Source

CWE-94: Improper Control of Generation of Code ('Code Injection'), MITRE (2024-11-19).