Skip to content

Content-Security-Policy unsafe-eval in Next.js 9.4+ dev env #14221

Description

@Darep

Bug report

Describe the bug

Content-Security-Policy support is broken requires the use of unsafe-eval in Next.js 9.4+ in dev env/mode. Presumably works without it in production. I think this is due to react-refresh using eval().

To Reproduce

You can use the with-strict-csp example to test this:

  1. npx create-next-app --example with-strict-csp with-strict-csp-app
  2. cd with-strict-csp-app
  3. yarn dev
  4. open http://localhost:3000
  5. Notice that nothing appears. Open browser console and see CSP errors

Expected behavior

Website opens normally and React Fast Refresh works normally in dev env.

Screenshots

Screenshots of CSP errors in Chrome's console:

image

image

Additional context

This issue was fixed back in Next.js 8 🎉 We've been using Next since v5 and ran into this issue with Next.js 7 upgrade, which got fixed in v8 and now it's back again 😄

We are using a small bit of code to patch this in dev env:

  // In dev we allow 'unsafe-eval', so HMR doesn't trigger the CSP
  if (process.env.NODE_ENV !== 'production') {
    policies['script-src'].push("'unsafe-eval'");
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    examplesIssue was opened via the examples template.good first issueEasy to fix issues, good for newcomers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions