buffer: use stricter from()
input validation
So far we did not throw an error for all types of invalid input. Functions do not return a buffer anymore and `number` and `symbol` validation is also improved. PR-URL: https://github.com/nodejs/node/pull/26825 Fixes: https://github.com/nodejs/node/issues/26741 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
ef0701d31f
commit
75eaf25e78
@ -195,33 +195,23 @@ Buffer.from = function from(value, encodingOrOffset, length) {
|
|||||||
if (typeof value === 'string')
|
if (typeof value === 'string')
|
||||||
return fromString(value, encodingOrOffset);
|
return fromString(value, encodingOrOffset);
|
||||||
|
|
||||||
if (isAnyArrayBuffer(value))
|
if (typeof value === 'object' && value !== null) {
|
||||||
return fromArrayBuffer(value, encodingOrOffset, length);
|
if (isAnyArrayBuffer(value))
|
||||||
|
return fromArrayBuffer(value, encodingOrOffset, length);
|
||||||
|
|
||||||
if (value === null || value === undefined) {
|
const valueOf = value.valueOf && value.valueOf();
|
||||||
throw new ERR_INVALID_ARG_TYPE(
|
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
|
||||||
'first argument',
|
return Buffer.from(valueOf, encodingOrOffset, length);
|
||||||
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
|
|
||||||
value
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof value === 'number') {
|
const b = fromObject(value);
|
||||||
throw new ERR_INVALID_ARG_TYPE('value', 'not number', value);
|
if (b)
|
||||||
}
|
return b;
|
||||||
|
|
||||||
const valueOf = value.valueOf && value.valueOf();
|
if (typeof value[Symbol.toPrimitive] === 'function') {
|
||||||
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
|
return Buffer.from(value[Symbol.toPrimitive]('string'),
|
||||||
return Buffer.from(valueOf, encodingOrOffset, length);
|
encodingOrOffset,
|
||||||
|
length);
|
||||||
const b = fromObject(value);
|
}
|
||||||
if (b)
|
|
||||||
return b;
|
|
||||||
|
|
||||||
if (typeof value[Symbol.toPrimitive] === 'function') {
|
|
||||||
return Buffer.from(value[Symbol.toPrimitive]('string'),
|
|
||||||
encodingOrOffset,
|
|
||||||
length);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new ERR_INVALID_ARG_TYPE(
|
throw new ERR_INVALID_ARG_TYPE(
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
const common = require('../common');
|
|
||||||
const assert = require('assert');
|
|
||||||
|
|
||||||
Buffer.allocUnsafe(10); // Should not throw.
|
|
||||||
|
|
||||||
const err = common.expectsError({
|
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
|
||||||
type: TypeError,
|
|
||||||
message: 'The "value" argument must not be of type number. ' +
|
|
||||||
'Received type number'
|
|
||||||
});
|
|
||||||
assert.throws(function() {
|
|
||||||
Buffer.from(10, 'hex');
|
|
||||||
}, err);
|
|
||||||
|
|
||||||
Buffer.from('deadbeaf', 'hex'); // Should not throw.
|
|
@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const common = require('../common');
|
require('../common');
|
||||||
const { deepStrictEqual, throws } = require('assert');
|
const { deepStrictEqual, throws } = require('assert');
|
||||||
const { runInNewContext } = require('vm');
|
const { runInNewContext } = require('vm');
|
||||||
|
|
||||||
@ -40,27 +40,25 @@ deepStrictEqual(
|
|||||||
[{ valueOf() { return null; } }, 'object'],
|
[{ valueOf() { return null; } }, 'object'],
|
||||||
[{ valueOf() { return undefined; } }, 'object'],
|
[{ valueOf() { return undefined; } }, 'object'],
|
||||||
[{ valueOf: null }, 'object'],
|
[{ valueOf: null }, 'object'],
|
||||||
[Object.create(null), 'object']
|
[Object.create(null), 'object'],
|
||||||
|
[new Number(true), 'number'],
|
||||||
|
[new MyBadPrimitive(), 'number'],
|
||||||
|
[Symbol(), 'symbol'],
|
||||||
|
[5n, 'bigint'],
|
||||||
|
[(one, two, three) => {}, 'function'],
|
||||||
|
[undefined, 'undefined'],
|
||||||
|
[null, 'object']
|
||||||
].forEach(([input, actualType]) => {
|
].forEach(([input, actualType]) => {
|
||||||
const err = common.expectsError({
|
const errObj = {
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
type: TypeError,
|
name: 'TypeError',
|
||||||
message: 'The first argument must be one of type string, Buffer, ' +
|
message: 'The first argument must be one of type string, Buffer, ' +
|
||||||
'ArrayBuffer, Array, or Array-like Object. Received ' +
|
'ArrayBuffer, Array, or Array-like Object. Received ' +
|
||||||
`type ${actualType}`
|
`type ${actualType}`
|
||||||
});
|
};
|
||||||
throws(() => Buffer.from(input), err);
|
throws(() => Buffer.from(input), errObj);
|
||||||
|
throws(() => Buffer.from(input, 'hex'), errObj);
|
||||||
});
|
});
|
||||||
|
|
||||||
[
|
Buffer.allocUnsafe(10); // Should not throw.
|
||||||
new Number(true),
|
Buffer.from('deadbeaf', 'hex'); // Should not throw.
|
||||||
new MyBadPrimitive()
|
|
||||||
].forEach((input) => {
|
|
||||||
const errMsg = common.expectsError({
|
|
||||||
code: 'ERR_INVALID_ARG_TYPE',
|
|
||||||
type: TypeError,
|
|
||||||
message: 'The "value" argument must not be of type number. ' +
|
|
||||||
'Received type number'
|
|
||||||
});
|
|
||||||
throws(() => Buffer.from(input), errMsg);
|
|
||||||
});
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user