commit 31138dd: [Fix] Neural: Add protection agains infinities

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Oct 18 17:35:06 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-10-18 18:31:46 +0100
URL: https://github.com/rspamd/rspamd/commit/31138dd8acc790c0129fb488584311c85c96f87c (HEAD -> master)

[Fix] Neural: Add protection agains infinities

---
 lualib/lua_meta.lua | 20 +++++++++++++++-----
 src/lua/lua_task.c  |  2 +-
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/lualib/lua_meta.lua b/lualib/lua_meta.lua
index 984f5fec8..3ed416158 100644
--- a/lualib/lua_meta.lua
+++ b/lualib/lua_meta.lua
@@ -181,9 +181,11 @@ local function meta_received_function(task)
     end,
     fun.filter(function(rc) return not rc.flags or not rc.flags['artificial'] end, rh))
 
-    invalid_factor = invalid_factor / ntotal
-    secure_factor = secure_factor / ntotal
-    count_factor = 1.0 / ntotal
+    if ntotal > 0 then
+      invalid_factor = invalid_factor / ntotal
+      secure_factor = secure_factor / ntotal
+      count_factor = 1.0 / ntotal
+    end
 
     if time_factor ~= 0 then
       time_factor = 1.0 / time_factor
@@ -194,8 +196,9 @@ local function meta_received_function(task)
 end
 
 local function meta_urls_function(task)
-  if task:has_urls() then
-    return {1.0 / #(task:get_urls())}
+  local has_urls,nurls = task:has_urls()
+  if has_urls and nurls > 0 then
+    return {1.0 / nurls}
   end
 
   return {0}
@@ -481,6 +484,13 @@ local function rspamd_gen_metatokens_table(task)
   for _,mt in ipairs(metafunctions) do
     local ct = mt.cb(task)
     for i,tok in ipairs(ct) do
+      if tok ~= tok or tok == math.huge() then
+        local logger = require "rspamd_logger"
+        logger.errx(task, 'metatoken %s returned %s; replace it with 0 for sanity',
+            mt.names[i], tok)
+        tok = 0.0
+      end
+
       metatokens[mt.names[i]] = tok
     end
   end
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 5be5b3f94..263aa83fa 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -4667,7 +4667,7 @@ lua_task_process_ann_tokens (lua_State *L)
 			sres = rspamd_task_find_symbol_result (task, sym);
 
 			if (sres && !(sres->flags & RSPAMD_SYMBOL_RESULT_IGNORED)) {
-				if (!isnan (sres->score) &&
+				if (!isnan (sres->score) && !isinf (sres->score) &&
 					!(rspamd_symcache_item_flags (sres->sym->cache_item) &
 					  SYMBOL_TYPE_NOSTAT)) {
 


More information about the Commits mailing list