Improper Control of Generation of Code ('Code Injection')

ID

scala.script.scala_script_rule_scriptinjection

Severity

high

Resource

Script

Language

Scala

Description

The software constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.

Rationale

The software constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.

The following code illustrates a vulnerable pattern detected by this rule:

def scripting(userInput: String): Unit = {
  val scriptEngineManager = new ScriptEngineManager
  val scriptEngine: javax.script.ScriptEngine = scriptEngineManager.getEngineByExtension("js")
  // VULNERABLE: Improper Control of Generation of Code ('Code Injection')
  val result = scriptEngine.eval("test=1;" + userInput)
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

References