diff --git a/CHANGELOG.md b/CHANGELOG.md index ec9bc120c..607593b59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - feat(nextjs): Update turbopack warning ([#1089](https://github.com/getsentry/sentry-wizard/pull/1089)) - feat(nextjs): Set `sendDefaultPii: true` by default ([#1052](https://github.com/getsentry/sentry-wizard/pull/1052)) - feat(nuxt): Set `sendDefaultPii: true` by default ([#1060](https://github.com/getsentry/sentry-wizard/pull/1060)) +- feat(sveltekit): Set `sendDefaultPii: true` by default ([#1055](https://github.com/getsentry/sentry-wizard/pull/1055)) - fix(apple): Remove `options.debug: true` from SDK init snippet ([#1096](https://github.com/getsentry/sentry-wizard/pull/1096)) diff --git a/e2e-tests/tests/sveltekit-hooks.test.ts b/e2e-tests/tests/sveltekit-hooks.test.ts index 8a01bdb7d..c9a298c9c 100644 --- a/e2e-tests/tests/sveltekit-hooks.test.ts +++ b/e2e-tests/tests/sveltekit-hooks.test.ts @@ -241,6 +241,10 @@ describe('Sveltekit', () => { // If you don't want to use Session Replay, just remove the line below: integrations: [replayIntegration()], + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, });`, 'export const handleError = handleErrorWithSentry(', ]); @@ -257,6 +261,11 @@ describe('Sveltekit', () => { // Enable logs to be sent to Sentry enableLogs: true, + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, + // uncomment the line below to enable Spotlight (https://spotlightjs.com) // spotlight: import.meta.env.DEV, });`, @@ -308,7 +317,8 @@ describe('Sveltekit', () => { replaysSessionSampleRate: 0.1, replaysOnErrorSampleRate: 1, integrations: [Sentry.replayIntegration()], - enableLogs: true + enableLogs: true, + sendDefaultPii: true })`, 'export const handleError = Sentry.handleErrorWithSentry(', ]); @@ -320,7 +330,8 @@ describe('Sveltekit', () => { `Sentry.init({ dsn: "${TEST_ARGS.PROJECT_DSN}", tracesSampleRate: 1, - enableLogs: true + enableLogs: true, + sendDefaultPii: true })`, 'export const handleError = Sentry.handleErrorWithSentry();', ]); diff --git a/e2e-tests/tests/sveltekit-tracing.test.ts b/e2e-tests/tests/sveltekit-tracing.test.ts index f5469bc54..15b8103cd 100644 --- a/e2e-tests/tests/sveltekit-tracing.test.ts +++ b/e2e-tests/tests/sveltekit-tracing.test.ts @@ -142,6 +142,10 @@ describe('Sveltekit with instrumentation and tracing', () => { // If you don't want to use Session Replay, just remove the line below: integrations: [replayIntegration()], + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, }); // If you have a custom error handler, pass it to \`handleErrorWithSentry\` diff --git a/src/sveltekit/sdk-setup/setup.ts b/src/sveltekit/sdk-setup/setup.ts index ae3fac83d..cf148fe2d 100644 --- a/src/sveltekit/sdk-setup/setup.ts +++ b/src/sveltekit/sdk-setup/setup.ts @@ -418,7 +418,7 @@ Skipping adding Sentry functionality to it.`, Sentry.setTag(`modified-instrumentation-server`, 'success'); } -function insertClientInitCall( +export function insertClientInitCall( dsn: string, // eslint-disable-next-line @typescript-eslint/no-explicit-any originalHooksMod: ProxifiedModule, @@ -439,6 +439,7 @@ function insertClientInitCall( replaysOnErrorSampleRate?: number; integrations?: string[]; enableLogs?: boolean; + sendDefaultPii?: boolean; } = { dsn, }; @@ -457,6 +458,8 @@ function insertClientInitCall( initArgs.enableLogs = true; } + initArgs.sendDefaultPii = true; + // This assignment of any values is fine because we're just creating a function call in magicast // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const initCall = builders.functionCall('Sentry.init', initArgs); @@ -493,6 +496,7 @@ function insertServerInitCall( dsn: string; tracesSampleRate?: number; enableLogs?: boolean; + sendDefaultPii?: boolean; } = { dsn, }; @@ -505,6 +509,8 @@ function insertServerInitCall( initArgs.enableLogs = true; } + initArgs.sendDefaultPii = true; + // This assignment of any values is fine because we're just creating a function call in magicast // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const initCall = builders.functionCall('Sentry.init', initArgs); diff --git a/src/sveltekit/templates.ts b/src/sveltekit/templates.ts index 2f0c51c93..4ec1785d2 100644 --- a/src/sveltekit/templates.ts +++ b/src/sveltekit/templates.ts @@ -39,6 +39,10 @@ ${ integrations: [replayIntegration()],` : '' } + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, }); // If you have a custom error handler, pass it to \`handleErrorWithSentry\` @@ -74,6 +78,11 @@ ${ ` : '' } + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, + // uncomment the line below to enable Spotlight (https://spotlightjs.com) // spotlight: import.meta.env.DEV, });` diff --git a/test/sveltekit/templates.test.ts b/test/sveltekit/templates.test.ts index 3d4b3bed2..ddbeeda88 100644 --- a/test/sveltekit/templates.test.ts +++ b/test/sveltekit/templates.test.ts @@ -4,6 +4,9 @@ import { getInstrumentationServerTemplate, getServerHooksTemplate, } from '../../src/sveltekit/templates'; +import { insertClientInitCall } from '../../src/sveltekit/sdk-setup/setup'; +// @ts-expect-error - magicast is ESM and TS complains about that. It works though +import { parseModule } from 'magicast'; vi.mock('../../src/utils/clack/mcp-config', () => ({ offerProjectScopedMcpConfig: vi.fn().mockResolvedValue(undefined), @@ -39,6 +42,10 @@ describe('getClientHooksTemplate', () => { // If you don't want to use Session Replay, just remove the line below: integrations: [replayIntegration()], + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, }); // If you have a custom error handler, pass it to \`handleErrorWithSentry\` @@ -72,6 +79,10 @@ describe('getClientHooksTemplate', () => { // If you don't want to use Session Replay, just remove the line below: integrations: [replayIntegration()], + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, }); // If you have a custom error handler, pass it to \`handleErrorWithSentry\` @@ -98,6 +109,10 @@ describe('getClientHooksTemplate', () => { + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, }); // If you have a custom error handler, pass it to \`handleErrorWithSentry\` @@ -124,6 +139,10 @@ describe('getClientHooksTemplate', () => { enableLogs: true, + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, }); // If you have a custom error handler, pass it to \`handleErrorWithSentry\` @@ -158,6 +177,11 @@ describe('getServerHooksTemplate', () => { // Enable logs to be sent to Sentry enableLogs: true, + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, + // uncomment the line below to enable Spotlight (https://spotlightjs.com) // spotlight: import.meta.env.DEV, }); @@ -191,6 +215,11 @@ describe('getServerHooksTemplate', () => { dsn: 'https://sentry.io/123', + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, + // uncomment the line below to enable Spotlight (https://spotlightjs.com) // spotlight: import.meta.env.DEV, }); @@ -226,6 +255,11 @@ describe('getServerHooksTemplate', () => { // Enable logs to be sent to Sentry enableLogs: true, + + // Enable sending user PII (Personally Identifiable Information) + // https://docs.sentry.io/platforms/javascript/guides/sveltekit/configuration/options/#sendDefaultPii + sendDefaultPii: true, + // uncomment the line below to enable Spotlight (https://spotlightjs.com) // spotlight: import.meta.env.DEV, }); @@ -346,3 +380,207 @@ describe('getInstrumentationServerTemplate', () => { });"`); }); }); + +describe('insertClientInitCall', () => { + it('should insert client init call with all features enabled', () => { + const originalHooksMod = parseModule(` + import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + export const handleError = handleErrorWithSentry(); + `); + + insertClientInitCall('https://sentry.io/123', originalHooksMod, { + performance: true, + replay: true, + logs: true, + }); + + const result = originalHooksMod.generate().code; + + expect(result).toMatchInlineSnapshot(` + "import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + // If you don't want to use Session Replay, remove the \`Replay\` integration, + // \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options. + Sentry.init({ + dsn: "https://sentry.io/123", + tracesSampleRate: 1, + replaysSessionSampleRate: 0.1, + replaysOnErrorSampleRate: 1, + integrations: [Sentry.replayIntegration()], + enableLogs: true, + sendDefaultPii: true + }) + + export const handleError = handleErrorWithSentry();" + `); + }); + + it('should insert client init call with performance disabled', () => { + const originalHooksMod = parseModule(` + import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + export const handleError = handleErrorWithSentry(); + `); + + insertClientInitCall('https://sentry.io/456', originalHooksMod, { + performance: false, + replay: true, + logs: false, + }); + + const result = originalHooksMod.generate().code; + + expect(result).toMatchInlineSnapshot(` + "import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + // If you don't want to use Session Replay, remove the \`Replay\` integration, + // \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options. + Sentry.init({ + dsn: "https://sentry.io/456", + replaysSessionSampleRate: 0.1, + replaysOnErrorSampleRate: 1, + integrations: [Sentry.replayIntegration()], + sendDefaultPii: true + }) + + export const handleError = handleErrorWithSentry();" + `); + }); + + it('should insert client init call with replay disabled', () => { + const originalHooksMod = parseModule(` + import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + export const handleError = handleErrorWithSentry(); + `); + + insertClientInitCall('https://sentry.io/789', originalHooksMod, { + performance: true, + replay: false, + logs: true, + }); + + const result = originalHooksMod.generate().code; + + expect(result).toMatchInlineSnapshot(` + "import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + // If you don't want to use Session Replay, remove the \`Replay\` integration, + // \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options. + Sentry.init({ + dsn: "https://sentry.io/789", + tracesSampleRate: 1, + enableLogs: true, + sendDefaultPii: true + }) + + export const handleError = handleErrorWithSentry();" + `); + }); + + it('should insert client init call with only logs enabled', () => { + const originalHooksMod = parseModule(` + import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + export const handleError = handleErrorWithSentry(); + `); + + insertClientInitCall('https://sentry.io/xyz', originalHooksMod, { + performance: false, + replay: false, + logs: true, + }); + + const result = originalHooksMod.generate().code; + + expect(result).toMatchInlineSnapshot(` + "import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + // If you don't want to use Session Replay, remove the \`Replay\` integration, + // \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options. + Sentry.init({ + dsn: "https://sentry.io/xyz", + enableLogs: true, + sendDefaultPii: true + }) + + export const handleError = handleErrorWithSentry();" + `); + }); + + it('should insert client init call with all features disabled', () => { + const originalHooksMod = parseModule(` + import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + export const handleError = handleErrorWithSentry(); + `); + + insertClientInitCall('https://sentry.io/minimal', originalHooksMod, { + performance: false, + replay: false, + logs: false, + }); + + const result = originalHooksMod.generate().code; + + expect(result).toMatchInlineSnapshot(` + "import { handleErrorWithSentry } from "@sentry/sveltekit"; + import * as Sentry from "@sentry/sveltekit"; + + // If you don't want to use Session Replay, remove the \`Replay\` integration, + // \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options. + Sentry.init({ + dsn: "https://sentry.io/minimal", + sendDefaultPii: true + }) + + export const handleError = handleErrorWithSentry();" + `); + }); + + it('should insert init call after imports', () => { + const originalHooksMod = parseModule(` + import { handleErrorWithSentry } from "@sentry/sveltekit"; + import { somethingElse } from "some-package"; + import * as Sentry from "@sentry/sveltekit"; + + export const handleError = handleErrorWithSentry(); + export const someOtherExport = somethingElse(); + `); + + insertClientInitCall('https://sentry.io/order-test', originalHooksMod, { + performance: true, + replay: false, + logs: false, + }); + + const result = originalHooksMod.generate().code; + + expect(result).toMatchInlineSnapshot(` + "import { handleErrorWithSentry } from "@sentry/sveltekit"; + import { somethingElse } from "some-package"; + import * as Sentry from "@sentry/sveltekit"; + + // If you don't want to use Session Replay, remove the \`Replay\` integration, + // \`replaysSessionSampleRate\` and \`replaysOnErrorSampleRate\` options. + Sentry.init({ + dsn: "https://sentry.io/order-test", + tracesSampleRate: 1, + sendDefaultPii: true + }) + + export const handleError = handleErrorWithSentry(); + export const someOtherExport = somethingElse();" + `); + }); +});