Skip to content

Commit 1aa909b

Browse files
committed
Add Python rule file
1 parent 8b79e02 commit 1aa909b

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ Current version: **1.0.0**
77
### Overview
88
This repository contains security rule files designed to be used with AI-assisted developer tools such as GitHub Copilot, Cursor, Windsurf, and other context-aware coding assistants.
99

10-
Each rule file defines security best practices that guide how AI tools interact with your codebase. They are organized by platform:
11-
- **Backend** (`backend.md`)
12-
- **Frontend** (`frontend.md`)
13-
- **Mobile** (`mobile.md`)
10+
Each rule file defines security best practices that guide how AI tools interact with your codebase.
11+
12+
#### Platform-Specific Rules
13+
- **Backend** (`backend.md`) - Server-side security practices
14+
- **Frontend** (`frontend.md`) - Client-side security practices
15+
- **Mobile** (`mobile.md`) - Mobile application security
16+
17+
#### Language-Specific Rules
18+
- **Python** (`language-specific/python.md`) - Python-specific security practices including deserialization, cryptography, and safe coding patterns
1419

1520
### Purpose
1621
AI coding assistants are powerful, but they can generate insecure or non-compliant code without proper guidance. These tools work best when contextual rules steer them toward safe, standardized output.

language-specific/python.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## General Coding Practices
2+
- Do not use `assert` statements for security checks or input validation.
3+
4+
## Database
5+
- Use parameterized queries and parameters as a second argument to `cursor.execute()` (e.g., `cursor.execute(sql, (param,))`).
6+
- For ORMs (SQLAlchemy, Django), use ORM methods and avoid raw SQL with string formatting.
7+
8+
## Code Execution
9+
- Do not use `eval()` or `exec()` with data from untrusted sources.
10+
- Always use `subprocess.run()` with `shell=False` (the default). Pass commands and arguments as a list (e.g., `subprocess.run(['ls', '-l', a_variable])`).
11+
12+
## Cryptography & Secrets Management
13+
- Use the `secrets` module for generating all security-sensitive tokens, keys, or passwords. Do not use the `random` module.
14+
- Use `hmac.compare_digest()` to compare secrets. Do not use the `==` operator for secrets like API keys or tokens.
15+
- When working with secret or sensitive information in variables, after use set the variable to `None` and force garbage collection.
16+
17+
## File System and I/O
18+
- Validate all file paths built from user input. Use `os.path.abspath()` to canonicalize the path, then verify it is inside the intended base directory using `os.path.commonpath([full_path, base_dir]) == os.path.abspath(base_dir)`.
19+
- Use secure functions for creating temporary files. Prefer `tempfile.mkstemp()` or `tempfile.NamedTemporaryFile` over `tempfile.mktemp()`.
20+
21+
## Networking
22+
- Configure secure TLS/SSL contexts. Use `ssl.create_default_context()` or `ssl.SSLContext` with modern protocols (e.g., `ssl.PROTOCOL_TLS_CLIENT`).
23+
- Validate URLs with `ipaddress` before connecting to prevent SSRF.
24+
25+
## Deserialization and Data Parsing
26+
- Never use `pickle`, `cPickle`, or `dill` to deserialize data from untrusted sources.
27+
- Do not use `shelve` or `marshal` to deserialize untrusted data.
28+
- When parsing YAML, always use `yaml.safe_load()`. Never use `yaml.load()`.
29+
- When parsing XML, disable external entity resolution. Use `xml.etree.ElementTree.XMLParser` with `resolve_entities=False`.
30+
- When parsing data with `json`, limit the size of the input.
31+
- Validate regex patterns for ReDoS vulnerabilities. Avoid nested quantifiers like `(a+)+` or `(a*)*`.
32+
33+
34+

0 commit comments

Comments
 (0)