Overview
Indirect eval is a JavaScript technique used in obfuscation to prevent static code analysis tools and developers from easily identifying and interpreting dynamic code execution. While direct eval is called directly, indirect eval involves calling eval through a variable or function reference, which breaks static analysis.
This method is commonly used in JavaScript obfuscation tools to make code harder to reverse engineer, especially in environments where code security is a concern. It is not a language feature per se but a pattern of usage that leverages the flexibility of JavaScript's dynamic nature.

Why It Matters
For developers working with obfuscated or security-sensitive code, indirect eval is a critical technique for evading static analysis. It prevents automated tools from identifying dynamic code execution, which is often flagged as a security risk. This makes it harder for attackers to understand or manipulate the behavior of the application.
In production, indirect eval can be used to dynamically load modules or configuration, but it must be carefully controlled to avoid introducing vulnerabilities. Misuse can lead to runtime errors, performance degradation, or exposure to injection attacks, particularly when input is not properly sanitized.
How It Works
JavaScript's eval function evaluates and executes code passed as a string. When eval is called directly, it is easily detectable by static analysis. However, when eval is invoked through a variable or function reference, it becomes harder to detect and analyze statically.
- Direct
evalis recognized by static analysis tools and often flagged for security concerns. - Indirect
evaluses a variable or function to referenceeval, bypassing static detection. - The technique relies on JavaScript's dynamic nature, where
evalcan be assigned to a variable and called later. - Indirect
evalis commonly used in obfuscation to prevent code decompilation or static analysis. - It does not change the runtime behavior of
eval, but it affects how tools interpret the code.
Quick Reference
| Item | Purpose | Notes |
|---|---|---|
| Direct eval | Executes code directly | Static analysis can detect |
| Indirect eval | Executes code through variable | Bypasses static detection |
| eval() | Function to evaluate code | Can be dangerous if used improperly |
| Function reference | Variable referencing eval | Used to obfuscate |
| Obfuscation | Technique to obscure code | Improves security |
Basic Example
This basic example demonstrates how to use indirect eval to execute code dynamically while bypassing static analysis.
const indirectEval = eval;
const code = 'console.log("Hello, world!");';
indirectEval(code);
The key line is assigning eval to indirectEval. This allows the code to be executed without directly calling eval, making it harder for static tools to detect.
Production Example
This production example shows how indirect eval can be used in a controlled environment for dynamic configuration loading, with appropriate validation and error handling.
function safeIndirectEval(config) {
if (typeof config !== 'string') {
throw new Error('Invalid configuration');
}
const evalFunc = eval;
try {
return evalFunc(config);
} catch (e) {
console.error('Evaluation failed:', e);
return null;
}
}
const config = 'return { enabled: true };';
const result = safeIndirectEval(config);
This version includes input validation and error handling to ensure that only safe code is executed. It is suitable for production use when dynamic configuration is necessary.
Common Mistakes
- Using
evalwithout proper input validation leads to injection vulnerabilities. - Not handling errors from
evalcan cause runtime crashes or unexpected behavior. - Over-relying on indirect
evalcan obscure code and reduce maintainability. - Using indirect
evalin security-sensitive contexts without proper sandboxing is dangerous. - Assuming that indirect
evalhides all security concerns is a misconception that can lead to flawed implementations.
Security And Production Notes
- Always sanitize and validate inputs before using
evalor indirecteval. - Use
evalsparingly and only when necessary for dynamic behavior. - Consider using
Functionconstructor as a safer alternative toevalin many cases. - Implement strict content security policies to limit potential impact of dynamic code execution.
- Monitor and log dynamic code execution to detect suspicious behavior in production.
Related Concepts
Indirect eval is closely related to several JavaScript concepts and practices:
- eval: The core function used for dynamic code execution.
- Obfuscation: The practice of making code harder to understand, often involving indirect
eval. - Dynamic Code Loading: Techniques for loading and executing code at runtime, sometimes using indirect
eval. - Function Constructor: An alternative to
evalfor dynamic code execution with better security. - Static Analysis: Tools and methods that detect code patterns, including the use of
eval.