Skip to content
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

Fix #180355662 Body not being logged correctly #23

Merged
merged 2 commits into from
Nov 19, 2021
Merged

Conversation

bakennedy
Copy link
Contributor

https://www.pivotaltracker.com/n/projects/1646401/stories/180355662

http.Request.Body is an implementaition of the interface io.ReadCloser, this means you can read it and close it, and that's all it guarantees. Where our bug comes in is that both the customer's server will read request.Body and then if body logging is enabled, our middleware will read request.Body, but there is no Seek implementation on this or anything. Body is a stream which cannot be reversed and re-read. The only reason it worked in my testing earlier is that our example app doesn't read the body itself. When you read the body in the example handler it reproduces the customers bug

@bakennedy bakennedy requested review from dkm199 and keyur9 November 19, 2021 06:32
@bakennedy bakennedy self-assigned this Nov 19, 2021
@bakennedy
Copy link
Contributor Author

https://www.moesif.com/wrap/app/640:128-617:188/search/events/dates/-7d contains some example data from my testing both reading and not reading the request body using the original code and then using this buffered two ReadCloser approach

@dgilling dgilling merged commit 3be5377 into master Nov 19, 2021
@bakennedy bakennedy deleted the bk-log-request-body branch November 19, 2021 07:32
Copy link
Member

@keyur9 keyur9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bakennedy please make the changes and bump the version.

if err = b.Close(); err != nil {
return nil, b, err
}
return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bakennedy the correct package here is ioutil which has NopCloser function. It should be ioutil.NopCloser instead of io.NopCloser. This version won't get installed as it'll throw undefined error on io.NopCloser.

@bakennedy
Copy link
Contributor Author

bakennedy commented Nov 19, 2021 via email

@keyur9
Copy link
Member

keyur9 commented Nov 19, 2021

It's not backward compatible, so if anyone using go1.14.3 and similar, it'd throw an error while importing [email protected] package. In earlier go version, ReadAll, NopCloser, and all are still part of ioutil package. So this would break for existing users who are on not on the latest go version.

@bakennedy
Copy link
Contributor Author

bakennedy commented Nov 19, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants