commit c6822d6: [Minor] Try to process images before text part heuristic

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Apr 15 14:21:06 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-04-15 15:19:52 +0100
URL: https://github.com/rspamd/rspamd/commit/c6822d66ed9825d317f6e59526d35974ccdaa1b2 (HEAD -> master)

[Minor] Try to process images before text part heuristic

---
 src/libmime/images.c  | 34 ++++++++++++++++++++++++----------
 src/libmime/images.h  |  9 +++++++++
 src/libmime/message.c |  5 ++++-
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/src/libmime/images.c b/src/libmime/images.c
index 218e947fc..c54f5845a 100644
--- a/src/libmime/images.c
+++ b/src/libmime/images.c
@@ -43,7 +43,24 @@ static const guint8 jpg_sig_exif[] = {0xff, 0xe1};
 static const guint8 gif_signature[] = {'G', 'I', 'F', '8'};
 static const guint8 bmp_signature[] = {'B', 'M'};
 
-static void process_image (struct rspamd_task *task, struct rspamd_mime_part *part);
+static bool process_image (struct rspamd_task *task, struct rspamd_mime_part *part);
+
+
+bool
+rspamd_images_process_mime_part_maybe (struct rspamd_task *task,
+											struct rspamd_mime_part *part)
+{
+	if (part->part_type == RSPAMD_MIME_PART_UNDEFINED) {
+		if (part->detected_type &&
+			strcmp (part->detected_type, "image") == 0 &&
+			part->parsed_data.len > 0) {
+
+			return process_image (task, part);
+		}
+	}
+
+	return false;
+}
 
 void
 rspamd_images_process (struct rspamd_task *task)
@@ -52,14 +69,7 @@ rspamd_images_process (struct rspamd_task *task)
 	struct rspamd_mime_part *part;
 
 	PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, part) {
-		if (part->part_type == RSPAMD_MIME_PART_UNDEFINED) {
-			if (part->detected_type &&
-				strcmp (part->detected_type, "image") == 0 &&
-				part->parsed_data.len > 0) {
-
-				process_image (task, part);
-			}
-		}
+		rspamd_images_process_mime_part_maybe (task, part);
 	}
 
 }
@@ -592,7 +602,7 @@ rspamd_maybe_process_image (rspamd_mempool_t *pool,
 	return img;
 }
 
-static void
+static bool
 process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
 {
 	struct rspamd_image *img;
@@ -612,7 +622,11 @@ process_image (struct rspamd_task *task, struct rspamd_mime_part *part)
 
 		part->part_type = RSPAMD_MIME_PART_IMAGE;
 		part->specific.img = img;
+
+		return true;
 	}
+
+	return false;
 }
 
 const gchar *
diff --git a/src/libmime/images.h b/src/libmime/images.h
index 964a1b35a..887f30a37 100644
--- a/src/libmime/images.h
+++ b/src/libmime/images.h
@@ -39,6 +39,15 @@ struct rspamd_image {
  */
 void rspamd_images_process (struct rspamd_task *task);
 
+/**
+ * Process image if possible in a single mime part
+ * @param task
+ * @param part
+ * @return
+ */
+bool rspamd_images_process_mime_part_maybe (struct rspamd_task *task,
+		struct rspamd_mime_part *part);
+
 /*
  * Link embedded images to the HTML parts
  */
diff --git a/src/libmime/message.c b/src/libmime/message.c
index 4847c4046..4b00d2dd0 100644
--- a/src/libmime/message.c
+++ b/src/libmime/message.c
@@ -1464,6 +1464,10 @@ rspamd_message_process (struct rspamd_task *task)
 			lua_settop (L, funcs_top);
 		}
 
+		/* Try to detect image before checking for text */
+		rspamd_images_process_mime_part_maybe (task, part);
+
+		/* Still no content detected, try text heuristic */
 		if (part->part_type == RSPAMD_MIME_PART_UNDEFINED) {
 			rspamd_message_process_text_part_maybe (task, part);
 		}
@@ -1601,7 +1605,6 @@ rspamd_message_process (struct rspamd_task *task)
 		}
 	}
 
-	rspamd_images_process (task);
 	rspamd_images_link (task);
 
 	rspamd_tokenize_meta_words (task);


More information about the Commits mailing list