Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no absolute way to prevent an end user or addon developer from executing specific code in JavaScript. That's why security measures in an open source language like JavaScript is said to be foolproof (as in it's only effective against fools).</p> <p>That being said however let's build a sandbox security layer to prevent inexperienced developers from breaking your site. Personally I prefer using the <code>Function</code> constructor over <code>eval</code> to execute user code for the following reasons:</p> <ol> <li>The code is wrapped in an anonymous function. Hence it may be stored in a variable and called as many times as needed.</li> <li>The function always exists in the global scope. Hence it doesn't have access to the local variables in the block which created the function.</li> <li>The function may be passed arbitrary named parameters. Hence you may exploit this feature to pass or import modules required by the user code (e.g. <code>jQuery</code>).</li> <li>Most importantly you may set a custom <code>this</code> pointer and create local variables named <code>window</code> and <code>document</code> to prevent access to the global scope and the DOM. This allows you to create your own version of the DOM and pass it to the user code.</li> </ol> <p>Note however that even this pattern has disadvantages. Most importantly it may only prevent direct access to the global scope. User code may still create global variables by simply declaring variables without <code>var</code>, and malicious code may use hacks like creating a function and using it's <code>this</code> pointer to access the global scope (the default behavior of JavaScript).</p> <p>So let's look at some code: <a href="http://jsfiddle.net/C3Kw7/" rel="noreferrer">http://jsfiddle.net/C3Kw7/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload