Back to results for “Angular Security (XSS”
Angular › Angular Security (XSS, Sanitization & CSP)

What is a Content Security Policy, often shortened to CSP, and how does it add another layer of protection to an Angular app?

Intermediate

Answer

A Content Security Policy is a set of rules, usually sent as an HTTP header from the server, that tells the browser exactly which sources of scripts, styles, and other content are allowed to load on a page. Even if an attacker somehow manages to inject a malicious script, a properly configured CSP can prevent that script from actually running or from communicating with an unapproved server.
Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-cdn.com

// This tells the browser to only allow scripts from the app's own origin
// and one specific trusted content delivery network
Real-world example A financial services company configures a strict Content Security Policy on their Angular app's server, ensuring that even if an unexpected vulnerability were exploited, any injected malicious script would be blocked from executing or sending data anywhere.

Common follow-ups: How would you configure a CSP to work correctly with Angular's own generated inline styles?;What tools can help you test whether a CSP is configured correctly?