# general-hazards-python-v1

> general-hazards-python-v1: 14 Keryn rules for python (1 error, 7 warning, 6 info).

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

14 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.http-request-without-timeout

`warning` python requests, httpx

A requests call has no timeout (requests waits forever by default), or a requests/httpx call sets timeout=None. A slow or malicious server can then hold the worker, its connection and its thread indefinitely.

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

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-400](https://keryn.dev/rules/?cwe=CWE-400) [CWE-1088](https://keryn.dev/rules/?cwe=CWE-1088) [LLM10](https://keryn.dev/taxonomies/owasp-llm/#llm10-unbounded-consumption)

**Fix:** Pass an explicit timeout to every call (requests.get(url, timeout=(3.05, 30))) or set it once on an httpx.Client(timeout=...).

**References:** [https://requests.readthedocs.io/en/latest/user/advanced/#timeouts](https://requests.readthedocs.io/en/latest/user/advanced/#timeouts) · [https://www.python-httpx.org/advanced/timeouts/](https://www.python-httpx.org/advanced/timeouts/)

## keryn.general.python.marshal-load

`warning` python python

marshal.load / loads reads an internal Python format that is not safe against crafted input: malformed data can crash the interpreter, and code objects it returns are run by whoever loads them.

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

Edge inspected: [depends_on](https://keryn.dev/docs/edges/edge-kinds/#depends-on) something relies on a component it does not verify; it crosses the [`supply-chain`](https://keryn.dev/docs/edges/trust-boundaries/#supply-chain) boundary (code, models or prompts pulled in from outside).

[CWE-502](https://keryn.dev/taxonomies/cwe/#cwe-502-deserialization-of-untrusted-data) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling) [LLM03](https://keryn.dev/taxonomies/owasp-llm/#llm03-supply-chain)

**Fix:** Exchange data as JSON (or another schema-validated format); keep marshal for files this process wrote and protects.

**References:** [https://docs.python.org/3/library/marshal.html](https://docs.python.org/3/library/marshal.html)

## keryn.general.python.tempfile-mktemp

`warning` python python

tempfile.mktemp returns a name without creating the file, so another process can create (or symlink) that path before this one opens it.

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

Edge inspected: [writes](https://keryn.dev/docs/edges/edge-kinds/#writes) one thing writes to another.

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

**Fix:** Use tempfile.NamedTemporaryFile(delete=False), tempfile.mkstemp() or tempfile.TemporaryDirectory(), which create the file atomically.

**References:** [https://docs.python.org/3/library/tempfile.html#tempfile.mktemp](https://docs.python.org/3/library/tempfile.html#tempfile.mktemp)

## keryn.general.python.random-for-security

`info` python python

The random module is a Mersenne Twister: its output can be predicted after enough samples. If this value is a token, password, key, nonce or any other secret, an attacker can guess it.

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.

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

**Fix:** Use the secrets module (secrets.token\_urlsafe, secrets.choice, secrets.randbelow) or random.SystemRandom for anything security-relevant.

**References:** [https://docs.python.org/3/library/random.html](https://docs.python.org/3/library/random.html) · [https://docs.python.org/3/library/secrets.html](https://docs.python.org/3/library/secrets.html)

## keryn.general.python.xml-stdlib-parser

`warning` python python, xml

The standard-library XML parsers expand internal entities without limit (billion laughs, quadratic blowup) and, depending on the expat version and handler, can resolve external entities. Untrusted XML should go through defusedxml.

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.

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

**Fix:** Parse untrusted documents with defusedxml (defusedxml.ElementTree.fromstring, defusedxml.minidom.parseString, defusedxml.sax.make\_parser).

**References:** [https://docs.python.org/3/library/xml.html#xml-vulnerabilities](https://docs.python.org/3/library/xml.html#xml-vulnerabilities) · [https://github.com/tiran/defusedxml](https://github.com/tiran/defusedxml)

## keryn.general.python.logging-config-listen

`error` python python

logging.config.listen starts a socket server that accepts logging configuration and passes it to dictConfig/fileConfig, which can import and call arbitrary code. Without verify= any process that can reach the port runs code here.

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

Edge inspected: [sends](https://keryn.dev/docs/edges/edge-kinds/#sends) data leaves for another party; it crosses the [`secret->outside`](https://keryn.dev/docs/edges/trust-boundaries/#secret-to-outside) boundary (a secret leaving the trusted side).

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

**Fix:** Do not expose the listener; if it is needed bind it to localhost and pass verify= with a function that checks a signature or decrypts the payload.

**References:** [https://docs.python.org/3/library/logging.config.html#logging.config.listen](https://docs.python.org/3/library/logging.config.html#logging.config.listen)

## keryn.general.python.bidi-control-character

`warning` python python

The source contains a Unicode bidirectional embedding, override or isolate control (U+202A-U+202E, U+2066-U+2069). Editors render the code in a different order than the interpreter runs it, which hides logic from review (Trojan Source, CVE-2021-42574). Directional marks (U+200E, U+200F, U+061C) cannot reorder code and are normal in right-to-left text: not reported.

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

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-838](https://keryn.dev/rules/?cwe=CWE-838) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Remove the control characters; if right-to-left text is needed in a string literal, write it with \\u escapes.

**References:** [https://trojansource.codes/](https://trojansource.codes/) · [https://www.unicode.org/reports/tr9/](https://www.unicode.org/reports/tr9/)

## keryn.general.python.chmod-permissive

`warning` python python

The file mode grants group or other write or execute permission (for example 0o777, 0o775, 0o766). Other users on the host can then change or run the file.

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

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

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

**Fix:** Use the narrowest mode that works: 0o600 for secrets, 0o644 for readable data, 0o700/0o755 only for directories and programs that need it.

**References:** [https://docs.python.org/3/library/os.html#os.chmod](https://docs.python.org/3/library/os.html#os.chmod) · [https://docs.python.org/3/library/stat.html#stat.S_IWOTH](https://docs.python.org/3/library/stat.html#stat.S_IWOTH)

## keryn.general.python.bind-all-interfaces

`info` python python

The server binds to 0.0.0.0, every network interface of the host, so it is reachable from any network the host is on, not only from localhost or the intended interface.

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: [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-1327](https://keryn.dev/rules/?cwe=CWE-1327) [CWE-200](https://keryn.dev/taxonomies/cwe/#cwe-200-exposure-of-sensitive-information-to-an-unauthorized-actor) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Bind to 127.0.0.1 (or the specific interface) unless the service must be public, and read the address from configuration.

**References:** [https://docs.python.org/3/library/socket.html#socket.socket.bind](https://docs.python.org/3/library/socket.html#socket.socket.bind)

## keryn.general.python.hardcoded-temp-path

`info` python python

A fixed path under /tmp, /var/tmp or /dev/shm is used. Those directories are shared by every user, so another account can create or symlink the path first and read or redirect what this program writes.

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: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

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

**Fix:** Create temporary files and directories with tempfile.mkstemp / NamedTemporaryFile / TemporaryDirectory, which pick unique names with safe permissions.

**References:** [https://docs.python.org/3/library/tempfile.html](https://docs.python.org/3/library/tempfile.html)

## keryn.general.python.exception-silently-ignored

`info` python python

A bare `except:` or `except Exception:` handler does nothing but pass or continue. Every error, including failed authentication, permission or integrity checks, is dropped without a trace, so the code carries on as if the check had passed.

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.

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

**Fix:** Catch the specific exception you expect, log it (logger.exception) and decide explicitly whether to continue, retry or fail closed.

**References:** [https://docs.python.org/3/tutorial/errors.html#handling-exceptions](https://docs.python.org/3/tutorial/errors.html#handling-exceptions)

## keryn.general.python.assert-used

`info` python python

An assert statement in application code. Python drops every assert when it runs with -O or PYTHONOPTIMIZE, so a check written as an assert (a permission, an input bound, an invariant the code relies on) is not there in an optimised deployment.

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.

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

**Fix:** Raise an explicit exception (if not ok: raise PermissionError(...)) for checks the program depends on; keep assert for test code and for conditions that are only documentation.

**References:** [https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement](https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement) · [https://docs.python.org/3/using/cmdline.html#cmdoption-O](https://docs.python.org/3/using/cmdline.html#cmdoption-O)

## keryn.general.python.hardcoded-password-string

`info` python python

A password, passphrase, token or secret is written into the source as a string literal (assigned, compared against, passed as a keyword argument or used as a parameter default). Anyone with the code has the credential, and rotating it needs a release.

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: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-259](https://keryn.dev/rules/?cwe=CWE-259) [CWE-798](https://keryn.dev/taxonomies/cwe/#cwe-798-use-of-hard-coded-credentials) [LLM02](https://keryn.dev/taxonomies/owasp-llm/#llm02-sensitive-information-disclosure)

**Fix:** Read the credential at run time from the environment or a secret manager, and compare secrets with hmac.compare\_digest against a stored hash.

**References:** [https://cwe.mitre.org/data/definitions/798.html](https://cwe.mitre.org/data/definitions/798.html)

## keryn.general.python.urlopen-any-scheme

`warning` python python

urllib.request.urlopen / urlretrieve (and the URLopener classes) open file://, ftp:// and data: URLs as readily as http(s)://. With a URL that is not a fixed http(s) literal, input can make the program read local files or reach internal hosts.

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: [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) [CWE-22](https://keryn.dev/taxonomies/cwe/#cwe-22-path-traversal) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Parse the URL first and allow only https (urllib.parse.urlsplit(url).scheme == "https") and expected hosts, or use requests/httpx, which do not open file: URLs.

**References:** [https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen](https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen) · [https://docs.python.org/3/library/urllib.request.html#legacy-interface](https://docs.python.org/3/library/urllib.request.html#legacy-interface)
