What is really a sandbox in JavaScript?

I understand the term sandbox. But my limited skills in JS cannot help me understand what a sandbox is in JS. So what is a sandbox? Beyond security, why do we need a JS sandbox?

+4
source share
3 answers

javascript sandbox does exactly what you said. It limits the scope of the script. There are also advantages in terms of resource virtualization that the script can invoke. This allows the sandbox node to combine these resources to improve performance and say stop the endless script looping that causes the entire browser to crash.

+2
source

A sandbox is an act of creating an area in which no other part of the application can work (unless an opportunity is provided). More specifically, it is usually a function domain that provides a limited subset of what is actually going on inside it.

One library based on the sandbox idea is YUI3. The main unit of the application is the test environment of the YUI instance:

var Y = YUI(); // creates a configurable YUI instance // Creates a sandbox for one part of your application, // including the 'node' module. Y.use('node', function(Z) { // Z is a YUI instance that specific to this sandbox. // Operations inside it are protected from outside code // unless exposed explicitly. Any modules you request in // use statement will be separately instanced just for // this sandbox (in this case, the 'node' module) // // That way, if another part of your application decides // to delete Z.Node (or worse, replace it with a // malicious proxy of Z.Node) the code you've written // here won't be affected. }); 

The benefits of a sandbox are basically reduced application complexity: since sandboxes are immutable, it’s much easier to reason and verify. They also improve runtime security because a well-designed sandbox should work as a black box for other scripts running on the page. It does not prevent all possible attacks, but protects against many simple ones.

+2
source

The sandbox creates a limited area of ​​use for the script. Assuming you're coding a website, you're standing in the sandbox to avoid making changes to a live website when you are not sure if they will work exactly as you expect - and you cannot be sure without testing. Even if it works correctly, if it is likely that you will make a number of changes to JS until you change it the way you like, you can easily violate anyone who tries to use the site while updating it.

It is also much easier to say what broke when you break things due to the limited nature of the sandbox.

0
source

All Articles