Repository navigation
fix: Only add main process context lines to renderer frames in the app directory #1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import type { Event, Integration } from '@sentry/core'; | ||
| import { isAbsolute, relative, resolve } from 'path'; | ||
|
|
||
| const RENDERER_EVENT_KEY = '__sentry_electron_renderer_event__'; | ||
|
|
||
| /** Marks the event as sent from a renderer process */ | ||
| export function markRendererEvent(event: Event): void { | ||
| event.sdkProcessingMetadata = { ...event.sdkProcessingMetadata, [RENDERER_EVENT_KEY]: true }; | ||
| } | ||
|
|
||
| /** Was the event sent from a renderer process */ | ||
| export function isRendererEvent(event: Event): boolean { | ||
| return event.sdkProcessingMetadata?.[RENDERER_EVENT_KEY] === true; | ||
| } | ||
|
|
||
| function isPathInDirectory(path: string | undefined, directory: string): boolean { | ||
| if (!path || !isAbsolute(path)) { | ||
| return false; | ||
| } | ||
|
|
||
| const rel = relative(resolve(directory), resolve(path)); | ||
| return !!rel && !rel.startsWith('..') && !isAbsolute(rel); | ||
| } | ||
|
|
||
| /** | ||
| * Renderer stack frames can only get main process context lines from files | ||
| * in the app directory, such as preload scripts. | ||
| */ | ||
| export function limitContextLinesForRendererEvents(integration: Integration, getAppPath: () => string): Integration { | ||
| const processEvent = integration.processEvent?.bind(integration); | ||
|
|
||
| if (integration.name !== 'ContextLines' || !processEvent) { | ||
| return integration; | ||
| } | ||
|
|
||
| return { | ||
| ...integration, | ||
| processEvent: async (event, hint, client) => { | ||
| if (!isRendererEvent(event)) { | ||
| return processEvent(event, hint, client); | ||
| } | ||
|
|
||
| const appPath = getAppPath(); | ||
| // ContextLines adds context to the frame objects so the frames in the | ||
| // original event get it too | ||
| const values = (event.exception?.values || []).map((exception) => ({ | ||
| ...exception, | ||
| stacktrace: exception.stacktrace && { | ||
| ...exception.stacktrace, | ||
| frames: exception.stacktrace.frames?.filter((frame) => isPathInDirectory(frame.filename, appPath)), | ||
| }, | ||
| })); | ||
|
|
||
| await processEvent({ ...event, exception: { values } }, hint, client); | ||
|
|
||
| return event; | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import type { Event, Integration, StackFrame } from '@sentry/core'; | ||
| import { join } from 'path'; | ||
| import { describe, expect, test, vi } from 'vitest'; | ||
| import { limitContextLinesForRendererEvents, markRendererEvent } from '../../src/main/renderer-event'; | ||
|
|
||
| const APP_PATH = join(__dirname, 'app'); | ||
|
|
||
| function createIntegration(name: string): { integration: Integration; processEvent: ReturnType<typeof vi.fn> } { | ||
| const processEvent = vi.fn((event: Event) => { | ||
| for (const frame of event.exception?.values?.[0]?.stacktrace?.frames || []) { | ||
| frame.context_line = 'context'; | ||
| } | ||
| return event; | ||
| }); | ||
| return { integration: { name, processEvent }, processEvent }; | ||
| } | ||
|
|
||
| function createEvent(filenames: string[]): Event { | ||
| return { | ||
| exception: { | ||
| values: [{ type: 'Error', stacktrace: { frames: filenames.map((filename) => ({ filename, lineno: 1 })) } }], | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function frames(event: Event | null | PromiseLike<Event | null>): StackFrame[] { | ||
| return (event as Event).exception?.values?.[0]?.stacktrace?.frames || []; | ||
| } | ||
|
|
||
| describe('limitContextLinesForRendererEvents', () => { | ||
| test('Only adds context lines to renderer frames in the app directory', async () => { | ||
| const { integration } = createIntegration('ContextLines'); | ||
| const wrapped = limitContextLinesForRendererEvents(integration, () => APP_PATH); | ||
|
|
||
| const inApp = join(APP_PATH, 'src', 'preload.js'); | ||
| const outsideApp = join(__dirname, 'other', 'file.js'); | ||
| const event = createEvent([ | ||
| inApp, | ||
| outsideApp, | ||
| join(APP_PATH, '..', 'app-other', 'file.js'), | ||
| 'app:///src/index.html', | ||
| ]); | ||
| markRendererEvent(event); | ||
|
|
||
| const result = await wrapped.processEvent?.(event, {}, {} as never); | ||
|
|
||
| expect(result).toBe(event); | ||
| expect(frames(event).map((frame) => frame.context_line)).toEqual(['context', undefined, undefined, undefined]); | ||
| }); | ||
|
|
||
| test('Adds context lines to all frames of other events', async () => { | ||
| const { integration, processEvent } = createIntegration('ContextLines'); | ||
| const wrapped = limitContextLinesForRendererEvents(integration, () => APP_PATH); | ||
|
|
||
| const event = createEvent([join(__dirname, 'other', 'file.js')]); | ||
|
|
||
| const result = await wrapped.processEvent?.(event, {}, {} as never); | ||
|
|
||
| expect(processEvent).toHaveBeenCalledTimes(1); | ||
| expect(frames(result as Event).map((frame) => frame.context_line)).toEqual(['context']); | ||
| }); | ||
|
|
||
| test('Does not change other integrations', () => { | ||
| const { integration } = createIntegration('NormalizePaths'); | ||
|
|
||
| expect(limitContextLinesForRendererEvents(integration, () => APP_PATH)).toBe(integration); | ||
|
timfish marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.