Skip to content

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

    general-hazards-python-v1

    View as Markdown

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

    Section titled “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 authority is given or left unchecked.

    CWE-400 CWE-1088 LLM10

    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://www.python-httpx.org/advanced/timeouts/

    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 something relies on a component it does not verify; it crosses the supply-chain boundary (code, models or prompts pulled in from outside).

    CWE-502 LLM05 LLM03

    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

    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 one thing writes to another.

    CWE-377 LLM05

    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

    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 one thing invokes another.

    CWE-330 CWE-338 LLM05

    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/secrets.html

    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 one thing invokes another.

    CWE-776 CWE-611 LLM05

    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://github.com/tiran/defusedxml

    keryn.general.python.logging-config-listen

    Section titled “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 data leaves for another party; it crosses the secret->outside boundary (a secret leaving the trusted side).

    CWE-94 LLM05

    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

    keryn.general.python.bidi-control-character

    Section titled “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 content or traffic gets to a thing; it crosses the user->prompt boundary (untrusted input reaching a prompt).

    CWE-838 LLM05

    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://www.unicode.org/reports/tr9/

    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 authority is given or left unchecked.

    CWE-732 LLM05

    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/stat.html#stat.S_IWOTH

    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 content or traffic gets to a thing; it crosses the user->prompt boundary (untrusted input reaching a prompt).

    CWE-1327 CWE-200 LLM05

    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

    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 authority is given or left unchecked.

    CWE-377 CWE-379 LLM05

    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

    keryn.general.python.exception-silently-ignored

    Section titled “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 one thing invokes another.

    CWE-390 LLM05

    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

    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 one thing invokes another.

    CWE-617 LLM05

    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/using/cmdline.html#cmdoption-O

    keryn.general.python.hardcoded-password-string

    Section titled “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 authority is given or left unchecked.

    CWE-259 CWE-798 LLM02

    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

    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 content or traffic gets to a thing; it crosses the user->prompt boundary (untrusted input reaching a prompt).

    CWE-918 CWE-22 LLM05

    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#legacy-interface