From 47c000a2938b63b1b6c12b555d41e88f29f37faa Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 26 Feb 2025 19:16:41 +0100 Subject: [PATCH] Add unittest that lints builtin JSON files --- games/devtest/mods/unittests/misc.lua | 23 +++++++++++++++++++++++ src/script/cpp_api/s_security.cpp | 7 +++++++ 2 files changed, 30 insertions(+) diff --git a/games/devtest/mods/unittests/misc.lua b/games/devtest/mods/unittests/misc.lua index d01eed17d..65dc3259e 100644 --- a/games/devtest/mods/unittests/misc.lua +++ b/games/devtest/mods/unittests/misc.lua @@ -189,6 +189,29 @@ local function test_write_json() end unittests.register("test_write_json", test_write_json) +local function lint_json_files() + -- Check that files we ship with Luanti are valid JSON + local stack = {core.get_builtin_path()} + local checked = 0 + while #stack > 0 do + local path = table.remove(stack) + for _, name in ipairs(core.get_dir_list(path, true)) do + stack[#stack+1] = path .. "/" .. name + end + for _, name in ipairs(core.get_dir_list(path, false)) do + if name:match("%.json$") then + local f = io.open(path .. "/" .. name, "rb") + print(path .. "/" .. name) + assert(core.parse_json(f:read("*all"), -1) ~= nil) + f:close() + checked = checked + 1 + end + end + end + assert(checked > 0, "no files found?!") +end +unittests.register("lint_json_files", lint_json_files) + local function test_game_info() local info = core.get_game_info() local game_conf = Settings(info.path .. "/game.conf") diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 685403b4b..834650fdc 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -659,6 +659,13 @@ bool ScriptApiSecurity::checkPathWithGamedef(lua_State *L, } } + // Allow read-only access to builtin + if (!write_required) { + str = fs::AbsolutePath(Server::getBuiltinLuaPath()); + if (!str.empty() && fs::PathStartsWith(abs_path, str)) + return true; + } + // Allow read-only access to game directory if (!write_required) { const SubgameSpec *game_spec = gamedef->getGameSpec();