commit c27548d: [Feature] Mime_types: Use detected content type as well

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Dec 27 18:28:06 UTC 2018


Author: Vsevolod Stakhov
Date: 2018-12-11 12:26:36 +0000
URL: https://github.com/rspamd/rspamd/commit/c27548d3eb996dceb3a86a3ef1184249cee5fc40

[Feature] Mime_types: Use detected content type as well

---
 lualib/lua_fuzzy.lua           | 11 +++++++++--
 src/plugins/lua/mime_types.lua | 23 +++++++++++++++++++++--
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/lualib/lua_fuzzy.lua b/lualib/lua_fuzzy.lua
index 325a8ff6c..ea74b4131 100644
--- a/lualib/lua_fuzzy.lua
+++ b/lualib/lua_fuzzy.lua
@@ -241,6 +241,8 @@ local function mime_types_check(task, part, rule)
   if not t then return false, false end
 
   local ct = string.format('%s/%s', t, st)
+  t,st = part:get_detected_type()
+  local detected_ct = string.format('%s/%s', t, st)
   local id = part:get_id()
   lua_util.debugm(N, task, 'check binary part %s: %s', id, ct)
 
@@ -264,9 +266,14 @@ local function mime_types_check(task, part, rule)
   if rule.mime_types then
 
     if fun.any(function(gl_re)
-      if gl_re:match(ct) then return true else return false end
+      if gl_re:match(ct) or (detected_ct and gl_re:match(detected_ct)) then
+        return true
+      else
+        return false
+      end
     end, rule.mime_types) then
-      lua_util.debugm(N, task, 'found mime type match for part %s: %s', id, ct)
+      lua_util.debugm(N, task, 'found mime type match for part %s: %s (%s detected)',
+          id, ct, detected_ct)
       return check_length(task, part, rule),false
     end
 
diff --git a/src/plugins/lua/mime_types.lua b/src/plugins/lua/mime_types.lua
index 9b82779d0..65b5524a7 100644
--- a/src/plugins/lua/mime_types.lua
+++ b/src/plugins/lua/mime_types.lua
@@ -911,6 +911,7 @@ local function check_mime_type(task)
   if parts then
     for _,p in ipairs(parts) do
       local mtype,subtype = p:get_type()
+      local dtype,dsubtype = p:get_detected_type()
 
       if not mtype then
         task:insert_result(settings['symbol_unknown'], 1.0, 'missing content type')
@@ -920,6 +921,10 @@ local function check_mime_type(task)
         -- Check for attachment
         local filename = p:get_filename()
         local ct = string.format('%s/%s', mtype, subtype):lower()
+        local detected_ct
+        if dtype and dsubtype then
+          detected_ct = string.format('%s/%s', dtype, dsubtype)
+        end
 
         if filename then
           filename = filename:gsub('[^%s%g]', '?')
@@ -971,13 +976,27 @@ local function check_mime_type(task)
         end
 
         if map then
-          local v = map:get_key(ct)
+          local v
+          local detected_different = false
+          if detected_ct and detected_ct ~= ct then
+            v = map:get_key(detected_ct)
+            detected_different = true
+          else
+            v = map:get_key(ct)
+          end
           if v then
             local n = tonumber(v)
 
             if n then
               if n > 0 then
-                task:insert_result(settings['symbol_bad'], n, ct)
+                if detected_different then
+                  -- Penalize case
+                  n = n * 1.5
+                  task:insert_result(settings['symbol_bad'], n,
+                      string.format('%s:%s', ct, detected_ct))
+                else
+                  task:insert_result(settings['symbol_bad'], n, ct)
+                end
                 task:insert_result('MIME_TRACE', 0.0,
                     string.format("%s:%s", p:get_id(), '-'))
               elseif n < 0 then


More information about the Commits mailing list