Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Unsandbox Lua scripts (#26365)
Richard Feldman6da099a9d7b7
diff --git a/crates/assistant_scripting/src/sandbox_preamble.lua b/crates/assistant_scripting/src/sandbox_preamble.luaindex 22cde87badf..fe4445ed6a1 100644--- a/crates/assistant_scripting/src/sandbox_preamble.lua+++ b/crates/assistant_scripting/src/sandbox_preamble.lua@@ -3,6 +3,16 @@-- Create a sandbox environmentlocal sandbox = {}+-- For now, add all globals to `sandbox` (so there effectively is no sandbox).+-- We still need the logic below so that we can do things like overriding print() to write+-- to our in-memory log rather than to stdout, we will delete this loop (and re-enable+-- the I/O module being sandboxed below) to have things be sandboxed again.+for k, v in pairs(_G) do+ if sandbox[k] == nil then+ sandbox[k] = v+ end+end+-- Allow access to standard libraries (safe subset)sandbox.string = stringsandbox.table = table@@ -25,8 +35,7 @@ local io = {}io.open = sb_io_open-- Add the sandboxed io library to the sandbox environment-sandbox.io = io-+-- sandbox.io = io -- Uncomment this line to re-enable sandboxed file I/O.-- Load the script with the sandbox environmentlocal user_script_fn, err = load(user_script, nil, "t", sandbox)diff --git a/crates/assistant_scripting/src/session.rs b/crates/assistant_scripting/src/session.rsindex 08ed3f805a5..1e26bb5bebe 100644--- a/crates/assistant_scripting/src/session.rs+++ b/crates/assistant_scripting/src/session.rs@@ -119,6 +119,14 @@ impl ScriptSession {let lua = Lua::new();lua.set_memory_limit(2 * 1024 * 1024 * 1024)?; // 2 GBlet globals = lua.globals();++ // Use the project root dir as the script's current working dir.+ if let Some(root_dir) = &root_dir {+ if let Some(root_dir) = root_dir.to_str() {+ globals.set("cwd", root_dir)?;+ }+ }+globals.set("sb_print",lua.create_function({