Skip to content

Commit 75eaf25

Browse files
BridgeARtargos
authored andcommitted
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: #26825 Fixes: #26741 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent ef0701d commit 75eaf25

File tree

3 files changed

+30
-59
lines changed

3 files changed

+30
-59
lines changed

lib/buffer.js

+14-24
Original file line numberDiff line numberDiff line change
@@ -195,33 +195,23 @@ Buffer.from = function from(value, encodingOrOffset, length) {
195195
if (typeof value === 'string')
196196
return fromString(value, encodingOrOffset);
197197

198-
if (isAnyArrayBuffer(value))
199-
return fromArrayBuffer(value, encodingOrOffset, length);
198+
if (typeof value === 'object' && value !== null) {
199+
if (isAnyArrayBuffer(value))
200+
return fromArrayBuffer(value, encodingOrOffset, length);
200201

201-
if (value === null || value === undefined) {
202-
throw new ERR_INVALID_ARG_TYPE(
203-
'first argument',
204-
['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'],
205-
value
206-
);
207-
}
208-
209-
if (typeof value === 'number') {
210-
throw new ERR_INVALID_ARG_TYPE('value', 'not number', value);
211-
}
202+
const valueOf = value.valueOf && value.valueOf();
203+
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
204+
return Buffer.from(valueOf, encodingOrOffset, length);
212205

213-
const valueOf = value.valueOf && value.valueOf();
214-
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
215-
return Buffer.from(valueOf, encodingOrOffset, length);
216-
217-
const b = fromObject(value);
218-
if (b)
219-
return b;
206+
const b = fromObject(value);
207+
if (b)
208+
return b;
220209

221-
if (typeof value[Symbol.toPrimitive] === 'function') {
222-
return Buffer.from(value[Symbol.toPrimitive]('string'),
223-
encodingOrOffset,
224-
length);
210+
if (typeof value[Symbol.toPrimitive] === 'function') {
211+
return Buffer.from(value[Symbol.toPrimitive]('string'),
212+
encodingOrOffset,
213+
length);
214+
}
225215
}
226216

227217
throw new ERR_INVALID_ARG_TYPE(

test/parallel/test-buffer-bad-overload.js

-17
This file was deleted.

test/parallel/test-buffer-from.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const { deepStrictEqual, throws } = require('assert');
55
const { runInNewContext } = require('vm');
66

@@ -40,27 +40,25 @@ deepStrictEqual(
4040
[{ valueOf() { return null; } }, 'object'],
4141
[{ valueOf() { return undefined; } }, 'object'],
4242
[{ valueOf: null }, 'object'],
43-
[Object.create(null), 'object']
43+
[Object.create(null), 'object'],
44+
[new Number(true), 'number'],
45+
[new MyBadPrimitive(), 'number'],
46+
[Symbol(), 'symbol'],
47+
[5n, 'bigint'],
48+
[(one, two, three) => {}, 'function'],
49+
[undefined, 'undefined'],
50+
[null, 'object']
4451
].forEach(([input, actualType]) => {
45-
const err = common.expectsError({
52+
const errObj = {
4653
code: 'ERR_INVALID_ARG_TYPE',
47-
type: TypeError,
54+
name: 'TypeError',
4855
message: 'The first argument must be one of type string, Buffer, ' +
4956
'ArrayBuffer, Array, or Array-like Object. Received ' +
5057
`type ${actualType}`
51-
});
52-
throws(() => Buffer.from(input), err);
58+
};
59+
throws(() => Buffer.from(input), errObj);
60+
throws(() => Buffer.from(input, 'hex'), errObj);
5361
});
5462

55-
[
56-
new Number(true),
57-
new MyBadPrimitive()
58-
].forEach((input) => {
59-
const errMsg = common.expectsError({
60-
code: 'ERR_INVALID_ARG_TYPE',
61-
type: TypeError,
62-
message: 'The "value" argument must not be of type number. ' +
63-
'Received type number'
64-
});
65-
throws(() => Buffer.from(input), errMsg);
66-
});
63+
Buffer.allocUnsafe(10); // Should not throw.
64+
Buffer.from('deadbeaf', 'hex'); // Should not throw.

0 commit comments

Comments
 (0)