feat(lua): expose io.open (limited to scripts_config folder) (#2495)
This commit is contained in:
@ -16,8 +16,8 @@
|
||||
#include "bindings/stats.hpp"
|
||||
#include "bindings/tunables.hpp"
|
||||
#include "bindings/vector.hpp"
|
||||
#include "bindings/weapons.hpp"
|
||||
#include "bindings/vehicles.hpp"
|
||||
#include "bindings/weapons.hpp"
|
||||
#include "file_manager.hpp"
|
||||
#include "script_mgr.hpp"
|
||||
|
||||
@ -96,6 +96,7 @@ namespace big
|
||||
sol::lib::math,
|
||||
sol::lib::table,
|
||||
sol::lib::bit32,
|
||||
sol::lib::io,
|
||||
sol::lib::utf8
|
||||
);
|
||||
// clang-format on
|
||||
@ -179,6 +180,45 @@ namespace big
|
||||
m_state["os"] = sandbox_os;
|
||||
}
|
||||
|
||||
void lua_module::sandbox_lua_io_library()
|
||||
{
|
||||
auto io = m_state["io"];
|
||||
sol::table sandbox_io(m_state, sol::create);
|
||||
|
||||
m_io_open = io["open"];
|
||||
sandbox_io["open"] = [this](const std::string& filename, const std::string& mode) {
|
||||
constexpr auto make_absolute = [](const std::filesystem::path& root, const std::filesystem::path& user_path) -> std::optional<std::filesystem::path> {
|
||||
auto final_path = std::filesystem::weakly_canonical(root / user_path);
|
||||
|
||||
auto [root_end, nothing] = std::mismatch(root.begin(), root.end(), final_path.begin());
|
||||
|
||||
if (root_end != root.end())
|
||||
return std::nullopt;
|
||||
|
||||
return final_path;
|
||||
};
|
||||
|
||||
const auto scripts_config_sub_path = make_absolute(g_lua_manager->get_scripts_config_folder().get_path(), filename);
|
||||
if (!scripts_config_sub_path)
|
||||
{
|
||||
LOG(WARNING) << "io.open is restricted to the scripts_config folder, and the filename provided (" << filename << ") is outside of it.";
|
||||
|
||||
return sol::reference(sol::lua_nil);
|
||||
}
|
||||
|
||||
const auto res = m_io_open(scripts_config_sub_path.value().u8string().c_str(), mode).get<sol::reference>();
|
||||
|
||||
if (res.get_type() == sol::type::lua_nil)
|
||||
{
|
||||
LOG(WARNING) << "Couldn't io.open a file called " << filename << " mode (" << mode << "). Note that io.open is restricted to the scripts_config folder.";
|
||||
}
|
||||
|
||||
return res;
|
||||
};
|
||||
|
||||
m_state["io"] = sandbox_io;
|
||||
}
|
||||
|
||||
template<size_t N>
|
||||
static constexpr auto not_supported_lua_function(const char (&function_name)[N])
|
||||
{
|
||||
@ -216,6 +256,7 @@ namespace big
|
||||
// https://blog.rubenwardy.com/2020/07/26/sol3-script-sandbox/
|
||||
// https://www.lua.org/manual/5.4/manual.html#pdf-require
|
||||
sandbox_lua_os_library();
|
||||
sandbox_lua_io_library();
|
||||
sandbox_lua_loads(scripts_folder);
|
||||
|
||||
lua::log::bind(m_state);
|
||||
|
Reference in New Issue
Block a user