0
0
Djangoframework~10 mins

Content Security Policy in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Content Security Policy
Browser sends request
Django server processes request
Django adds CSP header to response
Browser receives response
Browser checks CSP header
Browser allows or blocks resources based on CSP
Page renders with enforced security
The browser requests a page, Django adds a Content Security Policy header, and the browser enforces rules to allow or block resources.
Execution Sample
Django
from django.middleware.security import SecurityMiddleware

# Add CSP header in middleware
response['Content-Security-Policy'] = "default-src 'self'; script-src 'self' https://trusted.com"
This code adds a Content Security Policy header to responses to restrict resource loading.
Execution Table
StepActionCSP Header ValueBrowser BehaviorResult
1Browser sends request to Django serverN/AN/ARequest received by server
2Django processes request and prepares responseN/AN/AResponse ready to send
3Django adds CSP header to responsedefault-src 'self'; script-src 'self' https://trusted.comN/AResponse includes CSP header
4Browser receives response with CSP headerdefault-src 'self'; script-src 'self' https://trusted.comReads CSP rulesPrepares to enforce rules
5Browser loads page resourcesdefault-src 'self'; script-src 'self' https://trusted.comBlocks scripts from untrusted sourcesOnly trusted scripts run
6Page renders with enforced CSPdefault-src 'self'; script-src 'self' https://trusted.comPage secure from unauthorized scriptsSafe page display
7If script from untrusted source requesteddefault-src 'self'; script-src 'self' https://trusted.comBlocks script loadScript blocked, console error shown
8End of request-response cycleN/AN/ACycle complete
💡 Request-response cycle ends after browser enforces CSP rules on loaded resources
Variable Tracker
VariableStartAfter Step 3After Step 4Final
response['Content-Security-Policy']null"default-src 'self'; script-src 'self' https://trusted.com""default-src 'self'; script-src 'self' https://trusted.com""default-src 'self'; script-src 'self' https://trusted.com"
browser_allowed_scriptsAll sourcesN/AOnly 'self' and https://trusted.comOnly 'self' and https://trusted.com
Key Moments - 3 Insights
Why does the browser block some scripts even though the server sent the page?
Because the CSP header tells the browser to only allow scripts from trusted sources, as shown in execution_table step 5 where scripts from untrusted sources are blocked.
Can the CSP header be changed after the response is sent?
No, the CSP header is part of the HTTP response headers sent by Django before the browser receives the page, as shown in execution_table step 3 and 4.
What happens if the CSP header is missing?
The browser does not enforce any content restrictions, so all scripts and resources load freely, unlike the controlled behavior shown in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What value is assigned to the CSP header?
A"script-src 'none'"
B"img-src *"
C"default-src 'self'; script-src 'self' https://trusted.com"
DNo CSP header assigned
💡 Hint
Check the 'CSP Header Value' column at step 3 in the execution_table.
At which step does the browser start enforcing the CSP rules?
AStep 5
BStep 4
CStep 2
DStep 7
💡 Hint
Look at the 'Browser Behavior' column to see when scripts are blocked.
If the CSP header allowed scripts from any source, how would 'browser_allowed_scripts' change in variable_tracker?
AIt would remain 'Only self and https://trusted.com'
BIt would change to 'All sources'
CIt would be empty
DIt would cause an error
💡 Hint
Refer to the 'browser_allowed_scripts' row in variable_tracker and imagine no restrictions.
Concept Snapshot
Content Security Policy (CSP) is a security feature that tells browsers which sources are allowed to load content like scripts.
In Django, you add CSP headers to HTTP responses.
The browser reads these headers and blocks anything not allowed.
This helps prevent attacks like loading malicious scripts.
Always specify trusted sources clearly in your CSP header.
CSP headers are set before the response is sent to the browser.
Full Transcript
Content Security Policy (CSP) is a way to protect web pages by telling browsers which sources of content are safe. When a browser requests a page from a Django server, the server adds a CSP header to the response. This header lists trusted sources for scripts and other resources. When the browser receives the response, it reads the CSP header and blocks any scripts or resources not on the trusted list. This prevents harmful code from running on the page. The CSP header must be set before the response is sent. If the header is missing, the browser allows all content, which can be unsafe. Using CSP helps keep your web pages secure by controlling what content can load.