Skip to content

CSP inline script violation when loading Sentry SvelteKit #8925

Description

@ncvc

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/sveltekit

SDK Version

7.66.0

Framework Version

7.66.0

Link to Sentry event

No response

SDK Setup

  Sentry.init({
    dsn: env.PUBLIC_SENTRY_SVELTEKIT_CLIENT_DSN,
    integrations: [
      new Sentry.BrowserTracing(),
      new CaptureConsoleIntegration({
        levels: ["warn", "error", "assert"],
      }),
    ],
    release,
    environment: env.PUBLIC_ENV_NAME,
    // Set tracesSampleRate to 1.0 to capture 100%
    // of transactions for performance monitoring.
    // We recommend adjusting this value in production
    tracesSampleRate: 1.0,
    attachStacktrace: true,
    ignoreErrors: [
      "TypeError: Failed to fetch dynamically imported module",
      "Load failed",
      "TypeError: Load failed",
      "TypeError: Failed to fetch",
      "Failed to fetch",
      "TypeError: NetworkError when attempting to fetch resource",
      "TypeError: Importing a module script failed",
      "TypeError: error loading dynamically imported module",
    ],
  });
  Sentry.setTags({
    server_name: env.PUBLIC_DATADOG_HOST_NAME,
    version: release,
    service: "agent-webapp",
  });

Steps to Reproduce

Open the browser console and load a page where Sentry is loaded

Expected Result

No CSP errors

Actual Result

On page load, I get the following CSP error in the console:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' <the rest of my script-src>". Either the 'unsafe-inline' keyword, a hash ('sha256-+X7Z1KW2Vcl9pendYbp0FYL6F0HZek43aBP/14cwq+U='), or a nonce ('nonce-...') is required to enable inline execution.

I get this error in version 7.65.0 but not 7.64.0.

I believe this was introduced in this PR, where the following script tag is injected into the head tag:

<script>
    const f = window.fetch;
    if(f){
      window._sentryFetchProxy = function(...a){return f(...a)}
      window.fetch = function(...a){return window._sentryFetchProxy(...a)}
    }

  </script>

As a temporary workaround, I've added sha256-+X7Z1KW2Vcl9pendYbp0FYL6F0HZek43aBP/14cwq+U= to my script-src CSP, but ideally I wouldn't have to update this hash each time the injected script is changed.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status
No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions