# general-injection-python-v1

> general-injection-python-v1: 23 Keryn rules for python (4 error, 13 warning, 6 info).

Source: https://keryn.dev/rules/general-injection-python-v1/

23 rules · Keryn Software Licence · rules 0.8.0 · [all packs](https://keryn.dev/rules/)

**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

`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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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/os.html#os.system) · [https://docs.python.org/3/library/subprocess.html#security-considerations](https://docs.python.org/3/library/subprocess.html#security-considerations)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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/os.html#os.system) · [https://docs.python.org/3/library/subprocess.html#replacing-os-system](https://docs.python.org/3/library/subprocess.html#replacing-os-system)

## keryn.general.python.exec-spawn-process

`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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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.execl) · [https://docs.python.org/3/library/os.html#os.spawnl](https://docs.python.org/3/library/os.html#os.spawnl) · [https://docs.python.org/3/library/os.html#os.startfile](https://docs.python.org/3/library/os.html#os.startfile)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-155](https://keryn.dev/rules/?cwe=CWE-155) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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://www.gnu.org/software/tar/manual/html_node/checkpoints.html) · [https://docs.python.org/3/library/subprocess.html#security-considerations](https://docs.python.org/3/library/subprocess.html#security-considerations)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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](https://docs.paramiko.org/en/stable/api/client.html#paramiko.client.SSHClient.exec_command)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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://peps.python.org/pep-0249/#paramstyle) · [https://docs.python.org/3/library/sqlite3.html#how-to-use-placeholders-to-bind-values-in-sql-queries](https://docs.python.org/3/library/sqlite3.html#how-to-use-placeholders-to-bind-values-in-sql-queries)

## keryn.general.python.sql-fstring

`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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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](https://peps.python.org/pep-0249/#paramstyle)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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](https://docs.djangoproject.com/en/stable/ref/models/querysets/#extra)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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](https://docs.djangoproject.com/en/stable/ref/models/expressions/#raw-sql-expressions)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-79](https://keryn.dev/taxonomies/cwe/#cwe-79-cross-site-scripting) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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](https://jinja.palletsprojects.com/en/stable/api/#autoescaping)

## keryn.general.python.mako-template-unescaped

`warning` python mako

A Mako Template is built without an HTML-escaping default filter. Mako emits $&#123;...&#125; 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-79](https://keryn.dev/taxonomies/cwe/#cwe-79-cross-site-scripting) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Pass default\_filters=["h"] (or ["html\_escape"]) to Template/TemplateLookup, or escape each expression with $&#123; value | h &#125;.

**References:** [https://docs.makotemplates.org/en/latest/filtering.html](https://docs.makotemplates.org/en/latest/filtering.html)

## keryn.general.python.markup-non-literal

`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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-79](https://keryn.dev/taxonomies/cwe/#cwe-79-cross-site-scripting) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Build the markup with Markup("&lt;b&gt;&#123;&#125;&lt;/b&gt;").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](https://markupsafe.palletsprojects.com/en/stable/escaping/#markupsafe.Markup)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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#security-considerations) · [https://docs.python.org/3/library/subprocess.html#frequently-used-arguments](https://docs.python.org/3/library/subprocess.html#frequently-used-arguments)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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#security-considerations) · [https://docs.python.org/3/library/subprocess.html#frequently-used-arguments](https://docs.python.org/3/library/subprocess.html#frequently-used-arguments)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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#security-considerations) · [https://docs.python.org/3/library/subprocess.html#frequently-used-arguments](https://docs.python.org/3/library/subprocess.html#frequently-used-arguments)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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#security-considerations) · [https://docs.python.org/3/library/subprocess.html#frequently-used-arguments](https://docs.python.org/3/library/subprocess.html#frequently-used-arguments)

## keryn.general.python.process-partial-path

`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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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://docs.python.org/3/library/subprocess.html#subprocess.Popen) · [https://cwe.mitre.org/data/definitions/426.html](https://cwe.mitre.org/data/definitions/426.html)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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, &#123;'id': value&#125;). 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.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.djangoproject.com/en/stable/topics/db/sql/#passing-parameters-into-raw) · [https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.text](https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.text)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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/subprocess.html#security-considerations) · [https://docs.python.org/3/library/shlex.html#shlex.quote](https://docs.python.org/3/library/shlex.html#shlex.quote)

## 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](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-22](https://keryn.dev/taxonomies/cwe/#cwe-22-path-traversal) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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/os.path.html#os.path.commonpath) · [https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.is_relative_to](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://flask.palletsprojects.com/en/stable/api/#flask.send_from_directory) · [https://werkzeug.palletsprojects.com/en/stable/utils/#werkzeug.utils.secure_filename](https://werkzeug.palletsprojects.com/en/stable/utils/#werkzeug.utils.secure_filename)

## 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](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-918](https://keryn.dev/taxonomies/cwe/#cwe-918-server-side-request-forgery) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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/urllib.parse.html#urllib.parse.urlsplit) · [https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address.is_private](https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address.is_private) · [https://requests.readthedocs.io/en/latest/api/#requests.request](https://requests.readthedocs.io/en/latest/api/#requests.request)

## 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](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-601](https://keryn.dev/rules/?cwe=CWE-601) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** In Django check the value with url\_has\_allowed\_host\_and\_scheme(url, allowed\_hosts=&#123;request.get\_host()&#125;) 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://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://flask.palletsprojects.com/en/stable/api/#flask.redirect) · [https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse](https://fastapi.tiangolo.com/advanced/custom-response/#redirectresponse)

## 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](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-79](https://keryn.dev/taxonomies/cwe/#cwe-79-cross-site-scripting) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**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://docs.python.org/3/library/html.html#html.escape) · [https://flask.palletsprojects.com/en/stable/quickstart/#html-escaping](https://flask.palletsprojects.com/en/stable/quickstart/#html-escaping) · [https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.html.format_html](https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.html.format_html) · [https://fastapi.tiangolo.com/advanced/custom-response/#html-response](https://fastapi.tiangolo.com/advanced/custom-response/#html-response)
