Skip to main content

Code reviews rule: Insecure JavaScript operations

Written by David Martin
Updated this week

Insecure JavaScript operations

Why is this an issue?

The eval() function executes a string as code at runtime. If that string contains user-controlled or untrusted input, an attacker could execute arbitrary code in the application's context.

Examples

Example of incorrect code:

// Aura component
evalCode: function(component, event, helper) {
var userInput = component.get("v.userInput");
eval(userInput);
}

Example of correct code:

// Use safe alternatives
handleAction() {
const config = JSON.parse(this.configString); // Parse JSON safely
this.processConfig(config);
}

How can I fix violations?

  1. Parse the data into an object before use, e.g., with JSON.parse() for JSON.

  2. Refactor logic to remove the need for dynamic code execution.

Resources

Did this answer your question?