Skip to content

Commit 94e7347

Browse files
authored
feat(sveltekit)!: Remove fetchProxyScriptNonce option (getsentry#15123)
Removes the `fetchProxyScriptNonce` and in turn simplifies our `transformPageChunk` callback a bit.
1 parent 580b8b5 commit 94e7347

File tree

3 files changed

+19
-36
lines changed

3 files changed

+19
-36
lines changed

docs/migration/v8-to-v9.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ The following changes are unlikely to affect users of the SDK. They are listed h
269269
This function was primarily internally used.
270270
It's functionality was misleading and should not be used.
271271

272+
### `@sentry/sveltekit`
273+
274+
- The `fetchProxyScriptNonce` option in `sentryHandle()` was removed due to security concerns. If you previously specified this option for your CSP policy, specify a [script hash](https://docs.sentry.io/platforms/javascript/guides/sveltekit/manual-setup/#configure-csp-for-client-side-fetch-instrumentation) in your CSP config or [disable](https://docs.sentry.io/platforms/javascript/guides/sveltekit/manual-setup/#disable-client-side-fetch-proxy-script) the injection of the script entirely.
275+
272276
## 5. Build Changes
273277

274278
Previously the CJS versions of the SDK code (wrongfully) contained compatibility statements for default exports in ESM:
@@ -491,6 +495,10 @@ Sentry.init({
491495

492496
- Deprecated the `hideSourceMaps` option. There are no replacements for this option. The SDK emits hidden sourcemaps by default.
493497

498+
### `@sentry/sveltekit`
499+
500+
- The `fetchProxyScriptNonce` option in `sentryHandle()` was deprecated due to security concerns. If you previously specified this option for your CSP policy, specify a [script hash](https://docs.sentry.io/platforms/javascript/guides/sveltekit/manual-setup/#configure-csp-for-client-side-fetch-instrumentation) in your CSP config or [disable](https://docs.sentry.io/platforms/javascript/guides/sveltekit/manual-setup/#disable-client-side-fetch-proxy-script) the injection of the script entirely.
501+
494502
## `@sentry/opentelemetry`
495503

496504
- Deprecated the `generateSpanContextForPropagationContext` method. There are no replacements for this method.

packages/sveltekit/src/server/handle.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ export type SentryHandleOptions = {
4343
* @default true
4444
*/
4545
injectFetchProxyScript?: boolean;
46-
47-
/**
48-
* If this option is set, the `sentryHandle` handler will add a nonce attribute to the script
49-
* tag it injects into the page. This script is used to enable instrumentation of `fetch` calls
50-
* in `load` functions.
51-
*
52-
* Use this if your CSP policy blocks the fetch proxy script injected by `sentryHandle`.
53-
*/
54-
fetchProxyScriptNonce?: string;
5546
};
5647

5748
/**
@@ -68,21 +59,17 @@ export const FETCH_PROXY_SCRIPT = `
6859
/**
6960
* Adds Sentry tracing <meta> tags to the returned html page.
7061
* Adds Sentry fetch proxy script to the returned html page if enabled in options.
71-
* Also adds a nonce attribute to the script tag if users specified one for CSP.
7262
*
7363
* Exported only for testing
7464
*/
75-
export function addSentryCodeToPage(options: SentryHandleOptions): NonNullable<ResolveOptions['transformPageChunk']> {
76-
const { fetchProxyScriptNonce, injectFetchProxyScript } = options;
77-
// if injectFetchProxyScript is not set, we default to true
78-
const shouldInjectScript = injectFetchProxyScript !== false;
79-
const nonce = fetchProxyScriptNonce ? `nonce="${fetchProxyScriptNonce}"` : '';
80-
65+
export function addSentryCodeToPage(options: { injectFetchProxyScript: boolean }): NonNullable<
66+
ResolveOptions['transformPageChunk']
67+
> {
8168
return ({ html }) => {
8269
const metaTags = getTraceMetaTags();
8370
const headWithMetaTags = metaTags ? `<head>\n${metaTags}` : '<head>';
8471

85-
const headWithFetchScript = shouldInjectScript ? `\n<script ${nonce}>${FETCH_PROXY_SCRIPT}</script>` : '';
72+
const headWithFetchScript = options.injectFetchProxyScript ? `\n<script>${FETCH_PROXY_SCRIPT}</script>` : '';
8673

8774
const modifiedHead = `${headWithMetaTags}${headWithFetchScript}`;
8875

@@ -106,7 +93,7 @@ export function addSentryCodeToPage(options: SentryHandleOptions): NonNullable<R
10693
* ```
10794
*/
10895
export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
109-
const options = {
96+
const options: Required<SentryHandleOptions> = {
11097
handleUnknownRoutes: false,
11198
injectFetchProxyScript: true,
11299
...handlerOptions,
@@ -144,7 +131,7 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
144131

145132
async function instrumentHandle(
146133
{ event, resolve }: Parameters<Handle>[0],
147-
options: SentryHandleOptions,
134+
options: Required<SentryHandleOptions>,
148135
): Promise<Response> {
149136
if (!event.route?.id && !options.handleUnknownRoutes) {
150137
return resolve(event);
@@ -174,7 +161,7 @@ async function instrumentHandle(
174161
normalizedRequest: winterCGRequestToRequestData(event.request.clone()),
175162
});
176163
const res = await resolve(event, {
177-
transformPageChunk: addSentryCodeToPage(options),
164+
transformPageChunk: addSentryCodeToPage({ injectFetchProxyScript: options.injectFetchProxyScript }),
178165
});
179166
if (span) {
180167
setHttpStatus(span, res.status);

packages/sveltekit/test/server/handle.test.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -432,36 +432,24 @@ describe('addSentryCodeToPage', () => {
432432
</html>`;
433433

434434
it("Adds add meta tags and fetch proxy script if there's no active transaction", () => {
435-
const transformPageChunk = addSentryCodeToPage({});
435+
const transformPageChunk = addSentryCodeToPage({ injectFetchProxyScript: true });
436436
const transformed = transformPageChunk({ html, done: true });
437437

438438
expect(transformed).toContain('<meta name="sentry-trace"');
439439
expect(transformed).toContain('<meta name="baggage"');
440440
expect(transformed).not.toContain('sentry-transaction=');
441-
expect(transformed).toContain(`<script >${FETCH_PROXY_SCRIPT}</script>`);
441+
expect(transformed).toContain(`<script>${FETCH_PROXY_SCRIPT}</script>`);
442442
});
443443

444444
it('adds meta tags and the fetch proxy script if there is an active transaction', () => {
445-
const transformPageChunk = addSentryCodeToPage({});
445+
const transformPageChunk = addSentryCodeToPage({ injectFetchProxyScript: true });
446446
SentryNode.startSpan({ name: 'test' }, () => {
447447
const transformed = transformPageChunk({ html, done: true }) as string;
448448

449449
expect(transformed).toContain('<meta name="sentry-trace"');
450450
expect(transformed).toContain('<meta name="baggage"');
451451
expect(transformed).toContain('sentry-transaction=test');
452-
expect(transformed).toContain(`<script >${FETCH_PROXY_SCRIPT}</script>`);
453-
});
454-
});
455-
456-
it('adds a nonce attribute to the script if the `fetchProxyScriptNonce` option is specified', () => {
457-
const transformPageChunk = addSentryCodeToPage({ fetchProxyScriptNonce: '123abc' });
458-
SentryNode.startSpan({ name: 'test' }, () => {
459-
const transformed = transformPageChunk({ html, done: true }) as string;
460-
461-
expect(transformed).toContain('<meta name="sentry-trace"');
462-
expect(transformed).toContain('<meta name="baggage"');
463-
expect(transformed).toContain('sentry-transaction=test');
464-
expect(transformed).toContain(`<script nonce="123abc">${FETCH_PROXY_SCRIPT}</script>`);
452+
expect(transformed).toContain(`<script>${FETCH_PROXY_SCRIPT}</script>`);
465453
});
466454
});
467455

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy