From 5244e863178e776d8af230e966da502920d1197b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 3 Mar 2017 14:28:47 +0000 Subject: [PATCH] [Feature] Add rule to detect broken content type --- rules/misc.lua | 12 +++++++++++- src/lua/lua_mimepart.c | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/rules/misc.lua b/rules/misc.lua index 8cfd71619..de088ae4c 100644 --- a/rules/misc.lua +++ b/rules/misc.lua @@ -168,7 +168,7 @@ rspamd_config.R_SUSPICIOUS_URL = { end return false end, - score = 6.0, + score = 5.0, one_shot = true, description = 'Obfusicated or suspicious URL has been found in a message', group = 'url' @@ -183,6 +183,16 @@ rspamd_config.BROKEN_HEADERS = { description = 'Headers structure is likely broken' } +rspamd_config.BROKEN_CONTENT_TYPE = { + callback = function(task) + return fun.any(function(p) return p:is_broken() end, + task:get_parts()) + end, + score = 3.0, + group = 'header', + description = 'Message has part with broken content type' +} + rspamd_config.HEADER_RCONFIRM_MISMATCH = { callback = function (task) local header_from = nil diff --git a/src/lua/lua_mimepart.c b/src/lua/lua_mimepart.c index dba66b818..6d17c3a66 100644 --- a/src/lua/lua_mimepart.c +++ b/src/lua/lua_mimepart.c @@ -288,6 +288,12 @@ LUA_FUNCTION_DEF (mimepart, get_text); * @return {string} 128 characters hex string with digest of the part */ LUA_FUNCTION_DEF (mimepart, get_digest); +/*** + * @method mime_part:is_broken() + * Returns true if mime part has incorrectly specified content type + * @return {bool} true if a part has bad content type + */ +LUA_FUNCTION_DEF (mimepart, is_broken); static const struct luaL_reg mimepartlib_m[] = { LUA_INTERFACE_DEF (mimepart, get_content), @@ -302,6 +308,7 @@ static const struct luaL_reg mimepartlib_m[] = { LUA_INTERFACE_DEF (mimepart, is_archive), LUA_INTERFACE_DEF (mimepart, get_archive), LUA_INTERFACE_DEF (mimepart, is_text), + LUA_INTERFACE_DEF (mimepart, is_broken), LUA_INTERFACE_DEF (mimepart, get_text), LUA_INTERFACE_DEF (mimepart, get_digest), {"__tostring", rspamd_lua_class_tostring}, @@ -738,6 +745,26 @@ lua_mimepart_is_text (lua_State * L) return 1; } +static gint +lua_mimepart_is_broken (lua_State * L) +{ + struct rspamd_mime_part *part = lua_check_mimepart (L); + + if (part == NULL) { + return luaL_error (L, "invalid arguments"); + } + + if (part->ct) { + lua_pushboolean (L, (part->ct->flags & RSPAMD_CONTENT_TYPE_BROKEN) ? + true : false); + } + else { + lua_pushboolean (L, true); + } + + return 1; +} + static gint lua_mimepart_get_image (lua_State * L) { -- 2.39.5