]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Known senders: More recipients test logic 5428/head
authorVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 22 May 2025 09:29:14 +0000 (10:29 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 22 May 2025 09:29:14 +0000 (10:29 +0100)
src/plugins/lua/known_senders.lua
test/functional/cases/400_known_senders.robot

index cefdcc44c9d5a317a531c2b6c983db2f88b1265b..0cbf3cdcf17bdab432c36376d83790327c4a2e92 100644 (file)
@@ -106,21 +106,26 @@ local function configure_scripts(_, _, _)
   -- script checks if given recipients are in the local replies set of the sender
   local redis_zscore_script = [[
     local replies_recipients_addrs = ARGV
-    if replies_recipients_addrs then
+    if replies_recipients_addrs and #replies_recipients_addrs > 0 then
+      local found = false
       for _, rcpt in ipairs(replies_recipients_addrs) do
         local score = redis.call('ZSCORE', KEYS[1], rcpt)
-        -- check if score is nil (for some reason redis script does not see if score is a nil value)
-        if type(score) == 'boolean' then
-          score = nil
-          -- 0 is stand for failure code
-          return 0
+        if score then
+          -- If we found at least one recipient, consider it a match
+          found = true
+          break
         end
       end
-      -- first number in return statement is stands for the success/failure code
-      -- where success code is 1 and failure code is 0
-      return 1
+
+      if found then
+        -- Success code is 1
+        return 1
+      else
+        -- Failure code is 0
+        return 0
+      end
     else
-    -- 0 is a failure code
+      -- No recipients to check, failure code is 0
       return 0
     end
   ]]
@@ -261,6 +266,12 @@ local function verify_local_replies_set(task)
 
   local replies_recipients = task:get_recipients('smtp') or E
 
+  -- If no recipients, don't proceed
+  if #replies_recipients == 0 then
+    lua_util.debugm(N, task, 'No recipients to verify')
+    return nil
+  end
+
   local replies_sender_string = lua_util.maybe_obfuscate_string(tostring(replies_sender), settings,
       settings.sender_prefix)
   local replies_sender_key = make_key_replies(replies_sender_string:lower(), 8)
@@ -268,13 +279,16 @@ local function verify_local_replies_set(task)
   local function redis_zscore_script_cb(err, data)
     if err ~= nil then
       rspamd_logger.errx(task, 'Could not verify %s local replies set %s', replies_sender_key, err)
-    end
-    if data ~= 1 then
-      lua_util.debugm(N, task, 'Recipients were not verified')
       return
     end
-    lua_util.debugm(N, task, 'Recipients were verified')
-    task:insert_result(settings.symbol_check_mail_local, 1.0, replies_sender_key)
+
+    -- We need to ensure we're properly checking the result
+    if data == 1 then
+      lua_util.debugm(N, task, 'Recipients were verified')
+      task:insert_result(settings.symbol_check_mail_local, 1.0, replies_sender_key)
+    else
+      lua_util.debugm(N, task, 'Recipients were not verified, data=%s', data)
+    end
   end
 
   local replies_recipients_addrs = {}
@@ -284,12 +298,24 @@ local function verify_local_replies_set(task)
     table.insert(replies_recipients_addrs, replies_recipients[i].addr)
   end
 
-  lua_util.debugm(N, task, 'Making redis request to local replies set')
-  lua_redis.exec_redis_script(zscore_script_id,
+  -- Only proceed if we have recipients to check
+  if #replies_recipients_addrs == 0 then
+    lua_util.debugm(N, task, 'No recipient addresses to verify')
+    return nil
+  end
+
+  lua_util.debugm(N, task, 'Making redis request to local replies set with key %s and recipients %s',
+      replies_sender_key, table.concat(replies_recipients_addrs, ", "))
+
+  local ret = lua_redis.exec_redis_script(zscore_script_id,
       { task = task, is_write = true },
       redis_zscore_script_cb,
       { replies_sender_key },
       replies_recipients_addrs)
+
+  if not ret then
+    rspamd_logger.errx(task, "redis script request wasn't scheduled")
+  end
 end
 
 local function check_known_incoming_mail_callback(task)
index ea5fdea1e64c76851d4d9dcf3a14d6b519ff3bdd..a7cde59cb86b93c1d204c9566430e4b745a7b769 100644 (file)
@@ -43,7 +43,7 @@ INCOMING MAIL SENDER IS UNKNOWN
   ...  Settings={symbols_enabled [${SYMBOL_GLOBAL}, ${SYMBOL_LOCAL}]}
   Do Not Expect Symbol  ${SYMBOL_GLOBAL}
   Do Not Expect Symbol  ${SYMBOL_LOCAL}
-  
+
 INCOMING MAIL SENDER IS KNOWN RECIPIENTS ARE UNKNOWN
   Scan File  ${RSPAMD_TESTDIR}/messages/set_replyto_1_1.eml
   ...  IP=8.8.8.8
@@ -64,16 +64,16 @@ INCOMING MAIL SENDER IS KNOWN RECIPIENTS ARE UNKNOWN
 
 INCOMING MAIL SENDER IS KNOWN RECIPIENTS ARE KNOWN
   Scan File  ${RSPAMD_TESTDIR}/messages/set_replyto_1_1.eml
-  ...  IP=8.8.8.8  User=user@emailbl.com
+  ...  IP=8.8.8.8  User=user@emailbl.com  From=user@emailbl.com
   ...  Settings=${SETTINGS_REPLIES}
   Scan File  ${RSPAMD_TESTDIR}/messages/replyto_1_1.eml
-  ...  IP=8.8.8.8  User=user@emailbl.com
+  ...  IP=8.8.8.8  User=user@emailbl.com  Rcpt=user@emailbl.com
   ...  Settings=${SETTINGS_REPLIES}
   Scan File  ${RSPAMD_TESTDIR}/messages/inc_mail_known_sender.eml
-  ...  IP=8.8.8.8  User=user@emailbl.com
+  ...  IP=8.8.8.8  User=user@emailbl.com  Rcpt=user@emailbl.com
   ...  Settings=${SETTINGS_REPLIES}
   Scan File  ${RSPAMD_TESTDIR}/messages/inc_mail_known_sender.eml
-  ...  IP=8.8.8.8  User=user@emailbl.com
+  ...  IP=8.8.8.8  User=user@emailbl.com  Rcpt=user@emailbl.com
   ...  Settings={symbols_enabled [${SYMBOL_GLOBAL}, ${SYMBOL_LOCAL}]}
   Expect Symbol  ${SYMBOL_GLOBAL}
   Expect Symbol  ${SYMBOL_LOCAL}