command-injection

Untrusted input run as a shell command.

Group
Code
Runs
Runs under ur scan: ur scan --rule command-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, an environment variable, or a message another process sends, or a parameter of the method that a caller may give one, is run as a shell command, so whoever chose it can run commands of their own. Pass the program and its arguments separately, as Ruby's system("ls", path) or execv, which run no shell, or escape each value, as with Shellwords.escape.

The fix, as Ur suggests it: pass the arguments separately, without a shell.

For agents

Never build a shell command from strings; pass the program and its arguments separately.

Example

app/controllers/backups_controller.rb
class BackupsController < ApplicationController
  def create
    system("tar czf backup.tgz #{params[:dir]}")
  end
end
What Ur prints
$ ur scan --rule command-injection
warning[command-injection]: `params`, a request parameter, reaches `system`
  --> ./app/controllers/backups_controller.rb:3:5 method `create`
  = step 1: ./app/controllers/backups_controller.rb:3:34 `params`, a request parameter,
  = step 2: ./app/controllers/backups_controller.rb:3:12 interpolated into a string
  = hint: pass the arguments separately, without a shell
  = see: CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection'), MITRE (2024-11-19), https://cwe.mitre.org/data/definitions/78.html
scanned 1 file (no framework recognized)
  command-injection: 1 high, 0 medium
1 finding and 0 of medium confidence

Try it in the playground →

Source

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