Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Build your sandbox (or multiple sandboxes if they each have different requirements) and move the untrusted code into the sandbox one piece at a time. In my quick cli tests, both 5.1 and 5.2 will run functions that were defined outside of the sandbox without modification. To use Doug's example, assume <code>display</code> is part of your pre-existing code that uses <code>print</code>:</p> <pre class="lang-lua prettyprint-override"><code>-- 5.1 local function display(...) print(...) end local script = loadstring "display(math.log(2, 3))" local env = {display = display, math = math, string = string} setfenv(script, env) print(pcall(script)) -- 5.2 local function display(...) print(...) end local script = loadstring "display(math.log(2, 3))" local e=_ENV _ENV={display = display, math = math, string = string} e.print(e.pcall(script)) _ENV=e </code></pre> <p>Note that in both of the above examples, the <code>display</code> function is using <code>print</code> without modification to that code since you were not in the sandbox when this function was created.</p> <p>In the past, I've stored a local pointer to the un-sandboxed environment, but I can't reproduce the situation where that's needed in my quick cli tests. If you can come up with an example, I can probably come up with a workaround that doesn't require the <code>e</code> variable. Here's an example of that code using 5.2:</p> <pre><code>local e=_ENV for k,v in e.pairs(value) do -- iterate end </code></pre> <p>another example, for my read only table code, I'm again using the <code>e</code>:</p> <pre><code>function ro_table (t) local t = t if t then return e.setmetatable({}, { __index=t, __newindex= function(_,_,_) e.error ("Attempt to modify read-only table") end, }) else return nil end end </code></pre>
 

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