Skip to content

typescript.unsafe_eval

Security Critical

Detects usage of eval() and other dynamic code execution patterns.

Using eval:

  • Code injection — Attackers can execute arbitrary code
  • XSS attacks — User input becomes executable
  • CSP violations — Breaks Content Security Policy
// ❌ Before (unsafe eval)
function calculate(expression: string): number {
return eval(expression); // User controls expression!
}
// Or with Function constructor:
const fn = new Function('x', 'return x * 2');
// ✅ After (safe alternatives)
import { evaluate } from 'mathjs';
function calculate(expression: string): number {
// Use a safe math parser
return evaluate(expression);
}
// For dynamic functions, use well-typed alternatives:
const double = (x: number): number => x * 2;
  • Direct eval() calls
  • new Function() constructor
  • setTimeout/setInterval with string args

Unfault suggests safe alternatives like math parsers or template systems.