From b8d8d3c4d1a4658a024ab8821de00987f939ddc6 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Mon, 9 Aug 2021 14:30:55 +0200 Subject: [PATCH] Fix SQLite3 requiring for trusted modlib --- init.lua | 6 ++++++ persistence.lua | 9 ++++++++- persistence/sqlite3.lua | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index bc4c56e..5ccb050 100644 --- a/init.lua +++ b/init.lua @@ -132,6 +132,12 @@ modlib = setmetatable({ modlib.mod = minetest and dofile(get_resource(modlib.modname, "mod.lua")) if minetest then + local ie = minetest.request_insecure_environment() + if ie then + -- Force load persistence namespace to pass insecure require + -- TODO currently no need to set _G.require, lsqlite3 loads no dependencies that way + modlib.persistence = assert(loadfile(get_resource"persistence.lua"))(ie.require) + end modlib.conf.build_setting_tree() modlib.mod.get_resource = get_resource end diff --git a/persistence.lua b/persistence.lua index a00a7b3..8940ba7 100644 --- a/persistence.lua +++ b/persistence.lua @@ -1,3 +1,4 @@ +local require = ... or require -- TODO consider moving serializers in this namespace local function load(module_name) return assert(loadfile(modlib.mod.get_resource(modlib.modname, "persistence", module_name .. ".lua"))) @@ -9,7 +10,13 @@ local _ENV = setmetatable({}, {__index = function(_ENV, module_name) return module end if module_name == "sqlite3" then - local module = load(module_name) + local func = load(module_name) + local module = function(sqlite3) + if sqlite3 then + return func(sqlite3) + end + return func(require"lsqlite3") + end _ENV[module_name] = module return module end diff --git a/persistence/sqlite3.lua b/persistence/sqlite3.lua index 4c6e09b..a49c213 100644 --- a/persistence/sqlite3.lua +++ b/persistence/sqlite3.lua @@ -1,7 +1,7 @@ local assert, error, math_huge, modlib, minetest, setmetatable, type, table_insert, table_sort, pairs, ipairs = assert, error, math.huge, modlib, minetest, setmetatable, type, table.insert, table.sort, pairs, ipairs -local sqlite3 = ... or require"lsqlite3" +local sqlite3 = ... --! experimental