Django - Security Best PracticesHow can you dynamically add a nonce value to your Django CSP header to allow inline scripts safely?ASet CSP header to "script-src 'unsafe-inline'" to allow all inline scriptsBGenerate a random nonce per request and include it in the CSP header and script tagsCDisable CSP for inline scripts by removing script-src directiveDUse a fixed nonce value hardcoded in settings.pyCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand nonce usage in CSPA nonce is a random token generated per request to allow specific inline scripts safely.Step 2: Implement nonce in DjangoGenerate a random nonce in middleware, add it to CSP header as 'nonce-', and add same nonce to inline script tags.Step 3: Avoid unsafe-inline and fixed nonceUsing 'unsafe-inline' disables CSP protection; fixed nonce defeats security purpose.Final Answer:Generate a random nonce per request and include it in the CSP header and script tags -> Option BQuick Check:Dynamic nonce per request = Generate a random nonce per request and include it in the CSP header and script tags [OK]Quick Trick: Use dynamic nonce per request for safe inline scripts [OK]Common Mistakes:MISTAKESUsing 'unsafe-inline' which weakens securityHardcoding nonce valueRemoving script-src directive entirely
Master "Security Best Practices" in Django9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More Django Quizzes Async Django - When async helps and when it does not - Quiz 9hard Celery and Background Tasks - Redis as message broker - Quiz 14medium DRF Advanced Features - Pagination (PageNumber, Cursor, Limit/Offset) - Quiz 14medium Deployment and Production - Nginx as reverse proxy - Quiz 8hard Deployment and Production - WhiteNoise for static files - Quiz 6medium Django REST Framework Basics - APIView for custom endpoints - Quiz 9hard Django REST Framework Basics - DRF installation and setup - Quiz 3easy Django REST Framework Basics - Generic views in DRF - Quiz 6medium Signals - pre_save and post_save signals - Quiz 6medium Testing Django Applications - Testing forms - Quiz 10hard