forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Sync high-value upstream fixes: response queue poisoning (GHSA-35p6-xmwp-9g52), multi-byte setEncoding, null queue guard #5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f1050a1
fix(client): validate idle keep-alive sockets before reuse (GHSA-35p6…
ronag da650fc
fix(readable): decode multi-byte UTF-8 split across chunks in setEnco…
ronag 88f723a
fix(client): guard against null queue entries in errorRequest loops
ronag e5cd8e9
fix: address Copilot review feedback
ronag 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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,58 @@ | ||
| 'use strict' | ||
|
|
||
| const { tspl } = require('@matteo.collina/tspl') | ||
| const { test, after } = require('node:test') | ||
| const { createServer } = require('node:http') | ||
| const { Client } = require('..') | ||
|
|
||
| test('setEncoding(\'utf8\') handles 3-byte UTF-8 characters split across chunks', async (t) => { | ||
| t = tspl(t, { plan: 2 }) | ||
|
|
||
| // CJK character '傳' is 3 bytes: 0xe5, 0x82, 0xb3 | ||
| // Build a payload where this character will be split at the chunk boundary | ||
| const cjkChar = '傳' // U+50B3, bytes: e5 82 b3 | ||
| const prefix = 'a'.repeat(10) // 10 ASCII bytes | ||
| const text = prefix + cjkChar + 'end' | ||
| const buf = Buffer.from(text) // 10 + 3 + 3 = 16 bytes | ||
|
|
||
| // Split at byte 11, which is in the middle of the 3-byte CJK character | ||
| // prefix (10 bytes) + first byte of '傳' (0xe5) | remaining 2 bytes (0x82 0xb3) + 'end' | ||
| const chunk1 = buf.subarray(0, 11) | ||
| const chunk2 = buf.subarray(11) | ||
|
|
||
| const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { | ||
| // Send raw buffers to ensure the split is exactly where we want it | ||
| res.writeHead(200, { 'content-type': 'text/plain; charset=utf-8' }) | ||
| res.write(chunk1) | ||
| // Use setTimeout to force separate TCP packets / chunks | ||
| setTimeout(() => { | ||
| res.end(chunk2) | ||
| }, 50) | ||
| }) | ||
| after(() => { | ||
| server.closeAllConnections?.() | ||
| server.close() | ||
| }) | ||
|
|
||
| server.listen(0, async () => { | ||
| const client = new Client(`http://localhost:${server.address().port}`) | ||
| after(client.destroy.bind(client)) | ||
|
|
||
| const { body } = await client.request({ | ||
| path: '/', | ||
| method: 'GET' | ||
| }) | ||
| body.setEncoding('utf8') | ||
|
|
||
| let result = '' | ||
| for await (const chunk of body) { | ||
| result += chunk | ||
| } | ||
|
|
||
| // Must not contain U+FFFD replacement characters | ||
| t.strictEqual(result.includes('\ufffd'), false, 'should not contain U+FFFD replacement characters') | ||
| t.strictEqual(result, text, 'decoded text should match original') | ||
| }) | ||
|
ronag marked this conversation as resolved.
|
||
|
|
||
| await t.completed | ||
| }) | ||
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 assert = require('node:assert') | ||
| const { once } = require('node:events') | ||
| const { createServer } = require('node:http') | ||
| const { after, test } = require('node:test') | ||
| const { Client } = require('..') | ||
|
|
||
| function readBody (body) { | ||
| return new Promise((resolve, reject) => { | ||
| let data = '' | ||
| body.setEncoding('latin1') | ||
| body.on('data', chunk => { data += chunk }) | ||
| body.on('end', () => resolve(data)) | ||
| body.on('error', reject) | ||
| }) | ||
| } | ||
|
|
||
| test('should not reuse an idle socket with buffered unsolicited response bytes', async () => { | ||
| let evilServerSocket | ||
|
|
||
| const server = createServer((req, res) => { | ||
| if (!evilServerSocket) { | ||
| evilServerSocket = req.socket | ||
| } | ||
|
|
||
| res.end(req.url) | ||
| }) | ||
| after(() => server.close()) | ||
|
|
||
| await new Promise(resolve => server.listen(0, resolve)) | ||
|
|
||
| const client = new Client(`http://localhost:${server.address().port}`, { | ||
| keepAliveTimeout: 300e3 | ||
| }) | ||
| after(() => client.close()) | ||
|
|
||
| const response1 = await client.request({ path: '/request1', method: 'GET' }) | ||
| assert.strictEqual(await readBody(response1.body), '/request1') | ||
|
|
||
| const disconnected = once(client, 'disconnect') | ||
|
|
||
| evilServerSocket.write( | ||
| 'HTTP/1.1 200 OK\r\n' + | ||
| 'Poison-Free-Socket: true\r\n' + | ||
| 'Connection: keep-alive\r\n' + | ||
| 'Keep-Alive: timeout=300\r\n' + | ||
| 'Content-Length: 0\r\n' + | ||
| '\r\n' | ||
| ) | ||
|
|
||
| await disconnected | ||
|
|
||
| const response2 = await client.request({ path: '/request2', method: 'GET' }) | ||
| assert.strictEqual(response2.headers['poison-free-socket'], undefined) | ||
| assert.strictEqual(await readBody(response2.body), '/request2') | ||
| }) |
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.