From ad36a88b58202c9a4a899b7ffd51b4a9cb54c0ac Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Fri, 5 Aug 2016 16:05:54 +0100 Subject: [PATCH] [Feature] Properly implement R_WHITE_ON_WHITE rule --- conf/metrics.conf | 6 +----- rules/html.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/conf/metrics.conf b/conf/metrics.conf index ce20753c7..407f1c3ba 100644 --- a/conf/metrics.conf +++ b/conf/metrics.conf @@ -157,10 +157,6 @@ metric { weight = 3.0; description = "Detects bad content-transfer-encoding for text parts"; } - symbol "R_FLASH_REDIR_IMGSHACK" { - weight = 10.0; - description = "Flash redirect on imageshack.us"; - } symbol "INVALID_MSGID" { weight = 1.7; description = "Message id is incorrect"; @@ -365,7 +361,7 @@ metric { group "body" { symbol "R_WHITE_ON_WHITE" { - weight = 9.0; + weight = 6.0; description = "White color on white background in HTML messages"; } symbol "HTML_SHORT_LINK_IMG_1" { diff --git a/rules/html.lua b/rules/html.lua index bab636322..955d5a42e 100644 --- a/rules/html.lua +++ b/rules/html.lua @@ -164,3 +164,50 @@ rspamd_config.R_SUSPICIOUS_IMAGES = { group = 'html', description = 'Message contains many suspicious messages' } + +rspamd_config.R_WHITE_ON_WHITE = { + callback = function(task) + local tp = task:get_text_parts() -- get text parts in a message + local ret = false + local diff = 0.0 + + for _,p in ipairs(tp) do -- iterate over text parts array using `ipairs` + if p:is_html() then -- if the current part is html part + local hc = p:get_html() -- we get HTML context + + hc:foreach_tag('font', function(tag, len) + local bl = tag:get_extra() + if bl then + if bl['bgcolor'] and bl['color'] then + local color = bl['color'] + local bgcolor = bl['bgcolor'] + -- Should use visual approach here some day + local diff_r = math.abs(color[1] - bgcolor[1]) / 255.0 + local diff_g = math.abs(color[2] - bgcolor[2]) / 255.0 + local diff_b = math.abs(color[3] - bgcolor[3]) / 255.0 + diff = (diff_r + diff_g + diff_b) / 3.0 + + if diff < 0.2 then + ret = true + return true + end + end + end + + return false -- Continue search + end) + + end + end + + if ret then + return true,(0.2 - diff) * 5.0,tostring(diff * 100.0) + end + + return false + end, + + score = 6.0, + group = 'html', + description = 'Message contains low contrast text' +} -- 2.39.5