From 998042a4acece1e0454ed574b239c00aee805090 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 30 Jan 2023 21:16:26 +0100 Subject: [PATCH 1/2] stream: validate readable defaultEncoding --- lib/internal/streams/readable.js | 10 +++- .../test-stream-readable-default-encoding.js | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-stream-readable-default-encoding.js diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index aad4c594501ba6..3f94ff92d84b6e 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -65,6 +65,7 @@ const { ERR_OUT_OF_RANGE, ERR_STREAM_PUSH_AFTER_EOF, ERR_STREAM_UNSHIFT_AFTER_END_EVENT, + ERR_UNKNOWN_ENCODING } } = require('internal/errors'); const { validateObject } = require('internal/validators'); @@ -162,7 +163,14 @@ function ReadableState(options, stream, isDuplex) { // Crypto is kind of old and crusty. Historically, its default string // encoding is 'binary' so we have to make this configurable. // Everything else in the universe uses 'utf8', though. - this.defaultEncoding = (options && options.defaultEncoding) || 'utf8'; + const defaultEncoding = options?.defaultEncoding; + if (defaultEncoding == null) { + this.defaultEncoding = 'utf8'; + } else if (Buffer.isEncoding(defaultEncoding)) { + this.defaultEncoding = defaultEncoding; + } else { + throw new ERR_UNKNOWN_ENCODING(defaultEncoding); + } // Ref the piped dest which we need a drain event on it // type: null | Writable | Set. diff --git a/test/parallel/test-stream-readable-default-encoding.js b/test/parallel/test-stream-readable-default-encoding.js new file mode 100644 index 00000000000000..c036ff70f8e00f --- /dev/null +++ b/test/parallel/test-stream-readable-default-encoding.js @@ -0,0 +1,48 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { Readable } = require('stream'); + +{ + assert.throws(() => { + new Readable({ + read: () => {}, + defaultEncoding: 'my invalid encoding', + }); + }, { + code: 'ERR_UNKNOWN_ENCODING', + }); +} + +{ + const r = new Readable({ + read() {}, + defaultEncoding: 'hex' + }); + + r.push('ab'); + + const chunks = []; + r.on('data', (chunk) => chunks.push(chunk)); + + process.nextTick(common.mustCall(() => { + assert.strictEqual(Buffer.concat(chunks).toString('hex'), 'ab'); + }), 1); + +} + +{ + const r = new Readable({ + read() {}, + defaultEncoding: 'hex', + }); + + r.push('ab', 'utf-8'); + + const chunks = []; + r.on('data', (chunk) => chunks.push(chunk)); + + process.nextTick(common.mustCall(() => { + assert.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'ab'); + }), 1); +} From 2234b50b6598015d808e6ba5079cf29889f36ebe Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Thu, 2 Feb 2023 14:26:47 +0100 Subject: [PATCH 2/2] stream: fixed test --- .../test-stream-readable-default-encoding.js | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/test/parallel/test-stream-readable-default-encoding.js b/test/parallel/test-stream-readable-default-encoding.js index c036ff70f8e00f..954f1643baad4c 100644 --- a/test/parallel/test-stream-readable-default-encoding.js +++ b/test/parallel/test-stream-readable-default-encoding.js @@ -22,13 +22,7 @@ const { Readable } = require('stream'); r.push('ab'); - const chunks = []; - r.on('data', (chunk) => chunks.push(chunk)); - - process.nextTick(common.mustCall(() => { - assert.strictEqual(Buffer.concat(chunks).toString('hex'), 'ab'); - }), 1); - + r.on('data', common.mustCall((chunk) => assert.strictEqual(chunk.toString('hex'), 'ab')), 1); } { @@ -37,12 +31,7 @@ const { Readable } = require('stream'); defaultEncoding: 'hex', }); - r.push('ab', 'utf-8'); - - const chunks = []; - r.on('data', (chunk) => chunks.push(chunk)); + r.push('xy', 'utf-8'); - process.nextTick(common.mustCall(() => { - assert.strictEqual(Buffer.concat(chunks).toString('utf-8'), 'ab'); - }), 1); + r.on('data', common.mustCall((chunk) => assert.strictEqual(chunk.toString('utf-8'), 'xy')), 1); }