-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
withSentry in nextjs can not be configured to scrub cookies (sensitive data) #4723
Comments
Hi, @wereHamster. Thanks for bringing this up. We'll chat as a team to see how we best want to handle this. In the meantime, as a workaround, you can add an event processor, and it'll work just like Sentry.addGlobalEventProcessor(event => {
// make any changes to event data that you'd like here
return event;
}); The one drawback is that in order to make sure that the processor runs after |
Do I really have to do it for each request? Can't I call |
Yes, and no, unfortunately you can't. I wouldn't be calling it a workaround in that case, LOL! The reason is that if you do that, In the long run, my team agrees that it's reasonable to add the options from the original |
Couldn't get it to work with the global event processor, but this appears to work (uses the same means to add the event processor as the code in withSentry, getCurrentHub().currentScope().addEventProcessor()): /**
* A wrapper around Sentry.withSentry() that removes sensitive data (cookies)
* from the event.
*
* Update this function once there is a nicer way to do that.
* https://github.com/getsentry/sentry-javascript/issues/4723.
*/
export const withSentry = (origHandler: NextApiHandler): WrappedNextApiHandler => {
function sanitizeEvent(event: Sentry.Event) {
if (event.request) {
delete event.request.cookies;
delete event.request.headers;
}
return event;
}
return Sentry.withSentry((req, res) => {
Sentry.getCurrentHub().getScope()?.addEventProcessor(sanitizeEvent);
return origHandler(req, res);
});
}; |
Nice, thanks for sharing, @wereHamster! |
Hi @lobsterkatie, I'm running into this issue as well except that I'm not using Would you like me to log a separate issue for this since it's not specific to using |
I also tried to scrub some data with |
there is now beforeSendTransaction which should be able to resolve this issue where beforeSend cannot be used for transaction events. Hopefully this makes things much simpler. available with latest versions since 7.18.0 I will close this issue, please reopen if you need anything else! |
Actually, you can prevent the data from being attached in the first place using the TL;DR, you're going to want to do something like this in your import { Integrations } from '@sentry/nextjs';
const { RequestData } = Integrations
Sentry.init({
integrations: [ new RequestData({
include: {
cookies: false,
},
})],
}); |
Problem Statement
All I want is to scrub certain (or all) cookies from the events sent to Sentry. Basic data scrubbing of sensitive fields.
The
beforeSend
hook is never called. Don't know why, but I saw in the code that thebeforeSend
hook is not called when the event type is transaction, and all events I see go through the sentry code are transactions.I'm using
withSentry
from the@sentry/nextjs
package, which internally callsparseRequest
that's responsible for extracting the relevant sensitive data from the request. TheparseRequest
function accepts (optional) options, that AFAICS can be used to limit what keys are extracted from the request (defaults include cookies). ThewithSentry
function however does not allow passing any options toparseRequest
.Solution Brainstorm
Allow options to be passed to
withSentry
to allow it to override what keys are extracted byextractRequestData
. TherequestHandler
function can be configured in such a way, for example.Or provide a hook that's called on /all/ events before they are sent to Sentry, not just some.
The text was updated successfully, but these errors were encountered: