# 🔒 SĂ©curitĂ© - Blog Duhaz ## ✅ AmĂ©liorations de SĂ©curitĂ© ImplĂ©mentĂ©es ### 1. Variables d'Environnement - **SECRET_KEY** : Maintenant stockĂ©e dans `.env` (non commitĂ©e sur Git) - **DEBUG** : Configurable par environnement - **ALLOWED_HOSTS** : Liste blanche des domaines autorisĂ©s ### 2. Protection Git - `.env` ajoutĂ© au `.gitignore` - `.env.example` créé comme modĂšle (sans secrets) - Ancienne SECRET_KEY compromise → nouvelle clĂ© gĂ©nĂ©rĂ©e ### 3. Configuration Production ParamĂštres de sĂ©curitĂ© prĂȘts dans `settings.py` (Ă  dĂ©commenter) : - `SECURE_SSL_REDIRECT` : Force HTTPS - `SESSION_COOKIE_SECURE` : Cookies sĂ©curisĂ©s - `CSRF_COOKIE_SECURE` : Protection CSRF renforcĂ©e - `SECURE_BROWSER_XSS_FILTER` : Protection XSS - `X_FRAME_OPTIONS` : Protection clickjacking --- ## ⚠ VulnĂ©rabilitĂ©s Restantes Ă  Corriger ### 1. XSS (Cross-Site Scripting) **Fichier** : `blog/views.py` et templates **ProblĂšme** : Le contenu HTML des articles est injectĂ© sans validation. ```python # Dans blog/views.py, ligne 99 page.p_contenu = art.b_description # Potentiellement dangereux ``` **Solution** : - Utiliser `{{ content|safe }}` uniquement pour le contenu de confiance - Valider/nettoyer le HTML cĂŽtĂ© serveur avec `bleach` ou `html5lib` - Ou utiliser `escape()` pour afficher du texte brut **Installation** : ```bash pip install bleach ``` **Utilisation** : ```python import bleach ALLOWED_TAGS = ['p', 'strong', 'em', 'u', 'h1', 'h2', 'h3', 'ul', 'ol', 'li', 'a', 'img'] ALLOWED_ATTRS = {'a': ['href', 'title'], 'img': ['src', 'alt']} cleaned_content = bleach.clean( art.b_contenu, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRS, strip=True ) ``` ### 2. Injection SQL **État** : ✅ ProtĂ©gĂ© par l'ORM Django Django utilise des requĂȘtes paramĂ©trĂ©es qui prĂ©viennent l'injection SQL. ### 3. CSRF (Cross-Site Request Forgery) **État** : ✅ ProtĂ©gĂ© Le middleware CSRF est activĂ©. Assurez-vous d'utiliser `{% csrf_token %}` dans tous les formulaires. ### 4. Gestion des Exceptions **Fichier** : `core/views.py`, `blog/views.py` **ProblĂšme** : ```python except: # Attrape TOUT, mĂȘme les erreurs critiques pass ``` **Solution** : Capturer des exceptions spĂ©cifiques ```python except ObjectDoesNotExist: # Gestion spĂ©cifique pass except Exception as e: logger.error(f"Erreur: {e}") # Retourner une erreur appropriĂ©e ``` ### 5. Upload de Fichiers **Fichier** : `core/models.py` (Fichier model) **Recommandations** : - Valider les types MIME - Limiter la taille des fichiers - Scanner les uploads (antivirus) - Stocker hors du MEDIA_ROOT si possible **Configuration** : ```python # Dans settings.py FILE_UPLOAD_MAX_MEMORY_SIZE = 5242880 # 5 MB DATA_UPLOAD_MAX_MEMORY_SIZE = 5242880 # 5 MB ALLOWED_UPLOAD_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.pdf'] ``` --- ## 🔐 Checklist SĂ©curitĂ© Production ### Avant dĂ©ploiement - [ ] DEBUG = False - [ ] SECRET_KEY unique et forte - [ ] ALLOWED_HOSTS configurĂ© correctement - [ ] HTTPS activĂ© (Let's Encrypt gratuit) - [ ] ParamĂštres de sĂ©curitĂ© dĂ©commentĂ©s - [ ] Base de donnĂ©es PostgreSQL (pas SQLite) - [ ] Sauvegardes automatiques configurĂ©es ### Monitoring - [ ] Logs d'erreurs configurĂ©s - [ ] Alertes de sĂ©curitĂ© activĂ©es - [ ] Surveillance des tentatives d'accĂšs - [ ] Mises Ă  jour rĂ©guliĂšres de Django ### Headers de sĂ©curitĂ© ```python # À ajouter dans settings.py pour production SECURE_HSTS_SECONDS = 31536000 # 1 an SECURE_HSTS_INCLUDE_SUBDOMAINS = True SECURE_HSTS_PRELOAD = True SECURE_REFERRER_POLICY = 'same-origin' ``` --- ## 📚 Ressources - [Django Security Guide](https://docs.djangoproject.com/en/5.1/topics/security/) - [OWASP Top 10](https://owasp.org/www-project-top-ten/) - [Mozilla Web Security](https://infosec.mozilla.org/guidelines/web_security)