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
| Field | Value |
|---|---|
| ID | CODE-0063 |
| Category | Injection |
| Severity | HIGH |
| CWE | CWE-94 |
| Confidence | HIGH |
| Impact | HIGH |
| Likelihood | MEDIUM |
| Exploitability | EASY |
| Tags | code injection, script engine |
| OWASP | A1:2017-Injection, A03:2021-Injection |