This repository has been archived by the owner on Dec 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Ensure the request body buffer is not exhausted #4
Merged
Conversation
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
Was notified of the issue when @dlapiduz pinged me about POST requests to https://tock.18f.gov/ resulting in 500s. Digging in the oauth2_proxy logs, I noticed messages like this appearing before every 500: ``` 2015/10/09 16:08:15 reverseproxy.go:184: http: proxy error: http: ContentLength=128 with Body length 0 ``` Searching for that error string turned up: https://stackoverflow.com/questions/23070876/reading-body-of-http-request-without-modifying-request-state The issue was that reading from `http.Request.Body` to generate the request signature was exhausting the `io.ReadCloser`. The solution was to read the entire body into a new buffer, wrap that buffer in an `ioutil.NopCloser` and assign it back to `http.Request.Body`, and then use that same buffer to compute the signature. The new assert statement in `TestRequestSignaturePost` reproduced the original bug and verified the fix.
jcscottiii
added a commit
that referenced
this pull request
Oct 9, 2015
Ensure the request body buffer is not exhausted
mbland
pushed a commit
to cloud-gov/oauth2_proxy
that referenced
this pull request
Oct 10, 2015
Using fakeNetConn in the test exposes a bug in 18F/hmacauth when handling POST requests, addressed by 18F/hmacauth#4. The bug was that the strings.Reader does not consume its buffer contents the same way that a net.Conn does. So the test would pass because its request body would still be intact after signing, but during live testing, the request body would be consumed by HmacAuth.requestSignature(). It also happened to expose a subsequent 18F/hmacauth bug addressed in 18F/hmacauth#5. It turns out that checking Request.ContentLength is an unreliable way of detecting that a body is present, and checking body != nil is sufficient. 18F/hmacauth#4 is already merged; when 18F/hmacauth#5 is in, I'll tag 18F/hmacauth v1.0.1 and update the Godeps to use that version, at which point the test should pass.
mbland
pushed a commit
to cloud-gov/oauth2_proxy
that referenced
this pull request
Oct 13, 2015
v1.0.1 contains 18F/hmacauth#4 and 18F/hmacauth#5, needed to make TestRequestSignaturePostRequest pass again.
mbland
pushed a commit
to cloud-gov/oauth2_proxy
that referenced
this pull request
Nov 9, 2015
Using fakeNetConn in the test exposes a bug in 18F/hmacauth when handling POST requests, addressed by 18F/hmacauth#4. The bug was that the strings.Reader does not consume its buffer contents the same way that a net.Conn does. So the test would pass because its request body would still be intact after signing, but during live testing, the request body would be consumed by HmacAuth.requestSignature(). It also happened to expose a subsequent 18F/hmacauth bug addressed in 18F/hmacauth#5. It turns out that checking Request.ContentLength is an unreliable way of detecting that a body is present, and checking body != nil is sufficient. 18F/hmacauth#4 is already merged; when 18F/hmacauth#5 is in, I'll tag 18F/hmacauth v1.0.1 and update the Godeps to use that version, at which point the test should pass.
mbland
pushed a commit
to cloud-gov/oauth2_proxy
that referenced
this pull request
Nov 9, 2015
v1.0.1 contains 18F/hmacauth#4 and 18F/hmacauth#5, needed to make TestRequestSignaturePostRequest pass again.
mbland
pushed a commit
to cloud-gov/oauth2_proxy
that referenced
this pull request
Nov 9, 2015
Using fakeNetConn in the test exposes a bug in 18F/hmacauth when handling POST requests, addressed by 18F/hmacauth#4. The bug was that the strings.Reader does not consume its buffer contents the same way that a net.Conn does. So the test would pass because its request body would still be intact after signing, but during live testing, the request body would be consumed by HmacAuth.requestSignature(). It also happened to expose a subsequent 18F/hmacauth bug addressed in 18F/hmacauth#5. It turns out that checking Request.ContentLength is an unreliable way of detecting that a body is present, and checking body != nil is sufficient. 18F/hmacauth#4 is already merged; when 18F/hmacauth#5 is in, I'll tag 18F/hmacauth v1.0.1 and update the Godeps to use that version, at which point the test should pass.
mbland
pushed a commit
to cloud-gov/oauth2_proxy
that referenced
this pull request
Nov 9, 2015
v1.0.1 contains 18F/hmacauth#4 and 18F/hmacauth#5, needed to make TestRequestSignaturePostRequest pass again.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Was notified of the issue when @dlapiduz pinged me about POST requests
to https://tock.18f.gov/ resulting in 500s. Digging in the oauth2_proxy logs,
I noticed messages like this appearing before every 500:
Searching for that error string turned up:
https://stackoverflow.com/questions/23070876/reading-body-of-http-request-without-modifying-request-state
The issue was that reading from
http.Request.Body
to generate the requestsignature was exhausting the
io.ReadCloser
. The solution was to read theentire body into a new buffer, wrap that buffer in an
ioutil.NopCloser
andassign it back to
http.Request.Body
, and then use that same buffer tocompute the signature.
The new assert statement in
TestRequestSignaturePost
reproduced the originalbug and verified the fix.
cc: @jcscottiii