Skip to main content
Bethemesh
GuideBest practices

Content Security Policy (CSP): reduce XSS risks and unwanted loads

Learn how to introduce a CSP progressively, understand its main directives, Report-Only mode, nonces and common mistakes.

Published 29 August 2026Reading : 1 minBy Bethemesh Team
Beginner
Show contents
  1. Begin with observation
  2. Understand the main directives
  3. Inline code, nonces and hashes
  4. CSP complements application security
  5. Roll out progressively

Content Security Policy tells the browser which origins may provide scripts, styles, images, fonts, frames and network connections. Its main value is to reduce what injected markup can execute or load.

Begin with observation

A strict policy deployed in one step can break a real site. Inventory first-party and third-party resources, then use Content-Security-Policy-Report-Only to observe violations without blocking them. The CSP generator helps build a starting policy.

Understand the main directives

default-src provides a fallback. script-src and style-src deserve special attention because executable code and inline content are frequent sources of risk. img-src, font-src, connect-src, frame-src and object-src narrow other capabilities. Prefer explicit origins over broad wildcards.

Inline code, nonces and hashes

Allowing 'unsafe-inline' weakens an important CSP benefit. When inline code is unavoidable, nonces or CSP hashes can authorize specific blocks instead of all inline execution. A nonce must be unpredictable and generated for each response.

CSP complements application security

CSP does not sanitize untrusted HTML and does not repair broken authorization. Treat it as defense in depth. Combine it with secure coding, HTTPS and the security headers.

Roll out progressively

Start with reporting, remove unnecessary exceptions, test critical journeys, then enforce the policy. Revisit it when analytics, advertising, fonts or other third-party services change. Document every exceptional origin so the policy does not slowly become an unrestricted allow-list.

Related tools

Security & privacy

CSP generator

Create a Content Security Policy to strengthen your website security.

100% local
Use this tool
Security & privacy

Security headers generator

Generate HTTP security headers for the protections you want to enable on your site.

100% local
Use this tool

Collection

Secure a website

  1. 01Web security: understand the essential protections of a website
  2. 02Content Security Policy (CSP): reduce XSS risks and unwanted loads
  3. 03HTTP security headers: which ones to enable and why
  4. 04Subresource Integrity (SRI): verify external resource integrity
  5. 05JWT: understand structure, signature, expiration and common mistakes
  6. 06Passwords: generation, strength and useful policies
  7. 07Hashes, integrity and fingerprint comparison: what a hash can really prove

Was this article useful?