mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-28 22:03:20 +01:00
fix(scripting): sandbox the scenery Lua scripts like the Lua screens
The scenery scripts (lua <file> in the .scn) ran in a state with all of
LuaJIT's libraries (io, os, ffi, debug, require, load*), loaded with
luaL_dofile from any path, '..' and absolute ones included, and their
errors in event handlers went through an unprotected lua_call.
- scripting/luasandbox.{h,cpp}: what the Lua states share: the safe
library set (moved from lua_program::open_libraries), loading a text-only
chunk (moved from load_chunk), reading a script from a folder through the
screens' path check (regular file, at most 16 MB), running it under pcall,
and a clock-only os table (time, clock, difftime, date with the C89
conversions; MSVC's strftime ends the program on an unknown one).
- lua.cpp: the scenery state gets the safe set and the clock; interpret()
takes the scenery folder and the file and logs a refused or failing script
with its file name; event handlers run under pcall and are logged with the
event name; the scriptapi functions check their string argument
(luaL_checkstring) instead of making a std::string from NULL; a memcell
table without str updates the text to "" instead of from NULL.
- luaprogram.cpp: calls the shared helpers, no change for the screens.
- eu07.ini lua.fulllibraries (default no) gives trusted sceneries all
libraries back. Documented in script_screens_doc.md.
- tests/unit/test_lua_sandbox.cpp: 8 cases for the shared sandbox.
This commit is contained in:
@@ -10,6 +10,7 @@ struct memcell_values { const char *str; double num1; double num2; };
|
||||
class lua
|
||||
{
|
||||
lua_State *state;
|
||||
bool full_libraries = false; // lua.fulllibraries: all of LuaJIT's libraries opened
|
||||
|
||||
static int atpanic(lua_State *s);
|
||||
|
||||
@@ -41,7 +42,9 @@ public:
|
||||
~lua();
|
||||
|
||||
std::string get_error() const;
|
||||
void interpret(const std::string& file) const;
|
||||
// runs the script file, a relative path inside folder (the scenery folder), sandboxed (scripting/luasandbox.h).
|
||||
// a script that can't be read or fails is logged
|
||||
void interpret(const std::string &folder, const std::string &file);
|
||||
static void unref(lua_State *L, int ref);
|
||||
static void dispatch_event(lua_State *L, int handler, basic_event *event, const TDynamicObject *activator);
|
||||
static void push_memcell_values(lua_State *L, const TMemCell *mc);
|
||||
|
||||
Reference in New Issue
Block a user