Python (Django & FastAPI) Security Pitfalls in Code Reviews
Top security pitfalls in Python Django and FastAPI codebases: ORM raw queries, pickle deserialization, Pydantic/FastAPI auth dependencies, and subprocess shells.
Source Code Review · By Thimmagurudu Bathini, CTO · Published · 6 min read
Where Python Web Frameworks Still Break
Django and FastAPI provide strong defaults, yet manual source code reviews consistently uncover dangerous patterns in custom utilities, background Celery tasks, and FastAPI dependency chains. Knowing where Python apps break helps engineering teams ship cleaner code before pentesting.
# Vulnerable raw query:
cursor.execute(f"SELECT * FROM orders WHERE id = '{order_id}'")
# Safe parameterized query:
cursor.execute("SELECT * FROM orders WHERE id = %s", (order_id,))Auditing Routers, Celery Tasks & Deserializers
- Search the codebase for raw SQL (.raw, cursor.execute), subprocess(shell=True), eval/exec, and pickle/yaml.load.
- Audit FastAPI APIRouter definitions to confirm every non-public route enforces authentication and tenant/role dependencies.
- Check Django settings for DEBUG = True, wildcard ALLOWED_HOSTS, and CSRF_COOKIE_HTTPONLY/SECURE flags.
- Review Celery background tasks for unvalidated object IDs or SSRF URLs.
Dangerous Patterns in Django & FastAPI Code
| Framework / Module | Insecure Pattern | Secure Alternative |
|---|---|---|
| Django ORM | .raw() or .extra() with f-strings / % formatting | Use ORM QuerySets or parameterized params=[] |
| FastAPI Dependencies | Missing Depends(get_current_user) or role check on router | Enforce auth at APIRouter(dependencies=...) + per-object check |
| Serialization | pickle.loads(), yaml.load() without SafeLoader | Use JSON/Pydantic or yaml.safe_load() |
| Subprocess / Files | subprocess.run(..., shell=True) or os.path.join traversal | Pass argument lists with shell=False and resolve/verify base path |
Pairing SAST With Manual Business-Logic Review
Pair automated SAST (Bandit/Semgrep) with manual business-logic and dependency-injection review across every router and background worker.
From the Field: Unprotected FastAPI Routers & Raw SQL Helpers
During Python source code reviews, the most common misses aren't in standard ORM queries—they hide in custom reporting endpoints using f-strings inside cursor.execute() and FastAPI routers where an individual route overrides or omits the shared authentication dependency.
Book a manual Python/Django/FastAPI source code review starting at ₹36,000 + 18% GST. Request a scoped proposal →
Python Code Review Questions
Do you offer manual Python source code review?
Yes—our source code review service combines static analysis with manual line-by-line review from ₹36,000 + 18% GST.
Should we do code review before or after VAPT?
White-box/gray-box testing or pre-release code review catches deeper flaws faster before production launch.