Skip to content

Commit 3701010

Browse files
committed
ref(sveltekit): Remove nonce approach in favor of hash for optional CSP config
1 parent 85ceb31 commit 3701010

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

docs/platforms/javascript/guides/sveltekit/manual-setup.mdx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ If you don't already have a [client hooks](https://kit.svelte.dev/docs/hooks#sha
3333
At the top of your client hooks file, import and initialize the Sentry SDK as shown in the snippet below. See the [Basic Options](../configuration/options/) page to view other SDK configuration options.
3434
Also, add the `handleErrorWithSentry` function to the [`handleError` hook](https://kit.svelte.dev/docs/hooks#shared-hooks-handleerror):
3535

36-
3736
```javascript {filename:hooks.client.(js|ts)} {1, 3-14, 20}
3837
import * as Sentry from "@sentry/sveltekit";
3938

@@ -68,7 +67,6 @@ At the top of your server hooks file, import and initialize the Sentry SDK as sh
6867
Add the `handleErrorWithSentry` function to the [`handleError` hook](https://kit.svelte.dev/docs/hooks#shared-hooks-handleerror) and add the Sentry request handler to the [`handle` hook](https://kit.svelte.dev/docs/hooks#server-hooks-handle).
6968
If you're already using your own handler(s), use SvelteKit's [`sequence`](https://kit.svelte.dev/docs/modules#sveltejs-kit-hooks-sequence) function to add the Sentry handler _before_ your handler(s).
7069

71-
7270
```javascript {filename:hooks.server.(js|ts)} {1, 3-9, 15, 19}
7371
import * as Sentry from "@sentry/sveltekit";
7472

@@ -134,7 +132,6 @@ Or, you can set them by passing a `sourceMapsUploadOptions` object to `sentrySve
134132
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
135133
```
136134

137-
138135
```javascript {filename:vite.config.(js|ts)} {7-16}
139136
import { sveltekit } from "@sveltejs/kit/vite";
140137
import { sentrySvelteKit } from "@sentry/sveltekit";
@@ -260,7 +257,7 @@ export default {
260257
};
261258
```
262259

263-
#### Configure Client-side `fetch` Instrumentation
260+
#### Configure CSP for Client-side `fetch` Instrumentation
264261

265262
<Note>
266263

@@ -271,32 +268,42 @@ Available since version `7.91.0`
271268
The `sentryHandle` function you added to your `handle` hook in `hooks.server.ts` during [server-side setup](#server-side-setup) injects an inline `<script>` tag into the HTML response of the server.
272269
This script attempts to proxy all client-side `fetch` calls, so that `fetch` calls inside your `load` functions are captured by the SDK.
273270
However, if you configured CSP rules to block inline fetch scripts by default, this script will be [blocked by the browser](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src#unsafe_inline_script).
274-
To enable the script, you need to add an exception for the `sentryHandle` script:
275-
276-
First, specify your nonce in the `fetchProxyScriptNonce` option in your `sentryHandle` call:
277271

278-
```javascript {filename:hooks.server.(js|ts)}
279-
// Add the nonce to the <script> tag:
280-
export const handle = sentryHandle({ fetchProxyScriptNonce: "<your-nonce>" });
281-
```
272+
To enable the script, you need to add an exception for the `sentryHandle` script by adding a hash to your CSP script source rules.
282273

283-
Then, adjust your [SvelteKit CSP configuration](https://kit.svelte.dev/docs/configuration#csp):
274+
If your CSP is defined in `svelte.config.js`, you can add the hash to the `csp.directives.script-src` array:
284275

285-
```javascript {filename: svelte.config.js} {5}
276+
```javascript {filename:svelte.config.js} {5-7}
286277
const config = {
287278
kit: {
288279
csp: {
289280
directives: {
290-
"script-src": ["nonce-<your-nonce>"],
281+
"script-src": ["sha256-G1hFVvgk25rc5WZBnUFUiRzXWk1UpKviM/ShtacW9vA="], // + rest of your values
291282
},
292283
},
293284
},
294285
};
295286
```
296287

288+
For external CSP configurations, add the following hash to your `script-src` directive:
289+
290+
```txt
291+
sha256-G1hFVvgk25rc5WZBnUFUiRzXWk1UpKviM/ShtacW9vA=
292+
```
293+
294+
We will not make changes to this script any time soon (in fact, this script will be removed in version 9 of the SDK).
295+
296+
<Note>
297+
298+
Previous versions of this documentation recommended setting a nonce in your `sentryHandle` options.
299+
We no longer recommend this approach due to security concerns with re-using nonces.
300+
Instead, we recommend setting the hash as outlined above.
301+
302+
</Note>
303+
297304
##### Disable Client-side `fetch` Proxy Script
298305

299-
If you do not want to inject the script responsible for instrumenting client-side `load` calls, you can disable injection by passing `injectFetchProxyScript: false` to `sentryHandle`:
306+
If you don't want to inject the script responsible for instrumenting client-side `fetch` calls, you can disable injection by passing `injectFetchProxyScript: false` to `sentryHandle`:
300307

301308
```javascript {filename:hooks.server.(js|ts)}
302309
export const handle = sentryHandle({ injectFetchProxyScript: false });

next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
/// <reference types="next/navigation-types/compat/navigation" />
44

55
// NOTE: This file should not be edited
6-
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
6+
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

0 commit comments

Comments
 (0)