-
-
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.
feat(node): Ensure request bodies are reliably captured for http requ…
…ests (#13746) This PR started out as trying to fix capturing request bodies for Koa. Some investigation later, we found out that the fundamental problem was that we relied on the request body being on `request.body`, which is non-standard and thus does not necessarily works. It seems that in express this works because it under the hood writes the body there, but this is non-standard and rather undefined behavior. For other frameworks (e.g. Koa and probably more) this did not work, the body was not on the request and thus never captured. We also had no test coverage for this overall. This PR ended up doing a few things: * Add tests for this for express and koa * Streamline types for `sdkProcessingMetadata` - this used to be `any`, which lead to any usage of this not really being typed at all. I added proper types for this now. * Generic extraction of the http request body in the http instrumentation - this should now work for any node framework Most importantly, I opted to not force this into the existing, rather complicated and hard to follow request data integration flow. This used to take an IsomorphicRequest and then did a bunch of conversion etc. Since now in Node, we always have the same, proper http request (for any framework, because this always goes through http instrumentation), we can actually streamline this and normalize this properly at the time where we set this. So with this PR, we set a `normalizedRequest` which already has the url, headers etc. set in a way that we need it/it makes sense. Additionally, the parsed & stringified request body will be set on this too. If this normalized request is set in sdkProcessingMetadata, we will use it as source of truth instead of the plain `request`. (Note that we still need the plain request for some auxiliary data that is non-standard, e.g. `request.user`). For the body parsing itself, we monkey-patch `req.on('data')`. this way, we ensure to not add more handlers than a user has, and we only extract the body if the user is extracting it anyhow, ensuring we do not alter behavior. Closes #13722 --------- Co-authored-by: Luca Forstner <luca.forstner@sentry.io>
- Loading branch information
Showing
16 changed files
with
711 additions
and
102 deletions.
There are no files selected for viewing
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
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
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
Oops, something went wrong.