commit 7e4aa92: [Fix] Fix smtp message on passthrough result

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Feb 20 21:49:07 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-02-20 21:35:41 +0000
URL: https://github.com/rspamd/rspamd/commit/7e4aa922ca608d4567c5e9c775bbe614a4e72566

[Fix] Fix smtp message on passthrough result
Issue: #3269

---
 src/libmime/message.c        |  6 ------
 src/libmime/scan_result.c    | 27 ++++++++++++++++++++++++---
 src/libmime/scan_result.h    |  4 +++-
 src/libserver/protocol.c     | 17 ++++++++++++++---
 src/libserver/roll_history.c |  2 +-
 src/libserver/task.c         | 18 +++---------------
 src/lua/lua_task.c           |  7 +++++--
 7 files changed, 50 insertions(+), 31 deletions(-)

diff --git a/src/libmime/message.c b/src/libmime/message.c
index 37ac0a223..e5f244a3e 100644
--- a/src/libmime/message.c
+++ b/src/libmime/message.c
@@ -849,12 +849,6 @@ rspamd_message_process_text_part_maybe (struct rspamd_task *task,
 			rspamd_add_passthrough_result (task, action,
 					RSPAMD_PASSTHROUGH_CRITICAL,
 					score, "Gtube pattern", "GTUBE", 0);
-
-			if (ucl_object_lookup (task->messages, "smtp_message") == NULL) {
-				ucl_object_replace_key (task->messages,
-						ucl_object_fromstring ("Gtube pattern"),
-						"smtp_message", 0, false);
-			}
 		}
 
 		rspamd_task_insert_result (task, GTUBE_SYMBOL, 0, NULL);
diff --git a/src/libmime/scan_result.c b/src/libmime/scan_result.c
index babf80abe..8242c7de2 100644
--- a/src/libmime/scan_result.c
+++ b/src/libmime/scan_result.c
@@ -669,12 +669,13 @@ rspamd_task_add_result_option (struct rspamd_task *task,
 }
 
 struct rspamd_action*
-rspamd_check_action_metric (struct rspamd_task *task)
+rspamd_check_action_metric (struct rspamd_task *task,
+							struct rspamd_passthrough_result **ppr)
 {
 	struct rspamd_action_result *action_lim,
 			*noaction = NULL;
 	struct rspamd_action *selected_action = NULL, *least_action = NULL;
-	struct rspamd_passthrough_result *pr;
+	struct rspamd_passthrough_result *pr, *sel_pr = NULL;
 	double max_score = -(G_MAXDOUBLE), sc;
 	int i;
 	struct rspamd_scan_result *mres = task->result;
@@ -696,6 +697,10 @@ rspamd_check_action_metric (struct rspamd_task *task)
 						}
 					}
 
+					if (ppr) {
+						*ppr = pr;
+					}
+
 					return selected_action;
 				}
 				else {
@@ -721,10 +726,12 @@ rspamd_check_action_metric (struct rspamd_task *task)
 						else {
 							sc = selected_action->threshold;
 							max_score = sc;
+							sel_pr = pr;
 						}
 					}
 					else {
 						max_score = sc;
+						sel_pr = pr;
 					}
 				}
 			}
@@ -767,17 +774,31 @@ rspamd_check_action_metric (struct rspamd_task *task)
 						selected_action->action_type != METRIC_ACTION_DISCARD) {
 					/* Override score based action with least action */
 					selected_action = least_action;
+
+					if (ppr) {
+						*ppr = sel_pr;
+					}
 				}
 			}
 			else {
 				/* Adjust score if needed */
-				mres->score = MAX (max_score, mres->score);
+				if (max_score > mres->score) {
+					if (ppr) {
+						*ppr = sel_pr;
+					}
+
+					mres->score = max_score;
+				}
 			}
 		}
 
 		return selected_action;
 	}
 
+	if (ppr) {
+		*ppr = sel_pr;
+	}
+
 	return noaction->action;
 }
 
