Question presented to candidate: "You are reviewing a pull request for a Python web service. What security mistakes would you look for that are specific to Python or common in Python code, and what would you ask the author to use instead?"
What a strong answer should cover:
- Turning data into code:
eval/execon input (useast.literal_evalorjson),pickleor an unsafe YAML loader on untrusted bytes (they can run code), user-controlledstr.formattemplates that read attributes. - Injection into other interpreters: SQL built with f-strings (use
?/%splaceholders), shell commands withshell=True(pass an argument list), and path traversal with../(resolve and checkis_relative_to). - Secrets:
randomis predictable, usesecrets; compare tokens withhmac.compare_digest; store passwords with a slow, salted KDF (hashlib.scrypt,pbkdf2_hmac, or argon2/bcrypt), never a bare hash. - Python-specific traps:
assertdisappears underpython -O;tarfile.extractallwithoutfilter="data"before 3.14; untrusted XML without defusedxml. - Supply chain and configuration: typosquatted packages, unpinned dependencies (lock with hashes),
verify=False, debug mode and secrets in logs or source.
Clarifying questions expected:
- Where does untrusted input enter (HTTP bodies, uploaded files, message queues), and what does the service do with it?
Code / implementation expected: Optional โ short before and after snippets, such as a parameterised query or literal_eval instead of eval.