-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node): Ensure express requests are properly handled (#14850)
Fixes #14847 After some digging, I figured out that the `req.user` handling on express is not working anymore, because apparently express does not mutate the actual http `request` object, but clones/forkes it (??) somehow. So since we now set the request in the SentryHttpInstrumentation generally, it would not pick up express-specific things anymore. IMHO this is not great and we do not want this anymore in v9 anyhow, but it was the behavior before. This PR fixes this by setting the express request again on the isolation scope in an express middleware, which is registered by `Sentry.setupExpressErrorHandler(app)`. Note that we plan to change this behavior here: #14806 but I figured it still makes sense to fix this on develop first, so we have a proper history/tests for this. I will backport this to v8 too. Then, in PR #14806 I will remove the middleware again.
- Loading branch information
Showing
4 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
dev-packages/node-integration-tests/suites/express/requestUser/server.js
This file contains 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,49 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
transport: loggingTransport, | ||
debug: true, | ||
}); | ||
|
||
// express must be required after Sentry is initialized | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); | ||
|
||
const app = express(); | ||
|
||
app.use(cors()); | ||
|
||
app.use((req, _res, next) => { | ||
// We simulate this, which would in other cases be done by some middleware | ||
req.user = { | ||
id: '1', | ||
email: 'test@sentry.io', | ||
}; | ||
|
||
next(); | ||
}); | ||
|
||
app.get('/test1', (_req, _res) => { | ||
throw new Error('error_1'); | ||
}); | ||
|
||
app.use((_req, _res, next) => { | ||
Sentry.setUser({ | ||
id: '2', | ||
email: 'test2@sentry.io', | ||
}); | ||
|
||
next(); | ||
}); | ||
|
||
app.get('/test2', (_req, _res) => { | ||
throw new Error('error_2'); | ||
}); | ||
|
||
Sentry.setupExpressErrorHandler(app); | ||
|
||
startExpressServerAndSendPortToRunner(app); |
49 changes: 49 additions & 0 deletions
49
dev-packages/node-integration-tests/suites/express/requestUser/test.ts
This file contains 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,49 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
describe('express user handling', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('picks user from request', done => { | ||
createRunner(__dirname, 'server.js') | ||
.expect({ | ||
event: { | ||
user: { | ||
id: '1', | ||
email: 'test@sentry.io', | ||
}, | ||
exception: { | ||
values: [ | ||
{ | ||
value: 'error_1', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done) | ||
.makeRequest('get', '/test1', { expectError: true }); | ||
}); | ||
|
||
test('setUser overwrites user from request', done => { | ||
createRunner(__dirname, 'server.js') | ||
.expect({ | ||
event: { | ||
user: { | ||
id: '2', | ||
email: 'test2@sentry.io', | ||
}, | ||
exception: { | ||
values: [ | ||
{ | ||
value: 'error_2', | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
.start(done) | ||
.makeRequest('get', '/test2', { expectError: true }); | ||
}); | ||
}); |
This file contains 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 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