From b127120031fa7cf7029dd7b58bc901d61b3d4c5c Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 5 Dec 2023 11:44:26 +0100 Subject: [PATCH] fix(node): Guard `process.env.NODE_ENV` access in Spotlight integration --- packages/node/src/integrations/spotlight.ts | 4 +- .../node/test/integrations/spotlight.test.ts | 38 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/packages/node/src/integrations/spotlight.ts b/packages/node/src/integrations/spotlight.ts index cef0c27e2a4a..55fabc284cad 100644 --- a/packages/node/src/integrations/spotlight.ts +++ b/packages/node/src/integrations/spotlight.ts @@ -41,8 +41,8 @@ export class Spotlight implements Integration { * Sets up forwarding envelopes to the Spotlight Sidecar */ public setup(client: Client): void { - if (process.env.NODE_ENV !== 'development') { - logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spoltight enabled?"); + if (typeof process === 'object' && process.env && process.env.NODE_ENV !== 'development') { + logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spotlight enabled?"); } connectToSpotlight(client, this._options); } diff --git a/packages/node/test/integrations/spotlight.test.ts b/packages/node/test/integrations/spotlight.test.ts index 71af071cbf3c..f9e5571128a9 100644 --- a/packages/node/test/integrations/spotlight.test.ts +++ b/packages/node/test/integrations/spotlight.test.ts @@ -1,6 +1,6 @@ import * as http from 'http'; import type { Envelope, EventEnvelope } from '@sentry/types'; -import { createEnvelope, logger } from '@sentry/utils'; +import { GLOBAL_OBJ, createEnvelope, logger } from '@sentry/utils'; import { NodeClient } from '../../src'; import { Spotlight } from '../../src/integrations'; @@ -138,7 +138,7 @@ describe('Spotlight', () => { integration.setup(client); expect(loggerSpy).toHaveBeenCalledWith( - expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spoltight enabled?"), + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), ); process.env.NODE_ENV = oldEnvValue; @@ -152,9 +152,41 @@ describe('Spotlight', () => { integration.setup(client); expect(loggerSpy).not.toHaveBeenCalledWith( - expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spoltight enabled?"), + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), ); process.env.NODE_ENV = oldEnvValue; }); + + it('handles `process` not being available', () => { + const originalProcess = process; + + // @ts-expect-error - TS complains but we explicitly wanna test this + delete GLOBAL_OBJ.process; + + const integration = new Spotlight({ sidecarUrl: 'http://localhost:8969' }); + integration.setup(client); + + expect(loggerSpy).not.toHaveBeenCalledWith( + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), + ); + + globalThis.process = originalProcess; + }); + + it('handles `process.env` not being available', () => { + const originalEnv = process.env; + + // @ts-expect-error - TS complains but we explicitly wanna test this + delete process.env; + + const integration = new Spotlight({ sidecarUrl: 'http://localhost:8969' }); + integration.setup(client); + + expect(loggerSpy).not.toHaveBeenCalledWith( + expect.stringContaining("It seems you're not in dev mode. Do you really want to have Spotlight enabled?"), + ); + + process.env = originalEnv; + }); });