commit 663a156: [Fix] Fix white on white rule and add is_leaf flag

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Jan 23 19:00:10 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-01-23 18:58:46 +0000
URL: https://github.com/rspamd/rspamd/commit/663a156d5cef3310a2c2017e092626642eabba44 (HEAD -> master)

[Fix] Fix white on white rule and add is_leaf flag

---
 rules/html.lua       | 13 ++++++-------
 src/libserver/html.c | 18 ++++++++++++------
 src/lua/lua_html.c   | 10 +++++++++-
 3 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/rules/html.lua b/rules/html.lua
index 9e582a731..1891cfefa 100644
--- a/rules/html.lua
+++ b/rules/html.lua
@@ -194,18 +194,18 @@ local vis_check_id = rspamd_config:register_symbol{
       if p:is_html() and p:get_html() then -- if the current part is html part
         local hc = p:get_html() -- we get HTML context
 
-        hc:foreach_tag({'font', 'span', 'div', 'p', 'td'}, function(tag)
+        hc:foreach_tag({'font', 'span', 'div', 'p', 'td'}, function(tag, clen, is_leaf)
           local bl = tag:get_extra()
           if bl then
-            if not bl['visible'] then
+            if not bl['visible'] and is_leaf then
               invisible_blocks = invisible_blocks + 1
             end
 
-            if bl['font_size'] and bl['font_size'] == 0 then
+            if bl['font_size'] and bl['font_size'] == 0 and is_leaf then
               zero_size_blocks = zero_size_blocks + 1
             end
 
-            if bl['bgcolor'] and bl['color'] and bl['visible'] then
+            if bl['bgcolor'] and bl['color'] and bl['visible'] and is_leaf then
 
               local color = bl['color']
               local bgcolor = bl['bgcolor']
@@ -225,10 +225,9 @@ local vis_check_id = rspamd_config:register_symbol{
 
               if diff < 0.1 then
                 ret = true
-                local content_len = #(tag:get_content() or {})
                 invisible_blocks = invisible_blocks + 1 -- This block is invisible
-                transp_len = transp_len + content_len * (0.1 - diff) * 10.0
-                normal_len = normal_len - content_len
+                transp_len = transp_len + clen * (0.1 - diff) * 10.0
+                normal_len = normal_len - clen
                 local tr = transp_len / (normal_len + transp_len)
                 if tr > transp_rate then
                   transp_rate = tr
diff --git a/src/libserver/html.c b/src/libserver/html.c
index a6b037861..395987cce 100644
--- a/src/libserver/html.c
+++ b/src/libserver/html.c
@@ -2324,14 +2324,16 @@ rspamd_html_process_block_tag (rspamd_mempool_t *pool, struct html_tag *tag,
 				fstr.len = comp->len;
 				rspamd_html_process_color (comp->start, comp->len,
 						&bl->font_color);
-				msg_debug_html ("got color: %xd", bl->font_color.d.val);
+				msg_debug_html ("tag %*s; got color: %xd",
+						tag->name.len, tag->name.start, bl->font_color.d.val);
 				break;
 			case RSPAMD_HTML_COMPONENT_BGCOLOR:
 				fstr.begin = (gchar *) comp->start;
 				fstr.len = comp->len;
 				rspamd_html_process_color (comp->start, comp->len,
 						&bl->background_color);
-				msg_debug_html ("got color: %xd", bl->font_color.d.val);
+				msg_debug_html ("tag %*s; got color: %xd",
+						tag->name.len, tag->name.start, bl->font_color.d.val);
 
 				if (tag->id == Tag_BODY) {
 					/* Set global background color */
@@ -2342,21 +2344,25 @@ rspamd_html_process_block_tag (rspamd_mempool_t *pool, struct html_tag *tag,
 			case RSPAMD_HTML_COMPONENT_STYLE:
 				bl->style.len = comp->len;
 				bl->style.start = comp->start;
-				msg_debug_html ("got style: %*s", (gint) bl->style.len,
-						bl->style.start);
+				msg_debug_html ("tag: %*s; got style: %*s",
+						tag->name.len, tag->name.start,
+						(gint) bl->style.len, bl->style.start);
 				rspamd_html_process_style (pool, bl, hc, comp->start, comp->len);
 				break;
 			case RSPAMD_HTML_COMPONENT_CLASS:
 				fstr.begin = (gchar *) comp->start;
 				fstr.len = comp->len;
 				bl->html_class = rspamd_mempool_ftokdup (pool, &fstr);
-				msg_debug_html ("got class: %s", bl->html_class);
+				msg_debug_html ("tag: %*s; got class: %s",
+						tag->name.len, tag->name.start, bl->html_class);
 				break;
 			case RSPAMD_HTML_COMPONENT_SIZE:
 				/* Not supported by html5 */
 				/* FIXME maybe support it */
 				bl->font_size = 16;
-				msg_debug_html ("got size: %*s", (gint)comp->len, comp->start);
+				msg_debug_html ("tag %*s; got size: %*s",
+						tag->name.len, tag->name.start,
+						(gint)comp->len, comp->start);
 				break;
 			default:
 				/* NYI */
diff --git a/src/lua/lua_html.c b/src/lua/lua_html.c
index c0e07de36..7d7e9b7a3 100644
--- a/src/lua/lua_html.c
+++ b/src/lua/lua_html.c
@@ -470,7 +470,15 @@ lua_html_node_foreach_cb (GNode *n, gpointer d)
 		rspamd_lua_setclass (ud->L, "rspamd{html_tag}", -1);
 		lua_pushinteger (ud->L, tag->content_length);
 
-		if (lua_pcall (ud->L, 2, 1, 0) != 0) {
+		/* Leaf flag */
+		if (g_node_first_child (n)) {
+			lua_pushboolean (ud->L, false);
+		}
+		else {
+			lua_pushboolean (ud->L, true);
+		}
+
+		if (lua_pcall (ud->L, 3, 1, 0) != 0) {
 			msg_err ("error in foreach_tag callback: %s", lua_tostring (ud->L, -1));
 			lua_pop (ud->L, 1);
 			return TRUE;


More information about the Commits mailing list