Skip to content

    Type a page, a command such as keryn scan, or a rule id.

    general-injection-python-v1

    View as Markdown

    23 rules · Keryn Software Licence · rules 0.8.0 · all packs

    How to read a rule
    Severity
    How bad the finding is if it is real: error fails a default gate, warning and info do not.
    Confidence
    P0 exploitable as written; P1 dangerous in context; P2 a heuristic or a posture check: confirm by reading the code.
    False-positive risk
    low: an exact API or flag, so hits are almost always real; medium: the pattern is legitimate in many programs, so check each hit; high: it finds a lead to confirm, so expect benign hits.
    Edge
    The relationship the finding sits on. reaches: content or traffic gets to a thing; calls: one thing invokes another; reads: one thing reads from another; writes: one thing writes to another; sends: data leaves for another party; grants: authority is given or left unchecked; depends_on: something relies on a component it does not verify; trusts: one party believes another without evidence. The boundary it crosses, when there is one: user->prompt (untrusted input reaching a prompt); model->code (model output reaching code, a shell, a query or a page); secret->outside (a secret leaving the trusted side); supply-chain (code, models or prompts pulled in from outside).
    Taxonomy chips
    References for readers who work in OWASP LLM 2025, OWASP Agentic, CWE or NIST AI RMF. They say where a rule belongs, not that a framework is satisfied.

    keryn.general.python.shell-function-dynamic-command

    Section titled “keryn.general.python.shell-function-dynamic-command”

    error python python

    The command string of a shell-backed call (os.system, os.popen2/3/4, popen2, commands, subprocess.getoutput / getstatusoutput) is built at run time. /bin/sh parses the whole string, so metacharacters in any part of it run as commands. Pass an argument list to subprocess.run with shell left off.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Replace the call with subprocess.run([program, arg1, arg2], shell=False, check=True) and pass every value as its own list element; if a shell feature is really needed, quote each value with shlex.quote and allow-list the program.

    References: https://docs.python.org/3/library/os.html#os.system · https://docs.python.org/3/library/subprocess.html#security-considerations

    keryn.general.python.shell-function-constant-command

    Section titled “keryn.general.python.shell-function-constant-command”

    info python python

    A fixed command string goes to /bin/sh through os.system / os.popen / popen2 / commands / subprocess.getoutput. It is not injectable as written, but every later edit that splices a value into it is; an argument list keeps the shell out of the path.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk high: it finds a lead to confirm, so expect benign hits.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Use subprocess.run([“/bin/program”, “arg”], check=True) instead of a shell string.

    References: https://docs.python.org/3/library/os.html#os.system · https://docs.python.org/3/library/subprocess.html#replacing-os-system

    info python python

    os.exec* / os.spawn* / os.startfile starts a program directly. No shell is involved, but the program path and arguments decide what runs; make sure neither comes from request, model or file content without an allow-list.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk high: it finds a lead to confirm, so expect benign hits.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Use an absolute program path from configuration, validate arguments against an allow-list, and prefer subprocess.run with an argument list.

    References: https://docs.python.org/3/library/os.html#os.execl · https://docs.python.org/3/library/os.html#os.spawnl · https://docs.python.org/3/library/os.html#os.startfile

    keryn.general.python.shell-wildcard-argument

    Section titled “keryn.general.python.shell-wildcard-argument”

    warning python python

    A shell command runs chown, chmod, tar or rsync with a * glob. The shell expands the glob to file names, and a file named like an option (–checkpoint-action=exec=…, –reference=…) becomes an option of the command.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-155 LLM05

    Fix: Expand the file list in Python (glob.glob / pathlib) and pass the names after -- in an argument list, or prefix the glob with ./ so names cannot start with a dash.

    References: https://www.gnu.org/software/tar/manual/html_node/checkpoints.html · https://docs.python.org/3/library/subprocess.html#security-considerations

    keryn.general.python.paramiko-exec-command

    Section titled “keryn.general.python.paramiko-exec-command”

    warning python paramiko

    SSHClient.exec_command sends one command string to the remote login shell, which parses it like /bin/sh does. Any value spliced into the string runs as a command on the remote host.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Keep the command a constant, quote every inserted value with shlex.quote, or move the operation to SFTP / a fixed remote script that takes validated arguments.

    References: https://docs.paramiko.org/en/stable/api/client.html#paramiko.client.SSHClient.exec_command

    keryn.general.python.sql-string-formatting

    Section titled “keryn.general.python.sql-string-formatting”

    warning python python, sql

    A SQL statement is built by string formatting (%, +, .format, .replace). Values joined into the text become SQL syntax, so a quote in them changes the query. Pass values as driver parameters instead.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-89 LLM05

    Fix: Write the statement with placeholders (cursor.execute(“SELECT … WHERE id = %s”, (value,)) or the driver’s paramstyle) and allow-list identifiers such as table or column names before they reach the text.

    References: https://peps.python.org/pep-0249/#paramstyle · https://docs.python.org/3/library/sqlite3.html#how-to-use-placeholders-to-bind-values-in-sql-queries

    warning python python, sql

    A SQL statement is an f-string with interpolated values. The values become SQL syntax, so a quote in them changes the query. Pass them as driver parameters instead.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-89 LLM05

    Fix: Keep the SQL text constant with placeholders and pass the values as the execute() parameters; allow-list identifiers.

    References: https://peps.python.org/pep-0249/#paramstyle

    keryn.general.python.django-extra-dynamic-sql

    Section titled “keryn.general.python.django-extra-dynamic-sql”

    warning python django

    QuerySet.extra() pastes its select, where and tables entries into the SQL text unescaped. One of them here is not a plain string literal, so run-time data can reach the query as SQL.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-89 LLM05

    Fix: Replace extra() with annotate()/filter() and query expressions; if raw SQL is unavoidable keep the text constant and pass values through params=[…].

    References: https://docs.djangoproject.com/en/stable/ref/models/querysets/#extra

    keryn.general.python.django-rawsql-dynamic

    Section titled “keryn.general.python.django-rawsql-dynamic”

    warning python django

    RawSQL() puts its first argument into the query as SQL. Here it is not a string literal, so text built at run time becomes SQL syntax.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-89 LLM05

    Fix: Keep the SQL text a constant with %s placeholders and pass the values in the params list, or express the query with the ORM.

    References: https://docs.djangoproject.com/en/stable/ref/models/expressions/#raw-sql-expressions

    keryn.general.python.jinja2-autoescape-disabled

    Section titled “keryn.general.python.jinja2-autoescape-disabled”

    warning python jinja2

    A jinja2.Environment is created without autoescape=True or select_autoescape(). Jinja2 does not escape by default, so values rendered into HTML templates are emitted as markup (cross-site scripting).

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-79 LLM05

    Fix: Create the environment with autoescape=select_autoescape([“html”, “htm”, “xml”]) (or autoescape=True) and mark trusted markup explicitly with Markup.

    References: https://jinja.palletsprojects.com/en/stable/api/#autoescaping

    keryn.general.python.mako-template-unescaped

    Section titled “keryn.general.python.mako-template-unescaped”

    warning python mako

    A Mako Template is built without an HTML-escaping default filter. Mako emits ${…} expressions as-is unless a filter escapes them, so values rendered into HTML become markup (cross-site scripting).

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-79 LLM05

    Fix: Pass default_filters=[“h”] (or [“html_escape”]) to Template/TemplateLookup, or escape each expression with ${ value | h }.

    References: https://docs.makotemplates.org/en/latest/filtering.html

    warning python markupsafe, flask

    markupsafe.Markup (flask.Markup) declares its argument already-safe HTML. The argument here is built at run time, so any value inside it reaches the page unescaped (cross-site scripting).

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-79 LLM05

    Fix: Build the markup with Markup(“<b>{}</b>”).format(value) or escape(value), which escape the inserted values, and keep Markup() for constant strings.

    References: https://markupsafe.palletsprojects.com/en/stable/escaping/#markupsafe.Markup

    keryn.general.python.subprocess-shell-dynamic-command

    Section titled “keryn.general.python.subprocess-shell-dynamic-command”

    warning python python

    A subprocess call runs its command through /bin/sh (shell= is set to a true value) and the command is not a fixed string. The shell parses the whole text, so metacharacters in any part built at run time run as commands.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Pass the program and its arguments as a list with shell left off: subprocess.run([prog, arg], check=True). If a shell feature is needed, quote every value with shlex.quote and allow-list the program.

    References: https://docs.python.org/3/library/subprocess.html#security-considerations · https://docs.python.org/3/library/subprocess.html#frequently-used-arguments

    keryn.general.python.subprocess-shell-constant-command

    Section titled “keryn.general.python.subprocess-shell-constant-command”

    info python python

    A subprocess call runs a fixed command string through /bin/sh (shell= is set to a true value). It is not injectable as written, but every later edit that splices a value into the string is, and the shell resolves the program through PATH.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk high: it finds a lead to confirm, so expect benign hits.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Pass the program and its arguments as a list with shell left off: subprocess.run([“/usr/bin/prog”, “arg”], check=True).

    References: https://docs.python.org/3/library/subprocess.html#security-considerations · https://docs.python.org/3/library/subprocess.html#frequently-used-arguments

    keryn.general.python.subprocess-without-shell

    Section titled “keryn.general.python.subprocess-without-shell”

    info python python

    A process is started with subprocess and no shell. No shell parsing happens, but the program and every argument still come from this call: review where they come from and that the program is given by absolute path.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk high: it finds a lead to confirm, so expect benign hits.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Keep shell off, pass a fixed absolute program path as the first list element and validate every argument that is not a constant.

    References: https://docs.python.org/3/library/subprocess.html#security-considerations · https://docs.python.org/3/library/subprocess.html#frequently-used-arguments

    keryn.general.python.shell-keyword-other-function

    Section titled “keryn.general.python.shell-keyword-other-function”

    info python python

    A function other than the subprocess launchers is called with shell= set to a true value. Wrappers that take this flag usually hand their command to /bin/sh, so the same command-injection review applies as for subprocess with a shell.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk high: it finds a lead to confirm, so expect benign hits.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Check what the function does with the flag; prefer an argument list with the shell off and quote any value with shlex.quote.

    References: https://docs.python.org/3/library/subprocess.html#security-considerations · https://docs.python.org/3/library/subprocess.html#frequently-used-arguments

    info python python

    A process is started with a program name that is not an absolute path, so the program that runs is whatever is first on PATH (or in the working directory on Windows) when the call happens.

    Confidence P2: a heuristic or a posture check: confirm by reading the code. False-positive risk high: it finds a lead to confirm, so expect benign hits.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Name the program by its absolute path (“/usr/bin/git”) or resolve it once with shutil.which and check the result.

    References: https://docs.python.org/3/library/subprocess.html#subprocess.Popen · https://cwe.mitre.org/data/definitions/426.html

    keryn.general.python.sql-injection-request-to-query

    Section titled “keryn.general.python.sql-injection-request-to-query”

    error python data-flow rule python, flask, django, fastapi

    A request value is formatted or concatenated into SQL text that is passed to a cursor, a connection, a raw ORM query or SQLAlchemy text(). The value can change the query. Keep the SQL text constant and pass the value as a bound parameter.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-89 LLM05

    Fix: Use placeholders and pass the values separately: cursor.execute(‘select * from t where id = %s’, [value]), Model.objects.raw(sql, [value]), or text(‘… where id = :id’) with session.execute(stmt, {‘id’: value}). For a column or direction name that cannot be a parameter, pick it from a fixed allow-list.

    References: https://docs.python.org/3/library/sqlite3.html#how-to-use-placeholders-to-bind-values-in-sql-queries · https://docs.djangoproject.com/en/stable/topics/db/sql/#passing-parameters-into-raw · https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.text

    keryn.general.python.command-injection-request-to-shell

    Section titled “keryn.general.python.command-injection-request-to-shell”

    error python data-flow rule python, flask, django, fastapi

    A request value reaches a command line that /bin/sh parses (os.system, os.popen, subprocess with shell=True, sh -c, asyncio.create_subprocess_shell). Shell metacharacters in the value run as commands. Pass an argument list with the shell left off.

    Confidence P1: dangerous in context. False-positive risk low: an exact API or flag, so hits are almost always real.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-78 LLM05

    Fix: Run a fixed program with subprocess.run([program, arg1, arg2], shell=False, check=True) and pass each request value as its own list element, after checking it against an allow-list or a strict pattern. If a shell is unavoidable, quote every value with shlex.quote.

    References: https://docs.python.org/3/library/subprocess.html#security-considerations · https://docs.python.org/3/library/shlex.html#shlex.quote

    keryn.general.python.path-traversal-request-to-file

    Section titled “keryn.general.python.path-traversal-request-to-file”

    error python data-flow rule python, flask, django, fastapi

    A request value reaches a file open, read, write, delete or file response (open, os and shutil calls, pathlib, send_file, FileResponse) without being reduced to a base name or confined to a directory. Sequences such as ../ escape the intended folder. Resolve the path against a fixed base and refuse it unless the result stays inside that base.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: reaches content or traffic gets to a thing; it crosses the user->prompt boundary (untrusted input reaching a prompt).

    CWE-22 LLM05

    Fix: Take only os.path.basename(name) or werkzeug’s secure_filename(name), or compute full = (base / name).resolve() and continue only when full.is_relative_to(base) (os.path.commonpath([base, full]) == base on older Pythons). In Flask, send_from_directory(base, name) refuses paths that leave the directory. Prefer an id that the server maps to a path.

    References: https://docs.python.org/3/library/os.path.html#os.path.commonpath · https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.is_relative_to · https://flask.palletsprojects.com/en/stable/api/#flask.send_from_directory · https://werkzeug.palletsprojects.com/en/stable/utils/#werkzeug.utils.secure_filename

    keryn.general.python.ssrf-request-to-outbound-url

    Section titled “keryn.general.python.ssrf-request-to-outbound-url”

    warning python data-flow rule python, flask, django, fastapi

    A request value decides the address of an outbound HTTP call (requests, httpx, urllib, aiohttp). The server can be made to call internal services, cloud metadata endpoints or the loopback interface. Compare the parsed host with an allow-list before the call.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: reaches content or traffic gets to a thing; it crosses the user->prompt boundary (untrusted input reaching a prompt).

    CWE-918 LLM05

    Fix: Parse the value with urllib.parse.urlsplit, require https, and continue only when the hostname is in a fixed allow-list. If arbitrary hosts are a feature, resolve the name first and refuse private, loopback and link-local addresses (ipaddress), and do not follow redirects without checking each hop. Build service URLs from a fixed origin plus a quoted path segment.

    References: https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlsplit · https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address.is_private · https://requests.readthedocs.io/en/latest/api/#requests.request

    keryn.general.python.open-redirect-request-to-location

    Section titled “keryn.general.python.open-redirect-request-to-location”

    warning python data-flow rule python, flask, django, fastapi

    A request value becomes the target of an HTTP redirect (Flask or Django redirect, HttpResponseRedirect, RedirectResponse). A crafted link sends the visitor from this site to an attacker’s page. Redirect only to a relative path of this site or to a host on an allow- list.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: reaches content or traffic gets to a thing; it crosses the user->prompt boundary (untrusted input reaching a prompt).

    CWE-601 LLM05

    Fix: In Django check the value with url_has_allowed_host_and_scheme(url, allowed_hosts={request.get_host()}) before redirecting. Elsewhere accept only a path that starts with a single / (not // or /\\), or parse it with urlsplit and require an empty netloc or a host on an allow-list. Otherwise redirect to a fixed default page.

    References: https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.http.url_has_allowed_host_and_scheme · https://flask.palletsprojects.com/en/stable/api/#flask.redirect · https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse

    keryn.general.python.xss-request-to-html-response

    Section titled “keryn.general.python.xss-request-to-html-response”

    warning python data-flow rule python, flask, django, fastapi

    A request value is written into an HTML response without encoding (a string returned from a Flask view, make_response, HttpResponse, HTMLResponse) or marked as safe markup (Markup, mark_safe). Markup in the value runs in the visitor’s browser. Render it through a template that escapes by default, or escape it first.

    Confidence P1: dangerous in context. False-positive risk medium: the pattern is legitimate in many programs, so check each hit.

    Edge inspected: calls one thing invokes another; it crosses the model->code boundary (model output reaching code, a shell, a query or a page).

    CWE-79 LLM05

    Fix: Render the value with a Jinja2 or Django template (autoescape on), or wrap it in html.escape / markupsafe.escape before placing it in markup; in Django build fragments with format_html instead of mark_safe. Answer with JSON or text/plain when the body is not meant to be HTML.

    References: https://docs.python.org/3/library/html.html#html.escape · https://flask.palletsprojects.com/en/stable/quickstart/#html-escaping · https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.html.format_html · https://fastapi.tiangolo.com/advanced/custom-response/#html-response