From 01ec4e59c348e98630faa1b95be68b18b5a3f198 Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Tue, 24 Nov 2026 13:59:45 +0100 Subject: [PATCH 1/2] stream: avoid mutable push in writable end callbacks Writable.prototype.end() queued its callback with .push() on a private array, which looks up Array.prototype.push at call time. Replacing that method from user code made end() throw or drop the callback. Use an indexed assignment instead. Signed-off-by: Roberto Bianchi --- lib/internal/streams/writable.js | 4 +- .../test-stream-writable-end-cb-array-push.js | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-stream-writable-end-cb-array-push.js diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 3f62489c0f7..0ab27342199 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -903,8 +903,8 @@ Writable.prototype.end = function(chunk, encoding, cb) { process.nextTick(cb, null); } else { state[kState] |= kOnFinished; - state[kOnFinishedValue] ??= []; - state[kOnFinishedValue].push(cb); + const onFinished = state[kOnFinishedValue] ??= []; + onFinished[onFinished.length] = cb; } } diff --git a/test/parallel/test-stream-writable-end-cb-array-push.js b/test/parallel/test-stream-writable-end-cb-array-push.js new file mode 100644 index 00000000000..29efb0cc9ad --- /dev/null +++ b/test/parallel/test-stream-writable-end-cb-array-push.js @@ -0,0 +1,46 @@ +'use strict'; + +// Queuing end() callbacks must not depend on a user-modified +// Array.prototype.push. + +const common = require('../common'); +const assert = require('assert'); +const { Writable } = require('stream'); + +const written = []; +const calls = []; +const writable = new Writable({ + write(chunk, encoding, cb) { + written[written.length] = chunk.toString(); + setImmediate(cb); + }, +}); + +writable.on('finish', common.mustCall(() => { + calls[calls.length] = 'finish'; +})); + +// Create the callbacks first: common.mustSucceed() itself uses push. +const first = common.mustSucceed(() => { + calls[calls.length] = 'first'; +}); +const second = common.mustSucceed(() => { + calls[calls.length] = 'second'; +}); + +const originalPush = Array.prototype.push; +Array.prototype.push = function() { + throw new Error('Array.prototype.push should not be called'); +}; +try { + // The write is still pending, so both callbacks are queued. + writable.end('data', first); + writable.end(second); +} finally { + Array.prototype.push = originalPush; +} + +process.on('exit', () => { + assert.deepStrictEqual(written, ['data']); + assert.deepStrictEqual(calls, ['first', 'second', 'finish']); +}); From e9bbb30520445f968fa1817c46a195f764f374c8 Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Wed, 25 Nov 2026 10:17:31 +0100 Subject: [PATCH 2/2] update Signed-off-by: Roberto Bianchi --- .../test-stream-writable-end-cb-array-push.js | 46 ------------------- 1 file changed, 46 deletions(-) delete mode 100644 test/parallel/test-stream-writable-end-cb-array-push.js diff --git a/test/parallel/test-stream-writable-end-cb-array-push.js b/test/parallel/test-stream-writable-end-cb-array-push.js deleted file mode 100644 index 29efb0cc9ad..00000000000 --- a/test/parallel/test-stream-writable-end-cb-array-push.js +++ /dev/null @@ -1,46 +0,0 @@ -'use strict'; - -// Queuing end() callbacks must not depend on a user-modified -// Array.prototype.push. - -const common = require('../common'); -const assert = require('assert'); -const { Writable } = require('stream'); - -const written = []; -const calls = []; -const writable = new Writable({ - write(chunk, encoding, cb) { - written[written.length] = chunk.toString(); - setImmediate(cb); - }, -}); - -writable.on('finish', common.mustCall(() => { - calls[calls.length] = 'finish'; -})); - -// Create the callbacks first: common.mustSucceed() itself uses push. -const first = common.mustSucceed(() => { - calls[calls.length] = 'first'; -}); -const second = common.mustSucceed(() => { - calls[calls.length] = 'second'; -}); - -const originalPush = Array.prototype.push; -Array.prototype.push = function() { - throw new Error('Array.prototype.push should not be called'); -}; -try { - // The write is still pending, so both callbacks are queued. - writable.end('data', first); - writable.end(second); -} finally { - Array.prototype.push = originalPush; -} - -process.on('exit', () => { - assert.deepStrictEqual(written, ['data']); - assert.deepStrictEqual(calls, ['first', 'second', 'finish']); -});