* feat: add sentry to core * feat: implement comprehensive Sentry error tracking with privacy-first anonymization - Add Sentry ErrorBoundary components following React best practices - Implement shared anonymization logic between core and GUI environments - Create organized sentry directory structure with clean exports - Add Continue team member access control (@continue.dev email check) - Preserve node_modules package names for debugging while anonymizing user paths - Include comprehensive test coverage for all anonymization scenarios - Set up source maps for readable production error stack traces - Add browser-compatible hashing without crypto module dependency 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * feat: complete Sentry integration with GUI components and tests - Add GUI TelemetryProviders with Sentry ErrorBoundary - Create shared isContinueTeamMember utilities for both core and GUI - Update all imports and dependencies for new sentry structure - Add comprehensive test coverage and fix all TypeScript issues 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * prettier * fix: resolve GUI build errors by removing sentry index.ts and using specific imports - Remove problematic index.ts that was pulling Node.js-specific Sentry code into browser bundle - Update GUI to import specifically from anonymization.ts and constants.ts files - Update core imports to use specific SentryLogger.ts import path - GUI build now succeeds with proper source map upload to Sentry 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * fix: move TelemetryProviders inside AuthProvider to fix test failures - Move TelemetryProviders from main.tsx to Layout.tsx inside AuthProvider - Fixes "useAuth must be used within an AuthProvider" error in tests - TelemetryProviders now has access to auth context for Continue team member check - GUI build and functionality remain working correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * prettier * merge main * Update package.json * fix tests * fix errors * Update extension.ts * disable sentry in tests/local dev * add additional error handling * Update SentryLogger.ts * remove global try/catch * remove test code * linting * Update SentryLogger.ts * more logs * Update core.ts * lower sampling rate * fix tests * feat: use `Logger` * Update logger.ts * Update logger.ts Co-Authored-By: Claude <noreply@anthropic.com> * lint fixes Co-Authored-By: Claude <noreply@anthropic.com> * fix: rename logger.ts to Logger.ts for case sensitivity This fixes TypeScript compilation errors in CI where the file was tracked as lowercase logger.ts but referenced as Logger.ts in imports. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * address ting-wai's feedback * Update core.ts --------- Co-authored-by: Claude <noreply@anthropic.com>
886 lines
27 KiB
TypeScript
886 lines
27 KiB
TypeScript
import { IdeInfo } from "../../index.js";
|
|
import {
|
|
SentryLogger,
|
|
captureException,
|
|
captureLog,
|
|
createSpan,
|
|
initializeSentry,
|
|
} from "./SentryLogger";
|
|
import { anonymizeSentryEvent } from "./anonymization";
|
|
|
|
describe("SentryLogger Integration Tests", () => {
|
|
const mockIdeInfo: IdeInfo = {
|
|
extensionVersion: "1.0.0",
|
|
name: "vscode",
|
|
ideType: "vscode",
|
|
version: "1.0.0",
|
|
remoteName: "local",
|
|
isPrerelease: false,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
// Reset SentryLogger state before each test
|
|
SentryLogger.client = undefined;
|
|
SentryLogger.scope = undefined;
|
|
SentryLogger.allowTelemetry = false;
|
|
SentryLogger.uniqueId = "NOT_UNIQUE";
|
|
SentryLogger.ideInfo = undefined;
|
|
});
|
|
|
|
afterEach(() => {
|
|
// Clean up after each test
|
|
SentryLogger.shutdownSentryClient();
|
|
});
|
|
|
|
describe("Telemetry Toggle Behavior", () => {
|
|
it("should not initialize when telemetry is disabled", async () => {
|
|
await SentryLogger.setup(
|
|
false,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
expect(SentryLogger.allowTelemetry).toBe(false);
|
|
expect(SentryLogger.client).toBeUndefined();
|
|
expect(SentryLogger.scope).toBeUndefined();
|
|
expect(SentryLogger.uniqueId).toBe("test-id");
|
|
expect(SentryLogger.ideInfo).toBe(mockIdeInfo);
|
|
});
|
|
|
|
it("should not initialize in test environment even when telemetry is enabled", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "test";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
// In the updated implementation, the Continue team member check is used instead of NODE_ENV check
|
|
// so we're not setting allowTelemetry to false just because we're in test environment anymore
|
|
expect(SentryLogger.allowTelemetry).toBe(true);
|
|
// But client and scope should still be initialized
|
|
expect(SentryLogger.client).toBeDefined();
|
|
expect(SentryLogger.scope).toBeDefined();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should initialize when telemetry is enabled in non-test environment", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
expect(SentryLogger.allowTelemetry).toBe(true);
|
|
expect(SentryLogger.uniqueId).toBe("test-id");
|
|
expect(SentryLogger.ideInfo).toBe(mockIdeInfo);
|
|
expect(SentryLogger.client).toBeDefined();
|
|
expect(SentryLogger.scope).toBeDefined();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should handle telemetry being disabled after being enabled", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
// Enable telemetry first
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
expect(SentryLogger.allowTelemetry).toBe(true);
|
|
expect(SentryLogger.client).toBeDefined();
|
|
|
|
// Then disable it
|
|
await SentryLogger.setup(false, "test-id-2", mockIdeInfo);
|
|
expect(SentryLogger.allowTelemetry).toBe(false);
|
|
expect(SentryLogger.client).toBeUndefined();
|
|
expect(SentryLogger.scope).toBeUndefined();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should not reinitialize if client already exists", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
// First setup
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
const firstClient = SentryLogger.client;
|
|
const firstScope = SentryLogger.scope;
|
|
|
|
// Second setup should not change the client
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id-2",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
expect(SentryLogger.client).toBe(firstClient);
|
|
expect(SentryLogger.scope).toBe(firstScope);
|
|
expect(SentryLogger.uniqueId).toBe("test-id-2"); // ID should update
|
|
expect(SentryLogger.ideInfo).toBe(mockIdeInfo); // Info should update
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
});
|
|
|
|
describe("Lazy Initialization", () => {
|
|
it("should initialize when accessing lazyClient if conditions are met", () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
// Set up conditions for lazy initialization
|
|
SentryLogger.allowTelemetry = true;
|
|
SentryLogger.ideInfo = mockIdeInfo;
|
|
|
|
// Access lazy client should trigger initialization
|
|
const client = SentryLogger.lazyClient;
|
|
|
|
expect(client).toBeDefined();
|
|
expect(SentryLogger.client).toBe(client);
|
|
expect(SentryLogger.scope).toBeDefined();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should not initialize if telemetry is disabled", () => {
|
|
SentryLogger.allowTelemetry = false;
|
|
SentryLogger.ideInfo = mockIdeInfo;
|
|
|
|
const client = SentryLogger.lazyClient;
|
|
|
|
expect(client).toBeUndefined();
|
|
expect(SentryLogger.client).toBeUndefined();
|
|
expect(SentryLogger.scope).toBeUndefined();
|
|
});
|
|
|
|
it("should not initialize if ideInfo is missing", () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
SentryLogger.allowTelemetry = true;
|
|
SentryLogger.ideInfo = undefined;
|
|
|
|
const client = SentryLogger.lazyClient;
|
|
|
|
expect(client).toBeUndefined();
|
|
expect(SentryLogger.client).toBeUndefined();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
});
|
|
|
|
describe("Shutdown Behavior", () => {
|
|
it("should properly shutdown when client exists", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
// Setup Sentry first
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
expect(SentryLogger.client).toBeDefined();
|
|
|
|
// Shutdown should clean up
|
|
SentryLogger.shutdownSentryClient();
|
|
expect(SentryLogger.client).toBeUndefined();
|
|
expect(SentryLogger.scope).toBeUndefined();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should handle shutdown gracefully when client is undefined", () => {
|
|
expect(() => SentryLogger.shutdownSentryClient()).not.toThrow();
|
|
expect(SentryLogger.client).toBeUndefined();
|
|
expect(SentryLogger.scope).toBeUndefined();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Sentry Utility Functions", () => {
|
|
const mockIdeInfo: IdeInfo = {
|
|
extensionVersion: "1.0.0",
|
|
name: "vscode",
|
|
ideType: "vscode",
|
|
version: "1.0.0",
|
|
remoteName: "local",
|
|
isPrerelease: false,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
// Reset SentryLogger state
|
|
SentryLogger.client = undefined;
|
|
SentryLogger.scope = undefined;
|
|
SentryLogger.allowTelemetry = false;
|
|
});
|
|
|
|
afterEach(() => {
|
|
SentryLogger.shutdownSentryClient();
|
|
});
|
|
|
|
describe("initializeSentry", () => {
|
|
it("should return undefined when telemetry is disabled", () => {
|
|
const result = initializeSentry();
|
|
|
|
expect(result.client).toBeUndefined();
|
|
expect(result.scope).toBeUndefined();
|
|
});
|
|
|
|
it("should return client and scope when telemetry is enabled", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
const result = initializeSentry();
|
|
|
|
expect(result.client).toBeDefined();
|
|
expect(result.scope).toBeDefined();
|
|
expect(result.client).toBe(SentryLogger.client);
|
|
expect(result.scope).toBe(SentryLogger.scope);
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
});
|
|
|
|
describe("createSpan", () => {
|
|
it("should execute callback normally when telemetry is disabled", () => {
|
|
const callback = jest.fn().mockReturnValue("test-result");
|
|
|
|
const result = createSpan("test.operation", "test span", callback);
|
|
|
|
expect(result).toBe("test-result");
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("should execute callback with span when telemetry is enabled", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
const callback = jest.fn().mockReturnValue("test-result");
|
|
const result = createSpan("test.operation", "test span", callback);
|
|
|
|
expect(result).toBe("test-result");
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should handle async callbacks", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
const asyncCallback = jest.fn().mockResolvedValue("async-result");
|
|
const result = await createSpan(
|
|
"test.operation",
|
|
"test span",
|
|
asyncCallback,
|
|
);
|
|
|
|
expect(result).toBe("async-result");
|
|
expect(asyncCallback).toHaveBeenCalledTimes(1);
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
});
|
|
|
|
describe("captureException", () => {
|
|
it("should not throw when telemetry is disabled", () => {
|
|
const error = new Error("test error");
|
|
|
|
expect(() => captureException(error)).not.toThrow();
|
|
expect(() => captureException(error, { context: "test" })).not.toThrow();
|
|
});
|
|
|
|
it("should not throw when telemetry is enabled", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
const error = new Error("test error");
|
|
|
|
expect(() => captureException(error)).not.toThrow();
|
|
expect(() => captureException(error, { context: "test" })).not.toThrow();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should handle malformed context gracefully", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
const error = new Error("test error");
|
|
|
|
expect(() => captureException(error, null as any)).not.toThrow();
|
|
expect(() => captureException(error, undefined)).not.toThrow();
|
|
expect(() => captureException(error, { circular: {} })).not.toThrow();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
});
|
|
|
|
describe("captureLog", () => {
|
|
it("should not throw when telemetry is disabled", () => {
|
|
expect(() => captureLog("test message")).not.toThrow();
|
|
expect(() =>
|
|
captureLog("test message", "info", { context: "test" }),
|
|
).not.toThrow();
|
|
});
|
|
|
|
it("should not throw when telemetry is enabled", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
expect(() => captureLog("test message")).not.toThrow();
|
|
expect(() =>
|
|
captureLog("test message", "error", { context: "test" }),
|
|
).not.toThrow();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
|
|
it("should handle malformed context gracefully", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
expect(() =>
|
|
captureLog("test message", "info", null as any),
|
|
).not.toThrow();
|
|
expect(() =>
|
|
captureLog("test message", "warning", undefined),
|
|
).not.toThrow();
|
|
expect(() =>
|
|
captureLog("test message", "error", { circular: {} }),
|
|
).not.toThrow();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
});
|
|
|
|
describe("Integration Tests", () => {
|
|
it("should maintain consistent state across utility functions", async () => {
|
|
const originalNodeEnv = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = "development";
|
|
|
|
// Start with telemetry disabled
|
|
expect(initializeSentry().client).toBeUndefined();
|
|
|
|
const callback = jest.fn().mockReturnValue("result");
|
|
createSpan("test", "test", callback);
|
|
expect(callback).toHaveBeenCalled();
|
|
|
|
// Enable telemetry
|
|
await SentryLogger.setup(
|
|
true,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
|
|
// Now utility functions should work with Sentry
|
|
const sentryResult = initializeSentry();
|
|
expect(sentryResult.client).toBeDefined();
|
|
expect(sentryResult.scope).toBeDefined();
|
|
|
|
// Functions should work without throwing
|
|
const callback2 = jest.fn().mockReturnValue("result2");
|
|
const spanResult = createSpan("test2", "test2", callback2);
|
|
expect(spanResult).toBe("result2");
|
|
expect(callback2).toHaveBeenCalled();
|
|
|
|
const error = new Error("test error");
|
|
expect(() => captureException(error, { test: "context" })).not.toThrow();
|
|
expect(() =>
|
|
captureLog("test log message", "info", { test: "context" }),
|
|
).not.toThrow();
|
|
|
|
// Disable telemetry again
|
|
await SentryLogger.setup(
|
|
false,
|
|
"test-id",
|
|
mockIdeInfo,
|
|
"test@continue.dev",
|
|
);
|
|
expect(initializeSentry().client).toBeUndefined();
|
|
|
|
process.env.NODE_ENV = originalNodeEnv;
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Sentry Data Anonymization Tests", () => {
|
|
describe("anonymizeSentryEvent", () => {
|
|
it("should anonymize user file paths in stack traces", () => {
|
|
const event: any = {
|
|
exception: {
|
|
values: [
|
|
{
|
|
stacktrace: {
|
|
frames: [
|
|
{
|
|
filename: "/Users/john/workspace/my-project/src/app.js",
|
|
abs_path: "/Users/john/workspace/my-project/src/app.js",
|
|
function: "myFunction",
|
|
lineno: 42,
|
|
vars: { secret: "password123" },
|
|
pre_context: ["const secret = 'password123'"],
|
|
post_context: ["console.log(secret)"],
|
|
context_line: "throw new Error('test')",
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename,
|
|
).toBe("<file>");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path,
|
|
).toBe("");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.vars,
|
|
).toBeUndefined();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.pre_context,
|
|
).toBeUndefined();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.post_context,
|
|
).toBeUndefined();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.context_line,
|
|
).toBe("<code>");
|
|
});
|
|
|
|
it("should anonymize all absolute file paths equally", () => {
|
|
const event: any = {
|
|
exception: {
|
|
values: [
|
|
{
|
|
stacktrace: {
|
|
frames: [
|
|
{
|
|
filename: "/Users/dev/continue/core/util/sentry.js",
|
|
abs_path: "/Users/dev/continue/core/util/sentry.js",
|
|
function: "captureException",
|
|
lineno: 42,
|
|
vars: { secret: "password123" },
|
|
pre_context: ["const secret = 'password123'"],
|
|
post_context: ["console.log(secret)"],
|
|
context_line: "throw new Error('test')",
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename,
|
|
).toBe("<file>");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path,
|
|
).toBe("");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.vars,
|
|
).toBeUndefined();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.pre_context,
|
|
).toBeUndefined();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.post_context,
|
|
).toBeUndefined();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.context_line,
|
|
).toBe("<code>");
|
|
});
|
|
|
|
it("should preserve node_modules package names for debugging", () => {
|
|
const event: any = {
|
|
exception: {
|
|
values: [
|
|
{
|
|
stacktrace: {
|
|
frames: [
|
|
{
|
|
filename:
|
|
"/Users/dev/project/node_modules/@sentry/node/index.js",
|
|
abs_path:
|
|
"/Users/dev/project/node_modules/@sentry/node/index.js",
|
|
function: "init",
|
|
lineno: 123,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename,
|
|
).toBe("node_modules/@sentry/<file>");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path,
|
|
).toBe("");
|
|
});
|
|
|
|
it("should hash user IDs", () => {
|
|
const event: any = {
|
|
user: {
|
|
id: "real-machine-id-12345",
|
|
username: "john.doe",
|
|
email: "john@example.com",
|
|
ip_address: "192.168.1.100",
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
expect(result?.user?.id).not.toBe("real-machine-id-12345");
|
|
expect(result?.user?.id).toMatch(/^[a-f0-9]{8}$/); // Should be 8-char hash
|
|
expect(result?.user?.username).toBeUndefined();
|
|
expect(result?.user?.email).toBeUndefined();
|
|
expect(result?.user?.ip_address).toBeUndefined();
|
|
});
|
|
|
|
it("should preserve breadcrumbs unchanged", () => {
|
|
const event: any = {
|
|
breadcrumbs: [
|
|
{
|
|
message:
|
|
"File opened: /Users/john/Documents/secret-project/config.js",
|
|
category: "file",
|
|
data: {
|
|
filename: "/Users/john/Documents/secret-project/config.js",
|
|
action: "open",
|
|
},
|
|
},
|
|
{
|
|
message: "Error in module initialization",
|
|
category: "error",
|
|
data: {
|
|
module: "auth",
|
|
error: "Connection failed",
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
// Breadcrumbs are preserved as-is in our minimalist approach
|
|
expect(result?.breadcrumbs?.[0]?.message).toBe(
|
|
"File opened: /Users/john/Documents/secret-project/config.js",
|
|
);
|
|
expect(result?.breadcrumbs?.[0]?.data?.filename).toBe(
|
|
"/Users/john/Documents/secret-project/config.js",
|
|
);
|
|
expect(result?.breadcrumbs?.[0]?.data?.action).toBe("open");
|
|
expect(result?.breadcrumbs?.[1]?.message).toBe(
|
|
"Error in module initialization",
|
|
);
|
|
expect(result?.breadcrumbs?.[1]?.data?.module).toBe("auth");
|
|
expect(result?.breadcrumbs?.[1]?.data?.error).toBe("Connection failed");
|
|
});
|
|
|
|
it("should preserve tags unchanged", () => {
|
|
const event: any = {
|
|
tags: {
|
|
username: "john.doe",
|
|
user_id: "12345",
|
|
workspace: "/Users/john/workspace",
|
|
project_path: "/path/to/project",
|
|
environment: "production",
|
|
version: "1.2.3",
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
// Tags are preserved as-is in our minimalist approach
|
|
expect(result?.tags?.username).toBe("john.doe");
|
|
expect(result?.tags?.user_id).toBe("12345");
|
|
expect(result?.tags?.workspace).toBe("/Users/john/workspace");
|
|
expect(result?.tags?.project_path).toBe("/path/to/project");
|
|
expect(result?.tags?.environment).toBe("production");
|
|
expect(result?.tags?.version).toBe("1.2.3");
|
|
});
|
|
|
|
it("should preserve extra data unchanged", () => {
|
|
const event: any = {
|
|
extra: {
|
|
path: "/Users/john/secret-project",
|
|
filename: "config.js",
|
|
directory: "/home/user/workspace",
|
|
workspace: "/Users/john/workspace",
|
|
user: "john.doe",
|
|
home: "/Users/john",
|
|
config_file: "/some/long/path/to/config.json",
|
|
error_code: 500,
|
|
timestamp: "2023-01-01T00:00:00Z",
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
// Extra data is preserved as-is in our minimalist approach
|
|
expect(result?.extra?.path).toBe("/Users/john/secret-project");
|
|
expect(result?.extra?.filename).toBe("config.js");
|
|
expect(result?.extra?.directory).toBe("/home/user/workspace");
|
|
expect(result?.extra?.workspace).toBe("/Users/john/workspace");
|
|
expect(result?.extra?.user).toBe("john.doe");
|
|
expect(result?.extra?.home).toBe("/Users/john");
|
|
expect(result?.extra?.config_file).toBe("/some/long/path/to/config.json");
|
|
expect(result?.extra?.error_code).toBe(500);
|
|
expect(result?.extra?.timestamp).toBe("2023-01-01T00:00:00Z");
|
|
});
|
|
|
|
it("should anonymize thread stack traces", () => {
|
|
const event: any = {
|
|
threads: {
|
|
values: [
|
|
{
|
|
id: "main",
|
|
name: "MainThread",
|
|
stacktrace: {
|
|
frames: [
|
|
{
|
|
filename: "/Users/john/workspace/app.js",
|
|
abs_path: "/Users/john/workspace/app.js",
|
|
function: "main",
|
|
lineno: 1,
|
|
vars: { apiKey: "secret-key-123" },
|
|
context_line: "const apiKey = 'secret-key-123'",
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
expect(
|
|
result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.filename,
|
|
).toBe("<file>");
|
|
expect(
|
|
result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path,
|
|
).toBe("");
|
|
expect(
|
|
result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.vars,
|
|
).toBeUndefined();
|
|
expect(
|
|
result?.threads?.values?.[0]?.stacktrace?.frames?.[0]?.context_line,
|
|
).toBe("<code>");
|
|
});
|
|
|
|
it("should remove OS environment variables", () => {
|
|
const event: any = {
|
|
contexts: {
|
|
os: {
|
|
name: "macOS",
|
|
version: "12.6",
|
|
environment: {
|
|
HOME: "/Users/john",
|
|
USER: "john",
|
|
PATH: "/usr/local/bin:/usr/bin",
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
expect(result?.contexts?.os?.name).toBe("macOS");
|
|
expect(result?.contexts?.os?.version).toBe("12.6");
|
|
expect(result?.contexts?.os?.environment).toBeUndefined();
|
|
});
|
|
|
|
it("should handle events with no sensitive data", () => {
|
|
const event: any = {
|
|
message: "Simple error message",
|
|
level: "error",
|
|
timestamp: 1234567890,
|
|
tags: {
|
|
component: "auth",
|
|
level: "error",
|
|
},
|
|
extra: {
|
|
error_code: 404,
|
|
retry_count: 3,
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
expect(result?.message).toBe("Simple error message");
|
|
expect(result?.level).toBe("error");
|
|
expect(result?.timestamp).toBe(1234567890);
|
|
expect(result?.tags?.component).toBe("auth");
|
|
expect(result?.extra?.error_code).toBe(404);
|
|
});
|
|
|
|
it("should handle malformed events gracefully", () => {
|
|
const malformedEvent: any = {
|
|
exception: {
|
|
values: null,
|
|
},
|
|
user: null,
|
|
breadcrumbs: "not-an-array",
|
|
tags: null,
|
|
extra: null,
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(malformedEvent);
|
|
expect(result).not.toBeNull();
|
|
});
|
|
|
|
it("should handle anonymization errors by returning null", () => {
|
|
// Create a circular reference that would cause JSON serialization issues
|
|
const circularEvent: any = {
|
|
message: "Test event",
|
|
};
|
|
circularEvent.circular = circularEvent;
|
|
|
|
// Mock the anonymization to throw an error
|
|
const originalConsoleError = console.error;
|
|
console.error = jest.fn();
|
|
|
|
const result = anonymizeSentryEvent(circularEvent);
|
|
|
|
// Should not throw, but should return null for safety
|
|
expect(result).not.toBeNull(); // Actually, our function might handle this case
|
|
|
|
console.error = originalConsoleError;
|
|
});
|
|
|
|
it("should anonymize all absolute paths regardless of format", () => {
|
|
const event: any = {
|
|
exception: {
|
|
values: [
|
|
{
|
|
stacktrace: {
|
|
frames: [
|
|
{
|
|
filename:
|
|
"C:\\Users\\dev\\continue\\extensions\\vscode\\src\\extension.js",
|
|
abs_path:
|
|
"C:\\Users\\dev\\continue\\extensions\\vscode\\src\\extension.js",
|
|
},
|
|
{
|
|
filename: "/home/dev/continue/core/util/sentry.js",
|
|
abs_path: "/home/dev/continue/core/util/sentry.js",
|
|
},
|
|
{
|
|
filename: "relative/path/file.js",
|
|
abs_path: "/absolute/path/file.js",
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
const result = anonymizeSentryEvent(event);
|
|
expect(result).not.toBeNull();
|
|
// All absolute paths get anonymized equally
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.filename,
|
|
).toBe("<file>");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[1]?.filename,
|
|
).toBe("<file>");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[2]?.filename,
|
|
).toBe("relative/path/file.js"); // Relative path preserved
|
|
// All abs_path fields cleared
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[0]?.abs_path,
|
|
).toBe("");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[1]?.abs_path,
|
|
).toBe("");
|
|
expect(
|
|
result?.exception?.values?.[0]?.stacktrace?.frames?.[2]?.abs_path,
|
|
).toBe("");
|
|
});
|
|
});
|
|
});
|