Sandboxing scripts

Run scripts you don't trust — mods, plugins, user submissions — with hard CPU and memory limits and host access removed.

Set limits and run

Lua::sandboxed returns a Lua state plus a handle to refill its budget. The limits apply to every thread, including coroutines, and can't be caught with pcall.

rust
use omnilua::{Lua, SandboxConfig};

let (lua, sandbox) = Lua::sandboxed(SandboxConfig::strict())?;
lua.load(untrusted_source).exec().ok();
sandbox.reset(); // refill the budget before the next run

What strict mode does

  • Caps total instructions and memory; exceeding either stops the run.
  • Removes host access (no os.execute, io, require, load, debug).
  • Refills on reset(), so one state can run many scripts.
In the browser

The same sandbox is available over wasm with setLimits. See Lua in the browser.

Next