Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uninitialized buffer allocation #319

Merged
merged 1 commit into from Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/contextify.js
Expand Up @@ -612,6 +612,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 @@ -626,7 +631,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 @@ -553,6 +553,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