django-security
Skill3 starsChanged 7 months agoMITView on GitHub
Django security best practices. Covers CSRF, XSS, SQL injection prevention, authentication hardening, Content Security Policy, secrets management, and Django-specific security middleware configuration.
- Reads credentials
- Installs packages
Files
Django Security Patterns
Security hardening for Django 5.x applications.
Settings Hardening
# config/settings/production.py
# HTTPS enforcement
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000 # 1 year
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
# Cookie security
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
CSRF_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = "Lax"
# Content security
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
X_FRAME_OPTIONS = "DENY"
# Host validation
ALLOWED_HOSTS = [".example.com"]
# Secret key from environment
import os
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"] # Never hardcodeCSRF Protection
# All POST forms require CSRF token
# Template:
# {% csrf_token %}
# For API views using DRF, CSRF is handled via authentication classes
from rest_framework.authentication import SessionAuthentication
class CsrfExemptSessionAuth(SessionAuthentication):
"""Only use for webhook endpoints with signature verification."""
def enforce_csrf(self, request):
return # Skip CSRF for verified webhooks only
# Verify webhook signatures instead of skipping CSRF blindly
import hmac
import hashlib
def verify_webhook_signature(payload: bytes, signature: str, secret: str) -> bool:
expected = hmac.new(secret.encode(), payload, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, signature)Input Validation
from django.core.validators import RegexValidator
from django.db import models
class User(models.Model):
# Constrain input at model level
username = models.CharField(
max_length=30,
validators=[RegexValidator(r"^[a-zA-Z0-9_]+$", "Alphanumeric and underscores only")],
)
email = models.EmailField(unique=True)
# DRF serializer validation
from rest_framework import serializers
class UserInputSerializer(serializers.Serializer):
username = serializers.RegexField(r"^[a-zA-Z0-9_]+$", max_length=30)
email = serializers.EmailField()
def validate_username(self, value):
if value.lower() in {"admin", "root", "system"}:
raise serializers.ValidationError("Reserved username.")
return valueSQL Injection Prevention
# Always use ORM or parameterized queries
# Good: ORM handles parameterization
users = User.objects.filter(name=user_input)
# Good: Parameterized raw SQL when ORM insufficient
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM users WHERE name = %s", [user_input])
# NEVER: String interpolation in SQL
# cursor.execute(f"SELECT * FROM users WHERE name = '{user_input}'") # SQL INJECTIONAuthentication Hardening
# Custom authentication backend with rate limiting
from django.contrib.auth.backends import ModelBackend
from django.core.cache import cache
class RateLimitedAuthBackend(ModelBackend):
MAX_ATTEMPTS = 5
LOCKOUT_SECONDS = 300 # 5 minutes
def authenticate(self, request, username=None, password=None, **kwargs):
cache_key = f"login_attempts:{username}"
attempts = cache.get(cache_key, 0)
if attempts >= self.MAX_ATTEMPTS:
return None # Account locked
user = super().authenticate(request, username=username, password=password, **kwargs)
if user is None:
cache.set(cache_key, attempts + 1, self.LOCKOUT_SECONDS)
else:
cache.delete(cache_key)
return user
# Password validation in settings
AUTH_PASSWORD_VALIDATORS = [
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", "OPTIONS": {"min_length": 12}},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]Content Security Policy
# Using django-csp
# pip install django-csp
MIDDLEWARE = [
"csp.middleware.CSPMiddleware",
# ...
]
CSP_DEFAULT_SRC = ("'self'",)
CSP_SCRIPT_SRC = ("'self'",)
CSP_STYLE_SRC = ("'self'", "'unsafe-inline'") # Tighten if possible
CSP_IMG_SRC = ("'self'", "data:", "https:")
CSP_FONT_SRC = ("'self'",)
CSP_CONNECT_SRC = ("'self'",)
CSP_FRAME_SRC = ("'none'",)
CSP_OBJECT_SRC = ("'none'",)XSS Prevention
# Django auto-escapes template variables by default
# {{ user_input }} -> escaped
# {{ user_input|safe }} -> DANGEROUS, only use for trusted HTML
# Sanitize rich text with bleach
import bleach
ALLOWED_TAGS = ["p", "b", "i", "a", "ul", "ol", "li", "br"]
ALLOWED_ATTRS = {"a": ["href", "title"]}
def sanitize_html(html_input: str) -> str:
return bleach.clean(html_input, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS, strip=True)Secrets Management
import os
from pathlib import Path
# Environment-based secrets (production)
DATABASE_URL = os.environ["DATABASE_URL"]
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
API_KEY = os.environ["EXTERNAL_API_KEY"]
# For local development, use .env file with django-environ
# pip install django-environ
import environ
env = environ.Env()
environ.Env.read_env(Path(__file__).resolve().parent / ".env")
SECRET_KEY = env("DJANGO_SECRET_KEY")
DEBUG = env.bool("DEBUG", default=False)
DATABASE_URL = env.db("DATABASE_URL")File Upload Security
import os
import uuid
from django.core.exceptions import ValidationError
def validate_file_extension(value):
ext = os.path.splitext(value.name)[1].lower()
allowed = {".jpg", ".jpeg", ".png", ".gif", ".pdf"}
if ext not in allowed:
raise ValidationError(f"Unsupported file extension: {ext}")
def upload_to(instance, filename):
"""Generate random filename to prevent path traversal."""
ext = os.path.splitext(filename)[1].lower()
return f"uploads/{uuid.uuid4().hex}{ext}"
class Document(models.Model):
file = models.FileField(
upload_to=upload_to,
validators=[validate_file_extension],
max_length=255,
)Security Middleware Stack
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware", # Must be first
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"csp.middleware.CSPMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
# ... application middleware
]Dependency Auditing
# Check for known vulnerabilities
pip-audit
# or
safety check
# Pin dependencies with hashes
pip-compile --generate-hashes requirements.in
pip install --require-hashes -r requirements.txtChecklist
SECRET_KEYloaded from environment, never committed- HTTPS enforced with HSTS in production
- CSRF protection enabled on all state-changing endpoints
- SQL queries use ORM or parameterized raw SQL only
- User input validated at serializer/form level
- File uploads use random filenames and extension validation
- Rate limiting on authentication endpoints
- CSP headers configured
- Dependencies audited for known vulnerabilities
- Session cookies marked Secure, HttpOnly, SameSite
Other versions23 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 23
- loulanyue/awesome-claude-notes★ 272same name
- Fmarzochi/EGC★ 61same name
- skateddu/claude-code-python-setup★ 28same name
- FaisalAlqarni/sp-ecc★ 9same name
- gongyijie85/dsh-ecc★ 8same name
- jdanigo/hydraia★ 8same name
- Lukasbrujula/claude-code-framework★ 4same name
- ronmkr/PromptBook★ 2same name
- ashrafiucse/security-audit-skills★ 1same name
- junimnjw/everything-claude-code★ 1same name
13 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
Used alongsideProjects with it also use configure-ecc, nuxt4-patterns and 10 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
royaluniondesign-sysSkillconfigure-ecc
Interactive installer for Everything Claude Code — guides users through selecting and installing skills and rules to user-level or project-level directories, verifies paths, and optionally optimizes installed files.
Also in 10 projects that use this
loulanyueSkillnuxt4-patterns
Nuxt 4 app patterns for hydration safety, performance, route rules, lazy loading, and SSR-safe data fetching with useFetch and useAsyncData.
Also in 9 projects that use this
ronmkrSkilldefi-amm-security
Security checklist for Solidity AMM contracts, liquidity pools, and swap flows. Covers reentrancy, CEI ordering, donation or inflation attacks, oracle manipulation, slippage, admin controls, and integer math.
Also in 7 projects that use this
ronmkrSkillquarkus-security
Quarkus Security best practices for authentication, authorization, JWT/OIDC, RBAC, input validation, CSRF, secrets management, and dependency security.
Also in 6 projects that use this
TironFoxSkillagent-payment-x402
Add x402 payment execution to AI agents — per-task budgets, spending controls, and non-custodial wallets via MCP tools. Use when agents need to pay for APIs, services, or other agents.
Also in 5 projects that use this
junimnjwSkillralphinho-rfc-pipeline
RFC-driven multi-agent DAG execution pattern with quality gates, merge queues, and work unit orchestration.
Also in 10 projects that use this
- +1 more from ronmkr
In this repository72 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- api-designerSkill
- backend-patternsSkill
- code-reviewerSkill
- debugging-assistantSkill
- golang-testingSkill
- project-guidelines-exampleSkill
- python-testingSkill
- refactoring-guideSkill
- spec-driven-developmentSkill
- tdd-workflowSkill
- verification-loopSkill
- update-docsSkill
- api-docsSkill
- changelog-writerSkill
- readme-generatorSkill
- django-patternsSkill
- django-tddSkill
- django-verificationSkill
- fastapi-patternsSkill
- nextjs-patternsSkill
- react-patternsSkill
- springboot-patternsSkill
- springboot-securitySkill
- springboot-tddSkill
- branch-strategySkill
- changelogSkill
- commit-helperSkill
- pr-creatorSkill
- prSkill
- evolveSkill
- instinct-exportSkill
- instinct-importSkill
- instinct-statusSkill
- cleanupSkill
- lint-fixSkill
- refactor-cleanSkill
- update-depsSkill
- continuous-learningSkill
- continuous-learning-v2Skill
- eval-harnessSkill
- iterative-retrievalSkill
- strategic-compactSkill
- golang-patternsSkill
- python-patternsSkill
- rust-patternsSkill
- typescript-patternsSkill
- accessibility-checkerSkill
- build-fixSkill
- code-reviewSkill
- e2eSkill
- performance-reviewerSkill
- security-auditorSkill
- security-reviewSkill
- test-coverageSkill
- checkpointSkill
- deploySkill
- evalSkill
- The first 60 shown.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

