You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Check before using eval-dependent featuresif(CspUtils.allowsEval()){// Use eval-based approach (e.g., template engines)consttemplate=compileWithEval(templateString);}else{// Use pre-compiled or string-based approachconsttemplate=precompiledTemplate;}// Check before inline stylesif(CspUtils.allowsInlineStyle()){element.style.cssText=dynamicStyles;}else{element.className=preDefinedClass;}
Generate Nonces
// Server generates nonce and adds to CSP headerconstnonce=CspUtils.generateNonce();// Content-Security-Policy: script-src 'nonce-${nonce}'// Use nonce in script tagsconstscript=document.createElement('script');script.nonce=nonce;script.src='/dynamic-script.js';document.head.appendChild(script);
Calculate Script Hash
// Calculate hash for CSP hash-sourceconstscriptContent='console.log("Hello");';consthash=awaitCspUtils.calculateHash(scriptContent);// hash = 'sha256-...'// Add to CSP: script-src 'sha256-...'
constselfOrigin=window.location.origin;constdirective="'self' https://cdn.example.com";// Check if script URL is allowedconstallowed=CspUtils.isUrlAllowedByDirective('https://cdn.example.com/script.js',selfOrigin,directive);if(allowed){loadScript('https://cdn.example.com/script.js');}
Cache Management
// Clear cache when CSP may have changed// (e.g., after page navigation in SPA)CspUtils.clearCache();// Re-check capabilitiesconstcanEval=CspUtils.allowsEval();
Conditional Loading
asyncfunctionloadTemplate(name: string): Promise<Template>{if(CspUtils.allowsEval()){// Load and compile at runtimeconstsource=awaitfetch(`/templates/${name}.hbs`).then((r)=>r.text());returnHandlebars.compile(source);}else{// Load pre-compiledconstmodule=awaitimport(`/templates/${name}.js`);returnmodule.default;}}
Directive Values
Common Keywords
Keyword
Description
'none'
Block all
'self'
Same origin only
'unsafe-inline'
Allow inline scripts/styles
'unsafe-eval'
Allow eval/Function constructor
'strict-dynamic'
Trust scripts loaded by trusted scripts
Source Types
Source
Example
Description
Host
cdn.example.com
Specific host
Wildcard host
*.example.com
Any subdomain
Scheme
https:
Any https URL
Nonce
'nonce-abc123'
Specific nonce value
Hash
'sha256-...'
Specific content hash
Server-Side Integration
CSP utilities complement server-side CSP headers:
// Server (PHP/Node/etc.) generates CSP header// Content-Security-Policy: script-src 'self' 'nonce-xxx'// Client detects and adaptsif(!CspUtils.allowsInlineScript()){// Use external scripts or nonce}
Security Considerations
Detection is Heuristic - Browser makes final CSP decisions
Cryptographic Nonces - Uses crypto.getRandomValues() for nonce
generation
Cache Invalidation - Call clearCache() if CSP may change during session
Violation Reporting - Use onViolation() to monitor for issues
Hash Algorithm - Uses SHA-256, recommended by CSP specification