WEB CYBERSECURITY

Content Security Policy: how it protects a website

A CSP tells the browser which resources it may load. When properly configured, it greatly reduces the impact of many content injection attacks.

Content Security Policy, or CSP, is a security policy sent in HTTP response headers. It complements server-side protections by giving the browser a precise list of allowed scripts, styles, images, fonts and connections.

What is a CSP for?

When a page contains injected content, a browser may execute a script or load a resource controlled by a third party. A restrictive CSP reduces this possibility: anything that has not been explicitly allowed is blocked.

It is particularly useful against some consequences of an XSS vulnerability. It can prevent unauthorized scripts from running, restrict the destinations a page can contact and prevent a malicious site from embedding the page in an iframe.

A CSP does not remove the original vulnerability. Input validation, output escaping, updates and other application controls remain essential.

Essential directives

DirectivePurposeKey consideration
default-srcDefines the fallback ruleStart with a restrictive value
script-srcControls scriptsAvoid broad sources and inline scripts
style-srcControls stylesheetsGradually move inline styles to files
img-srcControls imagesAllow only necessary schemes and domains
connect-srcRestricts browser network callsList required APIs, telemetry and WebSockets
frame-ancestorsControls who may embed the pageUse 'none' when no iframe is needed
object-srcControls legacy embedded content'none' suits most modern websites
base-uriRestricts the base elementReduces relative URL redirection

What does a restrictive policy look like?

Content-Security-Policy: default-src 'self';
  script-src 'self';
  style-src 'self';
  img-src 'self';
  connect-src 'self';
  object-src 'none';
  base-uri 'self';
  frame-ancestors 'none'

Do not copy this example blindly. A website using an external service, remote font or API must precisely declare the corresponding source. The goal is not to accumulate allowed domains, but to start from actual needs.

Configurations that create a false sense of security

The presence of the header is not enough. A policy can be technically valid and still remain overly permissive.

  • * allows a source that is far too broad;
  • 'unsafe-inline' allows content embedded directly in the page;
  • 'unsafe-eval' enables risky forms of dynamic evaluation;
  • generic schemes such as data: or blob: broaden accepted content;
  • a long list of third-party domains increases the trust surface;
  • a missing directive may leave the fallback rule to decide.

Risk depends on context. Allowing data: for images does not have the same impact as allowing it for scripts. Each directive must therefore be assessed according to the type of resource involved.

Strengthen CSP without breaking the website

  1. Inventory resources actually loaded. List necessary scripts, styles, images, fonts, iframes and network calls.
  2. Define a restrictive policy. Start with default-src 'self', then open only justified directives.
  3. Test in report-only mode. The Content-Security-Policy-Report-Only header reports violations without blocking resources.
  4. Fix dependencies. Move inline scripts and styles to files, remove unused sources and prefer local assets where appropriate.
  5. Enable blocking gradually. Test essential desktop and mobile journeys before general rollout.
  6. Monitor regressions. A new integration may require a precise policy adjustment.

How to check a website's CSP

In browser developer tools, open the Network tab, reload the page and inspect the HTML response headers. The console also reports blocked resources and the directive involved.

Then review the effective policy: number of directives, overly broad sources, risky values and protections such as frame-ancestors, object-src and base-uri. Automated analysis makes this easier, but the result must still be interpreted according to the site's actual behavior.

Check your domain's security headers

The TechAtelier audit analyzes CSP and other HTTP signals to highlight missing or overly permissive directives.

Audit a domain

Useful protection when it remains precise

A good CSP follows a simple principle: allow only what the page needs. It must be tested, understood and maintained like the rest of the security configuration.

The more precise the policy, the fewer possibilities unexpected content has in the browser. The best result combines a properly secured application, a restrictive CSP and regular monitoring of changes.

Published on 1 September 2026.