Repository navigation
net: reject immutable onread buffers #67394
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
fatihguzeldev
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
fatihguzeldev:net-onread-immutable
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.
+338
−4
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,155 @@ | ||
| // Flags: --js-immutable-arraybuffer | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const net = require('net'); | ||
| const { once } = require('events'); | ||
| const { test } = require('node:test'); | ||
|
|
||
| if (typeof ArrayBuffer.prototype.transferToImmutable !== 'function') | ||
| common.skip('ArrayBuffer.prototype.transferToImmutable is not available'); | ||
|
|
||
| const message = Buffer.from('test'); | ||
|
|
||
| async function createServer(t) { | ||
| const connections = []; | ||
| const server = net.createServer((socket) => { | ||
| connections.push(socket); | ||
| // Rejecting a replacement buffer may close the client with unread data. | ||
| socket.on('error', (err) => { | ||
| if (err.code !== 'ECONNRESET') throw err; | ||
| }); | ||
| socket.end(message); | ||
| }); | ||
| t.after(async () => { | ||
| for (const socket of connections) socket.destroy(); | ||
| await new Promise((resolve) => server.close(resolve)); | ||
| }); | ||
| server.listen(0, '127.0.0.1'); | ||
| await once(server, 'listening'); | ||
| return server.address().port; | ||
| } | ||
|
|
||
| for (const View of [Buffer, Uint8Array]) { | ||
| for (const offset of [0, 1]) { | ||
| for (const mode of ['direct', 'first', 'replacement']) { | ||
| test(`${View.name}, offset ${offset}, ${mode} immutable buffer`, { | ||
| timeout: common.platformTimeout(5000), | ||
| }, async (t) => { | ||
| const original = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]; | ||
| const backing = Uint8Array.from(original).buffer.transferToImmutable(); | ||
| const target = View === Buffer ? | ||
| Buffer.from(backing, offset, 4) : new Uint8Array(backing, offset, 4); | ||
| const mutable = new Uint8Array(1); | ||
| let generated = 0; | ||
| const factory = () => { | ||
| generated++; | ||
| return mode === 'replacement' && generated === 1 ? mutable : target; | ||
| }; | ||
| const reads = []; | ||
| const errors = []; | ||
| const socket = new net.Socket({ | ||
| onread: { | ||
| buffer: mode === 'direct' ? target : factory, | ||
| callback(nread, buffer) { | ||
| reads.push({ buffer, bytes: [...buffer.subarray(0, nread)] }); | ||
| }, | ||
| }, | ||
| }); | ||
| t.after(() => socket.destroy()); | ||
| socket.on('error', (err) => errors.push(err)); | ||
| socket.on('close', common.mustCall()); | ||
| socket.on('data', common.mustNotCall()); | ||
| const closed = new Promise((resolve) => socket.once('close', resolve)); | ||
| const port = await createServer(t); | ||
| socket.connect(port, '127.0.0.1'); | ||
| await closed; | ||
|
|
||
| assert.strictEqual(errors.length, 1); | ||
| assert.strictEqual(errors[0].name, 'TypeError'); | ||
| assert.strictEqual(errors[0].code, 'ERR_INVALID_ARG_VALUE'); | ||
| assert.match(errors[0].message, /onread\.buffer.*immutable ArrayBuffer/); | ||
| assert.strictEqual(socket.connecting, false); | ||
| assert.strictEqual(backing.immutable, true); | ||
| assert.deepStrictEqual([...new Uint8Array(backing)], original); | ||
| if (mode === 'replacement') { | ||
| assert.strictEqual(generated, 2); | ||
| assert.strictEqual(reads.length, 1); | ||
| assert.strictEqual(reads[0].buffer, mutable); | ||
| assert.deepStrictEqual(reads[0].bytes, [...message.subarray(0, 1)]); | ||
| } else { | ||
| assert.strictEqual(generated, mode === 'first' ? 1 : 0); | ||
| assert.deepStrictEqual(reads, []); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const kind of ['bound', 'retry']) { | ||
| test(`reject an immutable buffer during ${kind} initialization`, { | ||
| timeout: common.platformTimeout(5000), | ||
| }, async (t) => { | ||
| const port = await createServer(t); | ||
| const backing = new ArrayBuffer(4).transferToImmutable(); | ||
| const target = new Uint8Array(backing); | ||
| let generated = 0; | ||
| const options = { | ||
| host: '127.0.0.1', | ||
| port, | ||
| onread: { | ||
| buffer: target, | ||
| callback: common.mustNotCall(), | ||
| }, | ||
| }; | ||
| let socket; | ||
| if (kind === 'bound') { | ||
| options.handle = new net.BoundSocket({ host: '127.0.0.1' }); | ||
| socket = net.connect(options); | ||
| } else { | ||
| options.host = 'localhost'; | ||
| options.autoSelectFamily = true; | ||
| options.autoSelectFamilyAttemptTimeout = common.platformTimeout(10); | ||
| options.lookup = (host, opts, callback) => callback(null, [ | ||
| { address: '127.0.0.2', family: 4 }, | ||
| { address: '127.0.0.1', family: 4 }, | ||
| ]); | ||
| options.onread.buffer = () => (++generated === 1 ? new Uint8Array(4) : target); | ||
| socket = net.connect(options).unref(); | ||
| } | ||
| t.after(() => socket.destroy()); | ||
| socket.on('close', common.mustCall()); | ||
| socket.on('error', common.expectsError({ | ||
| name: 'TypeError', | ||
| code: 'ERR_INVALID_ARG_VALUE', | ||
| })); | ||
| await new Promise((resolve) => socket.once('close', resolve)); | ||
| assert.deepStrictEqual([...target], [0, 0, 0, 0]); | ||
| if (kind === 'retry') assert.strictEqual(generated, 2); | ||
| }); | ||
| } | ||
|
|
||
| for (const shared of [false, true]) { | ||
| test(`mutable ${shared ? 'shared' : 'on-heap'} replacement buffers`, { | ||
| timeout: common.platformTimeout(5000), | ||
| }, async (t) => { | ||
| const chunks = []; | ||
| const socket = new net.Socket({ | ||
| onread: { | ||
| buffer: () => (shared ? | ||
| new Uint8Array(new SharedArrayBuffer(1)) : new Uint8Array(1)), | ||
| callback(nread, buffer) { | ||
| chunks.push(Buffer.from(buffer.subarray(0, nread))); | ||
| }, | ||
| }, | ||
| }); | ||
| t.after(() => socket.destroy()); | ||
| socket.on('error', common.mustNotCall()); | ||
| const closed = new Promise((resolve) => socket.once('close', resolve)); | ||
| const port = await createServer(t); | ||
| socket.connect(port, '127.0.0.1'); | ||
| await closed; | ||
| assert.deepStrictEqual(Buffer.concat(chunks), message); | ||
| }); | ||
| } |
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,30 @@ | ||
| // Flags: --expose-internals --no-js-immutable-arraybuffer | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const { spawnSyncAndAssert } = require('../common/child_process'); | ||
| const assert = require('node:assert/strict'); | ||
| const { PassThrough } = require('node:stream'); | ||
| const { deserialize } = require('node:v8'); | ||
| const { setUserBuffer } = require('internal/stream_base_commons'); | ||
|
|
||
| assert.strictEqual( | ||
| Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'immutable'), | ||
| undefined, | ||
| ); | ||
|
|
||
| const { child } = spawnSyncAndAssert(process.execPath, [ | ||
| '--js-immutable-arraybuffer', | ||
| '-e', | ||
| `const { serialize } = require('node:v8'); | ||
| process.stdout.write(serialize(new ArrayBuffer(8).transferToImmutable()));`, | ||
| ], {}); | ||
|
|
||
| const stream = new PassThrough(); | ||
| assert.strictEqual(setUserBuffer(stream, new Uint8Array(8)), true); | ||
| stream.on('error', common.expectsError({ | ||
| code: 'ERR_INVALID_ARG_VALUE', | ||
| name: 'TypeError', | ||
| })); | ||
| const buffer = new Uint8Array(deserialize(child.stdout)); | ||
| assert.strictEqual(setUserBuffer(stream, buffer, true /* deferError */), false); |
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.
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.
It's not available via primordials yet because it is flagged, but there is already an
ArrayBuffer.prototype.immutableproperty that really should be used instead.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.
thanks. i was able to deserialize an immutable arraybuffer with the flag off, but the getter isn't available there. would you recommend keeping the native check for that case?