]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Fix embedded images linking logic
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 19 Sep 2019 11:07:03 +0000 (12:07 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 19 Sep 2019 11:07:03 +0000 (12:07 +0100)
rules/html.lua
src/libmime/images.c
src/libmime/images.h
src/libmime/message.c

index fb32e9179be3a289eefa8f28dc5b742f7ce213b5..9e582a7317ba1b6235e626a4e822ba29c60c6ce0 100644 (file)
@@ -101,7 +101,6 @@ rspamd_config.R_EMPTY_IMAGE = {
       if p:is_html() then -- if the current part is html part
         local hc = p:get_html() -- we get HTML context
         local len = p:get_length() -- and part's length
-
         if hc and len < 50 then -- if we have a part that has less than 50 bytes of text
           local images = hc:get_images() -- then we check for HTML images
 
index cb59bc88e48c77f4f286031a0fc1702a0e295268..073776359ca0e05d3b5a21c2bf4afcde6fa4f29b 100644 (file)
@@ -596,14 +596,8 @@ rspamd_maybe_process_image (rspamd_mempool_t *pool,
 static void
 process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
 {
-       struct rspamd_mime_header *rh;
-       struct rspamd_mime_text_part *tp;
-       struct html_image *himg;
-       const gchar *cid, *html_cid;
-       guint cid_len, i, j;
        struct rspamd_image *img;
 
-
        img = rspamd_maybe_process_image (task->task_pool, &part->parsed_data);
 
        if (img != NULL) {
@@ -619,7 +613,45 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
 
                part->flags |= RSPAMD_MIME_PART_IMAGE;
                part->specific.img = img;
+       }
+}
+
+const gchar *
+rspamd_image_type_str (enum rspamd_image_type type)
+{
+       switch (type) {
+       case IMAGE_TYPE_PNG:
+               return "PNG";
+               break;
+       case IMAGE_TYPE_JPG:
+               return "JPEG";
+               break;
+       case IMAGE_TYPE_GIF:
+               return "GIF";
+               break;
+       case IMAGE_TYPE_BMP:
+               return "BMP";
+               break;
+       default:
+               break;
+       }
 
+       return "unknown";
+}
+
+static void
+rspamd_image_process_part (struct rspamd_task *task, struct rspamd_mime_part *part)
+{
+       struct rspamd_mime_header *rh;
+       struct rspamd_mime_text_part *tp;
+       struct html_image *himg;
+       const gchar *cid, *html_cid;
+       guint cid_len, i, j;
+       struct rspamd_image *img;
+
+       img = (struct rspamd_image *)part->specific.img;
+
+       if (img) {
                /* Check Content-Id */
                rh = rspamd_message_get_header_from_hash (part->raw_headers,
                                "Content-Id");
@@ -638,15 +670,14 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
                                        cid_len --;
                                }
 
-
                                PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, text_parts), i, tp) {
                                        if (IS_PART_HTML (tp) && tp->html != NULL &&
-                                                       tp->html->images != NULL) {
+                                               tp->html->images != NULL) {
                                                for (j = 0; j < tp->html->images->len; j ++) {
                                                        himg = g_ptr_array_index (tp->html->images, j);
 
                                                        if ((himg->flags & RSPAMD_HTML_FLAG_IMAGE_EMBEDDED) &&
-                                                                       himg->src) {
+                                                               himg->src) {
                                                                html_cid = himg->src;
 
                                                                if (strncmp (html_cid, "cid:", 4) == 0) {
@@ -654,7 +685,7 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
                                                                }
 
                                                                if (strlen (html_cid) == cid_len &&
-                                                                               memcmp (html_cid, cid, cid_len) == 0) {
+                                                                       memcmp (html_cid, cid, cid_len) == 0) {
                                                                        img->html_image = himg;
                                                                        himg->embedded_image = img;
 
@@ -664,6 +695,7 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
                                                                        if (himg->height == 0) {
                                                                                himg->height = img->height;
                                                                        }
+
                                                                        if (himg->width == 0) {
                                                                                himg->width = img->width;
                                                                        }
@@ -672,32 +704,20 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
                                                }
                                        }
                                }
-
                        }
-
                }
        }
 }
 
-const gchar *
-rspamd_image_type_str (enum rspamd_image_type type)
+void
+rspamd_images_link (struct rspamd_task *task)
 {
-       switch (type) {
-       case IMAGE_TYPE_PNG:
-               return "PNG";
-               break;
-       case IMAGE_TYPE_JPG:
-               return "JPEG";
-               break;
-       case IMAGE_TYPE_GIF:
-               return "GIF";
-               break;
-       case IMAGE_TYPE_BMP:
-               return "BMP";
-               break;
-       default:
-               break;
-       }
+       struct rspamd_mime_part *part;
+       guint i;
 
-       return "unknown";
-}
+       PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) {
+               if (part->flags & RSPAMD_MIME_PART_IMAGE) {
+                       rspamd_image_process_part (task, part);
+               }
+       }
+}
\ No newline at end of file
index bf1175b2adcba8cc77192942580b2ce3c525a8ec..964a1b35af0544e4f61cf29b0803cac6edf077e1 100644 (file)
@@ -39,6 +39,11 @@ struct rspamd_image {
  */
 void rspamd_images_process (struct rspamd_task *task);
 
+/*
+ * Link embedded images to the HTML parts
+ */
+void rspamd_images_link (struct rspamd_task *task);
+
 /**
  * Processes image in raw data
  * @param task
index 0f260d726246f0ae514dc4df1f0cdf1c64288ed7..cb95945fe77c91ee0b6293475acf8c27bb929112 100644 (file)
@@ -1570,6 +1570,7 @@ rspamd_message_process (struct rspamd_task *task)
                }
        }
 
+       rspamd_images_link (task);
        rspamd_tokenize_meta_words (task);
 }