18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-28 22:03:20 +01:00

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.
This commit is contained in:
jerrrrycho
2026-11-13 18:35:47 +01:00
parent acda6562cd
commit 8467eab8b4
4 changed files with 19 additions and 3 deletions

View File

@@ -216,7 +216,7 @@ and works with memcells, tracks and vehicles through the table `eu07.events` (`e
`memcell_update`, `writelog` and the rest, see `scripting/lua.cpp`). It shares the sandbox of the Lua screens:
- the file is a path relative to the scenery folder (`scenery/`), and stays inside it: no `..`, no drive, no leading
`/`. It is text, of at most 16 MB; precompiled code is not loaded;
`/`. `/` and `\` both separate folders, on Linux too. It is text, of at most 16 MB; precompiled code is not loaded;
- the script gets the base library, `table`, `string`, `math` and `bit`, and of `os` only the clock: `os.time`,
`os.clock`, `os.difftime` and `os.date` (with the C89 conversions only, `%Y`, `%d`, `%H` and so on). It gets no
`io`, the rest of `os`, `ffi`, `debug`, `jit`, `package`, `require`, `dofile`, `load`, `loadfile` or `loadstring`;

View File

@@ -12,6 +12,7 @@ http://mozilla.org/MPL/2.0/.
#include "scripting/scriptscreenrules.h"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <system_error>
@@ -59,6 +60,13 @@ int api_date(lua_State *L)
} // namespace
std::string with_forward_slashes(std::string_view const Path)
{
std::string result{Path};
std::ranges::replace(result, '\\', '/');
return result;
}
void open_safe_libraries(lua_State *L)
{
open_library(L, luaopen_base, "");
@@ -121,7 +129,7 @@ std::optional<std::string> read_script_file(std::string const &Folder, std::stri
}
// a folder or a device (CON, NUL, COM1...) isn't a regular file. in the code page of the system, as the game opens
// its files
std::filesystem::path const path{Folder + std::string{Path}};
std::filesystem::path const path{Folder + with_forward_slashes(Path)};
std::error_code error;
if (false == std::filesystem::is_regular_file(path, error))
{

View File

@@ -38,8 +38,13 @@ void open_clock_library(lua_State *L);
// stack
bool load_text_chunk(lua_State *L, std::string_view Code, std::string const &Name);
// Path with each '\' turned into '/', as the game reads the paths in its data (replace_slashes): a scenery made on
// Windows may name its folders either way, and on Linux only '/' separates them
std::string with_forward_slashes(std::string_view Path);
// the file Path of Folder, when Path is a relative path inside Folder (no '..', no drive, no leading separator) and
// names a regular file of at most max_script_file bytes. nothing otherwise
// names a regular file of at most max_script_file bytes. nothing otherwise. '\' separates folders in Path as '/' does,
// on every system
std::optional<std::string> read_script_file(std::string const &Folder, std::string_view Path);
// reads the script Path of Folder (as read_script_file), loads it as text and runs it in protected mode. nothing when

View File

@@ -148,8 +148,11 @@ TEST_CASE("lua sandbox: a scenery reads its scripts only from its own folder") {
CHECK(lua_sandbox::read_script_file(folder, "test.lua") == std::optional<std::string>{"return 2"});
CHECK(lua_sandbox::read_script_file(folder, "scripts/module.lua") == std::optional<std::string>{"return 3"});
// '\' separates the folders on Linux too, as in the rest of the scenery's paths
CHECK(lua_sandbox::with_forward_slashes("a\\b/c\\d.lua") == "a/b/c/d.lua");
CHECK(lua_sandbox::read_script_file(folder, "scripts\\module.lua") == std::optional<std::string>{"return 3"});
for (auto const &path : {std::string{"../outside.lua"}, std::string{"scripts/../../outside.lua"}, std::string{"..\\outside.lua"},
std::string{"scripts\\..\\..\\outside.lua"}, std::string{"scripts/..\\../outside.lua"},
root.path("outside.lua"), std::string{"/outside.lua"}, std::string{"\\outside.lua"}, std::string{"C:outside.lua"},
std::string{"missing.lua"}, std::string{"folder.lua"}, std::string{"scripts"}, std::string{}}) {
CAPTURE(path);