diff --git a/src/libmime/scan_result.h b/src/libmime/scan_result.h
index 74d0d81b9..31a57a43a 100644
--- a/src/libmime/scan_result.h
+++ b/src/libmime/scan_result.h
@@ -52,6 +52,7 @@ struct rspamd_symbol_result {
 #define RSPAMD_PASSTHROUGH_CRITICAL 3
 
 #define RSPAMD_PASSTHROUGH_LEAST (1u << 0u)
+#define RSPAMD_PASSTHROUGH_NO_SMTP_MESSAGE (1u << 0u)
 
 struct rspamd_passthrough_result {
 	struct rspamd_action *action;
@@ -183,7 +184,8 @@ double rspamd_factor_consolidation_func (struct rspamd_task *task,
  * @param task
  * @return
  */
-struct rspamd_action *rspamd_check_action_metric (struct rspamd_task *task);
+struct rspamd_action *rspamd_check_action_metric (struct rspamd_task *task,
+												  struct rspamd_passthrough_result **ppr);
 
 #ifdef  __cplusplus
 }
diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c
index 727ada37f..ee5cc1f4d 100644
--- a/src/libserver/protocol.c
+++ b/src/libserver/protocol.c
@@ -1167,8 +1167,9 @@ rspamd_scan_result_ucl (struct rspamd_task *task,
 	struct rspamd_action *action;
 	ucl_object_t *obj = NULL, *sobj;
 	const gchar *subject;
+	struct rspamd_passthrough_result *pr = NULL;
 
-	action = rspamd_check_action_metric (task);
+	action = rspamd_check_action_metric (task, &pr);
 	is_spam = !(action->flags & RSPAMD_ACTION_HAM);
 
 	if (task->cmd == CMD_CHECK) {
@@ -1181,6 +1182,16 @@ rspamd_scan_result_ucl (struct rspamd_task *task,
 		obj = top;
 	}
 
+	if (pr && pr->message && !(pr->flags & RSPAMD_PASSTHROUGH_NO_SMTP_MESSAGE)) {
+		/* Add smtp message if it does not exists: see #3269 for details */
+		if (ucl_object_lookup (task->messages, "smtp_message") == NULL) {
+			ucl_object_insert_key (task->messages,
+					ucl_object_fromstring_common (pr->message, 0, UCL_STRING_RAW),
+					"smtp_message", 0,
+					false);
+		}
+	}
+
 	ucl_object_insert_key (obj,
 			ucl_object_frombool (RSPAMD_TASK_IS_SKIPPED (task)),
 			"is_skipped", 0, false);
@@ -1734,13 +1745,13 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg,
 end:
 	if (!(task->flags & RSPAMD_TASK_FLAG_NO_STAT)) {
 		/* Update stat for default metric */
+
 		msg_debug_protocol ("skip stats update due to no_stat flag");
 		metric_res = task->result;
 
 		if (metric_res != NULL) {
 
-			action = rspamd_check_action_metric (task);
-
+			action = rspamd_check_action_metric (task, NULL);
 			/* TODO: handle custom actions in stats */
 			if (action->action_type == METRIC_ACTION_SOFT_REJECT &&
 					(task->flags & RSPAMD_TASK_FLAG_GREYLISTED)) {
diff --git a/src/libserver/roll_history.c b/src/libserver/roll_history.c
index 1a742a441..a0197a32c 100644
--- a/src/libserver/roll_history.c
+++ b/src/libserver/roll_history.c
@@ -160,7 +160,7 @@ rspamd_roll_history_update (struct roll_history *history,
 	}
 	else {
 		row->score = metric_res->score;
-		action = rspamd_check_action_metric (task);
+		action = rspamd_check_action_metric (task, NULL);
 		row->action = action->action_type;
 		row->required_score = rspamd_task_get_required_score (task, metric_res);
 		cbdata.pos = row->symbols;
diff --git a/src/libserver/task.c b/src/libserver/task.c
index 5c4ca4565..3e8dd381f 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -1076,7 +1076,7 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
 	khiter_t k;
 
 	mres = task->result;
-	act = rspamd_check_action_metric (task);
+	act = rspamd_check_action_metric (task, NULL);
 
 	if (mres != NULL) {
 		switch (lf->type) {
@@ -1875,7 +1875,7 @@ rspamd_task_timeout (EV_P_ ev_timer *w, int revents)
 		if (task->cfg->soft_reject_on_timeout) {
 			struct rspamd_action *action, *soft_reject;
 
-			action = rspamd_check_action_metric (task);
+			action = rspamd_check_action_metric (task, NULL);
 
 			if (action->action_type != METRIC_ACTION_REJECT) {
 				soft_reject = rspamd_config_get_action_by_type (task->cfg,
@@ -1887,12 +1887,6 @@ rspamd_task_timeout (EV_P_ ev_timer *w, int revents)
 						"timeout processing message",
 						"task timeout",
 						0);
-
-				ucl_object_replace_key (task->messages,
-						ucl_object_fromstring_common ("timeout processing message",
-								0, UCL_STRING_RAW),
-						"smtp_message", 0,
-						false);
 			}
 		}
 
@@ -1910,7 +1904,7 @@ rspamd_task_timeout (EV_P_ ev_timer *w, int revents)
 		if (task->cfg->soft_reject_on_timeout) {
 			struct rspamd_action *action, *soft_reject;
 
-			action = rspamd_check_action_metric (task);
+			action = rspamd_check_action_metric (task, NULL);
 
 			if (action->action_type != METRIC_ACTION_REJECT) {
 				soft_reject = rspamd_config_get_action_by_type (task->cfg,
@@ -1922,12 +1916,6 @@ rspamd_task_timeout (EV_P_ ev_timer *w, int revents)
 						"timeout post-processing message",
 						"task timeout",
 						0);
-
-				ucl_object_replace_key (task->messages,
-						ucl_object_fromstring_common ("timeout post-processing message",
-								0, UCL_STRING_RAW),
-						"smtp_message", 0,
-						false);
 			}
 		}
 
diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c
index 1412a0035..cbbae7aac 100644
--- a/src/lua/lua_task.c
+++ b/src/lua/lua_task.c
@@ -2116,6 +2116,9 @@ lua_task_set_pre_result (lua_State * L)
 			if (strstr (fl_str, "least") != NULL) {
 				flags |= RSPAMD_PASSTHROUGH_LEAST;
 			}
+			else if (strstr (fl_str, "no_smtp_message") != NULL) {
+				flags |= RSPAMD_PASSTHROUGH_NO_SMTP_MESSAGE;
+			}
 		}
 
 
@@ -5853,7 +5856,7 @@ lua_task_get_metric_result (lua_State *L)
 		lua_pushnumber (L, metric_res->score);
 		lua_settable (L, -3);
 
-		action = rspamd_check_action_metric (task);
+		action = rspamd_check_action_metric (task, NULL);
 
 		if (action) {
 			lua_pushstring (L, "action");
@@ -5924,7 +5927,7 @@ lua_task_get_metric_action (lua_State *L)
 	struct rspamd_action *action;
 
 	if (task) {
-		action = rspamd_check_action_metric (task);
+		action = rspamd_check_action_metric (task, NULL);
 		lua_pushstring (L, action->name);
 	}
 	else {


More information about the Commits mailing list