]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Improve loadstring error handling for Lua 5.4 compatibility
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 6 Dec 2025 11:09:03 +0000 (11:09 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 6 Dec 2025 11:09:03 +0000 (11:09 +0000)
Ensure loadstring results are checked for nil (syntax errors) before
passing to pcall. This prevents errors when running with Lua 5.4
compatibility where load behavior differs slightly or when handling
invalid Lua chunks.

lualib/lua_dkim_tools.lua
lualib/lua_util.lua
src/plugins/lua/clickhouse.lua

index 1942f4c2d8d5003ca68a552b34a305024fd39e8c..be446f8bc0bc604372f0e6932339d4e7b21864f4 100644 (file)
@@ -385,7 +385,15 @@ local function prepare_dkim_signing(N, task, settings)
       if type(settings.use_domain_custom) == 'string' then
         -- Load custom function
         local loadstring = loadstring or load
-        local ret, res_or_err = pcall(loadstring(settings.use_domain_custom))
+        local chunk, err = loadstring(settings.use_domain_custom)
+        local ret, res_or_err
+        if chunk then
+          ret, res_or_err = pcall(chunk)
+        else
+          ret = false
+          res_or_err = err
+        end
+
         if ret then
           if type(res_or_err) == 'function' then
             settings.use_domain_custom = res_or_err
index 091bcc8ceb1c8a2cbb76a72d17a542abffd80061..06571e638da82826f2f3fcda30b886bcadb41593 100644 (file)
@@ -1529,7 +1529,12 @@ exports.callback_from_string = function(s)
     inp = 'return function(...)\n' .. s .. '; end'
   end
 
-  local ret, res_or_err = pcall(loadstring(inp))
+  local chunk, err = loadstring(inp)
+  if not chunk then
+    return false, err
+  end
+
+  local ret, res_or_err = pcall(chunk)
 
   if not ret or type(res_or_err) ~= 'function' then
     return false, res_or_err
index 16a8ad4ec1dac106ed94b078fb00e4910f0052b4..2811206b90437a2ba3b65ddb263a864dbc28da14 100644 (file)
@@ -1364,7 +1364,14 @@ if opts then
         if rule.schema and rule.first_row and rule.get_row then
           local first_row, get_row
           local loadstring = loadstring or load
-          local ret, res_or_err = pcall(loadstring(rule.first_row))
+          local ret, res_or_err
+          local chunk, err = loadstring(rule.first_row)
+          if chunk then
+            ret, res_or_err = pcall(chunk)
+          else
+            ret = false
+            res_or_err = err
+          end
 
           if not ret or type(res_or_err) ~= 'function' then
             rspamd_logger.errx(rspamd_config, 'invalid first_row (%s) - must be a function',
@@ -1373,7 +1380,13 @@ if opts then
             first_row = res_or_err
           end
 
-          ret, res_or_err = pcall(loadstring(rule.get_row))
+          chunk, err = loadstring(rule.get_row)
+          if chunk then
+            ret, res_or_err = pcall(chunk)
+          else
+            ret = false
+            res_or_err = err
+          end
 
           if not ret or type(res_or_err) ~= 'function' then
             rspamd_logger.errx(rspamd_config,