Skip to content

Improper control of generation of code ('Code Injection')

Description

The application was found calling a reflection method with user-controllable input. Reflection in Ruby allows a program to examine and modify its own structure and behavior at runtime. When user input is used unsafely with reflection methods, it poses a significant security risk, potentially leading to arbitrary code execution.

Examples

Insecure Code

ruby
User input directly used with reflection methods

Secure Code

ruby
class SafeClassHandler
  ALLOWED_CLASSES = {
    'user' => User,
    'product' => Product
  }.freeze

  def self.handle_class_action(class_key)
    klass = ALLOWED_CLASSES[class_key]
    raise ArgumentError, "Invalid class key" unless klass

    klass.some_method
  end
end

Remediation

Avoid direct user input in reflection, validate and sanitize input, use indirect references, and do not provide user-controllable input to reflection functionality.

Rule Details

FieldValue
IDCODE-0545
CategoryInjection
SeverityCRITICAL
CWECWE-94
ConfidenceHIGH
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagscode injection, reflection
OWASPA1:2017-Injection, A03:2021-Injection

References