2

I found some answers, but very old one and I wondered how can it be done?

I've seen that eval() function isn't safe:

Alternate for eval() to execute auto-generated JS code from the server

So what are my options in order to run a javascript code in isolated secured way?

10

1 Answer 1

-1

(as pointed out, this answer is outdated and the proposed solution could be easily broken).

Currently, draft for secure ecmascript has not been approved/implemented yet. One hack that has decent browser support (proxy is es6 feature) is using proxies and with to create a sandbox.

You just have to write a proxy which returns null for every requested key except for some safe functions. with would ask the proxy every time a var outside eval is required (for example, window) and so, provide null instead of the real var.

Check this website for a tutorial: code sandbox

Sign up to request clarification or add additional context in comments.

7 Comments

No. This doesn't provide real safety.
why? I could not find an example that breaks it - moreover I've seen it used in a commercial solution by a big tech company. Of course, it's an hack - not a true solution (iframes are too).
You can get access to the main window object with this.
no, you can bind "this" to another var (JavaScript perfectly allows it).
No, this is a reserved word. You can't name a variable after it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.