SKY-D202 · Security in code
Dynamic code execution with exec
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
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
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
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.YesWhy 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
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
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
- Python: resolves each call's name through imports and simple assignments, including the builtin stored in a variable (
run = exec), and matches the builtinexec. A method calledexecon another object does 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. - 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.
- TypeScript and JavaScript: reports every
new Function(...)as CRITICAL, andsetTimeout()orsetInterval()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.
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
# noqadoes 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.