Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Django security best practices, authentication, authorization, CSRF protection, SQL injection prevention, XSS prevention, and secure deployment configurations.
.claude/skills/loulanyue-django-security/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-02 | ✗→✓ | ▲ Improved | 193% | 0% |
| case-01 | ✓→✓ | = Same ✓ | 167% | 0% |
| case-14 | ✓→✓ | = Same ✓ | 172% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 119% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 404% | 0% |
Comprehensive security guidelines for Django applications to protect against common vulnerabilities.
python# settings/production.py import os DEBUG = False # CRITICAL: Never use True in production ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(',') # Security headers SECURE_SSL_REDIRECT = True SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True SECURE_HSTS_SECONDS = 31536000 # 1 year SECURE_HSTS_INCLUDE_SUBDOMAINS = True SECURE_HSTS_PRELOAD = True SECURE_CONTENT_TYPE_NOSNIFF = True SECURE_BROWSER_XSS_FILTER = True X_FRAME_OPTIONS = 'DENY' # HTTPS and Cookies SESSION_COOKIE_HTTPONLY = True CSRF_COOKIE_HTTPONLY = True SESSION_COOKIE_SAMESITE = 'Lax' CSRF_COOKIE_SAMESITE = 'Lax' # Secret key (must be set via environment variable) SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY') if not SECRET_KEY: raise ImproperlyConfigured('DJANGO_SECRET_KEY environment variable is required') # Password validation 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', }, ]
python# apps/users/models.py from django.contrib.auth.models import AbstractUser from django.db import models class User(AbstractUser): """Custom user model for better security.""" email = models.EmailField(unique=True) phone = models.CharField(max_length=20, blank=True) USERNAME_FIELD = 'email' # Use email as username REQUIRED_FIELDS = ['username'] class Meta: db_table = 'users' verbose_name = 'User' verbose_name_plural = 'Users' def __str__(self): return self.email # settings/base.py AUTH_USER_MODEL = 'users.User'
python# Django uses PBKDF2 by default. For stronger security: PASSWORD_HASHERS = [ 'django.contrib.auth.hashers.Argon2PasswordHasher', 'django.contrib.auth.hashers.PBKDF2PasswordHasher', 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', ]
python# Session configuration SESSION_ENGINE = 'django.contrib.sessions.backends.cache' # Or 'db' SESSION_CACHE_ALIAS = 'default' SESSION_COOKIE_AGE = 3600 * 24 * 7 # 1 week SESSION_SAVE_EVERY_REQUEST = False SESSION_EXPIRE_AT_BROWSER_CLOSE = False # Better UX, but less secure
python# models.py from django.db import models from django.contrib.auth.models import Permission class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() author = models.ForeignKey(User, on_delete=models.CASCADE) class Meta: permissions = [ ('can_publish', 'Can publish posts'), ('can_edit_others', 'Can edit posts of others'), ] def user_can_edit(self, user): """Check if user can edit this post.""" return self.author == user or user.has_perm('app.can_edit_others') # views.py from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin from django.views.generic import UpdateView class PostUpdateView(LoginRequiredMixin, PermissionRequiredMixin, UpdateView): model = Post permission_required = 'app.can_edit_others' raise_exception = True # Return 403 instead of redirect def get_queryset(self): """Only allow users to edit their own posts.""" return Post.objects.filter(author=self.request.user)
python# permissions.py from rest_framework import permissions class IsOwnerOrReadOnly(permissions.BasePermission): """Allow only owners to edit objects.""" def has_object_permission(self, request, view, obj): # Read permissions allowed for any request if request.method in permissions.SAFE_METHODS: return True # Write permissions only for owner return obj.author == request.user class IsAdminOrReadOnly(permissions.BasePermission): """Allow admins to do anything, others read-only.""" def has_permission(self, request, view): if request.method in permissions.SAFE_METHODS: return True return request.user and request.user.is_staff class IsVerifiedUser(permissions.BasePermission): """Allow only verified users.""" def has_permission(self, request, view): return request.user and request.user.is_authenticated and request.user.is_verified
python# models.py from django.contrib.auth.models import AbstractUser, Group class User(AbstractUser): ROLE_CHOICES = [ ('admin', 'Administrator'), ('moderator', 'Moderator'), ('user', 'Regular User'), ] role = models.CharField(max_length=20, choices=ROLE_CHOICES, default='user') def is_admin(self): return self.role == 'admin' or self.is_superuser def is_moderator(self): return self.role in ['admin', 'moderator'] # Mixins class AdminRequiredMixin: """Mixin to require admin role.""" def dispatch(self, request, *args, **kwargs): if not request.user.is_authenticated or not request.user.is_admin(): from django.core.exceptions import PermissionDenied raise PermissionDenied return super().dispatch(request, *args, **kwargs)
python# GOOD: Django ORM automatically escapes parameters def get_user(username): return User.objects.get(username=username) # Safe # GOOD: Using parameters with raw() def search_users(query): return User.objects.raw('SELECT * FROM users WHERE username = %s', [query]) # BAD: Never directly interpolate user input def get_user_bad(username): return User.objects.raw(f'SELECT * FROM users WHERE username = {username}') # VULNERABLE! # GOOD: Using filter with proper escaping def get_users_by_email(email): return User.objects.filter(email__iexact=email) # Safe # GOOD: Using Q objects for complex queries from django.db.models import Q def search_users_complex(query): return User.objects.filter( Q(username__icontains=query) | Q(email__icontains=query) ) # Safe
python# If you must use raw SQL, always use parameters User.objects.raw( 'SELECT * FROM users WHERE email = %s AND status = %s', [user_input_email, status] )
django{# Django auto-escapes variables by default - SAFE #} {{ user_input }} {# Escaped HTML #} {# Explicitly mark safe only for trusted content #} {{ trusted_html|safe }} {# Not escaped #} {# Use template filters for safe HTML #} {{ user_input|escape }} {# Same as default #} {{ user_input|striptags }} {# Remove all HTML tags #} {# JavaScript escaping #} <script> var username = {{ username|escapejs }}; </script>
pythonfrom django.utils.safestring import mark_safe from django.utils.html import escape # BAD: Never mark user input as safe without escaping def render_bad(user_input): return mark_safe(user_input) # VULNERABLE! # GOOD: Escape first, then mark safe def render_good(user_input): return mark_safe(escape(user_input)) # GOOD: Use format_html for HTML with variables from django.utils.html import format_html def greet_user(username): return format_html('<span class="user">{}</span>', escape(username))
python# settings.py SECURE_CONTENT_TYPE_NOSNIFF = True # Prevent MIME sniffing SECURE_BROWSER_XSS_FILTER = True # Enable XSS filter X_FRAME_OPTIONS = 'DENY' # Prevent clickjacking # Custom middleware from django.conf import settings class SecurityHeaderMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request) response['X-Content-Type-Options'] = 'nosniff' response['X-Frame-Options'] = 'DENY' response['X-XSS-Protection'] = '1; mode=block' response['Content-Security-Policy'] = "default-src 'self'" return response
python# settings.py - CSRF is enabled by default CSRF_COOKIE_SECURE = True # Only send over HTTPS CSRF_COOKIE_HTTPONLY = True # Prevent JavaScript access CSRF_COOKIE_SAMESITE = 'Lax' # Prevent CSRF in some cases CSRF_TRUSTED_ORIGINS = ['https://example.com'] # Trusted domains # Template usage <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Submit</button> </form> # AJAX requests function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== '') { const cookies = document.cookie.split(';'); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } fetch('/api/endpoint/', { method: 'POST', headers: { 'X-CSRFToken': getCookie('csrftoken'), 'Content-Type': 'application/json', }, body: JSON.stringify(data) });
pythonfrom django.views.decorators.csrf import csrf_exempt @csrf_exempt # Only use when absolutely necessary! def webhook_view(request): # Webhook from external service pass
pythonimport os from django.core.exceptions import ValidationError def validate_file_extension(value): """Validate file extension.""" ext = os.path.splitext(value.name)[1] valid_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.pdf'] if not ext.lower() in valid_extensions: raise ValidationError('Unsupported file extension.') def validate_file_size(value): """Validate file size (max 5MB).""" filesize = value.size if filesize > 5 * 1024 * 1024: raise ValidationError('File too large. Max size is 5MB.') # models.py class Document(models.Model): file = models.FileField( upload_to='documents/', validators=[validate_file_extension, validate_file_size] )
python# settings.py MEDIA_ROOT = '/var/www/media/' MEDIA_URL = '/media/' # Use a separate domain for media in production MEDIA_DOMAIN = 'https://media.example.com' # Don't serve user uploads directly # Use whitenoise or a CDN for static files # Use a separate server or S3 for media files
python# settings.py REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': [ 'rest_framework.throttling.AnonRateThrottle', 'rest_framework.throttling.UserRateThrottle' ], 'DEFAULT_THROTTLE_RATES': { 'anon': '100/day', 'user': '1000/day', 'upload': '10/hour', } } # Custom throttle from rest_framework.throttling import UserRateThrottle class BurstRateThrottle(UserRateThrottle): scope = 'burst' rate = '60/min' class SustainedRateThrottle(UserRateThrottle): scope = 'sustained' rate = '1000/day'
python# settings.py REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework_simplejwt.authentication.JWTAuthentication', ], 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticated', ], } # views.py from rest_framework.decorators import api_view, permission_classes from rest_framework.permissions import IsAuthenticated @api_view(['GET', 'POST']) @permission_classes([IsAuthenticated]) def protected_view(request): return Response({'message': 'You are authenticated'})
python# settings.py CSP_DEFAULT_SRC = "'self'" CSP_SCRIPT_SRC = "'self' https://cdn.example.com" CSP_STYLE_SRC = "'self' 'unsafe-inline'" CSP_IMG_SRC = "'self' data: https:" CSP_CONNECT_SRC = "'self' https://api.example.com" # Middleware class CSPMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request) response['Content-Security-Policy'] = ( f"default-src {CSP_DEFAULT_SRC}; " f"script-src {CSP_SCRIPT_SRC}; " f"style-src {CSP_STYLE_SRC}; " f"img-src {CSP_IMG_SRC}; " f"connect-src {CSP_CONNECT_SRC}" ) return response
python# Use python-decouple or django-environ import environ env = environ.Env( # set casting, default value DEBUG=(bool, False) ) # reading .env file environ.Env.read_env() SECRET_KEY = env('DJANGO_SECRET_KEY') DATABASE_URL = env('DATABASE_URL') ALLOWED_HOSTS = env.list('ALLOWED_HOSTS') # .env file (never commit this) DEBUG=False SECRET_KEY=your-secret-key-here DATABASE_URL=postgresql://user:password@localhost:5432/dbname ALLOWED_HOSTS=example.com,www.example.com
python# settings.py LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': 'WARNING', 'class': 'logging.FileHandler', 'filename': '/var/log/django/security.log', }, 'console': { 'level': 'INFO', 'class': 'logging.StreamHandler', }, }, 'loggers': { 'django.security': { 'handlers': ['file', 'console'], 'level': 'WARNING', 'propagate': True, }, 'django.request': { 'handlers': ['file'], 'level': 'ERROR', 'propagate': False, }, }, }
| Check | Description | |-------|-------------| | DEBUG = False | Never run with DEBUG in production | | HTTPS only | Force SSL, secure cookies | | Strong secrets | Use environment variables for SECRET_KEY | | Password validation | Enable all password validators | | CSRF protection | Enabled by default, don't disable | | XSS prevention | Django auto-escapes, don't use |safe with user input | | SQL injection | Use ORM, never concatenate strings in queries | | File uploads | Validate file type and size | | Rate limiting | Throttle API endpoints | | Security headers | CSP, X-Frame-Options, HSTS | | Logging | Log security events | | Updates | Keep Django and dependencies updated |
Remember: Security is a process, not a product. Regularly review and update your security practices.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | pass→pass | 10,353 | 2,749 | -73% | 1 | 1 | 0% | 1,785 | 4,759 | +167% | 0 | 0 | — |
case-14 | pass→pass | 12,851 | 11,295 | -12% | 1 | 1 | 0% | 2,367 | 6,432 | +172% | 0 | 0 | — |
case-02 | fail→pass | 9,100 | 4,363 | -52% | 1 | 1 | 0% | 1,741 | 5,103 | +193% | 0 | 0 | — |
case-03 | pass→pass | 12,929 | 6,418 | -50% | 1 | 1 | 0% | 2,530 | 5,550 | +119% | 0 | 0 | — |
case-04 | pass→pass | 32,288 | 4,297 | -87% | 1 | 1 | 0% | 1,022 | 5,153 | +404% | 0 | 0 | — |
case-05 | pass→pass | 7,914 | 3,572 | -55% | 1 | 1 | 0% | 1,334 | 4,994 | +274% | 0 | 0 | — |
case-06 | pass→pass | 5,954 | 6,244 | +5% | 1 | 1 | 0% | 1,093 | 5,552 | +408% | 0 | 0 | — |
case-07 | pass→pass | 6,419 | 4,652 | -28% | 1 | 1 | 0% | 1,241 | 5,214 | +320% | 0 | 0 | — |
case-08 | pass→pass | 8,324 | 5,884 | -29% | 1 | 1 | 0% | 1,567 | 5,544 | +254% | 0 | 0 | — |
case-09 | pass→pass | 4,830 | 2,679 | -45% | 1 | 1 | 0% | 805 | 4,776 | +493% | 0 | 0 | — |
case-10 | pass→pass | 4,128 | 3,596 | -13% | 1 | 1 | 0% | 747 | 5,106 | +584% | 0 | 0 | — |
case-11 | pass→pass | 4,551 | 3,988 | -12% | 1 | 1 | 0% | 788 | 5,070 | +543% | 0 | 0 | — |
case-12 | pass→pass | 6,520 | 4,205 | -36% | 1 | 1 | 0% | 1,155 | 5,149 | +346% | 0 | 0 | — |
case-13 | pass→pass | 5,675 | 3,529 | -38% | 1 | 1 | 0% | 850 | 4,941 | +481% | 0 | 0 | — |
case-15 | pass→pass | 9,390 | 7,553 | -20% | 1 | 1 | 0% | 1,763 | 5,851 | +232% | 0 | 0 | — |
case-16 | pass→pass | 3,815 | 3,440 | -10% | 1 | 1 | 0% | 677 | 4,885 | +622% | 0 | 0 | — |
case-17 | pass→pass | 11,489 | 5,217 | -55% | 1 | 1 | 0% | 2,290 | 5,310 | +132% | 0 | 0 | — |
case-18 | pass→pass | 3,548 | 3,314 | -7% | 1 | 1 | 0% | 682 | 4,907 | +620% | 0 | 0 | — |
case-19 | pass→pass | 4,532 | 3,239 | -29% | 1 | 1 | 0% | 745 | 4,928 | +561% | 0 | 0 | — |
case-20 | pass→pass | 7,119 | 6,831 | -4% | 1 | 1 | 0% | 1,271 | 5,727 | +351% | 0 | 0 | — |
case-21 | pass→pass | 19,557 | 21,791 | +11% | 1 | 1 | 0% | 3,573 | 8,352 | +134% | 0 | 0 | — |
case-22 | pass→pass | 10,948 | 7,728 | -29% | 1 | 1 | 0% | 2,071 | 5,940 | +187% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.