Skip to content

Commit 4848305

Browse files
committed
add test for spoofed length getter in concat
1 parent 1898304 commit 4848305

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/buffer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const {
5151
TypedArrayPrototypeGetLength,
5252
TypedArrayPrototypeSet,
5353
TypedArrayPrototypeSlice,
54+
TypedArrayPrototypeSubarray,
5455
Uint8Array,
5556
} = primordials;
5657

@@ -656,7 +657,7 @@ Buffer.concat = function concat(list, length) {
656657
const bufLength = TypedArrayPrototypeGetByteLength(buf);
657658
if (pos + bufLength > length) {
658659
TypedArrayPrototypeSet(buffer,
659-
TypedArrayPrototypeSlice(buf, 0, length - pos),
660+
TypedArrayPrototypeSubarray(buf, 0, length - pos),
660661
pos);
661662
pos = length;
662663
break;

test/parallel/test-buffer-concat.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,13 @@ assert.deepStrictEqual(
122122
assert.deepStrictEqual(Buffer.concat([new Uint8Array([0x41, 0x42]),
123123
new Uint8Array([0x43, 0x44])]),
124124
Buffer.from('ABCD'));
125+
126+
// Spoofed length getter should not cause uninitialized memory exposure
127+
{
128+
const u8_1 = new Uint8Array([1, 2, 3, 4]);
129+
const u8_2 = new Uint8Array([5, 6, 7, 8]);
130+
Object.defineProperty(u8_1, 'length', { get() { return 100; } });
131+
const buf = Buffer.concat([u8_1, u8_2]);
132+
assert.strictEqual(buf.length, 8);
133+
assert.deepStrictEqual(buf, Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]));
134+
}

0 commit comments

Comments
 (0)