Repository navigation
tls: coalesce buffered records into one read #66698
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
ronag
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
ronag:codex/tls-coalesce-buffered-records
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.
+313
−31
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
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,130 @@ | ||
| 'use strict'; | ||
| // TLS records that are already buffered are emitted as one chunk rather than | ||
| // one chunk per record. The end of the stream, clean or not, must still be | ||
| // reported after the data that precedes it. | ||
|
|
||
| const common = require('../common'); | ||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
|
|
||
| const { isBoringSSL } = require('../common/crypto'); | ||
| const assert = require('assert'); | ||
| const net = require('net'); | ||
| const tls = require('tls'); | ||
| const fixtures = require('../common/fixtures'); | ||
|
|
||
| const RECORD_SIZE = 512; | ||
| const RECORDS = 256; | ||
| const payload = Buffer.alloc(RECORD_SIZE * RECORDS); | ||
| for (let i = 0; i < RECORDS; i++) | ||
| payload.fill(i, i * RECORD_SIZE, (i + 1) * RECORD_SIZE); | ||
|
|
||
| const serverOptions = { | ||
| key: fixtures.readKey('agent1-key.pem'), | ||
| cert: fixtures.readKey('agent1-cert.pem'), | ||
| }; | ||
| const clientOptions = { | ||
| ca: fixtures.readKey('ca1-cert.pem'), | ||
| servername: 'agent1', | ||
| }; | ||
|
|
||
| function sendPayload(socket) { | ||
| assert(socket.setMaxSendFragment(RECORD_SIZE)); | ||
| socket.end(payload); | ||
| } | ||
|
|
||
| // Small records are coalesced, and 'end' follows all of the data. | ||
| { | ||
| const server = tls.createServer(serverOptions, common.mustCall(sendPayload)); | ||
| server.listen(0, common.mustCall(() => { | ||
| const chunks = []; | ||
| const client = tls.connect({ ...clientOptions, port: server.address().port }); | ||
| client.on('data', (chunk) => chunks.push(chunk)); | ||
| client.on('end', common.mustCall(() => { | ||
| assert.deepStrictEqual(Buffer.concat(chunks), payload); | ||
| assert(chunks.length < RECORDS, `${chunks.length} chunks`); | ||
| assert(chunks.every((chunk) => chunk.length <= 16 * 1024)); | ||
| server.close(); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| // An onread buffer smaller than a record still receives every byte. | ||
| { | ||
| const server = tls.createServer(serverOptions, common.mustCall(sendPayload)); | ||
| server.listen(0, common.mustCall(() => { | ||
| const chunks = []; | ||
| const client = tls.connect({ | ||
| ...clientOptions, | ||
| port: server.address().port, | ||
| onread: { | ||
| buffer: Buffer.alloc(100), | ||
| callback(nread, buf) { | ||
| chunks.push(Buffer.from(buf.subarray(0, nread))); | ||
| }, | ||
| }, | ||
| }); | ||
| client.on('end', common.mustCall(() => { | ||
| assert.deepStrictEqual(Buffer.concat(chunks), payload); | ||
| server.close(); | ||
| })); | ||
| })); | ||
| } | ||
|
|
||
| // A bad record that arrives together with data is reported after the data. | ||
| { | ||
| // Small enough to arrive in one read and to fit into one batch. | ||
| const data = payload.subarray(0, 8 * RECORD_SIZE); | ||
| const server = tls.createServer(serverOptions, common.mustCall((socket) => { | ||
| socket.once('data', common.mustCall(() => { | ||
| assert(socket.setMaxSendFragment(RECORD_SIZE)); | ||
| socket.end(data); | ||
| })); | ||
| })); | ||
| server.listen(0, common.mustCall(() => { | ||
| // Passes the handshake through, then holds the response until the server | ||
| // ends and forwards it in one write, with the last data record (the one | ||
| // before close_notify) corrupted. | ||
| let holding = false; | ||
| const held = []; | ||
| const proxy = net.createServer(common.mustCall((socket) => { | ||
| const upstream = net.connect(server.address().port); | ||
| socket.pipe(upstream); | ||
| socket.on('error', () => {}); | ||
| upstream.on('data', (chunk) => { | ||
| if (holding) | ||
| held.push(chunk); | ||
| else | ||
| socket.write(chunk); | ||
| }); | ||
| upstream.on('end', common.mustCall(() => { | ||
| const response = Buffer.concat(held); | ||
| let closeNotify = 0; | ||
| for (let i = 0; i < response.length; i += 5 + response.readUInt16BE(i + 3)) | ||
| closeNotify = i; | ||
| response[closeNotify - 1] ^= 1; | ||
| socket.end(response); | ||
| })); | ||
| })); | ||
| proxy.listen(0, common.mustCall(() => { | ||
| const chunks = []; | ||
| const client = tls.connect({ | ||
| ...clientOptions, | ||
| port: proxy.address().port, | ||
| }, common.mustCall(() => { | ||
| holding = true; | ||
| client.write('go'); | ||
| })); | ||
| client.on('data', (chunk) => chunks.push(chunk)); | ||
| client.on('error', common.mustCall((err) => { | ||
| assert.strictEqual(err.code, isBoringSSL ? | ||
| 'ERR_SSL_BAD_DECRYPT' : | ||
| 'ERR_SSL_DECRYPTION_FAILED_OR_BAD_RECORD_MAC'); | ||
| assert.deepStrictEqual(Buffer.concat(chunks), | ||
| data.subarray(0, -RECORD_SIZE)); | ||
| proxy.close(); | ||
| server.close(); | ||
| })); | ||
| })); | ||
| })); | ||
| } |
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,82 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
| const { isBoringSSL } = require('../common/crypto'); | ||
| if (isBoringSSL) | ||
| common.skip('BoringSSL does not support renegotiation'); | ||
|
|
||
| const assert = require('assert'); | ||
| const net = require('net'); | ||
| const tls = require('tls'); | ||
| const fixtures = require('../common/fixtures'); | ||
|
|
||
| // Deliver application data and a TLS 1.2 ClientHello in one transport write. | ||
| // The data handler must run before renegotiation starts, so it can disable it. | ||
| const payload = Buffer.from('disable renegotiation'); | ||
| let holding = false; | ||
| let client; | ||
|
|
||
| const server = tls.createServer({ | ||
| key: fixtures.readKey('agent1-key.pem'), | ||
| cert: fixtures.readKey('agent1-cert.pem'), | ||
| maxVersion: 'TLSv1.2', | ||
| }, common.mustCall((socket) => { | ||
| socket.on('data', common.mustCall((data) => { | ||
| assert.deepStrictEqual(data, payload); | ||
| socket.disableRenegotiation(); | ||
| })); | ||
| socket.on('secure', common.mustNotCall('renegotiation completed')); | ||
| socket.on('error', common.mustCall((err) => { | ||
| assert.strictEqual(err.code, 'ERR_TLS_RENEGOTIATION_DISABLED'); | ||
| client.destroy(); | ||
| socket.destroy(); | ||
| server.close(); | ||
| proxy.close(); | ||
| })); | ||
| })); | ||
|
|
||
| const proxy = net.createServer(common.mustCall((socket) => { | ||
| const upstream = net.connect(server.address().port); | ||
| upstream.pipe(socket); | ||
| socket.on('end', () => upstream.end()); | ||
| socket.on('close', () => upstream.destroy()); | ||
| const held = []; | ||
| let pending = Buffer.alloc(0); | ||
| socket.on('data', (data) => { | ||
| if (!holding) { | ||
| upstream.write(data); | ||
| return; | ||
| } | ||
| held.push(data); | ||
| pending = Buffer.concat([pending, data]); | ||
| while (pending.length >= 5) { | ||
| const length = pending.readUInt16BE(3); | ||
| if (pending.length < 5 + length) | ||
| break; | ||
| if (pending[0] === 22) { // handshake | ||
| holding = false; | ||
| upstream.write(Buffer.concat(held)); | ||
| return; | ||
| } | ||
| pending = pending.subarray(5 + length); | ||
| } | ||
| }); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| proxy.listen(0, common.mustCall(() => { | ||
| client = tls.connect({ | ||
| port: proxy.address().port, | ||
| ca: fixtures.readKey('ca1-cert.pem'), | ||
| servername: 'agent1', | ||
| maxVersion: 'TLSv1.2', | ||
| }, common.mustCall(() => { | ||
| holding = true; | ||
| client.write(payload, common.mustCall(() => { | ||
| assert(client.renegotiate({}, | ||
| common.mustNotCall('renegotiation completed'))); | ||
| })); | ||
| })); | ||
| })); | ||
| })); |
Oops, something went wrong.
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.