Skip to main content

SKY-D202 · Security in code

Dynamic code execution with exec

HighCWE-95

Python exec() and a string passed to setTimeout() or setInterval() are HIGH. TypeScript and JavaScript new Function(...) is reported as CRITICAL.

exec() runs a string as a whole Python program, and JavaScript's new Function(...) and string timers do the same in the browser or Node. SKY-D202 reports these calls when the code is not proven to be fixed text.

Languages
Python, TypeScript/JavaScript
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 exec
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A "custom rules" feature that exec()s an uploaded file lets whoever uploads it run any code on the server, read its secrets and reach the database. The same holds for formulas that a web page compiles with new Function.

What Skylos flags

app/rules.py · PythonReported on line 7
from pathlib import Path  def load_rules(upload_dir: str, name: str) -> dict:    source = Path(upload_dir, name).read_text()    namespace: dict = {}    exec(source, namespace)    return namespace["RULES"]

What Skylos leaves alone

app/dialog.py · Python
from PyQt6.QtWidgets import QMessageBox  def confirm_delete() -> bool:    box = QMessageBox()    box.setText("Delete the selected files?")    box.setStandardButtons(        QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No    )    return box.exec() == QMessageBox.StandardButton.Yes

Why Skylos leaves it alone

PyQt 6 names the method that shows a dialog exec(). Skylos resolves box to the QMessageBox it was created from, so the call is a method of that object, not the builtin exec.

More languages

TypeScript

Flagged

src/formula.ts · TypeScriptReported on line 4
type Row = Record<string, number>; export function compileFormula(formula: string): (row: Row) => number {  return new Function("row", `with (row) { return ${formula}; }`) as (    row: Row,  ) => number;}

Not flagged

src/refresh.ts · TypeScript
export function scheduleRefresh(refresh: () => void): void {  setTimeout(() => refresh(), 30_000);}

Why Skylos leaves it alone

The timer receives a function, not a string, so nothing is compiled from text. Only a string literal passed to setTimeout() or setInterval() is reported.

How Skylos decides

  1. Python: resolves each call's name through imports and simple assignments, including the builtin stored in a variable (run = exec), and matches the builtin exec. A method called exec on another object does 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.
  3. Reports the rest as HIGH. When every value that can reach the code is fixed text that reads no outside name, the finding is marked low-risk: hidden from the default list, still in JSON output and counted by the gate.
  4. TypeScript and JavaScript: reports every new Function(...) as CRITICAL, and setTimeout() or setInterval() with a string literal argument as HIGH.

How to fix it

Do not run uploaded text as code. Store rules as data (JSON, YAML loaded with yaml.safe_load, TOML) and interpret them with your own code.

app/rules.py · Python · the fix, against the flagged file
Added: import jsonfrom pathlib import Path  def load_rules(upload_dir: str, name: str) -> dict:    source = Path(upload_dir, name).read_text()Removed:     namespace: dict = {}Removed:     exec(source, namespace)Removed:     return namespace["RULES"]Added:     return json.loads(source)["RULES"]

When it can be wrong

  • Tools that build Python source from a template and exec() it, such as code generators, are reported even when every input is checked.
  • In TypeScript and JavaScript, new Function("return this")() and other fixed bodies are reported as CRITICAL.

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
    exec(code, namespace)  # skylos: ignore[SKY-D202] code is generated by build_stub()
  • Accept existing findings with skylos baseline ., then scan with --baseline.

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

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