18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 22:55:16 +01:00
Commit Graph

4 Commits

Author SHA1 Message Date
jerrrrycho
3ae8d4e72e fix(scripting): typed scenery handles, sandbox keys from eu07.ini only, GCC 13 build
- the scenery API (eu07.events) hands out a full userdata of its own locked
  metatable for each kind of object (event, track, isolated, vehicle,
  memcell), checked with luaL_checkudata: a handle of another kind, a light
  userdata or any other value is a Lua error, not an object taken for
  another. nil stays "no object". one object has one handle while the
  script holds it, so handles compare and key tables as before. event
  handlers get their handles built inside lua_cpcall, and nil for no
  activator
- memcell_values keeps its text as a std::string: the text of a number was
  read from a stack slot popped before use
- newproxy is gone from the sandboxed libraries
- a config block of a scenery no longer changes what only eu07.ini sets
  (lua.fulllibraries, execonexit, extcam.cmd/rec, screenshotsdir,
  network.server/client, zmq.address, motiontelemetry, uart, feedbackmode,
  feedbackport, python/luascreens/wasm.enabled): kept before its keys are
  read, put back before FinalizeConfig(), each attempt logged
- open_file_library() iterates luaL_Reg instead of a std::pair GCC 13
  deduces as a pair of an array and a function
2026-11-16 08:54:00 +01:00
jerrrrycho
fde6d10f32 feat(scripting): file functions for the scenery Lua scripts
The sandbox took io away from the scenery scripts; this gives them files
back the way the Lua screens have them. The screens have
eu07.read_file(path); the scenery state now gets it too, together with a
write function and one to read the written files back:

- eu07.read_file(path): a file of the scenery folder (scenery/), or nil.
  The path rule of `lua <file>` and the screens (relative, no '..', no
  drive, no leading separator), a regular file of at most 16 MB.
- eu07.write_file(path, text): writes the file in the scenery's data
  folder, replacing it, and makes the folders of path. true, or nil and
  the reason.
- eu07.read_data_file(path): a file of the data folder, or nil.

The data folder is per scenery and among the player's files, next to the
user eu07.ini and eu07_odometers.txt (user_config_path):
%APPDATA%/MaSzyna/scenery_data/<scenery without .scn>/, ~/.config/MaSzyna/
on Linux. Nothing is written into the game folder; with no user folder,
writes are refused. The log names the folder when the scenery loads its
first script.

Writes are limited to 1 MB a file, 256 files and 16 MB a data folder, and
to relative paths of at most 200 bytes and 8 folders, of portable names:
no '.', '..', empty names, :<>"|?* or control characters, no trailing dot
or space, no Windows device names (con, nul, com1, lpt1...). The parent
folder is checked with canonical paths, so a link in the data folder
doesn't lead a write out of it.

The code is in scripting/luasandbox.{h,cpp} (read_file, which
read_script_file now uses, is_safe_write_path, write_data_file,
scenery_data_folder, open_file_library); lua.cpp only opens the functions
and sets the folders in interpret(). The screens keep their own
read_screen_file, which #356 changes. Documented in the "Scenery scripts"
section of script_screens_doc.md; 8 new cases in test_lua_sandbox.cpp.
2026-11-13 18:55:19 +01:00
jerrrrycho
8467eab8b4 fix(scripting): read '\' as a folder separator in scenery script paths on Linux too
The unit test reading "scripts\module.lua" failed on Linux/GCC: there '\'
is part of a file name, not a separator. The game already reads '\' in its
data paths as '/' (replace_slashes, the model and texture names, the
parser's include tokens), and sceneries made on Windows often name their
folders with it, so the scenery scripts do the same: read_script_file
turns '\' into '/' (lua_sandbox::with_forward_slashes) after the path
check. is_safe_relative_path splits on both separators on every system,
so '..\' and 'a\..\..\' stay refused; the test now checks those too.
2026-11-13 18:35:47 +01:00
jerrrrycho
acda6562cd 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.
2026-11-13 15:23:01 +01:00