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