Skip to content
⚠ TESTING ENVIRONMENT You are viewing a testing copy of Tolioo. Changes here do not affect the live website.

CSP Header Generator

Build a Content Security Policy visually, review it for common mistakes and copy ready-to-use code. Fast, free and no signup.

  • 100% free
  • No Signup
  • Runs in your browser

Policy directives

Start from a preset
Import an existing policy

Paste a policy or a full header line to load it into the builder and review it.

Your Content Security Policy

Directives0
Policy length0
Warnings0


        

Security review

    Policy at a glance

    DirectiveAllowed sources
    ADVERTISEMENT
    Demo Ad 728 x 90

    How to create a Content Security Policy

    1Pick a preset

    Start from a preset, import your current policy, or build from scratch.

    2Choose sources

    Tick the sources each directive should allow and add your own hosts.

    3Review and copy

    Fix the warnings, then copy the code for your server or framework.

    CSP HEADER GENERATOR GUIDE

    CSP Header Generator – Build a Content Security Policy Online

    Looking for a free CSP header generator? Build your Content Security Policy with a simple visual editor, see a plain-English security review, and copy ready-to-use code for an HTTP header, a meta tag, Apache, Nginx, PHP, Node.js or Laravel. A Content Security Policy (CSP) tells the browser which scripts, styles, images, fonts, frames and connections your page may load, which is one of the best defenses against cross-site scripting (XSS) and data injection. Our CSP generator covers every common directive, includes presets for typical websites, can import and analyze an existing policy, and creates script hashes and test nonces for inline code. There is nothing to install and no account to create. Everything runs in your browser with no third-party service.

    Visual CSP Builder

    Tick the sources you want for default-src, script-src, style-src, img-src and more. Add your own hosts, and the tool fixes missing quotes and rejects invalid sources for you.

    Built-In Security Review

    Get warnings about unsafe-inline, unsafe-eval, wildcard sources, a missing object-src or base-uri, and other common CSP mistakes, with clear advice on how to fix them.

    Ready-to-Use Code

    Copy the finished policy as an HTTP header, a meta tag, or ready code for Apache, Nginx, PHP, Node.js and Laravel. Switch between Enforce and Report-Only with one click.

    How to Create a Content Security Policy with This Tool

    You can build a working CSP in a few minutes. Follow these simple steps.

    1. Choose a starting point: Pick a preset such as Strict or Basic website, or use Import an existing policy to load the one you already use.
    2. Set your directives: For each directive, tick the sources to allow. Use 'self' for your own domain, and add other hosts such as https://cdn.example.com in the extra sources box.
    3. Handle inline code safely: Use the hash and nonce helper to allow a specific inline script or style without using 'unsafe-inline'.
    4. Start in Report-Only mode: Switch the header mode to Report-Only and add a report-uri or report-to endpoint, so you can see what would be blocked without breaking your site.
    5. Review and copy: Fix the warnings in the security review, choose your server or framework tab and copy the code.

    Always test a new policy on a staging site first. A policy that is too strict can block scripts, styles or images that your pages need.

    What Is a Content Security Policy (CSP)?

    A Content Security Policy is a security standard that lets a website tell the browser which sources of content are trusted. The policy is delivered in the Content-Security-Policy HTTP response header, or in a meta tag. When the browser finds a script, image, stylesheet or frame that breaks the policy, it blocks it. This limits the damage if an attacker manages to inject malicious code into your pages, because the injected script is not allowed to run or to send data to an unknown server.

    A CSP does not replace secure coding, but it adds a strong second layer of defense. It is recommended for any site that handles logins, payments or personal data.

    How a CSP Header Works

    A policy is a list of directives separated by semicolons. Each directive has a name followed by a list of allowed sources.

    Content-Security-Policy: default-src 'self'; img-src 'self' data: https://images.example.com; script-src 'self' https://cdn.example.com; object-src 'none'

    In this example, everything may load from your own origin by default, images may also come from data URIs and one image host, scripts may also come from one CDN, and plugins are blocked completely. Directives that you do not list fall back to default-src, where that fallback applies.

    CSP Directives Reference

    DirectiveWhat it controlsExample
    default-srcFallback for most fetch directives that you do not setdefault-src 'self'
    script-srcJavaScript files and inline scriptsscript-src 'self' https://cdn.example.com
    style-srcStylesheets and inline stylesstyle-src 'self' https://fonts.googleapis.com
    img-srcImages and faviconsimg-src 'self' data: https:
    font-srcWeb fontsfont-src 'self' https://fonts.gstatic.com
    connect-srcFetch, XHR, WebSocket and other connectionsconnect-src 'self' https://api.example.com
    media-srcAudio and video filesmedia-src 'self' https://media.example.com
    object-srcPlugins such as object and embedobject-src 'none'
    frame-srcPages you embed in iframesframe-src https://www.youtube.com
    worker-srcWeb workers and service workersworker-src 'self' blob:
    manifest-srcWeb app manifest filesmanifest-src 'self'
    base-uriAllowed values for the base tagbase-uri 'self'
    form-actionWhere forms can be submittedform-action 'self'
    frame-ancestorsWho may embed your page in a frame, which helps stop clickjackingframe-ancestors 'none'
    upgrade-insecure-requestsAsks the browser to load http:// resources over https://upgrade-insecure-requests
    report-uri and report-toWhere violation reports are sentreport-uri /csp-report

    Note that base-uri, form-action and frame-ancestors do not fall back to default-src, so you must set them yourself.

    CSP Source Values Explained

    SourceMeaning
    'self'Your own origin: same scheme, host and port
    'none'Block everything for this directive
    'unsafe-inline'Allow inline scripts or styles. This weakens XSS protection for scripts.
    'unsafe-eval'Allow eval and similar functions that run text as code
    'strict-dynamic'Trust scripts loaded by a script that already has a nonce or hash
    'nonce-abc123'Allow only inline code that has a matching nonce attribute
    'sha256-...'Allow only inline code with this exact hash
    https:Any host over HTTPS. This is broad, so use it with care.
    data: and blob:Data URIs and blob URLs, often needed for images and workers
    https://cdn.example.comOne specific host
    *.example.comAny subdomain of example.com

    Keywords such as self and none must be written with single quotes. Without the quotes, the browser treats them as host names, which is one of the most common CSP mistakes. This generator adds the quotes for you.

    CSP Header Examples

    These examples match the presets in the tool. Adjust them to the hosts your own site uses.

    # Strict: only your own origin
    default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'; upgrade-insecure-requests
    
    # Basic website
    default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'self'
    
    # Embedding YouTube videos
    default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://i.ytimg.com; frame-src https://www.youtube.com https://www.youtube-nocookie.com; object-src 'none'; base-uri 'self'; frame-ancestors 'self'

    The Google Analytics and Fonts preset adds the Google Tag Manager, Analytics and Fonts hosts. The standard Google tag snippet is an inline script, so you will also need a nonce or a hash for it. Check Google's current documentation, because the required hosts can change.

    Nonce vs Hash vs unsafe-inline

    Inline scripts are the main reason a strict CSP breaks pages. You have three options.

    • Nonce: The server creates a new random value for every response, adds it to the policy as 'nonce-VALUE' and puts the same value in the script tag. Only scripts with that nonce can run. A nonce must be unpredictable and different on every page load.
    • Hash: You add a hash of the exact inline code to the policy. It works well for scripts that never change, and the browser runs only the code that matches the hash.
    • unsafe-inline: Allows every inline script, which also lets an attacker's injected script run. Avoid it for script-src.

    The nonce button in this tool creates a test value so you can try the policy. In production, generate a fresh nonce for each response on your server.

    How to Roll Out a CSP Safely

    1. Start with Report-Only: Send the policy in the Content-Security-Policy-Report-Only header. The browser reports violations but blocks nothing.
    2. Collect reports: Add a report-uri or report-to endpoint, or watch the browser console, to see which resources break the policy.
    3. Fix or allow: Remove inline code where possible, add hashes or nonces, and add only the hosts you really need.
    4. Enforce the policy: When the reports are clean, switch to the Content-Security-Policy header.
    5. Keep monitoring: New scripts, widgets and third-party tools can break a policy later, so keep reviewing reports.

    CSP Meta Tag vs HTTP Header

    You can set a policy with a meta tag in the head of your page, which is handy when you cannot change server headers. However, the meta tag has limits. It does not support frame-ancestors, report-uri, report-to or sandbox, it cannot be used in Report-Only mode, and it only affects content that comes after the tag. Whenever possible, send the policy as an HTTP header.

    How to Add a CSP Header in Apache, Nginx, PHP, Node.js and Laravel

    The tool creates the code for each platform. Here is what the common ones look like, using a short sample policy.

    # Apache (.htaccess or virtual host, needs mod_headers)
    Header always set Content-Security-Policy "default-src 'self'; object-src 'none'"
    
    # Nginx (server or location block)
    add_header Content-Security-Policy "default-src 'self'; object-src 'none'" always;
    
    // PHP
    <?php
    header("Content-Security-Policy: default-src 'self'; object-src 'none'");
    
    // Node.js (Express)
    res.setHeader("Content-Security-Policy", "default-src 'self'; object-src 'none'");
    
    // Laravel (in a middleware's handle method)
    $response->headers->set('Content-Security-Policy', "default-src 'self'; object-src 'none'");

    In Nginx, add_header lines are inherited only if the current block has no add_header of its own, so repeat the header where needed, and use the always flag so it is also sent on error pages. After you change server configuration, reload the service and check the response headers in your browser's developer tools.

    Common CSP Mistakes and Troubleshooting

    • Inline scripts and event handlers are blocked: Attributes such as onclick count as inline code. Move them into script files, or use nonces and hashes.
    • Missing quotes: Write 'self' and 'none' with quotes.
    • Google Fonts do not load: Allow https://fonts.googleapis.com in style-src and https://fonts.gstatic.com in font-src.
    • Images from data URIs are blocked: Add data: to img-src.
    • Embedded videos do not show: Add the video host to frame-src.
    • Too broad sources: A wildcard or https: in script-src lets scripts load from almost anywhere. Use exact hosts.
    • Forgetting object-src: Set object-src 'none' unless you really need plugins.
    • Multiple CSP headers: If several policies are sent, the browser applies all of them, so a resource must pass every one.
    • Report-Only blocks nothing: That is expected. Switch to the enforcing header when you are ready.

    Is a CSP Enough to Secure My Website?

    No single header can secure a website. A CSP reduces the impact of XSS, but you still need to escape output, validate input, keep software updated, use HTTPS and set other security headers such as Strict-Transport-Security and X-Content-Type-Options. Treat CSP as one important layer in a defense-in-depth approach.

    Who Uses a CSP Generator?

    Web developers use it to add a policy to new projects. Security engineers use it to review and tighten existing policies. WordPress and e-commerce site owners use it to protect their stores, DevOps engineers use it to prepare server configuration, and students use it to learn how browsers enforce security rules.

    More Free Security and Developer Tools

    Securing your site? Try these related tools: the HMAC Generator, the Hash Generator, the Base64 Encode / Decode tool and the JWT Decoder. You can also browse all of our free online tools.

    Free CSP Header Generator Online

    Our free CSP header generator helps you build, review and export a Content Security Policy without signup or a watermark. Everything runs in your browser, so you can create a policy, test it in Report-Only mode and copy the code whenever you need it.

    ADVERTISEMENT
    Demo Ad 300 x 250
    COMMON QUESTIONS

    Frequently Asked Questions About the CSP Header Generator

    Find answers to common questions about creating a Content Security Policy, report-only mode and CSP headers.

    What is a CSP header generator?

    A CSP header generator is a tool that helps you build a Content Security Policy without writing it by hand. You choose which sources are allowed for scripts, styles, images and other content, and the tool creates the header value and ready-to-use code for your server.

    How do I create a Content Security Policy?

    Pick a preset or start empty, tick the allowed sources for each directive, add any extra hosts, and review the warnings. Then choose your platform, such as Apache, Nginx, PHP, Node.js or Laravel, and copy the code. Start in Report-Only mode and test before you enforce it.

    What is the difference between enforce and report-only mode?

    In enforce mode the browser blocks anything that breaks the policy. In Report-Only mode the browser only reports violations and blocks nothing, which lets you test a new policy safely. Switch to enforce mode when the reports show no important problems.

    Why is unsafe-inline not recommended, and what can I use instead?

    Allowing unsafe-inline in script-src lets every inline script run, including scripts an attacker injects, which removes much of the protection a CSP offers. Use a nonce, which the server generates for each response, or a hash of the exact inline script instead.

    Can I use a CSP meta tag instead of an HTTP header?

    Yes, but with limits. A meta tag does not support frame-ancestors, report-uri, report-to or sandbox, it cannot be used for Report-Only mode, and it only applies to content after the tag. An HTTP header is the better choice whenever you can set one.

    Will a CSP break my website?

    It can if the policy blocks resources your pages need, such as inline scripts, analytics, fonts or embedded videos. Avoid surprises by testing in Report-Only mode, checking the browser console for violations, and allowing only the sources you really use before you enforce the policy.

    Is my policy uploaded to a server?

    No. The policy is built and reviewed inside your browser, so it stays on your device and is not uploaded or stored by this tool.

    ADVERTISEMENT
    Demo Ad 320 x 50

    All Tools, One Place

    Merge, split, compress and convert files in seconds. Explore powerful free tools for all your everyday needs.

    Explore All Tools →