---
title: "CSP Generator: Build a Content-Security-Policy | VulCode"
description: "Content-Security-Policy (CSP) limits where a page loads scripts and styles from. Build one from a preset, hash an inline script and check the risks."
url: "https://vulcode.com/en/tools/csp-generator"
lang: "en"
---

[Home](https://vulcode.com/en) [Resources](https://vulcode.com/en/resources) [Tools](https://vulcode.com/en/tools)

# CSP generator with a risk check

Content-Security-Policy (CSP) limits where a page loads scripts and styles from. Build one from a preset, hash an inline script and check the risks.

[Free quote](https://vulcode.com/en/quote) [Full pricing](https://vulcode.com/en/services/pricing)

Starting point

Preset

Strict with nonce Domain allowlist Empty

Mode

Block Report only

Or paste your current policy

Load into the fields

Scripts and styles

Default sources (default-src)

Scripts (script-src)

Styles (style-src)

Nonce in script-src 'nonce-{RANDOM}' 'strict-dynamic' in script-src scripts with a nonce may load others

Other resources

Images (img-src)

Fonts (font-src)

Fetch and WebSocket (connect-src)

Audio and video (media-src)

Frames on the page (frame-src)

Web Workers (worker-src)

App manifest (manifest-src)

Object and embed plugins (object-src)

Document and navigation

The base tag (base-uri)

Form targets (form-action)

Who may embed the page (frame-ancestors)

Upgrade http to https upgrade-insecure-requests

Hash of an inline script or style

For

script-src style-src

The tag content, without the tag

- SHA-256 hash every character counts, spaces and line breaks too
  Copy

Add to script-src

Violation reports

URL the reports go to

Group name for report-to

report-uri the older way, every browser reads it report-to the newer one, needs a Reporting-Endpoints header

Fill in at least one field or pick a preset.

## Policy

11 directives, 248 characters

```
default-src 'self';
script-src 'nonce-{RANDOM}' 'strict-dynamic';
style-src 'self';
img-src 'self' data:;
font-src 'self';
connect-src 'self';
object-src 'none';
base-uri 'none';
form-action 'self';
frame-ancestors 'none';
upgrade-insecure-requests;
```

Copy the policy

## What to check

## Ready to paste

- HTTP header
  Copy

- Reporting-Endpoints header
  Copy

- nginx
  Copy

- Apache (.htaccess)
  Copy

- Meta tag in the head
  Copy

## A sample nonce from your browser

the server draws a new one for every response

- nonce
  Copy

- This is what a tag with a nonce looks like
  Copy

## How to use it in five steps

Step by step, with this page's example.

- Pick a preset. Strict with nonce fits when the server can insert a random nonce, and Domain allowlist suits static sites. Already have a policy? Paste it and load it into the fields.

- Fill in the sources, meaning the domains of your scripts, fonts, APIs and frames, separated by spaces. Write keywords such as 'self' in quotes.

- Have a single inline script? Paste its content under Hash and add the result to script-src with one click.

- Go through What to check. Risks come first in bold, plain notes follow.

- Start in Report only with a report URL. Once a few days pass with no violations, switch to Block and paste the header into the server config.

What you enter

The Domain allowlist preset, with https://fonts.googleapis.com added to style-src and https://fonts.gstatic.com to font-src

What you get

```
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self' https://fonts.googleapis.com; img-src 'self' data: https:; font-src 'self' https://fonts.gstatic.com; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'; upgrade-insecure-requests
```

Without 'unsafe-inline' in style-src, style= attributes in the HTML stop working, so check the site in Report only first.

## Questions

- **What is CSP?**: Content-Security-Policy (CSP) is an HTTP header that tells the browser which sources of scripts, styles and images a page may load, limiting the damage of XSS. The generator builds one from a preset and checks it for risks.
- **Header or meta tag?**: The header, if you have access to the server. A meta tag does not support frame-ancestors, report-uri, sandbox or report-only mode, and it only protects what comes after it in the HTML. The generator shows what the meta tag leaves out.
- **How do I set CSP in nginx or Apache?**: The generator gives a ready line for nginx and for Apache. Paste it into the server config and reload the service. Without server access use the meta tag, keeping its limits in mind.
- **How do I check that CSP works correctly?**: Turn on Report only with a report URL and watch the browser console: it names the directive and the URL for every block. Also go through the What to check list under the result.
- **What is a nonce and where does it come from?**: A random value the server creates for every request and writes into the header and into the nonce attribute of the script tags. The browser only runs scripts with a matching nonce. It should carry at least 128 bits of randomness, for example 16 bytes in Base64, like the sample in the tool.
- **When should I use a hash instead of a nonce?**: When the site is static, the server cannot insert a nonce and the inline script does not change. The hash is computed from the exact tag content, so any change, even one space, needs a new hash.
- **What does 'strict-dynamic' do?**: It lets scripts with a nonce or a hash load further scripts, a tag manager for example. Browsers that know it then ignore the domain list and 'self' in script-src, so you do not have to keep a list of every server your scripts come from.
- **Why did something break after I turned CSP on?**: Usually it is style= attributes, onclick handlers or resources from a domain outside the list. The browser console names the directive and the URL for every block. That is why you start in Report only and switch blocking on once the reports go quiet.
- **Does CSP replace other protections?**: No. It is a second line of defence that limits the damage of XSS once a bug is already there. User input still has to be escaped, and headers such as HSTS or X-Content-Type-Options protect against other things.

## Related tools

The generator builds the policy for one site. When CSP has to cover a whole application along with the rest of its security, we check that in a security audit.

- ### [Meta tag generator with a Google preview](https://vulcode.com/en/tools/meta-tag-generator)
  A title and description with a Google result preview and their width in pixels, plus Open Graph, X, canonical and robots tags.

- ### [JWT decoder with expiry dates](https://vulcode.com/en/tools/jwt-decoder)
  The header and payload as JSON, exp, iat and nbf as dates with the time left, plus warnings. The signature is not verified.

- ### [Base64, JWT and hash](https://vulcode.com/en/tools/base64-jwt-and-hash)
  Encode and decode Base64 and get MD5, SHA-256 or SRI from text or a file. A bare JWT and URLs have their own tools.

- ### [URL encoder and decoder online](https://vulcode.com/en/tools/url-encoder-decoder)
  encodeURIComponent, encodeURI and form encoding side by side, query parameters in a table and IDN domains in punycode.

- ### [Security audit](https://vulcode.com/en/services/security-audit)
  When more than the headers needs checking: the whole application, its login, permissions and API against the OWASP Top 10.

- ### [IT administration](https://vulcode.com/en/services/it-administration)
  When the headers should be set and looked after by someone who runs the server day to day.

## More on this in our blog

- [Business website security: a checklist and a plan for when you get hacked A small business website is usually attacked by a bot that checks for known vulnerabilities and leaked passwords, not by someone interested in the business. What to secure, how to check for yourself that it is secured, and what to do once the site has already been taken over.](https://vulcode.com/en/blog/business-website-security)

## Tell us what you want to build.

A free quote comes back within an hour during working hours. You talk to the person who writes the code.

[Free quote](https://vulcode.com/en/quote) [Free consultation](https://vulcode.com/en/consultation)

- [hello@vulcode.com](mailto:hello@vulcode.com)

- [+48 576 043 920](tel:+48576043920)

- [discord.gg/vulcode](https://discord.gg/vulcode)
