]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Mime_types: Use detected content type as well
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 11 Dec 2018 12:26:36 +0000 (12:26 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 11 Dec 2018 12:26:36 +0000 (12:26 +0000)
lualib/lua_fuzzy.lua
src/plugins/lua/mime_types.lua

index 325a8ff6c6fdf922e4c0eda7cc283838ad615a5d..ea74b4131a7437db8934bb7ac13b66cecfcec9e7 100644 (file)
@@ -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
 
index 9b82779d0e150d8490d01e9c846b82cd51d4efc8..65b5524a79ab80bfd210d4a37989899429d92b35 100644 (file)
@@ -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