]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Fix more issues with nested messages + tests
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 13 Aug 2019 13:47:25 +0000 (14:47 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 13 Aug 2019 13:47:25 +0000 (14:47 +0100)
src/libmime/message.c
src/libmime/mime_parser.c
test/lua/unit/task.lua

index 7eec503c959b69e54eabe92c7741dbd6c2f82166..90df43b1222918a0a9ece3190157e985b39a63a7 100644 (file)
@@ -1412,20 +1412,21 @@ rspamd_message_process (struct rspamd_task *task)
        PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) {
                if (!rspamd_message_process_text_part_maybe (task, part) &&
                                part->parsed_data.len > 0) {
-                       const gchar *mb = magic_buffer (task->cfg->libs_ctx->libmagic,
-                                       part->parsed_data.begin,
-                                       part->parsed_data.len);
-
-                       if (mb) {
-                               rspamd_ftok_t srch;
-
-                               srch.begin = mb;
-                               srch.len = strlen (mb);
-                               part->detected_ct = rspamd_content_type_parse (srch.begin,
-                                               srch.len,
-                                               task->task_pool);
+                       if (task->cfg) {
+                               const gchar *mb = magic_buffer (task->cfg->libs_ctx->libmagic,
+                                               part->parsed_data.begin,
+                                               part->parsed_data.len);
+
+                               if (mb) {
+                                       rspamd_ftok_t srch;
+
+                                       srch.begin = mb;
+                                       srch.len = strlen (mb);
+                                       part->detected_ct = rspamd_content_type_parse (srch.begin,
+                                                       srch.len,
+                                                       task->task_pool);
+                               }
                        }
-
                }
        }
 
index b40353dcac7105a12d961757a568cbbfbbab7b91..db945f2ed1a1909f4ac6a67d2eef071e27aa5d7a 100644 (file)
@@ -1297,15 +1297,16 @@ rspamd_mime_parse_message (struct rspamd_task *task,
                ret = rspamd_mime_parse_multipart_part (task, npart, nst, err);
        }
        else if (sel->flags & RSPAMD_CONTENT_TYPE_MESSAGE) {
-               g_ptr_array_add (nst->stack, npart);
-               nst->nesting ++;
-               ret = rspamd_mime_parse_message (task, npart, nst, err);
+               if ((ret = rspamd_mime_parse_normal_part (task, npart, nst, err))
+                       == RSPAMD_MIME_PARSE_OK) {
+                       ret = rspamd_mime_parse_message (task, npart, nst, err);
+               }
        }
        else {
                ret = rspamd_mime_parse_normal_part (task, npart, nst, err);
        }
 
-       if (part) {
+       if (part && st->stack->len > 0) {
                /* Remove message part from the parent stack */
                g_ptr_array_remove_index_fast (st->stack, st->stack->len - 1);
                st->nesting --;
index 2825a076f5acc0bdb968a9e08df990fcd3fb9975..3ec583043161ec5597eb77262d999f002012cbe2 100644 (file)
@@ -1,35 +1,11 @@
 context("Task processing", function()
-  local ffi = require("ffi")
-  local rspamd_util = require("rspamd_util")
-  local logger = require("rspamd_logger")
-  local test_dir = string.gsub(debug.getinfo(1).source, "^@(.+/)[^/]+$", "%1")
-    
-  local tld_file = string.format('%s/%s', test_dir, "test_tld.dat")
-  local config = {
-    options = {
-      filters = {'spf', 'dkim', 'regexp'},
-      url_tld = tld_file,
-      dns = {
-        nameserver = {'8.8.8.8'}
-      },
-    },
-    logging = {
-      type = 'console',
-      level = 'debug'
-    },
-    metric = {
-      name = 'default',
-      actions = {
-        reject = 100500,
-      },
-      unknown_weight = 1
-    }
-  }
-  
+  local fun = require("fun")
+  local rspamd_task = require("rspamd_task")
+
   test("Process a simple task", function()
     --local cfg = rspamd_util.config_from_ucl(config)
     --assert_not_nil(cfg)
-    
+
     local msg = [[
 From: <>
 To: <nobody@example.com>
@@ -38,7 +14,101 @@ Content-Type: text/plain
 
 Test.
 ]]
-    --local obj = rspamd_util.process_message(cfg, msg)
-    --print(logger.slog("result: %1", obj))
+    local res,task = rspamd_task.load_from_string(msg)
+    assert_true(res, "failed to load message")
+    task:process_message()
+    task:destroy()
+  end)
+
+  local hdrs = [[
+From: <>
+To: <nobody@example.com>
+Subject: test
+]]
+  local mpart = [[
+Content-Type: multipart/mixed; boundary=XXX
+]]
+  local body = [[
+Content-Type: text/html
+Content-Transfer-Encoding: quoted-printable
+
+<html>
+<body>
+=0DAttached is your new documents.
+<br>
+<a href=3D"http://evil.com/Information/">http:=
+//example.com/privacy/XXX/YYY_April_25_2019.doc</a>
+<br>
+<br>
+<br>
+Thank you,
+<br>
+<b>Haloclaims.co</b>
+</body></html>
+]]
+  test("Process mime nesting: simple", function()
+    local msg = hdrs .. body
+    local res,task = rspamd_task.load_from_string(msg)
+    assert_true(res, "failed to load message")
+    task:process_message()
+    assert_rspamd_table_eq({actual = fun.totable(fun.map(function(u)
+      return u:get_host()
+    end, task:get_urls())), expect = {
+      'evil.com', 'example.com'
+    }})
+    task:destroy()
+  end)
+  test("Process mime nesting: multipart", function()
+    local msg = table.concat{
+      hdrs, mpart, '\n', '--XXX\n', body, '\n--XXX--\n'
+    }
+    local res,task = rspamd_task.load_from_string(msg)
+    assert_true(res, "failed to load message")
+    task:process_message()
+    assert_rspamd_table_eq({
+      actual = fun.totable(fun.map(function(u)
+        return u:get_host()
+      end, task:get_urls())),
+
+      expect = {
+        'evil.com', 'example.com'
+      }})
+    task:destroy()
+  end)
+  test("Process mime nesting: multipart, broken", function()
+    local msg = table.concat{
+      hdrs, mpart, '\n', '--XXX\n', 'garbadge\n', '\n--XXX--\n', '--XXX\n', body
+    }
+    local res,task = rspamd_task.load_from_string(msg)
+    assert_true(res, "failed to load message")
+    task:process_message()
+    assert_rspamd_table_eq({
+      actual = fun.totable(fun.map(function(u)
+        return u:get_host()
+      end, task:get_urls())),
+
+      expect = {
+        'evil.com', 'example.com'
+      }})
+
+    task:destroy()
+  end)
+  test("Process mime nesting: message", function()
+    local msg = table.concat{
+      hdrs, 'Content-Type: message/rfc822\n', '\n', hdrs, body
+    }
+    local res,task = rspamd_task.load_from_string(msg)
+    assert_true(res, "failed to load message")
+    task:process_message()
+    assert_rspamd_table_eq({
+      actual = fun.totable(fun.map(function(u)
+        return u:get_host()
+      end, task:get_urls())),
+
+      expect = {
+        'evil.com', 'example.com'
+      }})
+
+    task:destroy()
   end)
 end)
\ No newline at end of file