-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
http: fix just-merged change that could silently truncate responses #64507
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
Open
pimterry
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
pimterry:fix-64278
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−15
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
42 changes: 42 additions & 0 deletions
42
test/parallel/test-http-client-complete-response-open-request-reset.js
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||||||
| 'use strict'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const common = require('../common'); | ||||||||||||||||||||||
| const assert = require('assert'); | ||||||||||||||||||||||
| const http = require('http'); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const BODY = Buffer.alloc(1024 * 1024); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const server = http.createServer(common.mustCall((request, response) => { | ||||||||||||||||||||||
| response.writeHead(202, { 'content-length': 0 }); | ||||||||||||||||||||||
| response.end(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| request.once('data', common.mustCall(() => { | ||||||||||||||||||||||
| request.socket.resetAndDestroy(); | ||||||||||||||||||||||
| })); | ||||||||||||||||||||||
| })); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| server.on('clientError', common.mustNotCall()); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| server.listen(0, common.mustCall(() => { | ||||||||||||||||||||||
| let response; | ||||||||||||||||||||||
| const req = http.request({ | ||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||
| port: server.address().port, | ||||||||||||||||||||||
| headers: { 'content-length': BODY.length }, | ||||||||||||||||||||||
| }, common.mustCall((res) => { | ||||||||||||||||||||||
| response = res; | ||||||||||||||||||||||
| assert.strictEqual(res.statusCode, 202); | ||||||||||||||||||||||
| assert.strictEqual(req.writableEnded, false); | ||||||||||||||||||||||
| req.write(BODY); | ||||||||||||||||||||||
| })); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| req.on('error', common.mustCall((err) => { | ||||||||||||||||||||||
| assert(response); | ||||||||||||||||||||||
| assert.strictEqual(response.complete, true); | ||||||||||||||||||||||
| assert.strictEqual(req.writableFinished, false); | ||||||||||||||||||||||
| assert.strictEqual(err.code, 'ECONNRESET'); | ||||||||||||||||||||||
|
Check failure on line 37 in test/parallel/test-http-client-complete-response-open-request-reset.js
|
||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about replace using this ?
Suggested change
|
||||||||||||||||||||||
| })); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| req.on('close', common.mustCall(() => server.close())); | ||||||||||||||||||||||
| req.flushHeaders(); | ||||||||||||||||||||||
| })); | ||||||||||||||||||||||
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
test/parallel/test-http-client-incomplete-response-reset.js
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| // A response that is cut short by a connection reset must still surface the | ||
| // socket error on the request. The response exists, but it is not complete, so | ||
| // swallowing the error would leave the application with a silently truncated | ||
| // body it believes is intact. | ||
|
|
||
| const LENGTH = 1024; | ||
|
|
||
| let serverSocket; | ||
|
|
||
| const server = http.createServer(common.mustCall((request, response) => { | ||
| serverSocket = request.socket; | ||
| // Promise more body than is ever sent. | ||
| response.writeHead(200, { 'content-length': LENGTH }); | ||
| response.write('hello'); | ||
| })); | ||
|
|
||
| server.on('clientError', common.mustNotCall()); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| const req = http.request({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.strictEqual(res.statusCode, 200); | ||
|
|
||
| let received = 0; | ||
| let reset = false; | ||
|
|
||
| res.on('data', common.mustCallAtLeast((chunk) => { | ||
| received += chunk.length; | ||
| if (reset) return; | ||
| reset = true; | ||
| // The headers and part of the body have arrived. Reset from the server | ||
| // side so the client sees a genuine inbound RST mid-body. | ||
| serverSocket.resetAndDestroy(); | ||
| }, 1)); | ||
|
|
||
| res.on('end', common.mustNotCall()); | ||
| res.on('close', common.mustCall(() => { | ||
| assert.strictEqual(res.complete, false); | ||
| assert.strictEqual(res.errored.code, 'ECONNRESET'); | ||
| assert.strictEqual(res.errored.message, 'aborted'); | ||
| assert.ok(received > 0 && received < LENGTH, | ||
| `expected a truncated body, got ${received} of ${LENGTH}`); | ||
| server.close(); | ||
| })); | ||
| })); | ||
|
|
||
| req.on('error', common.mustCall((err) => { | ||
| assert.strictEqual(err.code, 'ECONNRESET'); | ||
| })); | ||
|
|
||
| req.end(); | ||
| })); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to metion that, I do these kind of check in my first version of commit ,but it would failed to pass macos tests and it seems that the macos platform has a bit difference.I'm sorry for that I don't deep dive this beacuse I don't have macos device to check the operating system real behavior. I hope this could be a kind of helpful reminder for you. I just think it maybe not easily to fix just check the req status