Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/profiling-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) }),
};
Expand Down
59 changes: 59 additions & 0 deletions packages/profiling-node/test/envelope-safe-timestamp.test.ts
Original file line number Diff line number Diff line change
@@ -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<symbol, unknown>;
const originalRunner = globals[SAFE_RANDOM_SYMBOL];
const RealDate = Date;
let inSafeContext = false;

globals[SAFE_RANDOM_SYMBOL] = <T>(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;
}
});
});
Loading