django
Application security guide, audit checklist, and static scanner for Django and DRF web applications. Use when writing, refactoring, or reviewing Django views, ORM queries, models, serializers, forms, authentication flows, file uploads, or production settings.
- License 许可
- MIT
- 最近更新
- 2026-09-21
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- django
- license
- Complete terms in LICENSE.txt
- description
- Application security guide, audit checklist, and static scanner for Django and DRF web applications. Use when writing, refactoring, or reviewing Django views, ORM queries, models, serializers, forms, authentication flows, file uploads, or production settings.
02 资源文件 10
- 脚本examples/secure_settings.py1.7 KB
- 脚本examples/vulnerable_vs_secure.py3.5 KB
- 许可证LICENSE.txt1.0 KB
- 参考文档references/api-drf-security.md4.1 KB
- 参考文档references/authz-idor.md5.1 KB
- 参考文档references/file-upload-security.md5.0 KB
- 参考文档references/orm-sql-injection.md4.2 KB
- 参考文档references/settings-hardening.md4.4 KB
- 参考文档references/xss-csrf-defense.md5.1 KB
- 脚本scripts/audit.py8.0 KB
03 SKILL.md 正文
Django Application Security
This skill guides AI agents in identifying, preventing, and remediating application security vulnerabilities across Django applications and Django REST Framework (DRF) APIs.
Helper Scripts Available:
scripts/audit.py- AST and pattern static security scanner for Django projects
Always run scripts with --help first to see usage. DO NOT read the source until you try running the script first and find that a customized solution is absolutely necessary. These scripts can be very large and thus pollute your context window. They exist to be called directly as black-box scripts rather than ingested into your context window.
Decision Tree: Choosing Your Approach
Django Task → What area of the application are you working on?
├─ Views / APIs → Is authentication and object-level ownership verified?
│ ├─ No → Filter database query by request.user / organization
│ └─ Yes → Check CSRF token & rate limiting
│
├─ Database / ORM → Are raw queries or cursors used?
│ ├─ Yes → Parameterize with placeholders (%s); eliminate f-strings
│ └─ No → Use standard ORM filter/exclude
│
├─ Serializers (DRF) → Is fields = '__all__' present?
│ ├─ Yes → Replace with explicit list; mark sensitive fields write_only=True
│ └─ No → Proceed with scoped validation
│
├─ File Uploads → Are uploaded filenames preserved from client?
│ ├─ Yes → Generate UUID filenames, validate magic bytes & size
│ └─ No → Verify storage isolation
│
└─ Deployment / Settings → Is DEBUG = True or ALLOWED_HOSTS = ['*']?
├─ Yes → Bind to environment variables; enforce HSTS & secure cookies
└─ No → Run: python manage.py check --deploy
Quick Reference Workflow
1. Run Automated Security Scanner
Run the included AST security scanner on your target directory:
python scripts/audit.py /path/to/django/project
2. View & API Authorization (IDOR Defense)
Never retrieve objects using an unvalidated primary key without verifying requesting user ownership:
# VULNERABLE: Direct access without ownership check
doc = Document.objects.get(id=doc_id)
# SECURE: Scoped to authenticated user
doc = get_object_or_404(Document, id=doc_id, owner=request.user)
See: authz-idor.md
3. ORM & SQL Injection Defense
Never use string formatting (f-strings, .format(), %) in raw SQL:
# VULNERABLE: f-string interpolation
User.objects.raw(f"SELECT * FROM auth_user WHERE username = '{username}'")
# SECURE: Parameterized placeholder
User.objects.raw("SELECT * FROM auth_user WHERE username = %s", [username])
See: orm-sql-injection.md
4. XSS & Template Escaping (mark_safe)
Avoid using mark_safe on raw user inputs. If rich HTML is required, sanitize with nh3:
import nh3
from django.utils.safestring import mark_safe
clean_html = nh3.clean(user_input, tags={"p", "b", "i", "strong", "em", "a"})
safe_content = mark_safe(clean_html)
See: xss-csrf-defense.md
5. Production Settings Checklist
DEBUG = FalseSECRET_KEYloaded from environmentALLOWED_HOSTSexplicit (no wildcard*)SESSION_COOKIE_SECURE = TrueCSRF_COOKIE_SECURE = TrueSECURE_SSL_REDIRECT = TrueSECURE_HSTS_SECONDS = 31536000See: settings-hardening.md and secure_settings.py