diff --git a/packages/profiling-node/src/utils.ts b/packages/profiling-node/src/utils.ts index 39f95fad1c71..9360c970a634 100644 --- a/packages/profiling-node/src/utils.ts +++ b/packages/profiling-node/src/utils.ts @@ -9,7 +9,14 @@ import type { ProfileChunkItem, SdkInfo, } from '@sentry/core'; -import { createEnvelope, debug, dsnToString, getDebugImagesForResources, uuid4 } from '@sentry/core'; +import { + _INTERNAL_safeDateNow as safeDateNow, + createEnvelope, + debug, + dsnToString, + getDebugImagesForResources, + uuid4, +} from '@sentry/core'; import type { RawChunkCpuProfile } from '@sentry/node-cpu-profiler'; import { isMainThread, threadId } from 'worker_threads'; import { DEBUG_BUILD } from './debug-build'; @@ -164,7 +171,7 @@ export function createEventEnvelopeHeaders( ): EventEnvelopeHeaders { return { event_id: uuid4(), - sent_at: new Date().toISOString(), + sent_at: new Date(safeDateNow()).toISOString(), ...(sdkInfo && { sdk: sdkInfo }), ...(!!tunnel && dsn && { dsn: dsnToString(dsn) }), }; diff --git a/packages/profiling-node/test/envelope-safe-timestamp.test.ts b/packages/profiling-node/test/envelope-safe-timestamp.test.ts new file mode 100644 index 000000000000..c813b7fdff98 --- /dev/null +++ b/packages/profiling-node/test/envelope-safe-timestamp.test.ts @@ -0,0 +1,59 @@ +import { GLOBAL_OBJ } from '@sentry/core'; +import { afterAll, describe, expect, it } from 'vitest'; +import { createEventEnvelopeHeaders } from '../src/utils'; + +const SAFE_RANDOM_SYMBOL = Symbol.for('__SENTRY_SAFE_RANDOM_ID_WRAPPER__'); +const globals = GLOBAL_OBJ as unknown as Record; +const originalRunner = globals[SAFE_RANDOM_SYMBOL]; +const RealDate = Date; +let inSafeContext = false; + +globals[SAFE_RANDOM_SYMBOL] = (callback: () => T): T => { + const previous = inSafeContext; + inSafeContext = true; + try { + return callback(); + } finally { + inSafeContext = previous; + } +}; + +afterAll(() => { + if (originalRunner === undefined) { + Reflect.deleteProperty(globals, SAFE_RANDOM_SYMBOL); + } else { + globals[SAFE_RANDOM_SYMBOL] = originalRunner; + } +}); + +describe('profile envelope timestamp', () => { + it('reads the clock only inside the safe context', () => { + const now = 1_700_000_000_000; + const GuardedDate = class extends RealDate { + public constructor(...args: unknown[]) { + if (args.length === 0 && !inSafeContext) { + throw new Error('Read new Date() outside the safe context'); + } + super(...(args as [number | string | Date])); + } + + public static now(): number { + if (!inSafeContext) { + throw new Error('Read Date.now() outside the safe context'); + } + return now; + } + }; + + (globalThis as { Date: DateConstructor }).Date = GuardedDate as DateConstructor; + try { + expect(createEventEnvelopeHeaders(undefined, undefined)).toMatchObject({ + event_id: expect.any(String), + sent_at: new RealDate(now).toISOString(), + }); + expect(inSafeContext).toBe(false); + } finally { + (globalThis as { Date: DateConstructor }).Date = RealDate; + } + }); +});