18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/scripting/scriptscreenrules.cpp
jerrrrycho 1bd7187db7 fix(scripting): close the small holes in the script screen sandbox
- Lua: once the instruction budget of a call is spent, the count hook
  runs at every instruction and raises again, so pcall, nested pcall and
  coroutine.resume can't swallow the limit; xpcall is rebuilt on pcall
  so its handler runs after the error left f, while the hook works
- Lua: the metatables of eu07.image/font/sprite are locked
  (__metatable = false); __gc destroys only an object of its own type,
  once
- Lua: read_png checks the size with stbi_info before decoding
- Lua: string.rep makes no string over 1 MiB
- Lua and WASM: at most 64 commands per call, names up to 64 bytes,
  32 log lines per call cut at 1024 bytes; the drops are logged once
- fonts: a glyph wider than 4096 is skipped; more than 4096 glyphs,
  8192 columns or an image side over 4096 refuse the font; cut_glyph
  reads only the columns inside the image
- the script path from the .mmd (cab screens, destination signs,
  make: textures) must stay inside the game folder; the files a screen
  reads, its script and its module must be regular files of 16 MiB at
  most

Tests for each in test_script_screen_host.cpp, test_script_screen_drawing.cpp
and test_script_screens.cpp; the test stubs can capture the log.
2026-11-13 14:25:18 +01:00

138 lines
2.9 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "scripting/scriptscreenrules.h"
#include "utilities/parser.h"
#include <algorithm>
namespace script_screens
{
void screen_config::deserialize(cParser &Input)
{
while (deserialize_mapping(Input))
{
// all work done by while()
}
}
bool screen_config::deserialize_mapping(cParser &Input)
{
// token can be a key or block end
auto const key{Input.getToken<std::string>(true, "\n\r\t ,;[]")};
if (key.empty() || key == "}")
{
return false;
}
if (key == "{")
{
script = Input.getToken<std::string>();
}
else if (key == "target:")
{
target = Input.getToken<std::string>();
}
else if (key == "frames:")
{
frames = Input.getToken<int>();
}
else if (key == "parameters:")
{
parameters = Input.getToken<std::string>();
}
else if (key == "instance:")
{
instancing = Input.getToken<std::string>();
}
else if (key == "background:")
{
background = Input.getToken<std::string>();
}
else
{
// a key of a later version: skip its value
Input.getToken<std::string>();
}
return true;
}
frame_schedule::frame_schedule(std::uint64_t const Start, int const Interval, int const Phase)
: m_interval{std::max(Interval, 1)}, m_next{Start + static_cast<std::uint64_t>(std::max(Phase, 0))}
{
}
int frame_schedule::effective_interval(int const Configured, int const Requested)
{
return std::max({Configured, Requested, 1});
}
int frame_schedule::phase(std::string_view const Key, int const Interval)
{
if (Interval <= 1)
{
return 0;
}
// FNV-1a: the same offset for the same name on every compiler
std::uint32_t hash{2166136261u};
for (auto const character : Key)
{
hash ^= static_cast<std::uint8_t>(character);
hash *= 16777619u;
}
return static_cast<int>(hash % static_cast<std::uint32_t>(Interval));
}
bool frame_schedule::poll(std::uint64_t const Frame)
{
if (Frame < m_next)
{
return false;
}
m_next = Frame + static_cast<std::uint64_t>(m_interval);
return true;
}
bool is_safe_relative_path(std::string_view const Path)
{
if (Path.empty() || Path.front() == '/' || Path.front() == '\\' || Path.find(':') != std::string_view::npos)
{
return false;
}
std::size_t start{0};
while (start <= Path.size())
{
auto const end{std::min(Path.find_first_of("/\\", start), Path.size())};
if (Path.substr(start, end - start) == "..")
{
return false;
}
start = end + 1;
}
return true;
}
std::optional<std::string> screen_script_path(std::string const &Folder, std::string const &Script)
{
// as substr_path() tells a folder
auto path{Script.find('/') == std::string::npos ? Folder + Script : Script};
if (false == is_safe_relative_path(path))
{
return std::nullopt;
}
return path;
}
} // namespace script_screens