From: Peter van Dijk Date: Thu, 9 Oct 2025 11:22:54 +0000 (+0200) Subject: store debug.traceback function before user can hide it from us X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3dc2224362f82cb281b7202811af8291cbac63f9;p=thirdparty%2Fpdns.git store debug.traceback function before user can hide it from us Signed-off-by: Peter van Dijk --- diff --git a/ext/luawrapper/include/LuaContext.hpp b/ext/luawrapper/include/LuaContext.hpp index b7de2c1c43..71d25d0615 100644 --- a/ext/luawrapper/include/LuaContext.hpp +++ b/ext/luawrapper/include/LuaContext.hpp @@ -67,6 +67,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #endif #define LUACONTEXT_GLOBAL_EQ "e5ddced079fc405aa4937b386ca387d2" +#define LUACONTEXT_DEBUG_TRACEBACK "8d143f0add7ad7fa323d0d1d12f3f114" // I would prefer a lightuserdata in the registry index #define EQ_FUNCTION_NAME "__eq" #define TOSTRING_FUNCTION_NAME "__tostring" @@ -113,7 +114,12 @@ public: if (openDefaultLibs) luaL_openlibs(mState); - writeGlobalEq(); + writeGlobalEq(); + lua_pushliteral(mState, LUACONTEXT_DEBUG_TRACEBACK); // ref + lua_getglobal(mState, "debug"); // ref, debug + lua_getfield(mState, -1, "traceback"); // ref, debug, traceback + lua_remove(mState, -2); // ref, traceback + lua_settable(mState, LUA_REGISTRYINDEX); // [] } void writeGlobalEq() { @@ -1418,9 +1424,8 @@ private: } static int gettraceback(lua_State* L) { - lua_getglobal(L, "debug"); // stack: error, debug library - lua_getfield(L, -1, "traceback"); // stack: error, debug library, debug.traceback function - lua_remove(L, -2); // stack: error, debug.traceback function + lua_pushliteral(L, LUACONTEXT_DEBUG_TRACEBACK); // stack: error, ref + lua_rawget(L, LUA_REGISTRYINDEX); // stack: error, debug.traceback lua_pushstring(L, ""); // stack: error, debug.traceback, "" lua_pushinteger(L, 2); // stack: error, debug.traceback, "", 2 lua_call(L, 2, 1); // stack: error, traceback diff --git a/regression-tests.dnsdist/test_Lua.py b/regression-tests.dnsdist/test_Lua.py index d81054f2b3..dc2372fd07 100644 --- a/regression-tests.dnsdist/test_Lua.py +++ b/regression-tests.dnsdist/test_Lua.py @@ -184,3 +184,22 @@ class TestLuaPoolBindings(DNSDistTest): sender = getattr(self, method) (_, receivedResponse) = sender(query, response=response) self.assertEqual(receivedResponse, response) + +class TestLuaError(DNSDistTest): + _consoleKey = DNSDistTest.generateConsoleKey() + _consoleKeyB64 = base64.b64encode(_consoleKey).decode('ascii') + + _config_params = ['_consoleKeyB64', '_consolePort'] + _config_template = """ + setKey("%s") + controlSocket("127.0.0.1:%s") + + debug = nil + """ + + def testLuaError(self): + """ + LuaError: Test exception handling while debug module is obscured + """ + res = self.sendConsoleCommand('error("expected" .. " " .. "error")') + self.assertIn('expected error', res)