]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Empty messages cases handling
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 12 Jul 2019 16:20:35 +0000 (17:20 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 12 Jul 2019 16:20:35 +0000 (17:20 +0100)
src/libmime/filter.c
src/libmime/message.c
src/libmime/message.h
src/libmime/mime_encoding.c
src/libmime/mime_headers.c
src/libserver/protocol.c
src/libserver/roll_history.c
src/libserver/task.c
src/lua/lua_task.c

index 83a9881d65404a82cf657471ae6f8be1a3fdd3fc..a193f5f7817fd0dd6f3827b151580036c1aa3d60 100644 (file)
@@ -139,14 +139,14 @@ rspamd_add_passthrough_result (struct rspamd_task *task,
        if (!isnan (target_score)) {
 
                msg_info_task ("<%s>: set pre-result to '%s' %s(%.2f): '%s' from %s(%d)",
-                               MESSAGE_FIELD (task, message_id), action->name,
+                               MESSAGE_FIELD_CHECK (task, message_id), action->name,
                                flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "",
                                target_score,
                                message, module, priority);
        }
        else {
                msg_info_task ("<%s>: set pre-result to '%s' %s(no score): '%s' from %s(%d)",
-                               MESSAGE_FIELD (task, message_id), action->name,
+                               MESSAGE_FIELD_CHECK (task, message_id), action->name,
                                flags & RSPAMD_PASSTHROUGH_LEAST ? "*least " : "",
                                message, module, priority);
        }
index fb488b71bfaa818f8a49e707f26f147c7fdfb3ad..3efd30da0bc2186b526151dd5cefd275e9d62c95 100644 (file)
@@ -1166,6 +1166,7 @@ rspamd_message_parse (struct rspamd_task *task)
 
        if (RSPAMD_TASK_IS_EMPTY (task)) {
                /* Don't do anything with empty task */
+               task->flags |= RSPAMD_TASK_FLAG_SKIP_PROCESS;
                return TRUE;
        }
 
index cc4db39f4edb7a6aaa78eb26ef4aa80e9749c369..a6b6f10229d61af644ce06018269a3c3ecec1fbf 100644 (file)
@@ -158,6 +158,9 @@ struct rspamd_message {
 };
 
 #define MESSAGE_FIELD(task, field) ((task)->message->field)
+#define MESSAGE_FIELD_CHECK(task, field) ((task)->message ? \
+       (task)->message->field : \
+       (__typeof__((task)->message->field))NULL)
 
 /**
  * Parse and pre-process mime message
index 4622ee0321ebe080176b945e73cee246ef131652..0fbba54b29445841aa2b8caeab38967bc7ab643d 100644 (file)
@@ -668,7 +668,7 @@ rspamd_mime_text_part_maybe_convert (struct rspamd_task *task,
 
        if (charset == NULL) {
                msg_info_task ("<%s>: has invalid charset",
-                               MESSAGE_FIELD (task, message_id));
+                               MESSAGE_FIELD_CHECK (task, message_id));
                SET_PART_RAW (text_part);
                text_part->utf_raw_content = part_content;
 
index 99936dd8cc7d6bbd9e746d2818f77958280be4c8..7c6c02709a81a50ca0b5d64637a6db5f0549b4fe 100644 (file)
@@ -1621,7 +1621,7 @@ struct rspamd_mime_header *
 rspamd_message_get_header_array (struct rspamd_task *task,
                                                                 const gchar *field)
 {
-       return rspamd_message_get_header_from_hash (MESSAGE_FIELD (task, raw_headers),
+       return rspamd_message_get_header_from_hash (MESSAGE_FIELD_CHECK (task, raw_headers),
                        field);
 }
 
index ff332d1ccb59fa3db0617f52f68f3f1d19d7e21a..8834529ee8675379c0807d98a338cd10a1a87b5e 100644 (file)
@@ -858,7 +858,7 @@ urls_protocol_cb (gpointer key, gpointer value, gpointer ud)
                }
 
                msg_notice_task_encrypted ("<%s> %s: %*s; ip: %s; URL: %*s",
-                       MESSAGE_FIELD (task, message_id),
+                       MESSAGE_FIELD_CHECK (task, message_id),
                        has_user ? "user" : "from",
                        len, user_field,
                        rspamd_inet_address_to_string (task->from_addr),
@@ -935,7 +935,7 @@ rspamd_protocol_rewrite_subject (struct rspamd_task *task)
        }
 
        p = c;
-       s = MESSAGE_FIELD (task, subject);
+       s = MESSAGE_FIELD_CHECK (task, subject);
 
        if (s) {
                slen = strlen (s);
@@ -1285,7 +1285,7 @@ rspamd_protocol_write_ucl (struct rspamd_task *task,
                }
        }
 
-       if (flags & RSPAMD_PROTOCOL_URLS) {
+       if (flags & RSPAMD_PROTOCOL_URLS && task->message) {
                if (g_hash_table_size (MESSAGE_FIELD (task, urls)) > 0) {
                        ucl_object_insert_key (top,
                                        rspamd_urls_tree_ucl (MESSAGE_FIELD (task, urls), task),
@@ -1307,7 +1307,7 @@ rspamd_protocol_write_ucl (struct rspamd_task *task,
 
        if (flags & RSPAMD_PROTOCOL_BASIC) {
                ucl_object_insert_key (top,
-                               ucl_object_fromstring (MESSAGE_FIELD (task, message_id)),
+                               ucl_object_fromstring (MESSAGE_FIELD_CHECK (task, message_id)),
                                "message-id", 0, false);
                ucl_object_insert_key (top,
                                ucl_object_fromdouble (task->time_real_finish - task->task_timestamp),
@@ -1331,7 +1331,7 @@ rspamd_protocol_write_ucl (struct rspamd_task *task,
                                        GString *folded_header;
                                        dkim_sig = (GString *) dkim_sigs->data;
 
-                                       if (task->flags & RSPAMD_TASK_FLAG_MILTER) {
+                                       if (task->flags & RSPAMD_TASK_FLAG_MILTER || !task->message) {
                                                folded_header = rspamd_header_value_fold ("DKIM-Signature",
                                                                dkim_sig->str, 80, RSPAMD_TASK_NEWLINES_LF, NULL);
                                        }
@@ -1792,7 +1792,7 @@ rspamd_protocol_write_reply (struct rspamd_task *task, ev_tstamp timeout)
 
        if (rspamd_http_connection_is_encrypted (task->http_conn)) {
                msg_info_protocol ("<%s> writing encrypted reply",
-                               MESSAGE_FIELD (task, message_id));
+                               MESSAGE_FIELD_CHECK (task, message_id));
        }
 
        if (!RSPAMD_TASK_IS_JSON (task)) {
index ee69d59d38e33da777081aee23ab141f147bfbc2..ceed8d04ff84048c448731b41e838e76de7f5834 100644 (file)
@@ -140,8 +140,10 @@ rspamd_roll_history_update (struct roll_history *history,
        row->timestamp = task->task_timestamp;
 
        /* Strings */
-       rspamd_strlcpy (row->message_id, MESSAGE_FIELD (task, message_id),
-               sizeof (row->message_id));
+       if (task->message) {
+               rspamd_strlcpy (row->message_id, MESSAGE_FIELD (task, message_id),
+                               sizeof (row->message_id));
+       }
        if (task->user) {
                rspamd_strlcpy (row->user, task->user, sizeof (row->user));
        }
index dfad41220da81633c832823c4099b90a2620067c..950af5ec8d38b8272a4e759219646395ca597b87 100644 (file)
@@ -930,7 +930,7 @@ rspamd_task_get_principal_recipient (struct rspamd_task *task)
                }
        }
 
