sandbox_preamble.lua

 1---@diagnostic disable: undefined-global
 2
 3-- Create a sandbox environment
 4local sandbox = {}
 5
 6-- Allow access to standard libraries (safe subset)
 7sandbox.string = string
 8sandbox.table = table
 9sandbox.math = math
10sandbox.print = sb_print
11sandbox.type = type
12sandbox.tostring = tostring
13sandbox.tonumber = tonumber
14sandbox.pairs = pairs
15sandbox.ipairs = ipairs
16sandbox.search = search
17
18-- Create a sandboxed version of LuaFileIO
19local io = {}
20
21-- File functions
22io.open = sb_io_open
23
24-- Add the sandboxed io library to the sandbox environment
25sandbox.io = io
26
27
28-- Load the script with the sandbox environment
29local user_script_fn, err = load(user_script, nil, "t", sandbox)
30
31if not user_script_fn then
32  error("Failed to load user script: " .. tostring(err))
33end
34
35-- Execute the user script within the sandbox
36local success, result = pcall(user_script_fn)
37
38if not success then
39  error("Error executing user script: " .. tostring(result))
40end