← All posts
CSP header scannerNext.js securityContent Security PolicyAI-built appssecurity headers

Free CSP Header Scanner for Next.js App: Find & Fix Issues

O

OverMCP Team

Quick answer

A free CSP header scanner for Next.js app checks your deployed site's HTTP response headers to see if a Content-Security-Policy is present and correctly configured. Tools like OverMCP's security headers checker can scan your Next.js app in seconds, flag missing CSP, and highlight common misconfigurations like unsafe-inline or missing directives. For AI-built apps, where security is often skipped, running a free CSP header scanner before launch is a quick win to protect against XSS and data injection.

Why CSP matters for Next.js apps (especially AI-built ones)

Content-Security-Policy is a browser security mechanism that restricts which resources (scripts, styles, images) your app can load. A strong CSP blocks malicious scripts injected via XSS attacks, which are a top vulnerability in AI-generated code. Many AI coding tools generate inline scripts or use dangerouslySetInnerHTML, making CSP even more critical. Without a CSP, your Next.js app is exposed to script injection and data theft.

For Next.js apps, especially those built with Cursor, Bolt.new, or v0, CSP is often missing because AI doesn't prioritize security headers. A free CSP header scanner is the easiest way to check if your app has one.

What to check first

Before you fix anything, run a free CSP header scanner on your Next.js app. Here's a checklist to guide you:

  • [ ] Is a Content-Security-Policy header present? Scan your production URL (e.g., https://yourapp.com) with a security headers checker. If the header is missing, that's your first issue.
  • [ ] Does the policy include `default-src 'self'`? This is the baseline directive that restricts all resource loads to your own domain.
  • [ ] Are `script-src` and `style-src` defined? If they're absent, browsers fall back to default-src, which may be too permissive.
  • [ ] Are inline scripts and styles blocked? Look for 'unsafe-inline' in your CSP. For Next.js, you'll often need hashes or nonces, not unsafe-inline.
  • [ ] Is `object-src 'none'` set? This prevents plugins like Flash, reducing attack surface.
  • [ ] Does the policy include `upgrade-insecure-requests`? This forces HTTPS, protecting against man-in-the-middle attacks.
  • [ ] Is CSP in report-only mode? If you see Content-Security-Policy-Report-Only, it's not enforcing—just logging violations.
  • [ ] Are there any syntax errors? A malformed CSP is ignored by browsers, leaving you unprotected.
  • Run these checks with a free CSP header scanner to get a baseline.

    Step-by-step fix

    Once you've scanned your Next.js app and identified missing or weak CSP, follow these steps to add or fix it.

    1. Add CSP via `next.config.js`

    The easiest way to set CSP headers in Next.js is using the headers() function in next.config.js. Here's a basic example:

    // next.config.js
    module.exports = {
      async headers() {
        return [
          {
            source: '/(.*)',
            headers: [
              {
                key: 'Content-Security-Policy',
                value: "default-src 'self'; script-src 'self' 'nonce-abc123'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; object-src 'none'; upgrade-insecure-requests"
              }
            ]
          }
        ]
      }
    }

    Note: The 'nonce-abc123' is a placeholder. For production, you need a dynamic nonce. Next.js supports nonce generation via middleware or server-side rendering.

    2. Use a nonce for inline scripts

    AI-generated code often includes inline scripts. To allow them securely, generate a nonce per request and add it to the CSP and the script tag. Here's a simplified middleware approach:

    // middleware.js
    import { NextResponse } from 'next/server'
    
    export function middleware(req) {
      const nonce = crypto.randomBytes(16).toString('base64')
      const csp = `default-src 'self'; script-src 'self' 'nonce-${nonce}'; style-src 'self' 'unsafe-inline';`
      const res = NextResponse.next()
      res.headers.set('Content-Security-Policy', csp)
      return res
    }

    Then, in your components, use the nonce on script tags:

    <script nonce={nonce} src="/script.js" />

    3. Verify with a scanner

    After deploying, run a free CSP header scanner again to confirm the header is correct. Use OverMCP's security headers checker to see if your CSP passes common checks.

    4. Test in report-only mode first

    To avoid breaking your app, start with Content-Security-Policy-Report-Only and monitor violations. Then switch to enforcing once you're confident.

    Common mistakes

    AI-built Next.js apps often have these CSP pitfalls:

  • Missing CSP entirely: The most common issue. AI doesn't add security headers unless asked.
  • Using `'unsafe-inline'` everywhere: AI-generated code often includes inline styles/scripts, so developers lazily allow unsafe-inline, defeating CSP's purpose.
  • Forgetting to update CSP after adding a new CDN or third-party script: Your app breaks or gets blocked.
  • Not using nonces or hashes: Instead of blocking inline scripts, they allow them all, leaving XSS holes.
  • Only setting CSP in development: Some devs add headers in next.config.js but forget to test in production build.
  • Ignoring report-only violations: They see violations but don't fix them, thinking it's fine.
  • How OverMCP helps

    Running a free CSP header scanner on your Next.js app is the first step. OverMCP's security headers checker not only checks for CSP but also gives you a grade. For continuous monitoring, you can connect your Vercel deployment to get alerts when headers change. This is especially useful for vibe-coded apps that update frequently.

    FAQ

    What is a CSP header scanner?

    A CSP header scanner is a tool that analyzes your website's HTTP response headers to check if a Content-Security-Policy is present and correctly configured. It flags missing or weak policies that could leave your site vulnerable to XSS attacks.

    Can I use a free CSP header scanner for Next.js apps on localhost?

    Yes, but most scanners require a publicly accessible URL. For localhost, you can use browser extensions or run a local proxy. For production, a free online scanner works best.

    How do I fix CSP issues in a Next.js app built with AI?

    Start by scanning with a free tool to see what's missing. Then add CSP via next.config.js or middleware, using nonces for inline scripts. Test in report-only mode before enforcing, and re-scan to verify.

    ---

    Note: Always test your CSP thoroughly to avoid breaking functionality. Use a free CSP header scanner regularly as part of your [continuous security monitoring](https://www.overmcp.com/monitor) routine.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free