]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Apply the similar fix to the url_reputation
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 10 Mar 2022 21:29:33 +0000 (21:29 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 10 Mar 2022 21:30:05 +0000 (21:30 +0000)
Issue: #4100

src/plugins/lua/reputation.lua

index 52c6c8ee7a72ff6bbbf6d0739dca2c7d3dbe1eba..01c81d481c6a3690b20aa862bf4b4f93839460a7 100644 (file)
@@ -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