-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(v9/node): Capture SystemError
context and remove paths from message
#17394
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
+165
−0
Merged
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions
11
dev-packages/node-core-integration-tests/suites/system-error/basic-pii.mjs
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,11 @@ | ||
import * as Sentry from '@sentry/node-core'; | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import { readFileSync } from 'fs'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
transport: loggingTransport, | ||
sendDefaultPii: true, | ||
}); | ||
|
||
readFileSync('non-existent-file.txt'); |
10 changes: 10 additions & 0 deletions
10
dev-packages/node-core-integration-tests/suites/system-error/basic.mjs
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,10 @@ | ||
import * as Sentry from '@sentry/node-core'; | ||
import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
import { readFileSync } from 'fs'; | ||
|
||
Sentry.init({ | ||
dsn: 'https://[email protected]/1337', | ||
transport: loggingTransport, | ||
}); | ||
|
||
readFileSync('non-existent-file.txt'); |
59 changes: 59 additions & 0 deletions
59
dev-packages/node-core-integration-tests/suites/system-error/test.ts
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 { afterAll, describe, test } from 'vitest'; | ||
import { cleanupChildProcesses, createRunner } from '../../utils/runner'; | ||
|
||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
describe('SystemError integration', () => { | ||
test('sendDefaultPii: false', async () => { | ||
await createRunner(__dirname, 'basic.mjs') | ||
.expect({ | ||
event: { | ||
contexts: { | ||
node_system_error: { | ||
errno: -2, | ||
code: 'ENOENT', | ||
syscall: 'open', | ||
}, | ||
}, | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'ENOENT: no such file or directory, open', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start() | ||
.completed(); | ||
}); | ||
|
||
test('sendDefaultPii: true', async () => { | ||
await createRunner(__dirname, 'basic-pii.mjs') | ||
.expect({ | ||
event: { | ||
contexts: { | ||
node_system_error: { | ||
errno: -2, | ||
code: 'ENOENT', | ||
syscall: 'open', | ||
path: 'non-existent-file.txt', | ||
}, | ||
}, | ||
exception: { | ||
values: [ | ||
{ | ||
type: 'Error', | ||
value: 'ENOENT: no such file or directory, open', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start() | ||
.completed(); | ||
}); | ||
}); |
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
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
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,76 @@ | ||
import * as util from 'node:util'; | ||
import { defineIntegration } from '@sentry/core'; | ||
|
||
const INTEGRATION_NAME = 'NodeSystemError'; | ||
|
||
type SystemErrorContext = { | ||
dest?: string; // If present, the file path destination when reporting a file system error | ||
errno: number; // The system-provided error number | ||
path?: string; // If present, the file path when reporting a file system error | ||
}; | ||
|
||
type SystemError = Error & SystemErrorContext; | ||
|
||
function isSystemError(error: unknown): error is SystemError { | ||
if (!(error instanceof Error)) { | ||
return false; | ||
} | ||
|
||
if (!('errno' in error) || typeof error.errno !== 'number') { | ||
return false; | ||
} | ||
|
||
// Appears this is the recommended way to check for Node.js SystemError | ||
// https://github.com/nodejs/node/issues/46869 | ||
return util.getSystemErrorMap().has(error.errno); | ||
} | ||
|
||
type Options = { | ||
/** | ||
* If true, includes the `path` and `dest` properties in the error context. | ||
*/ | ||
includePaths?: boolean; | ||
}; | ||
|
||
/** | ||
* Captures context for Node.js SystemError errors. | ||
*/ | ||
export const systemErrorIntegration = defineIntegration((options: Options = {}) => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
processEvent: (event, hint, client) => { | ||
if (!isSystemError(hint.originalException)) { | ||
return event; | ||
} | ||
|
||
const error = hint.originalException; | ||
|
||
const errorContext: SystemErrorContext = { | ||
...error, | ||
}; | ||
|
||
if (!client.getOptions().sendDefaultPii && options.includePaths !== true) { | ||
delete errorContext.path; | ||
delete errorContext.dest; | ||
} | ||
|
||
event.contexts = { | ||
...event.contexts, | ||
node_system_error: errorContext, | ||
}; | ||
|
||
for (const exception of event.exception?.values || []) { | ||
if (exception.value) { | ||
if (error.path && exception.value.includes(error.path)) { | ||
exception.value = exception.value.replace(`'${error.path}'`, '').trim(); | ||
} | ||
if (error.dest && exception.value.includes(error.dest)) { | ||
exception.value = exception.value.replace(`'${error.dest}'`, '').trim(); | ||
} | ||
} | ||
} | ||
|
||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Error Message Handling and Context Population Issues
The
systemErrorIntegration
has two distinct issues:path
anddest
from exception messages assumes they are wrapped in single quotes and usesreplace()
which only removes the first occurrence. This fails if paths are unquoted, use different quotes, contain single quotes, or appear multiple times, potentially leaving malformed messages.node_system_error
context is populated by spreading the entireError
object, including baseError
properties likename
,message
, andstack
. This adds unnecessary data and can lead to unintended information leakage. Only SystemError-specific properties should be included.