Overview
Security headers are HTTP response headers that provide defense-in-depth protection against common web vulnerabilities. This guide demonstrates the security headers implementation from the secure application.Why Security Headers Matter
Defense in Depth
Additional protection layer even if other defenses fail.
Browser Security
Leverage browser security features to protect users.
XSS Prevention
Restrict script execution to prevent XSS attacks.
Clickjacking Protection
Prevent your site from being embedded in malicious frames.
Implementation in Flask
Set security headers globally using Flask’safter_request decorator:
secure/app.py
The
@app.after_request decorator ensures these headers are added to every response automatically.Essential Security Headers
X-Content-Type-Options
Prevents MIME-type sniffing attacks by forcing browsers to respect declared content types.What it prevents
Stops browsers from interpreting files as a different MIME type than declared.
Attack scenario
Attacker uploads image.jpg containing JavaScript; browser might execute it without this header.
X-Frame-Options
Protects against clickjacking attacks by controlling whether your site can be embedded in frames.Use
DENY for maximum protection unless you need to embed your site in iframes on your own domain.X-XSS-Protection
Enables browser’s built-in XSS filtering (legacy header, but still useful for older browsers).0: Disables XSS filtering1: Enables XSS filtering1; mode=block: Blocks page rendering if XSS detected
Modern browsers rely on Content-Security-Policy instead, but this header provides fallback protection.
Strict-Transport-Security (HSTS)
Forces browsers to use HTTPS connections only, preventing downgrade attacks.max-age
Time (seconds) browser should remember to only use HTTPS:(1 year)
includeSubDomains
Apply HSTS to all subdomains:
preload
Opt-in to HSTS preload list:
Content-Security-Policy (CSP)
The most powerful security header - controls which resources can be loaded and executed.Content Security Policy Deep Dive
CSP Directives
default-src
Fallback for all resource types:
script-src
Control JavaScript sources:
style-src
Control CSS sources:
img-src
Control image sources:
connect-src
Control AJAX/WebSocket sources:
frame-ancestors
Control who can frame your site:
Basic CSP Example
Strict CSP Configuration
For maximum security (blocks all inline scripts and styles):This strict policy requires moving all inline JavaScript and CSS to external files.
CSP with CDNs
If using external resources like Bootstrap or jQuery:Additional Security Headers
Referrer-Policy
Controls how much referrer information is sent with requests:no-referrer: Never send referrersame-origin: Send only for same-origin requestsstrict-origin-when-cross-origin: Send origin only for cross-origin
Permissions-Policy
Controls which browser features can be used:This header replaced the older Feature-Policy header.
Complete Security Headers Implementation
secure/app.py
Testing Security Headers
Verify headers are set correctly using curl:CSP Reporting
Monitor CSP violations by adding report-uri:CSP reporting helps identify violations without blocking resources during development.
Security Headers Best Practices
Common Pitfalls
'unsafe-inline' in CSP
Avoid
'unsafe-inline' for script-src as it defeats XSS protection.Missing HSTS on subdomains
Use
includeSubDomains to protect all subdomains.Too permissive CSP
Don’t use
* or https: unless absolutely necessary.Not testing headers
Always verify headers are actually being set in responses.
Framework-Specific Helpers
For easier header management, consider using Flask-Talisman:Next Steps
Output Encoding
Learn about XSS prevention through output encoding
Secure Configuration
Configure your application securely