SKY-D357 · Security in code
Untrusted dynamic Python import
importlib.import_module(name) and __import__(name) load a module chosen at run time, and importing a module runs its top-level code. SKY-D357 reports these calls when the module name comes from an identified input source, such as a route argument or request data, instead of from your own code.
- Languages
- Python
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
A route like /jobs/<name> that imports name lets a visitor load any module Python can find, including standard-library modules with side effects and any .py file an upload put on the path. The next attribute call then runs code the developer never meant to expose.
What Skylos flags
import importlib from flask import Flask, request app = Flask(__name__) @app.post("/jobs/<name>")def run_job(name): handler = importlib.import_module(name) return handler.run(request.get_json())What Skylos leaves alone
import importlib ENABLED_PLUGINS = ("plugins.audit_log", "plugins.metrics") def load_plugins(): return [importlib.import_module(name) for name in ENABLED_PLUGINS]Why Skylos leaves it alone
The import name is a variable, but every value comes from a fixed tuple in the code. No input source reaches the call, and Skylos does not report a dynamic import just because its argument is not a literal.
How Skylos decides
- Finds
importlib.import_module(),builtins.__import__()and the builtin__import__()(when the module does not rebind it), resolved through imports, and reads the module name: the first argument orname=. - Reports HIGH when an identified source reaches that name: request data, a route, CLI or MCP tool argument,
input(), standard input or an environment variable, directly or through assignments, f-strings and local helper calls. - Also reports a function parameter used as the name when a call of that function at module level passes such a source, and follows callers across the project when it can resolve them.
- Leaves names that come only from your own code alone, such as a constant list of plugins. Unknown values are not treated as input.
How to fix it
Import the allowed modules normally and let the input choose from a dictionary of them. Unknown names then fail with a 404 instead of loading code.
Removed: import importlibAdded: from flask import Flask, abort, request Removed: from flask import Flask, requestAdded: from app.handlers import resize, thumbnail app = Flask(__name__)Added: HANDLERS = {"resize": resize, "thumbnail": thumbnail} @app.post("/jobs/<name>")def run_job(name):Removed: handler = importlib.import_module(name)Added: handler = HANDLERS.get(name)Added: if handler is None:Added: abort(404) return handler.run(request.get_json())When it can be wrong
- A name checked against an allowlist first, such as
if name not in HANDLERS: abort(404)followed byimport_module(f"jobs.{name}"), is still reported: the check is not recognised. - A backend chosen by the operator through an environment variable, such as
import_module(os.environ["CACHE_BACKEND"]), is reported too.
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 line and say why. A bare
# noqadoes not hide HIGH security findings.Python mod = importlib.import_module(path) # skylos: ignore[SKY-D357] checked against JOBSAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D357"]turns the rule off for the whole project.