-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(node): Capture SystemError
context and remove paths from message
#17331
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
base: develop
Are you sure you want to change the base?
Changes from all commits
1c8810d
8cc1e81
6801593
c11eb51
0bc0789
5a93302
636d955
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'); |
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'); |
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(); | ||
}); | ||
}); |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should expose an integration option to control this replacement as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally I would have used |
||
} | ||
if (error.dest && exception.value.includes(error.dest)) { | ||
exception.value = exception.value.replace(`'${error.dest}'`, '').trim(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Error Message Parsing Logic MismatchInconsistent logic for stripping |
||
} | ||
} | ||
|
||
return event; | ||
}, | ||
}; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ import { onUncaughtExceptionIntegration } from '../integrations/onuncaughtexcept | |
import { onUnhandledRejectionIntegration } from '../integrations/onunhandledrejection'; | ||
import { processSessionIntegration } from '../integrations/processSession'; | ||
import { INTEGRATION_NAME as SPOTLIGHT_INTEGRATION_NAME, spotlightIntegration } from '../integrations/spotlight'; | ||
import { systemErrorIntegration } from '../integrations/systemError'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we'll have to export this from all of our node packages as well |
||
import { makeNodeTransport } from '../transports'; | ||
import type { NodeClientOptions, NodeOptions } from '../types'; | ||
import { isCjs } from '../utils/commonjs'; | ||
|
@@ -52,6 +53,7 @@ export function getDefaultIntegrations(): Integration[] { | |
functionToStringIntegration(), | ||
linkedErrorsIntegration(), | ||
requestDataIntegration(), | ||
systemErrorIntegration(), | ||
// Native Wrappers | ||
consoleIntegration(), | ||
httpIntegration(), | ||
|
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: Incomplete Error Context Causes Type Mismatch
The
NodeSystemError
integration'sSystemErrorContext
type is incomplete, missingcode
andsyscall
properties found on Node.jsSystemError
objects. TheerrorContext
is populated using a spread operator (...error
), which copies all enumerable properties from the originalError
object. This creates a type mismatch between the declared type and the actual captured data. It also captures unintended properties (e.g.,message
,stack
,name
,code
,syscall
), potentially exposing sensitive data beyond whatsendDefaultPii
is designed to filter (path
,dest
).Additional Locations (1)
packages/node-core/src/integrations/systemError.ts#L40-L43
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.
The others dont add anything to the type because we're not using them