CSP Browser Support and Compatibility
CSP Browser Support and Compatibility
Understanding browser support is crucial for successful CSP implementation. While modern browsers have excellent CSP support, variations exist in feature implementation and older browser handling.
Current browser support for CSP Level 2 and Level 3 features:
// Feature detection for CSP support
function detectCSPSupport() {
const testPolicy = "default-src 'none'";
// Check for CSP support via meta tag
const meta = document.createElement('meta');
meta.httpEquiv = 'Content-Security-Policy';
meta.content = testPolicy;
// Test if the browser respects CSP
try {
document.head.appendChild(meta);
// Attempt to execute inline script (should fail with CSP)
const result = eval('true');
// If we reach here, CSP is not working
document.head.removeChild(meta);
return false;
} catch (e) {
// CSP blocked the eval, support confirmed
document.head.removeChild(meta);
return true;
}
}
Browser compatibility considerations:
- Chrome/Edge: Full support for CSP3 features including 'strict-dynamic'
- Firefox: Comprehensive CSP support with some experimental features
- Safari: Good CSP2 support, gradual adoption of CSP3 features
- Internet Explorer: Limited to CSP1 with the
X-Content-Security-Policy
header