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

Executing an array returns weird object. #198

Open
KonradLinkowski opened this issue Apr 7, 2019 · 14 comments
Open

Executing an array returns weird object. #198

KonradLinkowski opened this issue Apr 7, 2019 · 14 comments

Comments

@KonradLinkowski
Copy link

KonradLinkowski commented Apr 7, 2019

Code like this:

const { VM } = require('vm2')
const vm = new VM()
const res = vm.run('[1, 2, 3]')
console.log(res)

Prints:
[ 1, 2, 3, '0': 1, '1': 2, '2': 3 ]
Is this a bug or an expected behaviour?

The NodeJS's build in VM returns:
[1, 2, 3]
as expected.

@alvarperezwefox
Copy link

Hi @patriksimek, I quote you as main maintainer. I've this issue too. This happens with VM and with NodeVM.

In my case, inside the NodeVM i have a snippet that -among other things- does this:

const array = [];
array.push({a: 1, b: 2});
console.log(array);

And prints:

[{a: 1, b: 2}, '0': {a: 1, b: 2}]

The console in the NodeVM setup is set to inherit.

Do you have any clue of what could generate this behavior?

Thank you in advance and sorry to bother.

@KonradLinkowski
Copy link
Author

This object turned out to be a Proxy. I believe that somewhere in Decontextify.instance a wrong object is returned.

@OleksiyRudenko
Copy link

OleksiyRudenko commented Jun 2, 2019

Produces expected output under node v8.9.3 and node v8.10.0.
The issue is confirmed under node v10.14.2

@bugs181
Copy link

bugs181 commented Jun 26, 2019

Ran into this same problem. Frustrating tracking this bug down, thinking it was in my own code. Eventually decided to try a very minimal example and lo and behold, vm2 was the culprit. It appears the for of loop also does some odd things with arrays.

@KonradLinkowski
Copy link
Author

Produces expected output under node v8.9.3 and node v8.10.0.
The issue is confirmed under node v10.14.2

What I can propose is to run a debugger in v.8.10.0 and v10.14.2 and find a line of code where the output is different.

@XmiliaH
Copy link
Collaborator

XmiliaH commented Sep 12, 2019

This was caused by using a Proxy.handler.ownKeys() trap. This was an issue in node and seems to be fixed in v12.

@KonradLinkowski
Copy link
Author

Do you plan any fallback for older versions of node?

@XmiliaH
Copy link
Collaborator

XmiliaH commented Sep 19, 2019

The problem is only on node 10. We could do a fix, but that would require to check the caller of a function every time and to get the caller we would need to generate a stack trace. I think that it is not worth doing this.

@bugs181
Copy link

bugs181 commented Jan 24, 2020

The problem is only on node 10. We could do a fix, but that would require to check the caller of a function every time and to get the caller we would need to generate a stack trace. I think that it is not worth doing this.

What about a config or CLI parameter check instead? The code for that could be pretty minimal.

@XmiliaH XmiliaH changed the title Executing an array returns weird object. Executing an array returns weird object. [Node.js 10.x.x] Mar 25, 2020
@mr-shabani
Copy link

I add console to vm._context
vm._context.console = console
vm.run("console.log([1,2,3])")
it's working correctly!

@XmiliaH XmiliaH changed the title Executing an array returns weird object. [Node.js 10.x.x] Executing an array returns weird object. Feb 8, 2022
@XmiliaH
Copy link
Collaborator

XmiliaH commented Feb 8, 2022

This is back for all versions as the fix broke Object.keys. As this is only a issue when logging objects this is acceptable.

@bakso
Copy link

bakso commented Jul 5, 2022

This was caused by using a Proxy.handler.ownKeys() trap. This was an issue in node and seems to be fixed in v12.

still same now...

@BridgeAR
Copy link

The issue is actually a V8 bug. See nodejs/node#41714 (comment)

@fsuk
Copy link

fsuk commented Jun 29, 2023

A work arround for converting the proxy object to a js object (a bit hacky):

value = JSON.parse(JSON.stringify(proxy))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants