Home › Cookbook
Running Untrusted Scripts
When running player-submitted, UGC, or admin scripts, build one shared Sandbox policy with capability functions and :fork() an isolated environment per player or script.
Per-Player Sandboxes with Capability Globals
luau
--!strict
local Ouro = require("./ouro")
-- Define a narrow capability API instead of exposing raw game services or mutable host tables
local scores: { [string]: number } = {}
local logs: { string } = {}
local function makePlayerSandbox(basePolicy: Ouro.Sandbox, playerId: string): Ouro.Sandbox
return basePolicy:fork({
globals = {
getScore = function(): number
return scores[playerId] or 0
end,
addScore = function(delta: number)
if type(delta) ~= "number" or delta ~= delta then
error("delta must be a finite number", 2)
end
scores[playerId] = math.clamp((scores[playerId] or 0) + math.floor(delta), 0, 10_000)
end,
log = function(msg: string)
if type(msg) ~= "string" then
error("log expects a string", 2)
end
if #logs < 100 then
table.insert(logs, `{playerId}: {string.sub(msg, 1, 120)}`)
end
end,
},
})
end
local basePolicy = Ouro.sandbox({
preset = "strict", -- 250k insns, 64 calls, 50ms timeout, 4 MiB mem, clamped directives
instructionLimit = 50_000,
timeout = 0.02,
})
local alice = makePlayerSandbox(basePolicy, "alice")
local bob = makePlayerSandbox(basePolicy, "bob")
local okA, errA = alice:run([[
secret = "alice-only"
addScore(25)
log("scored")
]])
assert(okA, tostring(errA))
local okB, leaked = bob:run([[
addScore(10)
return secret
]])
assert(okB and leaked == nil)
assert(scores.alice == 25 and scores.bob == 10 and #logs == 1)Why This Pattern Works
- Isolated state:
aliceandbobeach get a fresh writable overlay table and independentPCG32math.randomgenerator state, while sharing the frozen standard-library base. - Clamped compiler directives: Under
"strict"and"balanced",allowDirectives = falseblocks guest scripts from escalating--!optimize 2or@native. - Safe failure containment: Syntax errors, compile errors, runtime errors,
instructionLimit,timeout, andmaxMemoryBytesaborts returnfalse, errwithout terminating the host thread.
Reusing a Compiled Tick Handler Across Frames
Compile and load a guest callback once, then call sandbox:reset() before each tick so the budget starts fresh every frame:
luau
--!strict
local Ouro = require("./ouro")
local sb = Ouro.sandbox({
preset = "strict",
instructionLimit = 20_000,
timeout = 0.01,
})
local handler, loadErr = sb:load([[
local dt = ...
total = (total or 0) + dt
return total
]], {
name = "=player_tick",
})
assert(handler ~= nil, loadErr)
for _ = 1, 5 do
sb:reset()
local ok, elapsed = pcall(handler, 0.016)
assert(ok and type(elapsed) == "number")
end
assert(math.abs((sb:get("total") :: number) - 0.08) < 1e-6)Stopping Infinite Loops & Memory Bombs
Same-context pcall, xpcall, and coroutine.resume cannot swallow budget aborts:
luau
--!strict
local Ouro = require("./ouro")
local okLoop, loopErr = Ouro.run([[
pcall(function()
while true do end
end)
return "escaped"
]], {
preset = "strict",
instructionLimit = 2_000,
})
assert(not okLoop and string.find(tostring(loopErr), "instruction limit", 1, true) ~= nil)
local okMem, memErr = Ouro.run([[
local t = {}
for i = 1, 10_000 do
t[i] = string.rep("x", 1024)
end
]], {
preset = "strict",
resourceLimits = { maxMemoryBytes = 32_768 },
})
assert(not okMem and string.find(tostring(memErr), "memory limit", 1, true) ~= nil)