Overview

Content Security Policy (CSP) is a powerful security layer that helps detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks.

How it Works

By defining a CSP, you tell the browser which domains are 'trusted' sources of content (scripts, styles, images). The browser will then refuse to execute any scripts that are not from those trusted domains.

Example Directive

script-src 'self' https://trustedscripts.com; - This tells the browser to only run scripts from the same origin as the page or from the specified trusted domain.

Importance

CSP is one of the most effective defenses against XSS, as it can prevent malicious scripts from running even if an attacker successfully injects them into a page.

Related Terms