Skip to content

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

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.

Examples

Insecure Code

scala
val engine = new ScriptEngineManager().getEngineByName("js"); engine.eval(userInput);

Secure Code

scala
val engine = new ScriptEngineManager().getEngineByName("js"); val safeInput = userInput.replace(";", "").replace("(", "").replace(")", ""); engine.eval(safeInput);

Remediation

Validate and sanitize any user-controlled input before passing it to the ScriptEngine.eval() method to prevent code injection attacks.

Rule Details

FieldValue
IDCODE-0063
CategoryInjection
SeverityHIGH
CWECWE-94
ConfidenceHIGH
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagscode injection, script engine
OWASPA1:2017-Injection, A03:2021-Injection