18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-28 12:36: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:
jerrrrycho
2026-11-13 15:23:01 +01:00
parent 39e8bcc59d
commit acda6562cd
12 changed files with 520 additions and 55 deletions

View File

@@ -405,7 +405,8 @@ void state_serializer::deserialize_lua( cParser &Input, scene::scratch_data &Scr
std::string file;
Input >> file;
#ifdef WITH_LUA
simulation::Lua.interpret(Global.asCurrentSceneryPath + file);
// the file is relative to the scenery folder, and stays inside it
simulation::Lua.interpret(Global.asCurrentSceneryPath, file);
#else
ErrorLog(file + ": lua scripts not supported in this build.");
#endif