Preventing Sandbox Escapes
Block debug library access, rawget/rawset misuse, and upvalue leaks.
Preventing Sandbox Escapes is a free Lua Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Escape Problem
Even with a whitelist environment, creative attackers may find ways to access the real global environment. This lesson covers known escape vectors and how to close them.
String Metatable Exploit
In Lua, strings have a shared metatable that exposes the string library. Sandboxed code can use a string to get a reference to string.rep and from there reach other libraries.
-- Escape vector (before patching):
local s = "hello"
local getenv = s.rep -- reaches string library
-- Fix: use setmetatable on all sandboxed strings (not practical)
-- Better fix: override debug.getmetatable in the sandboxBlocking via rawget/rawset
If rawget is in the sandbox, code can use it to bypass __index restrictions and read from real tables directly.
-- Fix: exclude rawget, rawset, rawequal, rawlen from the sandbox envdebug Library Access
The debug library can read and modify any upvalue, including _ENV. Exclude it entirely from the sandbox.
package.loaded Table
If require or package is accessible, sandboxed code can reach already-loaded modules including os and io.
-- Fix: exclude require and package from the sandbox envCoroutines and Resume
A sandboxed coroutine resumed by the host could be used to exfiltrate state. Use coroutine.wrap carefully and do not resume sandboxed coroutines with elevated privileges.
Upvalue Walking
Without the debug library, sandboxed code cannot walk upvalues. But if debug.getupvalue is accidentally included, it can be used to reach _G.
__newindex on safeEnv
Add a __newindex to the sandbox environment's metatable to prevent sandboxed code from creating new globals that shadow safe ones.
setmetatable(safeEnv, {
__newindex = function(_, k, v)
-- allow only whitelisted keys or reject
error("Global assignment not allowed: " .. k)
end
})Proxy for the math Library
Instead of passing the real math table (which shares a metatable with the standard library), create a shallow copy with only the needed functions.
Automated Escape Testing
Maintain a fuzzing test suite that tries known escape patterns against your sandbox. Run it on every sandbox code change.
Defense in Depth
No sandbox is perfect. Layer defenses: restricted environment + instruction limits + memory limits + OS-level process isolation (separate subprocess or container).
OS-Level Sandbox
For maximum security, run Lua in a subprocess with OS-level restrictions: seccomp (Linux), App Sandbox (macOS), or Docker with a read-only filesystem.
Sandbox Escape Question
Why must rawget be excluded from a sandbox?
Recap: Preventing Sandbox Escapes
Exclude debug, rawget/rawset, package, require, and load from the sandbox. Guard the sandbox env with __newindex. Copy library tables rather than referencing originals. Layer OS-level isolation for maximum security.
Frequently asked questions
Is the “Preventing Sandbox Escapes” lesson free?
Yes — the full text of “Preventing Sandbox Escapes” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Preventing Sandbox Escapes”?
Block debug library access, rawget/rawset misuse, and upvalue leaks. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Preventing Sandbox Escapes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The _ENV Model in Lua 5.2+
- Building a Restricted Sandbox
- Preventing Sandbox Escapes
- Resource Limits and Instrumentation