Repository navigation
Conversation
The reader goroutine passes the queued request method through so HEAD responses are read without a body. Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Response.Read delegates to ReadMethod, which skips interim 1xx blocks (except 101) and returns an empty body for HEAD, 1xx, 204 and 304. Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Wire-level and client-level tests for 204, 304, 100 Continue and HEAD. Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
dwisiswant0
left a comment
There was a problem hiding this comment.
This PR and #549 fix the same bug (#550) on the same lines, clientpipeline/client.go:550 and Response.Read, so only one of them can merge as written. #549's head (d89f80b) already skips interim 1xx responses. It also covers the raw client, 2xx responses to CONNECT, and a cap of five interim responses. I'd consolidate on #549. If this PR handles a case that #549 misses, please point it out there.
If this PR lands instead, nothing in the code blocks it, but it should link the issue with Fixes #550.
suggestion (non-blocking): Squash a3540a6 into 0a30c31. a3540a6 calls ReadMethod before 0a30c31 defines it, so that commit doesn't build on its own and would break git bisect if the PR merges with a merge commit.
| } | ||
| } | ||
| if err = w.resp.Read(br); err != nil { | ||
| if err = w.resp.ReadMethod(br, w.req.Method); err != nil { |
There was a problem hiding this comment.
praise: Taking the method from the same pipelineWork that the reader fills keeps each HEAD check tied to its own response. chR is FIFO, so the method always comes from the request this response answers, with no shared state to drift.
| // Content-Length describes what a GET would have returned, so waiting for | ||
| // those bytes would stall the connection and misalign every pipelined | ||
| // response after it. Interim 1xx responses (except 101) are skipped. | ||
| func (resp *Response) ReadMethod(r *bufio.Reader, method string) error { |
There was a problem hiding this comment.
suggestion (non-blocking): Rename ReadMethod, for example to ReadWithMethod as in #549.
The other Read* methods on Response (ReadVersion, ReadStatusCode, ReadStatusLine, ReadHeader, ReadBody) each read the named part from the wire, so ReadMethod reads as "read the method". It's a new exported name, so it's cheapest to settle before a release.
| // do 1xx, 204 and 304 responses (RFC 9110 s6.3 and s6.4.1); their | ||
| // Content-Length describes what a GET would have returned, so waiting for | ||
| // those bytes would stall the connection and misalign every pipelined |
There was a problem hiding this comment.
todo: Cite RFC 9112 section 6.3 for the body-length rule. RFC 9110 section 6.3 is "Header Fields".
RFC 9110 section 8.6 also forbids Content-Length on 1xx and 204 responses, so only a HEAD or 304 Content-Length describes a GET. The test comment at bodyless_test.go:16 has the same citation.
| // do 1xx, 204 and 304 responses (RFC 9110 s6.3 and s6.4.1); their | |
| // Content-Length describes what a GET would have returned, so waiting for | |
| // those bytes would stall the connection and misalign every pipelined | |
| // do 1xx, 204 and 304 responses (RFC 9112 s6.3, RFC 9110 s6.4.1). A HEAD | |
| // or 304 Content-Length describes what a GET would have returned, so waiting | |
| // for those bytes would stall the connection and misalign every pipelined |
| if method == http.MethodHead { | ||
| return false | ||
| } | ||
| return code >= 200 && code != 204 && code != 304 |
There was a problem hiding this comment.
issue (non-blocking): A response with a status code below 100 now loses its body and misaligns the connection.
ReadStatusCode returns 0–99 for a 0xx code or a non-numeric one such as ABC, and the loop above treats those codes as final. code >= 200 then makes them bodyless. For HTTP/1.1 000 X\r\nContent-Length: 5\r\n\r\nhello, the five body bytes stay in br, and the next read on the connection fails in ReadVersion, which fails the requests already sent on it. Before this PR, the body was read. net/http allows a body for these codes: its bodyAllowedForStatus excludes only 1xx, 204 and 304.
| return code >= 200 && code != 204 && code != 304 | |
| return (code < 100 || code >= 200) && code != 204 && code != 304 |
| wire := "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n" | ||
| br := bufio.NewReader(strings.NewReader(wire)) | ||
|
|
||
| var resp Response | ||
| require.NoError(t, resp.ReadMethod(br, http.MethodHead)) | ||
| body, err := io.ReadAll(resp.Body) | ||
| require.NoError(t, err) | ||
| require.Empty(t, body, "a HEAD response has no body even when Content-Length is set") | ||
| require.Zero(t, br.Buffered(), "the reader must not wait for body bytes") |
There was a problem hiding this comment.
suggestion (non-blocking, test): Put a second response after the HEAD response so the test covers the desync that #550 reports.
With nothing after the headers, this test can't tell whether the reader takes bytes from the next response. In TestPipelineClientHeadDoesNotHang, the two Do calls run one after the other, so the HEAD read never sees the GET's bytes. That test's GET response also claims Content-Length: 2 with no body, so its second check passes with or without the fix. A 304 with Content-Length in TestReadBodylessStatusesLeaveConnectionAligned would cover the same case for status codes.
| wire := "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n" | |
| br := bufio.NewReader(strings.NewReader(wire)) | |
| var resp Response | |
| require.NoError(t, resp.ReadMethod(br, http.MethodHead)) | |
| body, err := io.ReadAll(resp.Body) | |
| require.NoError(t, err) | |
| require.Empty(t, body, "a HEAD response has no body even when Content-Length is set") | |
| require.Zero(t, br.Buffered(), "the reader must not wait for body bytes") | |
| wire := "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n" + | |
| "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok" | |
| br := bufio.NewReader(strings.NewReader(wire)) | |
| var resp Response | |
| require.NoError(t, resp.ReadMethod(br, http.MethodHead)) | |
| body, err := io.ReadAll(resp.Body) | |
| require.NoError(t, err) | |
| require.Empty(t, body, "a HEAD response has no body even when Content-Length is set") | |
| var next Response | |
| require.NoError(t, next.ReadMethod(br, http.MethodGet)) | |
| nextBody, err := io.ReadAll(next.Body) | |
| require.NoError(t, err) | |
| require.Equal(t, "ok", string(nextBody), "a HEAD response must not take the next response's bytes") |
The pipelined response reader (
clientpipeline) decides how much body to read purely fromContent-Length/Transfer-Encoding. Per RFC 9110 (s6.3, s6.4.1), responses to HEAD and all 1xx, 204 and 304 responses carry no body regardless of those headers. This causes two failures on a live pipeline:Content-LengthleavesBodyreading the raw connection. The caller blocks until the read timeout (~10s in nuclei's default config, matching the report) and meanwhile consumes the bytes of the next pipelined response as this response's body, so every subsequent response on the connection is misaligned.Content-Lengtha GET would have returned, so the reader waits for bytes the server never sends. Same stall, same misalignment.This is the root cause of projectdiscovery/nuclei#7953 (pipelined templates hang ~10s and fail to match against 204/304/1xx/HEAD responses).
The fix:
Response.Readnow delegates to a newResponse.ReadMethod(r, method), which skips interim 1xx status blocks (except 101 Switching Protocols) and gives HEAD, 1xx, 204 and 304 responses an empty body without touching the connection stream.Read(r)keeps its existing signature and behavior for callers without a method (still applies the 1xx/204/304 rules).