Skip to content

Commit

Permalink
Merge pull request #319 from XmiliaH/fix-318
Browse files Browse the repository at this point in the history
Fix uninitialized buffer allocation
  • Loading branch information
XmiliaH committed Apr 21, 2021
2 parents cc63160 + 8feb2ae commit 6fee336
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/contextify.js
Expand Up @@ -618,6 +618,11 @@ Contextify.function = (fnc, traps, deepTraps, flags, mock) => {
let proxy;

base.apply = (target, context, args) => {
// Fixes buffer unsafe allocation for node v6/7
if (host.version < 8 && fnc === host.Buffer && 'number' === typeof args[0]) {
args[0] = new local.Array(args[0]).fill(0);
}

context = Decontextify.value(context);

// Set context of all arguments to host's context.
Expand All @@ -632,7 +637,7 @@ Contextify.function = (fnc, traps, deepTraps, flags, mock) => {
base.construct = (target, args, newTarget) => {
// Fixes buffer unsafe allocation for node v6/7
if (host.version < 8 && fnc === host.Buffer && 'number' === typeof args[0]) {
args[0] = new Array(args[0]).fill(0);
args[0] = new local.Array(args[0]).fill(0);
}

args = Decontextify.arguments(args);
Expand Down
15 changes: 15 additions & 0 deletions test/vm.js
Expand Up @@ -560,6 +560,21 @@ describe('VM', () => {
assert.strictEqual(vm2.run(`
class MyBuffer extends Buffer {}; MyBuffer.alloc(100).toString('hex');
`), '00'.repeat(100), '#4');

assert.strictEqual(vm2.run(`
new Buffer(100).toString('hex');
`), '00'.repeat(100), '#5');

if (NODE_VERSION < 8) {
assert.strictEqual(vm2.run(`
Buffer(100).toString('hex');
`), '00'.repeat(100), '#6');
}

assert.strictEqual(vm2.run(`
class MyBuffer2 extends Buffer {}; new MyBuffer2(100).toString('hex');
`), '00'.repeat(100), '#7');

});

it('instanceof attack', () => {
Expand Down

0 comments on commit 6fee336

Please sign in to comment.