Challenge - 5 Problems
CSP Mastery in Django
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a script violates the CSP in Django?
If a Django app has a Content Security Policy that disallows inline scripts, what will happen if the page tries to run an inline
<script> block?Attempts:
2 left
💡 Hint
Think about how browsers enforce CSP policies on the client side.
✗ Incorrect
Content Security Policy is enforced by the browser. If an inline script is disallowed, the browser blocks it and logs a violation in the developer console. The server does not throw errors for CSP violations.
📝 Syntax
intermediate2:00remaining
Which Django middleware setting correctly enables CSP with default-src 'self'?
Choose the correct way to add Content Security Policy middleware in Django settings to allow resources only from the same origin.
Attempts:
2 left
💡 Hint
Remember the CSP_DEFAULT_SRC value must be a tuple with quotes around 'self'.
✗ Incorrect
The CSP middleware must be added after SecurityMiddleware. The CSP_DEFAULT_SRC must be a tuple with the string "'self'" including quotes, so the browser understands it as the keyword self.
🔧 Debug
advanced2:00remaining
Why does this CSP header cause a browser error?
Given this Django CSP header:
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline', why might the browser still block inline scripts?Attempts:
2 left
💡 Hint
Consider modern browser security policies and how they treat 'unsafe-inline'.
✗ Incorrect
'unsafe-inline' allows inline scripts but modern browsers may still block inline scripts unless a nonce or hash is provided for better security. This is common in strict CSP enforcement modes.
❓ state_output
advanced2:00remaining
What is the effect of this Django CSP setting on image loading?
If Django settings include
CSP_IMG_SRC = ("'self'", 'https://images.example.com'), what images will the browser allow to load?Attempts:
2 left
💡 Hint
Think about what 'self' means in CSP and how multiple sources work.
✗ Incorrect
'self' means the same origin as the page. Adding https://images.example.com allows images from that domain too. Other sources are blocked by CSP.
🧠 Conceptual
expert3:00remaining
Why is using CSP with nonce better than 'unsafe-inline' in Django?
In Django, why is it more secure to use a nonce-based Content Security Policy for scripts instead of allowing 'unsafe-inline'?
Attempts:
2 left
💡 Hint
Consider how CSP nonces help prevent cross-site scripting attacks.
✗ Incorrect
Nonce-based CSP only allows inline scripts that have a server-generated nonce attribute matching the CSP header. This blocks injected scripts without the nonce, improving security over 'unsafe-inline' which allows all inline scripts.