python-injection

Python security rules for injection risks listed in the OWASP Top 10 security guide. They inspect database queries and operating-system command calls for unsafe use of input.

In plain words
What is it for?
Use them to detect SQL built with concatenation or formatting and shell commands assembled with variables, so developers can replace those patterns with safer approaches.
Why use it?
They help catch code that could let user-supplied text change a database query or execute an unintended system command.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/abderrahimghazali/cursor-rules/python-injection
Clone the repo
git clone --depth 1 https://github.com/abderrahimghazali/cursor-rules

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 2,203 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 1 finding. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.02203
Opus 5 $0.00000 $0.01102
Sonnet 5 $0.00000 $0.00441
Haiku 4.5 $0.00000 $0.00220

Measured 2d ago against content hash d3e54b843186, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

python-injection scanned grade A with 1 finding against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Runs shell commandslowCapability

Expected in a hook, worth knowing in a rule or an instructions file.

subprocess.run(["ls", "-l", user_dir], shell=False)
.cursor/rules/python-injection.mdc · 178 lines

How it starts

The opening of the file, as written. The whole thing — 178 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Python Injection Security Standards (OWASP A03:2021)

This rule enforces security best practices to prevent injection vulnerabilities in Python applications, as defined in OWASP Top 10:2021-A03.

actions:

  • type: enforce conditions:

    Pattern 1: SQL Injection - String concatenation in SQL queries

    • pattern: "cursor\.(execute|executemany)\(["'][^"']\s[\+%]|cursor\.(execute|executemany)\([^,]+\+\s*[a-zA-Z_][a-zA-Z0-9_]*" message: "Potential SQL injection vulnerability. Use parameterized queries with placeholders instead of string concatenation."

    Pattern 2: SQL Injection - String formatting in SQL queries

    • pattern: "cursor\.(execute|executemany)\(["'][^"']%[^"']["']\s*%\s*|cursor\.(execute|executemany)\(["'][^"']{[^"']}["']\.format" message: "Potential SQL injection vulnerability. Use parameterized queries with placeholders instead of string formatting."

    Pattern 3: Command Injection - Shell command execution with user input

    • pattern: "(os\.system|os\.popen|subprocess\.Popen|subprocess\.call|subprocess\.run|subprocess\.check_output)\([^)]\+\s[a-zA-Z_][a-zA-Z0-9_]*|\b(os\.system|os\.popen|subprocess\.Popen|subprocess\.call|subprocess\.run|subprocess\.check_output)\([^)]*format\(|\b(os\.system|os\.popen|subprocess\.Popen|subprocess\.call|subprocess\.run|subprocess\.check_output)\([^)]*f['"]" message: "Potential command injection vulnerability. Never use string concatenation or formatting with shell commands. Use subprocess with shell=False and pass arguments as a list."

    Pattern 4: Command Injection - Shell=True in subprocess

    • pattern: "(subprocess\.Popen|subprocess\.call|subprocess\.run|subprocess\.check_output)\([^)]shell\s=\s*True" message: "Using shell=True with subprocess functions is dangerous and can lead to command injection. Use shell=False (default) and pass arguments as a list."

    Pattern 5: XSS - Unescaped template variables

    • pattern: "\{\{\s*[^|]\s\}\}|\{\%\sautoescape\s+off\s\%\}" message: "Potential XSS vulnerability. Ensure all template variables are properly escaped. Avoid using 'autoescape off' in templates."

    Pattern 6: XSS - Unsafe HTML rendering in Flask/Django

    • pattern: "render_template\([^)]\)|render\([^)]\)|mark_safe\([^)]\)|safe\s\|" message: "Potential XSS vulnerability. Ensure all user-supplied data is properly escaped before rendering in templates."

    Pattern 7: Path Traversal - Unsafe file operations

    • pattern: "open\([^)]*\+|open\([^)]*format\(|open\([^)]*f['"]" message: "Potential path traversal vulnerability. Validate and sanitize file paths before opening files. Consider using os.path.abspath and os.path.normpath."

    Pattern 8: LDAP Injection - Unsafe LDAP queries

    • pattern: "ldap\.search\([^)]*\+|ldap\.search\([^)]*format\(|ldap\.search\([^)]*f['"]" message: "Potential LDAP injection vulnerability. Use proper LDAP escaping for user-supplied input in LDAP queries."

    Pattern 9: NoSQL Injection - Unsafe MongoDB queries

    • pattern: "find\(\{[^}]*\+|find\(\{[^}]*format\(|find\(\{[^}]*f['"]" message: "Potential NoSQL injection vulnerability. Use parameterized queries or proper escaping for MongoDB queries."

    Pattern 10: Template Injection - Unsafe template rendering

    • pattern: "Template\([^)]\)\.(render|substitute)\(|eval\([^)]\)|exec\([^)]*\)" message: "Potential template injection or code injection vulnerability. Avoid using eval() or exec() with user input, and ensure template variables are properly validated."

Read the full file on GitHub · 178 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 178 lines · 2,203 tokens per session scan A d3e54b843186

Subscribe to this mod's changes

python-injection is a cursor rule published in the GitHub repository abderrahimghazali/cursor-rules (2 stars, last pushed 1y ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,203 tokens. A static security scan graded it A with 1 finding (runs shell commands). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.