Skip to content

fix(nextjs): Use domains to prevent scope bleed on backend #3574

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 1 commit into from
May 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions packages/nextjs/src/utils/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { deepReadDirSync } from '@sentry/node';
import { hasTracingEnabled } from '@sentry/tracing';
import { Transaction } from '@sentry/types';
import { fill, logger } from '@sentry/utils';
import * as domain from 'domain';
import * as http from 'http';
import { default as createNextServer } from 'next';
import * as url from 'url';
Expand Down Expand Up @@ -173,35 +174,43 @@ function makeWrappedReqHandler(origReqHandler: ReqHandler): WrappedReqHandler {
res: NextResponse,
parsedUrl?: url.UrlWithParsedQuery,
): Promise<void> {
// We only want to record page and API requests
if (hasTracingEnabled() && shouldTraceRequest(req.url, publicDirFiles)) {
const transaction = Sentry.startTransaction({
name: `${(req.method || 'GET').toUpperCase()} ${req.url}`,
op: 'http.server',
});
Sentry.getCurrentHub()
.getScope()
?.setSpan(transaction);

res.__sentry__ = {};
res.__sentry__.transaction = transaction;
}

res.once('finish', () => {
const transaction = res.__sentry__?.transaction;
if (transaction) {
// Push `transaction.finish` to the next event loop so open spans have a chance to finish before the transaction
// closes
setImmediate(() => {
// TODO
// addExpressReqToTransaction(transaction, req);
transaction.setHttpStatus(res.statusCode);
transaction.finish();
// wrap everything in a domain in order to prevent scope bleed between requests
const local = domain.create();
local.add(req);
local.add(res);
// TODO could this replace wrapping the error logger?
// local.on('error', Sentry.captureException);

local.run(() => {
// We only want to record page and API requests
if (hasTracingEnabled() && shouldTraceRequest(req.url, publicDirFiles)) {
const transaction = Sentry.startTransaction({
name: `${(req.method || 'GET').toUpperCase()} ${req.url}`,
op: 'http.server',
});
Sentry.getCurrentHub()
.getScope()
?.setSpan(transaction);

res.__sentry__ = { transaction };

res.once('finish', () => {
const transaction = res.__sentry__?.transaction;
if (transaction) {
// Push `transaction.finish` to the next event loop so open spans have a chance to finish before the transaction
// closes
setImmediate(() => {
// TODO
// addExpressReqToTransaction(transaction, req);
transaction.setHttpStatus(res.statusCode);
transaction.finish();
});
}
});

return origReqHandler.call(this, req, res, parsedUrl);
}
});

return origReqHandler.call(this, req, res, parsedUrl);
};

return wrappedReqHandler;
Expand Down