diff --git a/packages/codemode/lua/README.md b/packages/codemode/lua/README.md index 6f8e87ed3fa..edeb2748113 100644 --- a/packages/codemode/lua/README.md +++ b/packages/codemode/lua/README.md @@ -6,7 +6,9 @@ There are two entry points sharing the same Lua AST evaluator, lexical cells, ac `sandbox.js` is the Lua execution profile for Code Mode. It uses fresh, Map-backed tables and an explicit native-function allowlist, **not** the prototype's v8lua runtime or standard libraries. `evaluateLua` advances a generator; it yields either a scheduler checkpoint or a capability path with detached JSON arguments. While the caller awaits a tool, no Lua executes and no ambient state is installed. The bridge resumes that same generator with validated inert JSON or closes it on interruption. `pcall` can recover from ordinary Lua and host errors, never resource exhaustion or interruption. -Source is limited to 32 KiB, 8,192 AST nodes and 48 levels of syntax. The evaluator caps normal activations at 96 and nested protected calls at 24. Each execution has 500,000 weighted work units, with checkpoints after 256 units at evaluator boundaries. Table writes, local lookup, argument copying and boundary traversal consume fuel too. Tables have at most 8,192 entries; string-producing operations check a 256 KiB per-string limit and a cumulative 8 MiB budget before materialization. Boundary output is capped at 1 MiB per call/result and 32 levels, including repeated expansion of shared references. These are implementation budgets, not an OS heap or CPU-time quota. Host tool execution, canonicalization of host-owned objects, timeout handling and log retention belong to the bridge. +Source is limited to 32 KiB, 8,192 AST nodes and 48 levels of syntax. The evaluator caps normal activations at 96 and nested protected calls at 24. Each execution has 500,000 weighted work units, with checkpoints after 256 units at evaluator boundaries, including empty loop bodies. Tables have at most 8,192 entries regardless of their numeric keys. Sequence length (`#`) charges for each lookup; `pairs` pays for a snapshot of the keys and for advancing it, including keys deleted during iteration. The standalone `next` function is not exposed. String-producing operations check a 256 KiB per-string limit and a cumulative 8 MiB budget before materialization. + +Host traffic has a separate **4 MiB cumulative budget** covering bindings, callable paths and arguments, replies and the final result. Each transfer also has a 1 MiB cap; exported values can nest up to 32 levels. The meter counts JSON escaping and every expansion of a shared value before the call can reach progress hooks. These are implementation budgets, not an OS heap or CPU-time quota. Host tool execution, canonicalization of host-owned objects, timeout handling and log retention belong to the bridge. The allowlist includes `type`, `tostring`, base-10 `tonumber`, `assert`, `error`, `pcall`, `pairs`, `ipairs`, `math.{abs,ceil,floor,sqrt,min,max,pi,huge}`, `string.{len,sub,rep,lower,upper}`, append-only `table.insert` and `table.concat`. Calls to `print` are capabilities rather than access to the host console. No patterns, format strings, metatables, dynamic loading, debug library, coroutine library or ambient JS objects. Source and JSON use Unicode; inside the VM strings are UTF-8 bytes. Exporting an invalid byte sequence as JSON fails. See the [Code Mode guide](../README.md) for table/JSON conventions. diff --git a/packages/codemode/lua/sandbox.js b/packages/codemode/lua/sandbox.js index e454e067016..44af658db7c 100644 --- a/packages/codemode/lua/sandbox.js +++ b/packages/codemode/lua/sandbox.js @@ -129,9 +129,19 @@ export function hostFailure(message) { // six ASCII bytes. One request shares its budget across all its arguments. class Boundary { bytes = 1024 * 1024 + constructor(vm) { + this.vm = vm + } charge(n) { this.bytes -= n if (this.bytes < 0) throw new LuaFault("InvalidDataValue", "Lua JSON exceeds the boundary byte limit") + // Core progress can retain each admitted call's input. Charge every copy, + // even when the guest reuses the very same value or callable path. + this.vm.traffic -= n + if (this.vm.traffic < 0) throw limit("Lua cumulative host traffic limit exceeded") + } + string(value) { + this.charge(Buffer.byteLength(JSON.stringify(value))) } } @@ -162,7 +172,9 @@ function table(v) { class Sandbox { fuel = 500_000 slice = 256 + nativeFuel = Infinity bytes = 8 * 1024 * 1024 + traffic = 4 * 1024 * 1024 frameLimit = 96 globals = new Table(this) protected = new Set() @@ -171,6 +183,8 @@ class Sandbox { charge(work) { this.fuel -= work this.slice -= work + this.nativeFuel -= work + if (this.nativeFuel < 0) throw limit("Lua native call work limit exceeded") if (this.fuel < 0) throw limit("Lua execution work limit exceeded") } poll(node) { @@ -302,14 +316,32 @@ class Sandbox { this.frames.length = base } } - if (fn instanceof Native) return fn.run(args) + if (fn instanceof Native) { + // These synchronous allowlisted functions never call back into Lua. + // Individually bounded inputs can still multiply (e.g. 1,024 large + // numeric strings supplied to math.max) inside a single scheduler step. + this.nativeFuel = 32768 + try { + return fn.run(args) + } finally { + this.nativeFuel = Infinity + } + } if (!(fn instanceof Host)) throw failure(`Cannot call Lua ${type(fn)}`) + // Even empty calls cause host admission and progress bookkeeping. + this.charge(256) const data = [] - const budget = new Boundary() + const budget = new Boundary(this) + budget.charge(2 + Math.max(0, args.length - 1) + 2 + Math.max(0, fn.path.length - 1)) + for (const name of fn.path) budget.string(name) for (const arg of args) data.push(yield* this.export(arg, new Set(), 0, budget)) const reply = yield { type: "call", path: fn.path.slice(), args: data, protected: this.barriers > 0 } - if (reply instanceof LuaHostFailure) throw failure(reply.message) - return [yield* this.import(reply, 0)] + const inbound = new Boundary(this) + if (reply instanceof LuaHostFailure) { + inbound.string(reply.message) + throw failure(reply.message) + } + return [yield* this.import(reply, 0, inbound)] } encode(s) { if (s.length > stringLimit) throw limit("Lua string exceeds the size limit") @@ -325,12 +357,18 @@ class Sandbox { throw invalid("Lua byte string is not valid UTF-8") } } - *import(v, depth) { + *import(v, depth, budget) { if (this.poll()) yield checkpoint if (depth > depthLimit) throw new LuaFault("InvalidDataValue", "Lua input exceeds the depth limit") - if (v === undefined || typeof v === "boolean" || Number.isFinite(v)) return v - if (v === null) return jsonNull - if (typeof v === "string") return this.encode(v) + if (v === undefined || v === null || typeof v === "boolean" || Number.isFinite(v)) { + budget.charge(v === undefined ? 4 : String(v).length) + return v === null ? jsonNull : v + } + if (typeof v === "string") { + const value = this.encode(v) + budget.string(v) + return value + } if (typeof v !== "object") throw invalid("Expected JSON data") const array = Array.isArray(v) const result = new Table(this, array) @@ -338,10 +376,17 @@ class Sandbox { // us. Never put the host container or function itself into the Lua heap. if (array) { if (v.length > entriesLimit) throw new LuaFault("InvalidDataValue", "Lua input exceeds the entry limit") - for (let i = 0; i < v.length; i++) result.set(i + 1, yield* this.import(v[i], depth + 1)) + budget.charge(2 + Math.max(0, v.length - 1)) + for (let i = 0; i < v.length; i++) result.set(i + 1, yield* this.import(v[i], depth + 1, budget)) } else { - for (const key in v) - if (Object.hasOwn(v, key)) result.set(this.encode(key), yield* this.import(v[key], depth + 1)) + budget.charge(2) + for (const key in v) { + if (!Object.hasOwn(v, key)) continue + const name = this.encode(key) + budget.string(key) + budget.charge(result.data.size ? 2 : 1) + result.set(name, yield* this.import(v[key], depth + 1, budget)) + } } return result } @@ -354,7 +399,7 @@ class Sandbox { } if (typeof v === "string") { const value = this.decode(v) - budget.charge(Buffer.byteLength(JSON.stringify(value))) + budget.string(value) return value } if (!(v instanceof Table)) throw invalid(`Cannot pass Lua ${type(v)} as JSON`) @@ -375,7 +420,8 @@ class Sandbox { } else { if (typeof k !== "string" || k === "__proto__") throw invalid("Invalid Lua JSON object key") const key = this.decode(k) - budget.charge(1 + Buffer.byteLength(JSON.stringify(key))) + budget.charge(1) + budget.string(key) out[key] = yield* this.export(item, seen, depth + 1, budget) } } @@ -437,9 +483,23 @@ function install(vm) { throw raised(value) }, pairs: ([v]) => { - vm.charge(table(v).data.size) - const iterator = table(v).data.entries() - return [new Native(() => iterator.next().value ?? []), v] + const seq = table(v) + vm.charge(seq.data.size) + // Iterating a live JS Map across arbitrary guest mutations can retain + // its deleted slots. Snapshot at most entriesLimit keys and skip deletes. + const keys = [...seq.data.keys()] + let i = 0 + return [ + new Native(() => { + while (i < keys.length) { + const key = keys[i++] + const value = seq.get(key) + if (value !== undefined) return [key, value] + } + return [] + }), + v, + ] }, ipairs: ([v]) => { const seq = table(v) @@ -550,10 +610,14 @@ export function* evaluateLua(source, options) { const program = new Closure(proto, null) program.env = vm.globals const run = (function* () { - for (const name of Object.keys(options.bindings ?? {})) - global(vm.encode(name), yield* vm.import(options.bindings[name], 0)) + const budget = new Boundary(vm) + for (const name of Object.keys(options.bindings ?? {})) { + const key = vm.encode(name) + budget.string(name) + global(key, yield* vm.import(options.bindings[name], 0, budget)) + } const values = yield* runFrames(vm, vm.frames, new Call(program, [])) - return yield* vm.export(values[0], new Set(), 0, new Boundary()) + return yield* vm.export(values[0], new Set(), 0, new Boundary(vm)) })() try { let step = run.next() diff --git a/packages/codemode/lua/test/sandbox.test.mjs b/packages/codemode/lua/test/sandbox.test.mjs index a20adca0a8d..59eb93bd23e 100644 --- a/packages/codemode/lua/test/sandbox.test.mjs +++ b/packages/codemode/lua/test/sandbox.test.mjs @@ -49,6 +49,37 @@ test("cancellation at either checkpoint cannot run the next statement", () => { assert.equal(parked.next("late reply").done, true) }) +test("reused arguments and replies cannot reset the cumulative host traffic budget", () => { + // JSON escapes each control byte to six characters. Every individual call + // fits under 1 MiB; the sixth copy (third reply) crosses the run-wide cap. + const payload = "\0".repeat(125000) + const vm = evaluateLua("local s=string.rep('\\000',125000); for i=1,10 do pcall(tools.save,s) end", options) + for (let i = 0; i < 3; i++) { + const call = resume(vm, i ? payload : undefined).value + assert.equal(call.type, "call") + assert.equal(call.args[0], payload) + } + assert.throws(() => resume(vm, payload), /cumulative host traffic limit/) +}) + +test("empty numeric and native generic loops yield repeatedly after entering the loop", () => { + for (const loop of ["for i=1,4096 do end", "for k,v in ipairs(t) do end"]) { + const vm = evaluateLua(`tools.start(); ${loop}; return 1`, { ...options, bindings: { t: Array(4096).fill(1) } }) + assert.equal(resume(vm).value.type, "call") + for (let i = 0; i < 3; i++) assert.equal(vm.next().value.type, "tick") + assert.equal(vm.return().done, true) + } +}) + +test("length and iterator setup charge table work, not the magnitude of numeric keys", () => { + assert.equal(run("local t={[2^52]='sparse'}; local n=0; for k in pairs(t) do n=n+1 end; return #t+n"), 1) + fault("return {[2^52]=42}", "InvalidDataValue", /contiguous/) + const t = Array(4096).fill(true) + for (const expr of ["#t", "pairs(t)"]) + assert.throws(() => run(`for i=1,200 do local n=${expr} end`, { t }), /work limit/) + assert.equal(run("local t={1,2,3}; local n=0; for k in pairs(t) do n=n+1; t[2]=nil; t[4]=4 end; return n"), 2) +}) + test("protected host calls unwind on ordinary errors, not interruption; errors keep Lua identity", () => { const vm = evaluateLua( `local function f() return tools.fail() end @@ -85,6 +116,7 @@ test("growing tables, strings and native operations charge before materializatio ["local s='a' for i=1,22 do s=s..s end", /size limit/], ["return table.concat({}, '', 1, 2^50)", /concat range/], ["local s=string.rep('x',200000); for i=1,64 do string.lower(s) end", /allocation limit/], + ["local n=string.rep('0',240000)..'1'; return math.max(n,n,n,n,n)", /native call work limit/], ["local t=tools; for i=1,64 do t=t.x end", /path exceeds/], ]) fault(`return pcall(function() ${source} end)`, "ExecutionFailure", msg)