JavaScriptMedium
Explain the concept of Content Security Policy (CSP) and how it enhances security
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
Content Security Policy (CSP) is a security feature that helps prevent various types of attacks, such as Cross-Site Scripting (XSS) and data injection attacks, by specifying which content sources are trusted. It works by allowing developers to define a whitelist of trusted sources for content like scripts, styles, and images. This is done through HTTP headers or meta tags. For example, you can use the Content-Security-Policy header to specify that only scripts from your own domain should be executed:
http
Content-Security-Policy: script-src 'self'