-       GPtrArray *rcpt_mime = MESSAGE_FIELD (task, rcpt_mime);
+       GPtrArray *rcpt_mime = MESSAGE_FIELD_CHECK (task, rcpt_mime);
        if (rcpt_mime != NULL && rcpt_mime->len > 0) {
                PTR_ARRAY_FOREACH (rcpt_mime, i, addr) {
                        if (addr->addr && !(addr->flags & RSPAMD_EMAIL_ADDR_ORIGINAL)) {
@@ -969,7 +969,7 @@ rspamd_task_log_check_condition (struct rspamd_task *task,
 
        switch (lf->type) {
        case RSPAMD_LOG_MID:
-               if (MESSAGE_FIELD (task, message_id) &&
+               if (MESSAGE_FIELD_CHECK (task, message_id) &&
                        strcmp (MESSAGE_FIELD (task, message_id) , "undef") != 0) {
                        ret = TRUE;
                }
@@ -997,7 +997,7 @@ rspamd_task_log_check_condition (struct rspamd_task *task,
                break;
        case RSPAMD_LOG_MIME_RCPT:
        case RSPAMD_LOG_MIME_RCPTS:
-               if (MESSAGE_FIELD (task, rcpt_mime) &&
+               if (MESSAGE_FIELD_CHECK (task, rcpt_mime) &&
                        MESSAGE_FIELD (task, rcpt_mime)->len > 0) {
                        ret = TRUE;
                }
@@ -1008,7 +1008,7 @@ rspamd_task_log_check_condition (struct rspamd_task *task,
                }
                break;
        case RSPAMD_LOG_MIME_FROM:
-               if (MESSAGE_FIELD (task, from_mime) &&
+               if (MESSAGE_FIELD_CHECK (task, from_mime) &&
                        MESSAGE_FIELD (task, from_mime)->len > 0) {
                        ret = TRUE;
                }
@@ -1328,7 +1328,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
        switch (lf->type) {
        /* String vars */
        case RSPAMD_LOG_MID:
-               if (MESSAGE_FIELD (task, message_id)) {
+               if (MESSAGE_FIELD_CHECK (task, message_id)) {
                        var.begin = MESSAGE_FIELD (task, message_id);
                        var.len = strlen (var.begin);
                }
@@ -1398,7 +1398,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
                }
                break;
        case RSPAMD_LOG_MIME_FROM:
-               if (MESSAGE_FIELD (task, from_mime)) {
+               if (MESSAGE_FIELD_CHECK (task, from_mime)) {
                        return rspamd_task_write_ialist (task,
                                        MESSAGE_FIELD (task, from_mime),
                                        1,
@@ -1413,7 +1413,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
                }
                break;
        case RSPAMD_LOG_MIME_RCPT:
-               if (MESSAGE_FIELD (task, rcpt_mime)) {
+               if (MESSAGE_FIELD_CHECK (task, rcpt_mime)) {
                        return rspamd_task_write_ialist (task,
                                        MESSAGE_FIELD (task, rcpt_mime),
                                        1,
@@ -1428,7 +1428,7 @@ rspamd_task_log_variable (struct rspamd_task *task,
                }
                break;
        case RSPAMD_LOG_MIME_RCPTS:
-               if (MESSAGE_FIELD (task, rcpt_mime)) {
+               if (MESSAGE_FIELD_CHECK (task, rcpt_mime)) {
                        return rspamd_task_write_ialist (task,
                                        MESSAGE_FIELD (task, rcpt_mime),
                                        -1, /* All addresses */
@@ -1437,10 +1437,16 @@ rspamd_task_log_variable (struct rspamd_task *task,
                }
                break;
        case RSPAMD_LOG_DIGEST:
-               var.len = rspamd_snprintf (numbuf, sizeof (numbuf), "%*xs",
-                               (gint)sizeof (MESSAGE_FIELD (task, digest)),
-                               MESSAGE_FIELD (task, digest));
-               var.begin = numbuf;
+               if (task->message) {
+                       var.len = rspamd_snprintf (numbuf, sizeof (numbuf), "%*xs",
+                                       (gint) sizeof (MESSAGE_FIELD (task, digest)),
+                                       MESSAGE_FIELD (task, digest));
+                       var.begin = numbuf;
+               }
+               else {
+                       var.begin = undef;
+                       var.len = sizeof (undef) - 1;
+               }
                break;
        case RSPAMD_LOG_FILENAME:
                if (task->msg.fpath) {
index 1a4c8dc34672477a2c2f1ba664a32330da6e3507..d77d5b01d6be8c31cc009ec005df641aaf6d8ddf 100644 (file)
@@ -1271,7 +1271,10 @@ lua_task_set_cached (lua_State *L, struct rspamd_task *task, const gchar *key,
        }
 
        entry->ref = luaL_ref (L, LUA_REGISTRYINDEX);
-       memcpy (entry->id, MESSAGE_FIELD (task, digest), sizeof (entry->id));
+
+       if (task->message) {
+               memcpy (entry->id, MESSAGE_FIELD (task, digest), sizeof (entry->id));
+       }
 }
 
 
@@ -1283,8 +1286,9 @@ lua_task_get_cached (lua_State *L, struct rspamd_task *task, const gchar *key)
 
        entry = g_hash_table_lookup (task->lua_cache, key);
 
-       if (entry != NULL && memcmp (entry->id, MESSAGE_FIELD (task, digest),
-                       sizeof (entry->id)) == 0) {
+       if (entry != NULL && (task->message &&
+                                                 memcmp (entry->id, MESSAGE_FIELD (task, digest),
+                                                                 sizeof (entry->id)) == 0)) {
                lua_rawgeti (L, LUA_REGISTRYINDEX, entry->ref);
 
                return TRUE;
@@ -1970,7 +1974,7 @@ lua_task_get_urls (lua_State * L)
                        PROTOCOL_FILE|PROTOCOL_FTP;
        gsize sz;
 
-       if (task) {
+       if (task && task->message) {
                if (lua_gettop (L) >= 2) {
                        if (lua_type (L, 2) == LUA_TBOOLEAN) {
                                protocols_mask = default_mask;
@@ -2087,7 +2091,7 @@ lua_task_has_urls (lua_State * L)
        struct rspamd_task *task = lua_check_task (L, 1);
        gboolean need_emails = FALSE, ret = FALSE;
 
-       if (task) {
+       if (task && task->message) {
                if (lua_gettop (L) >= 2) {
                        need_emails = lua_toboolean (L, 2);
                }
@@ -2159,20 +2163,25 @@ lua_task_get_rawbody (lua_State * L)
        struct rspamd_lua_text *t;
 
        if (task) {
-               t = lua_newuserdata (L, sizeof (*t));
-               rspamd_lua_setclass (L, "rspamd{text}", -1);
+               if (task->message != NULL) {
+                       t = lua_newuserdata (L, sizeof (*t));
+                       rspamd_lua_setclass (L, "rspamd{text}", -1);
+
+                       if (MESSAGE_FIELD (task, raw_headers_content).len > 0) {
+                               g_assert (MESSAGE_FIELD (task, raw_headers_content).len <= task->msg.len);
+                               t->start = task->msg.begin + MESSAGE_FIELD (task, raw_headers_content).len;
+                               t->len = task->msg.len - MESSAGE_FIELD (task, raw_headers_content).len;
+                       }
+                       else {
+                               t->len = task->msg.len;
+                               t->start = task->msg.begin;
+                       }
 
-               if (MESSAGE_FIELD (task, raw_headers_content).len > 0) {
-                       g_assert (MESSAGE_FIELD (task, raw_headers_content).len <= task->msg.len);
-                       t->start = task->msg.begin + MESSAGE_FIELD (task, raw_headers_content).len;
-                       t->len = task->msg.len - MESSAGE_FIELD (task, raw_headers_content).len;
+                       t->flags = 0;
                }
                else {
-                       t->len = task->msg.len;
-                       t->start = task->msg.begin;
+                       lua_pushnil (L);
                }
-
-               t->flags = 0;
        }
        else {
                return luaL_error (L, "invalid arguments");
@@ -2211,7 +2220,7 @@ lua_task_get_text_parts (lua_State * L)
        struct rspamd_task *task = lua_check_task (L, 1);
        struct rspamd_mime_text_part *part, **ppart;
 
-       if (task != NULL) {
+       if (task != NULL && task->message != NULL) {
 
                if (!lua_task_get_cached (L, task, "text_parts")) {
                        lua_createtable (L, MESSAGE_FIELD (task, text_parts)->len, 0);
@@ -2242,7 +2251,7 @@ lua_task_get_parts (lua_State * L)
        struct rspamd_task *task = lua_check_task (L, 1);
        struct rspamd_mime_part *part, **ppart;
 
-       if (task != NULL) {
+       if (task != NULL && task->message != NULL) {
                if (!lua_task_get_cached (L, task, "mime_parts")) {
                        lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0);
 
@@ -2519,7 +2528,7 @@ lua_task_get_raw_headers (lua_State *L)
        struct rspamd_task *task = lua_check_task (L, 1);
        struct rspamd_lua_text *t;
 
-       if (task) {
+       if (task && task->message) {
                t = lua_newuserdata (L, sizeof (*t));
                rspamd_lua_setclass (L, "rspamd{text}", -1);
                t->start = MESSAGE_FIELD (task, raw_headers_content).begin;
@@ -2543,7 +2552,7 @@ lua_task_get_received_headers (lua_State * L)
        const gchar *proto;
        guint k = 1;
 
-       if (task) {
+       if (task && task->message) {
                if (!lua_task_get_cached (L, task, "received")) {
                        lua_createtable (L, 0, 0);
 
@@ -3105,7 +3114,7 @@ lua_task_get_recipients (lua_State *L)
                        break;
                case RSPAMD_ADDRESS_MIME:
                        /* Here we check merely mime rcpt */
-                       ptrs = MESSAGE_FIELD (task, rcpt_mime);
+                       ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
                        break;
                case RSPAMD_ADDRESS_ANY:
                default:
@@ -3113,7 +3122,7 @@ lua_task_get_recipients (lua_State *L)
                                ptrs = task->rcpt_envelope;
                        }
                        else {
-                               ptrs = MESSAGE_FIELD (task, rcpt_mime);
+                               ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
                        }
                        break;
                }
@@ -3158,7 +3167,7 @@ lua_task_set_recipients (lua_State *L)
                        break;
                case RSPAMD_ADDRESS_MIME:
                        /* Here we check merely mime rcpt */
-                       ptrs = MESSAGE_FIELD (task, rcpt_mime);
+                       ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
                        need_update_digest = TRUE;
                        break;
                case RSPAMD_ADDRESS_ANY:
@@ -3167,7 +3176,7 @@ lua_task_set_recipients (lua_State *L)
                                ptrs = task->rcpt_envelope;
                        }
                        else {
-                               ptrs = MESSAGE_FIELD (task, rcpt_mime);
+                               ptrs = MESSAGE_FIELD_CHECK (task, rcpt_mime);
                                need_update_digest = TRUE;
                        }
                        break;
@@ -3251,14 +3260,14 @@ lua_task_has_from (lua_State *L)
                        break;
                case RSPAMD_ADDRESS_MIME:
                        /* Here we check merely mime rcpt */
-                       CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, from_mime));
+                       CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, from_mime));
                        break;
                case RSPAMD_ADDRESS_ANY:
                default:
                        CHECK_EMAIL_ADDR (task->from_envelope);
 
                        if (!ret) {
-                               CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, from_mime));
+                               CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, from_mime));
                        }
                        break;
                }
@@ -3293,14 +3302,14 @@ lua_task_has_recipients (lua_State *L)
                        break;
                case RSPAMD_ADDRESS_MIME:
                        /* Here we check merely mime rcpt */
-                       CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, rcpt_mime));
+                       CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, rcpt_mime));
                        break;
                case RSPAMD_ADDRESS_ANY:
                default:
                        CHECK_EMAIL_ADDR_LIST (task->rcpt_envelope);
 
                        if (!ret) {
-                               CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD (task, rcpt_mime));
+                               CHECK_EMAIL_ADDR_LIST (MESSAGE_FIELD_CHECK (task, rcpt_mime));
                        }
                        break;
                }
@@ -3336,7 +3345,7 @@ lua_task_get_from (lua_State *L)
                        break;
                case RSPAMD_ADDRESS_MIME:
                        /* Here we check merely mime rcpt */
-                       addrs = MESSAGE_FIELD (task, from_mime);
+                       addrs = MESSAGE_FIELD_CHECK (task, from_mime);
                        break;
                case RSPAMD_ADDRESS_ANY:
                default:
@@ -3344,7 +3353,7 @@ lua_task_get_from (lua_State *L)
                                addr = task->from_envelope;
                        }
                        else {
-                               addrs = MESSAGE_FIELD (task, from_mime);
+                               addrs = MESSAGE_FIELD_CHECK (task, from_mime);
                        }
                        break;
                }
@@ -3399,7 +3408,7 @@ lua_task_set_from (lua_State *L)
                        break;
                case RSPAMD_ADDRESS_MIME:
                        /* Here we check merely mime rcpt */
-                       addrs = MESSAGE_FIELD (task, from_mime);
+                       addrs = MESSAGE_FIELD_CHECK (task, from_mime);
                        need_update_digest = TRUE;
                        break;
                case RSPAMD_ADDRESS_ANY:
@@ -3408,7 +3417,7 @@ lua_task_set_from (lua_State *L)
                                paddr = &task->from_envelope;
                        }
                        else {
-                               addrs = MESSAGE_FIELD (task, from_mime);
+                               addrs = MESSAGE_FIELD_CHECK (task, from_mime);
                                need_update_digest = TRUE;
                        }
                        break;
@@ -3501,7 +3510,7 @@ lua_task_get_reply_sender (lua_State *L)
                if (rh) {
                        lua_pushstring (L, rh->decoded);
                }
-               else if (MESSAGE_FIELD (task, from_mime) &&
+               else if (MESSAGE_FIELD_CHECK (task, from_mime) &&
                                MESSAGE_FIELD (task, from_mime)->len == 1) {
                        struct rspamd_email_address *addr;
 
@@ -3689,7 +3698,7 @@ lua_task_get_subject (lua_State *L)
        struct rspamd_task *task = lua_check_task (L, 1);
 
        if (task) {
-               if (MESSAGE_FIELD (task, subject) != NULL) {
+               if (MESSAGE_FIELD_CHECK (task, subject) != NULL) {
                        lua_pushstring (L, MESSAGE_FIELD (task, subject));
                        return 1;
                }
@@ -3789,7 +3798,7 @@ lua_task_get_images (lua_State *L)
        struct rspamd_mime_part *part;
        struct rspamd_image **pimg;
 
-       if (task) {
+       if (task && task->message) {
                if (!lua_task_get_cached (L, task, "images")) {
                        lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0);
 
@@ -3821,7 +3830,7 @@ lua_task_get_archives (lua_State *L)
        struct rspamd_mime_part *part;
        struct rspamd_archive **parch;
 
-       if (task) {
+       if (task && task->message) {
                if (!lua_task_get_cached (L, task, "archives")) {
                        lua_createtable (L, MESSAGE_FIELD (task, parts)->len, 0);
 
@@ -4462,7 +4471,7 @@ lua_task_get_message_id (lua_State *L)
        struct rspamd_task *task = lua_check_task (L, 1);
 
        if (task != NULL) {
-               if (MESSAGE_FIELD (task, message_id) != NULL) {
+               if (MESSAGE_FIELD_CHECK (task, message_id) != NULL) {
                        lua_pushstring (L, MESSAGE_FIELD (task, message_id));
                }
                else {
@@ -4733,7 +4742,7 @@ lua_task_get_digest (lua_State *L)
        gchar hexbuf[sizeof(MESSAGE_FIELD (task, digest)) * 2 + 1];
        gint r;
 
-       if (task) {
+       if (task && task->message) {
                r = rspamd_encode_hex_buf (MESSAGE_FIELD (task, digest),
                                sizeof (MESSAGE_FIELD (task, digest)),
                                hexbuf, sizeof (hexbuf) - 1);
@@ -5407,7 +5416,7 @@ lua_task_get_newlines_type (lua_State *L)
        LUA_TRACE_POINT;
        struct rspamd_task *task = lua_check_task (L, 1);
 
-       if (task) {
+       if (task && task->message) {
                switch (MESSAGE_FIELD (task, nlines_type)) {
                case RSPAMD_TASK_NEWLINES_CR:
                        lua_pushstring (L, "cr");
@@ -5630,7 +5639,7 @@ lua_task_headers_foreach (lua_State *L)
        struct rspamd_mime_header *hdr, *cur;
        gint old_top;
 
-       if (task && lua_isfunction (L, 2)) {
+       if (task && task->message && lua_isfunction (L, 2)) {
                if (lua_istable (L, 3)) {
                        lua_pushstring (L, "full");
                        lua_gettable (L, 3);
@@ -5819,7 +5828,8 @@ lua_task_lookup_words (lua_State *L)
 
        guint i, matches = 0;
 
-       if (task == NULL || map == NULL || lua_type (L, 3) != LUA_TFUNCTION) {
+       if (task == NULL || map == NULL || task->message == NULL
+               || lua_type (L, 3) != LUA_TFUNCTION) {
                return luaL_error (L, "invalid arguments");
        }