nuxt-security: CSP fails with strict-dynamic when implementing Cloudflare Web Analytics

When following the @nuxt/scripts implementation instructions for Cloudflare Web Analytics here: https://scripts.nuxt.com/scripts/analytics/cloudflare-web-analytics (the “Loading Globally” section, as well as “useScriptCloudflareWebAnalytics” my CSP settings are consistently producing the following error:

Refused to load the script 'https://static.cloudflareinsights.com/beacon.min.js' because it
violates the following Content Security Policy directive: 
"script-src 'self' https: 'unsafe-inline' 'strict-dynamic' 'unsafe-eval' ...[a bunch of sha]".
Note that 'strict-dynamic' is present, so host-based allowlisting is disabled. 
Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

Here’s my nuxt.config.ts entry for the module:

security: {
  headers: {
    crossOriginEmbedderPolicy: 'unsafe-none',
    crossOriginOpenerPolicy: false, // Resolves a NS_ERROR_FAILURE error in Firefox
    xFrameOptions: 'SAMEORIGIN',
    contentSecurityPolicy: {
      'upgrade-insecure-requests': import.meta.env.NODE_ENV === 'development' ? false : true, 
      'img-src': ["'self'", 'data:', 'https:'],
      'frame-ancestors': ["'self'", 'localhost:*', 'http://localhost:*', 'https://localhost:*', 'https://*.example.com'],
      'script-src': [
        "'self'",
        'https:',
        "'unsafe-inline'",
        "'strict-dynamic'",
        "'nonce-{{nonce}}'",
        "'unsafe-eval'",
      ],
      'script-src-attr': ["'self'"],
      'style-src': [
        "'self'", 
        'https:',
        "'unsafe-inline'",
      ],
    },
  },
  rateLimiter: false,
  removeLoggers: false,
}

According to the Nuxt security docs on whitelisting external resources utilizing useScript (or the global loader, I’m assuming) should bypass this issue because of the strict-dynamic rule.

If I remove strict-dynamic from my config, the issue is resolved, but this isn’t ideal.

What am I doing wrong? Any help would be appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15

Most upvoted comments

@harlan-zw I need to double check whether rel=preload would be an issue, I don’t think it should

what striked me here is OP was saying the script was in the server response

@adamdehaven this is weird. You should have been able to use @nuxt/scripts out of the box without any custom code.

Would you mind sending a screenshot of the HTML portion where the script appears, as well as a screenshot of the CSP denial message in the console - in both cases trying to make sure the line numbers match? This would be helpful in trying to understand the potential relationship with the preload link