Skip to main content

SKY-D357 · Security in code

Untrusted dynamic Python import

High

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

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

app/jobs.py · PythonReported on line 10
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

app/plugins.py · Python
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

  1. 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 or name=.
  2. 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.
  3. 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.
  4. 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.

app/jobs.py · Python · the fix, against the flagged file
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 by import_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 # noqa does not hide HIGH security findings.

    Python
    mod = importlib.import_module(path)  # skylos: ignore[SKY-D357] checked against JOBS
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

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

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