]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Fix smtp message on passthrough result
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 20 Feb 2020 21:35:41 +0000 (21:35 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 20 Feb 2020 21:35:41 +0000 (21:35 +0000)
Issue: #3269

src/libmime/message.c
src/libmime/scan_result.c
src/libmime/scan_result.h
src/libserver/protocol.c
src/libserver/roll_history.c
src/libserver/task.c
src/lua/lua_task.c

index 37ac0a2236fccbfd5825330f73ebc3e32e68bde3..e5f244a3e53f2bc314a730be8eaa2ef1b4a4da0a 100644 (file)
@@ -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);
index babf80abe9d3b34fc76f3cea0aa77a0e69f82704..8242c7de2253e68a4dad5010a60a48457a845fc8 100644 (file)
@@ -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;
 }
 
index 74d0d81b902ee9cc2ba7586bd79958e6972ee81d..31a57a43a05e75dcb7516eaf76783a6f48b5f80d 100644 (file)
@@ -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
 }
index 727ada37fc695ed45ede4ea8eab48bb9c64c3ab1..ee5cc1f4d66fa311dbf07e2d84fcaa050a3b50a5 100644 (file)
@@ -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)) {
index 1a742a441beb816be5fefa54d33f7380f09a5d54..a0197a32c6cf0fc8e986ab65bb11908fd9f478fd 100644 (file)
@@ -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;
index 5c4ca45650e3ba6c1061593c9a9c8587037df4db..3e8dd381f1a8c0bc67baeb5783cac9291bbc5df4 100644 (file)
@@ -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);
                        }
                }
 
index 1412a0035f20212bf41e2fc2f4c4f7448215bdfc..cbbae7aacb19bf143e94fd1ba161794439f08754 100644 (file)
@@ -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 {