commit 6b6c4af: [Fix] Apply the similar fix to the url_reputation

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Mar 10 21:35:05 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-03-10 21:29:33 +0000
URL: https://github.com/rspamd/rspamd/commit/6b6c4af8dc90b38592ab0268aa2a3e66e1d8aa1f (HEAD -> master)

[Fix] Apply the similar fix to the url_reputation
Issue: #4100

---
 src/plugins/lua/reputation.lua | 37 ++++++++++++++++++++++++++++++-------
 1 file changed, 30 insertions(+), 7 deletions(-)

diff --git a/src/plugins/lua/reputation.lua b/src/plugins/lua/reputation.lua
index 52c6c8ee7..01c81d481 100644
--- a/src/plugins/lua/reputation.lua
+++ b/src/plugins/lua/reputation.lua
@@ -302,30 +302,53 @@ end
 
 local function url_reputation_filter(task, rule)
   local requests = gen_url_queries(task, rule)
+  local url_keys = lua_util.keys(requests)
+  local requests_left = #url_keys
   local results = {}
-  local nchecked = 0
 
   local function indexed_tokens_cb(err, index, values)
-    nchecked = nchecked + 1
+    requests_left = requests_left - 1
 
     if values then
       results[index] = values
     end
 
-    if nchecked == #requests then
+    if requests_left == 0 then
       -- Check the url with maximum hits
       local mhits = 0
+
+      local result_request_match_tbl = {}
+      -- XXX: ugly O(N^2) loop to match requests and responses
+      for result_k,_ in pairs(results) do
+        for _, request_k in ipairs(url_keys) do
+          if result_k:find(request_k) then
+            result_request_match_tbl[result_k] = request_k
+            break
+          end
+        end
+      end
+
       for k,_ in pairs(results) do
-        if requests[k][2] > mhits then
-          mhits = requests[k][2]
+        local req = result_request_match_tbl[k]
+        if req then
+          if requests[req] > mhits then
+            mhits = requests[req][2]
+          else
+            rspamd_logger.warnx(task, "cannot find the requested response for a request: %s (%s requests noticed)",
+                k, url_keys)
+          end
         end
       end
 
       if mhits > 0 then
         local score = 0
         for k,v in pairs(results) do
-          score = score + generic_reputation_calc(v, rule,
-              requests[k][2] / mhits, task)
+          local req = result_request_match_tbl[k]
+          if req then
+            score = score + generic_reputation_calc(v, rule,
+                requests[req][2] / mhits, task)
+
+          end
         end
 
         if math.abs(score) > 1e-3 then


More information about the Commits mailing list