""" Django settings for Website Analyzer project. This module contains all configuration settings for the Django application, including database, caching, security, and third-party integrations. """ import os from pathlib import Path from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-change-me-in-production') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv('DEBUG', 'False').lower() in ('true', '1', 'yes') ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',') # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Third-party apps 'rest_framework', 'corsheaders', # Local apps 'websites', 'scanner', 'api', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'core.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'core.wsgi.application' # Database # Parse DATABASE_URL or use SQLite for local development DATABASE_URL = os.getenv('DATABASE_URL', '') if DATABASE_URL: # Parse the DATABASE_URL for PostgreSQL import re db_pattern = r'postgres://(?P[^:]+):(?P[^@]+)@(?P[^:]+):(?P\d+)/(?P.+)' db_match = re.match(db_pattern, DATABASE_URL) if db_match: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': db_match.group('name'), 'USER': db_match.group('user'), 'PASSWORD': db_match.group('password'), 'HOST': db_match.group('host'), 'PORT': db_match.group('port'), } } else: # Invalid DATABASE_URL format, fallback to SQLite DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } else: # No DATABASE_URL set - use SQLite for local development DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } # Password validation AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_TZ = True # Static files (CSS, JavaScript, Images) STATIC_URL = 'static/' STATIC_ROOT = BASE_DIR / 'staticfiles' STATICFILES_DIRS = [BASE_DIR / 'static'] STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' # Default primary key field type DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' # ============================================================================= # REST Framework Configuration # ============================================================================= REST_FRAMEWORK = { 'DEFAULT_RENDERER_CLASSES': [ 'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', ], 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 'PAGE_SIZE': 20, 'DEFAULT_THROTTLE_CLASSES': [ 'rest_framework.throttling.AnonRateThrottle', 'rest_framework.throttling.UserRateThrottle' ], 'DEFAULT_THROTTLE_RATES': { 'anon': '100/hour', 'user': '1000/hour', 'scan': '10/hour', # Specific rate for scan creation }, 'EXCEPTION_HANDLER': 'api.exceptions.custom_exception_handler', } # ============================================================================= # CORS Configuration # ============================================================================= CORS_ALLOWED_ORIGINS = os.getenv( 'CORS_ALLOWED_ORIGINS', 'http://localhost:3000,http://localhost:8000' ).split(',') CORS_ALLOW_CREDENTIALS = True # ============================================================================= # Celery Configuration # ============================================================================= CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL', 'redis://localhost:6379/0') CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND', 'redis://localhost:6379/1') CELERY_ACCEPT_CONTENT = ['json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TIMEZONE = TIME_ZONE CELERY_TASK_TRACK_STARTED = True CELERY_TASK_TIME_LIMIT = int(os.getenv('MAX_SCAN_TIME_SECONDS', '300')) CELERY_TASK_SOFT_TIME_LIMIT = CELERY_TASK_TIME_LIMIT - 30 # ============================================================================= # Cache Configuration # ============================================================================= REDIS_URL = os.getenv('REDIS_URL', '') if REDIS_URL: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.redis.RedisCache', 'LOCATION': REDIS_URL, } } else: # Use local memory cache for development without Redis CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'unique-snowflake', } } # ============================================================================= # Scanner Configuration # ============================================================================= SCANNER_CONFIG = { # OWASP ZAP settings 'ZAP_API_KEY': os.getenv('ZAP_API_KEY', ''), 'ZAP_HOST': os.getenv('ZAP_HOST', 'http://localhost:8080'), 'ZAP_TIMEOUT': 120, # Lighthouse settings 'LIGHTHOUSE_CHROME_FLAGS': os.getenv( 'LIGHTHOUSE_CHROME_FLAGS', '--headless --no-sandbox --disable-gpu' ), 'LIGHTHOUSE_TIMEOUT': 60, # Playwright settings 'PLAYWRIGHT_TIMEOUT': 30000, # milliseconds 'PLAYWRIGHT_VIEWPORT': {'width': 1920, 'height': 1080}, # General scan settings 'MAX_SCAN_TIME_SECONDS': int(os.getenv('MAX_SCAN_TIME_SECONDS', '300')), 'SCAN_RATE_LIMIT_MINUTES': int(os.getenv('SCAN_RATE_LIMIT_MINUTES', '5')), 'MAX_CONCURRENT_SCANS': int(os.getenv('MAX_CONCURRENT_SCANS', '3')), # Safety settings - blocked IP ranges (RFC1918 private ranges + localhost) 'BLOCKED_IP_RANGES': [ '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', '127.0.0.0/8', '169.254.0.0/16', # Link-local '::1/128', # IPv6 localhost 'fc00::/7', # IPv6 private 'fe80::/10', # IPv6 link-local ], 'BLOCKED_HOSTS': ['localhost', 'localhost.localdomain'], # Large file thresholds 'LARGE_IMAGE_THRESHOLD_BYTES': 1024 * 1024, # 1 MB 'LARGE_JS_BUNDLE_THRESHOLD_BYTES': 500 * 1024, # 500 KB } # ============================================================================= # Logging Configuration # ============================================================================= LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'verbose': { 'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}', 'style': '{', }, 'simple': { 'format': '{levelname} {asctime} {module} {message}', 'style': '{', }, }, 'handlers': { 'console': { 'class': 'logging.StreamHandler', 'formatter': 'simple', }, 'file': { 'class': 'logging.FileHandler', 'filename': BASE_DIR / 'logs' / 'django.log', 'formatter': 'verbose', }, }, 'root': { 'handlers': ['console'], 'level': 'INFO', }, 'loggers': { 'django': { 'handlers': ['console'], 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'), 'propagate': False, }, 'scanner': { 'handlers': ['console'], 'level': 'DEBUG' if DEBUG else 'INFO', 'propagate': False, }, 'celery': { 'handlers': ['console'], 'level': 'INFO', 'propagate': False, }, }, } # Create logs directory if it doesn't exist (BASE_DIR / 'logs').mkdir(exist_ok=True)