From 138f0d762948c55707c8a48b1f6c2f5510b7320a Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 25 Oct 2022 02:59:14 +0200 Subject: [PATCH 1/5] buffer: fix validation of options in `Blob` constructor --- lib/internal/blob.js | 7 ++++--- lib/internal/validators.js | 15 +++++++++++++++ test/parallel/test-blob.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 69feb22c3304f0..feb5c016e914c0 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -63,6 +63,7 @@ const { const { validateObject, isUint32, + validateDictionary, } = require('internal/validators'); const kHandle = Symbol('kHandle'); @@ -138,17 +139,17 @@ class Blob { * }} [options] * @constructs {Blob} */ - constructor(sources = [], options = kEmptyObject) { + constructor(sources = [], options) { if (sources === null || typeof sources[SymbolIterator] !== 'function' || typeof sources === 'string') { throw new ERR_INVALID_ARG_TYPE('sources', 'a sequence', sources); } - validateObject(options, 'options'); + validateDictionary(options, 'options'); let { type = '', endings = 'transparent', - } = options; + } = options ?? kEmptyObject; endings = `${endings}`; if (endings !== 'transparent' && endings !== 'native') diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 695e198d2cbc5b..0f43e741b42d90 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -257,6 +257,20 @@ const validateObject = hideStackFrames( } }); +/** + * @callback validateDictionary + * @param {*} value + * @param {string} name + */ + +/** @type {validateDictionary} */ +const validateDictionary = hideStackFrames( + (value, name) => { + if (value != null && typeof value !== 'object' && typeof value !== 'function') { + throw new ERR_INVALID_ARG_TYPE(name, 'Object or Function', value); + } + }); + /** * @callback validateArray * @param {*} value @@ -511,6 +525,7 @@ module.exports = { validateBooleanArray, validateBoolean, validateBuffer, + validateDictionary, validateEncoding, validateFunction, validateInt32, diff --git a/test/parallel/test-blob.js b/test/parallel/test-blob.js index 7bec9dbf69e7c5..588fc0bfff0d49 100644 --- a/test/parallel/test-blob.js +++ b/test/parallel/test-blob.js @@ -287,3 +287,32 @@ assert.throws(() => new Blob({}), { assert.strictEqual(blob.size, 28); assert.strictEqual(blob.type, ''); })().then(common.mustCall()); + +{ + // Testing the defaults + [undefined, null, Object.create(null), { type: undefined }, { + get type() {}, // eslint-disable-line getter-return + }].forEach((options) => { + assert.strictEqual( + new Blob([], options).type, + new Blob([]).type, + ); + }); + + Reflect.defineProperty(Object.prototype, 'type', { + __proto__: null, + configurable: true, + get: common.mustCall(() => 3, 7), + }); + + [{}, [], () => {}, Number, new Number(), new String(), new Boolean()].forEach( + (options) => { + assert.strictEqual(new Blob([], options).type, '3'); + }, + ); + [0, '', true, Symbol(), 0n].forEach((options) => { + assert.throws(() => new Blob([], options), { code: 'ERR_INVALID_ARG_TYPE' }); + }); + + delete Object.prototype.type; +} From cfa1ef0fea6b625ea0e61e7157ccdc5c8cb82415 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 25 Oct 2022 03:08:46 +0200 Subject: [PATCH 2/5] fixup! buffer: fix validation of options in `Blob` constructor --- lib/internal/blob.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/blob.js b/lib/internal/blob.js index feb5c016e914c0..82a708a88db875 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -61,7 +61,6 @@ const { } = require('internal/errors'); const { - validateObject, isUint32, validateDictionary, } = require('internal/validators'); From e4dbc9ede485fe71f16978db30106c75758191d4 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 27 Oct 2022 15:52:20 +0200 Subject: [PATCH 3/5] add comment and link to the spec --- lib/internal/validators.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 0f43e741b42d90..ca8a8e123995cf 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -258,9 +258,13 @@ const validateObject = hideStackFrames( }); /** - * @callback validateDictionary + * @callback validateDictionary - We are using the Web IDL Standard definition + * of "dictionary" here, which means any value + * whose Type is either Undefined, Null, or + * Object (which includes functions). * @param {*} value * @param {string} name + * @see https://webidl.spec.whatwg.org/#es-dictionary */ /** @type {validateDictionary} */ From 96230ee286295825d4eab3d4b4eac0d1d1857c99 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 27 Oct 2022 15:57:17 +0200 Subject: [PATCH 4/5] update error message --- lib/internal/validators.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index ca8a8e123995cf..8120c5e06a0f02 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -271,7 +271,7 @@ const validateObject = hideStackFrames( const validateDictionary = hideStackFrames( (value, name) => { if (value != null && typeof value !== 'object' && typeof value !== 'function') { - throw new ERR_INVALID_ARG_TYPE(name, 'Object or Function', value); + throw new ERR_INVALID_ARG_TYPE(name, 'a dictionary', value); } }); From 1ff33ff3d78098820fe819ae816ffcf39a542f58 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 31 Oct 2022 17:37:50 +0100 Subject: [PATCH 5/5] fixup! buffer: fix validation of options in Blob constructor Co-authored-by: Joyee Cheung --- lib/internal/validators.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 8120c5e06a0f02..753b135d3d0361 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -265,6 +265,7 @@ const validateObject = hideStackFrames( * @param {*} value * @param {string} name * @see https://webidl.spec.whatwg.org/#es-dictionary + * @see https://tc39.es/ecma262/#table-typeof-operator-results */ /** @type {validateDictionary} */