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.

Starting point
Preset
Mode
Scripts and styles
Other resources
Document and navigation
Hash of an inline script or style
For
Violation reports

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;

What to check

    Ready to paste

    • HTTP header
    • nginx
    • Apache (.htaccess)
    • Meta tag in the head

    A sample nonce from your browser

    the server draws a new one for every response
    • nonce
    • This is what a tag with a nonce looks like

    How to use it in five steps

    Step by step, with this page's example.

    1. 01

      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.

    2. 02

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

    3. 03

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

    4. 04

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

    5. 05

      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.

    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.