Web Security / HTTP Headers

Content Security Policy Generator

Build a Content Security Policy for your website. Configure directives, control resource sources, and generate ready-to-use security header examples.

Configure Your CSP Policy

Important: A restrictive CSP can break scripts, styles, images, fonts, APIs, payment integrations, and other website features. Start with Report-Only mode and test your actual website before enforcing a policy.
Presets are starting points, not guaranteed configurations for every website.
Report-Only monitors violations without blocking resources. It must be delivered as an HTTP header.

Resource directives

Enable the directives you want to include. Enter source expressions separated by spaces. Leave the source field empty only when the directive does not require a value.

Fallback for fetch directives that are not otherwise specified.
Controls JavaScript files and inline script execution.
Controls stylesheets and inline CSS.
Controls image sources.
Controls font resources.
Controls fetch, XMLHttpRequest, WebSocket, EventSource, and similar connections.
Controls audio and video resources.
Controls plugins such as embedded objects. 'none' is generally recommended.
Controls sources that can be loaded into nested browsing contexts.
Controls which sites can embed your pages in frames.
Restricts the URLs that can be used in a base element.
Restricts the destinations to which forms can submit.
Controls web application manifest resources.
Controls workers, shared workers, and service workers.

Additional security directives

Requests that insecure resource URLs be upgraded to HTTPS. Use only when your site is ready for HTTPS.
Legacy directive that blocks mixed content. Modern browsers generally upgrade or block mixed content automatically.
Applies sandbox restrictions to the delivered document. An empty value applies the most restrictive sandbox.

Generated Content Security Policy

1. CSP policy value
2. HTTP response header
3. PHP example
4. Apache .htaccess example
5. HTML meta tag example
The meta tag is not equivalent to an HTTP response header. In particular, frame-ancestors and report-only policies cannot be implemented using a CSP meta tag. Prefer an HTTP header.
Before deployment: Test the policy on your actual website. Replace any example source with the real resources your site needs. A generated policy does not automatically configure your web server.

What Is Content Security Policy?

Content Security Policy (CSP) is a browser security mechanism that helps website owners control which resources a browser is allowed to load and execute. It is primarily used as an additional layer of defense against cross-site scripting (XSS), data injection, and certain unwanted resource-loading behaviors.

A CSP is normally delivered through the HTTP Content-Security-Policy response header. It can also be delivered using an HTML meta element, although meta-based CSP has important limitations.

Example policy

default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self';

This example restricts many resource types to the website's own origin, limits scripts to the same origin, disables plugin objects, and restricts the base element. It is only an illustration, not a universal policy.

Important CSP Directives

What Do CSP Source Expressions Mean?

Common source expressions include:

Be careful with broad sources and keywords such as 'unsafe-inline' and 'unsafe-eval'. They can substantially weaken the protections provided by CSP.

Report-Only Mode vs Enforced Mode

Report-Only mode

A report-only policy allows you to observe CSP violations without blocking the resources that caused them. It is useful when introducing a new policy or testing changes to an existing configuration.

Enforced mode

An enforced CSP actively blocks resources that violate the configured policy. A policy that is too restrictive may prevent legitimate website features from working.

A practical rollout is to test a policy using the Content-Security-Policy-Report-Only header, review violations, adjust the policy, and then move to enforcement after testing.

Strict CSP: Nonces and Hashes

A strict CSP generally uses a cryptographic nonce or hash to authorize scripts and styles, instead of relying only on broad domain allowlists.

Nonce-based CSP

A nonce is a random value generated by the server for each response. The same value is included in the CSP header and the corresponding authorized script or style elements. The value must be unpredictable and newly generated for every response.

script-src 'nonce-RANDOM_VALUE'; object-src 'none'; base-uri 'none';

The placeholder above is not a working nonce. Your application must generate a secure value for each response and insert that value into both the header and the authorized elements.

Hash-based CSP

A hash-based policy authorizes specific inline script or style content using a SHA-256, SHA-384, or SHA-512 hash. If the content changes, its hash must be recalculated.

These approaches require more careful implementation than simply allowing a list of third-party domains.

Related Security Tools

Explore more security utilities from Permitir.net: