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
16
17-- Access to custom functions
18sandbox.search = search
19sandbox.outline = outline
20
21-- Create a sandboxed version of LuaFileIO
22local io = {}
23
24-- File functions
25io.open = sb_io_open
26
27-- Add the sandboxed io library to the sandbox environment
28sandbox.io = io
29
30
31-- Load the script with the sandbox environment
32local user_script_fn, err = load(user_script, nil, "t", sandbox)
33
34if not user_script_fn then
35  error("Failed to load user script: " .. tostring(err))
36end
37
38-- Execute the user script within the sandbox
39local success, result = pcall(user_script_fn)
40
41if not success then
42  error("Error executing user script: " .. tostring(result))
43end