]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Project] Rework API for the modified headers
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 23 Feb 2021 16:08:24 +0000 (16:08 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 23 Feb 2021 16:08:24 +0000 (16:08 +0000)
src/libmime/images.c
src/libmime/mime_expressions.c
src/libmime/mime_headers.c
src/libmime/mime_headers.h
src/libmime/mime_parser.c
src/libserver/dkim.c
src/libserver/re_cache.c
src/lua/lua_mimepart.c
src/lua/lua_task.c
src/plugins/dkim_check.c

index b3baa8e4c0f65ddc891da0924e1fe3db138f1a09..73ff4391044dbbbef5eacad60f78dd8f4b821866 100644 (file)
@@ -666,8 +666,8 @@ rspamd_image_process_part (struct rspamd_task *task, struct rspamd_mime_part *pa
 
        if (img) {
                /* Check Content-Id */
-               rh = rspamd_message_get_header_from_hash (part->raw_headers,
-                               "Content-Id");
+               rh = rspamd_message_get_header_from_hash(part->raw_headers,
+                               "Content-Id", FALSE);
 
                if (rh) {
                        cid = rh->decoded;
index ab863aa71899cd1e03ab198c7465e3fc0893e251..d0c1704ad64ebf2896a85db0830ce00ece9bacb3 100644 (file)
@@ -1264,8 +1264,8 @@ rspamd_header_exists (struct rspamd_task * task, GArray * args, void *unused)
                return FALSE;
        }
 
-       rh = rspamd_message_get_header_array (task,
-                       (gchar *)arg->data);
+       rh = rspamd_message_get_header_array(task,
+                       (gchar *) arg->data, FALSE);
 
        debug_task ("try to get header %s: %d", (gchar *)arg->data,
                        (rh != NULL));
@@ -1653,7 +1653,7 @@ rspamd_raw_header_exists (struct rspamd_task *task, GArray * args, void *unused)
                return FALSE;
        }
 
-       return rspamd_message_get_header_array (task, arg->data) != NULL;
+       return rspamd_message_get_header_array(task, arg->data, FALSE) != NULL;
 }
 
 static gboolean
index 7a1667539e5ff3c344064746c71e8b5a51267053..832e1306dd55ca8b6b1f01f57941ab60dec59a34 100644 (file)
@@ -1662,11 +1662,13 @@ rspamd_smtp_received_parse (struct rspamd_task *task,
 }
 
 struct rspamd_mime_header *
-rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *headers,
-                                                                        const gchar *field)
+rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *hdrs,
+                                                                        const gchar *field,
+                                                                        gboolean need_modified)
 {
        khiter_t k;
-       khash_t(rspamd_mime_headers_htb) *htb = &headers->htb;
+       khash_t(rspamd_mime_headers_htb) *htb = &hdrs->htb;
+       struct rspamd_mime_header *hdr;
 
        if (htb) {
                k = kh_get (rspamd_mime_headers_htb, htb, (gchar *) field);
@@ -1675,19 +1677,34 @@ rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *headers,
                        return NULL;
                }
 
-               return kh_value (htb, k);
+               hdr = kh_value (htb, k);
+
+               if (!need_modified) {
+                       if (hdr->flags & RSPAMD_HEADER_NON_EXISTING) {
+                               return NULL;
+                       }
+
+                       return hdr;
+               }
+               else {
+                       if (hdr->flags & RSPAMD_HEADER_MODIFIED) {
+                               return hdr->modified_chain;
+                       }
+
+                       return hdr;
+               }
        }
 
        return NULL;
 }
 
 struct rspamd_mime_header *
-rspamd_message_get_header_array (struct rspamd_task *task,
-                                                                const gchar *field)
+rspamd_message_get_header_array (struct rspamd_task *task, const gchar *field,
+               gboolean need_modified)
 {
-       return rspamd_message_get_header_from_hash (
+       return rspamd_message_get_header_from_hash(
                        MESSAGE_FIELD_CHECK (task, raw_headers),
-                       field);
+                       field, need_modified);
 }
 
 static void
@@ -1743,7 +1760,7 @@ rspamd_message_set_modified_header (struct rspamd_task *task,
                if (k == kh_end (htb)) {
                        hdr_elt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*hdr_elt));
 
-                       hdr_elt->flags |= RSPAMD_HEADER_MODIFIED;
+                       hdr_elt->flags |= RSPAMD_HEADER_MODIFIED|RSPAMD_HEADER_NON_EXISTING;
                        hdr_elt->name = rspamd_mempool_strdup (task->task_pool, hdr_name);
 
                        int r;
index f01a8b649ac3ade8819bd84fdf8be2971eb450f7..ad8f1b68f95636bcb47f86fcc74d9b12914a3fb3 100644 (file)
@@ -52,6 +52,7 @@ enum rspamd_mime_header_flags {
        RSPAMD_HEADER_MODIFIED = 1u << 15u, /* Means we need to check modified chain */
        RSPAMD_HEADER_ADDED = 1u << 16u, /* A header has been artificially added */
        RSPAMD_HEADER_REMOVED = 1u << 17u, /* A header has been artificially removed */
+       RSPAMD_HEADER_NON_EXISTING = 1u << 18u, /* Header was not in the original message */
 };
 
 struct rspamd_mime_header {
@@ -164,7 +165,8 @@ gchar *rspamd_mime_message_id_generate (const gchar *fqdn);
  */
 struct rspamd_mime_header *
 rspamd_message_get_header_array (struct rspamd_task *task,
-                                                                const gchar *field);
+                                                               const gchar *field,
+                                                               gboolean need_modified);
 
 /**
  * Get an array of header's values with specified header's name using raw headers
@@ -174,7 +176,8 @@ rspamd_message_get_header_array (struct rspamd_task *task,
  */
 struct rspamd_mime_header *
 rspamd_message_get_header_from_hash (struct rspamd_mime_headers_table *hdrs,
-                                                                        const gchar *field);
+                                                                        const gchar *field,
+                                                                        gboolean need_modified);
 
 /**
  * Modifies a header (or insert one if not found)
index e74201f27d8eda1ddf6e50d6b0291deb6c49588b..0d665986745ea35b7331d5f7b32a64e63f52324d 100644 (file)
@@ -392,7 +392,7 @@ rspamd_mime_part_get_cte (struct rspamd_task *task,
        enum rspamd_cte cte = RSPAMD_CTE_UNKNOWN;
        gboolean parent_propagated = FALSE;
 
-       hdr = rspamd_message_get_header_from_hash (hdrs, "Content-Transfer-Encoding");
+       hdr = rspamd_message_get_header_from_hash(hdrs, "Content-Transfer-Encoding", FALSE);
 
        if (hdr == NULL) {
                if (part->parent_part && part->parent_part->cte != RSPAMD_CTE_UNKNOWN &&
@@ -471,8 +471,8 @@ rspamd_mime_part_get_cd (struct rspamd_task *task, struct rspamd_mime_part *part
        rspamd_ftok_t srch;
        struct rspamd_content_type_param *found;
 
-       hdr = rspamd_message_get_header_from_hash (part->raw_headers,
-                       "Content-Disposition");
+       hdr = rspamd_message_get_header_from_hash(part->raw_headers,
+                       "Content-Disposition", FALSE);
 
 
        if (hdr == NULL) {
@@ -859,8 +859,8 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task,
                        }
                }
 
-               hdr = rspamd_message_get_header_from_hash (npart->raw_headers,
-                               "Content-Type");
+               hdr = rspamd_message_get_header_from_hash(npart->raw_headers,
+                               "Content-Type", FALSE);
 
        }
        else {
@@ -1402,9 +1402,9 @@ rspamd_mime_parse_message (struct rspamd_task *task,
                                }
                        }
 
-                       hdr = rspamd_message_get_header_from_hash (
+                       hdr = rspamd_message_get_header_from_hash(
                                        MESSAGE_FIELD (task, raw_headers),
-                                       "Content-Type");
+                                       "Content-Type", FALSE);
                }
                else {
                        /* First apply heuristic, maybe we have just headers */
@@ -1432,9 +1432,9 @@ rspamd_mime_parse_message (struct rspamd_task *task,
                                        }
                                }
 
-                               hdr = rspamd_message_get_header_from_hash (
+                               hdr = rspamd_message_get_header_from_hash(
                                                MESSAGE_FIELD (task, raw_headers),
-                                               "Content-Type");
+                                               "Content-Type", FALSE);
                                task->flags |= RSPAMD_TASK_FLAG_BROKEN_HEADERS;
                        }
                        else {
@@ -1488,8 +1488,8 @@ rspamd_mime_parse_message (struct rspamd_task *task,
                                }
                        }
 
-                       hdr = rspamd_message_get_header_from_hash (npart->raw_headers,
-                                       "Content-Type");
+                       hdr = rspamd_message_get_header_from_hash(npart->raw_headers,
+                                       "Content-Type", FALSE);
                }
                else {
                        body_pos = 0;
index 55a874e32e64dd36840470d34e72346b1267231e..1cd6d09ffd6fce6c5f273dd85b2ff41c296964c0 100644 (file)
@@ -2334,7 +2334,7 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx,
        }
 
        if (dkim_header == NULL) {
-               rh = rspamd_message_get_header_array (task, header_name);
+               rh = rspamd_message_get_header_array(task, header_name, FALSE);
 
                if (rh) {
                        /* Check uniqueness of the header but we count from the bottom to top */
@@ -2451,7 +2451,7 @@ rspamd_dkim_canonize_header (struct rspamd_dkim_common_ctx *ctx,
                /* For signature check just use the saved dkim header */
                if (ctx->header_canon_type == DKIM_CANON_SIMPLE) {
                        /* We need to find our own signature and use it */
-                       rh = rspamd_message_get_header_array (task, header_name);
+                       rh = rspamd_message_get_header_array(task, header_name, FALSE);
 
                        if (rh) {
                                /* We need to find our own signature */
@@ -3287,7 +3287,7 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector,
                        /* Do oversigning */
                        guint count = 0;
 
-                       rh = rspamd_message_get_header_array (task, dh->name);
+                       rh = rspamd_message_get_header_array(task, dh->name, FALSE);
 
                        if (rh) {
                                DL_FOREACH (rh, cur) {
@@ -3315,7 +3315,7 @@ rspamd_dkim_sign (struct rspamd_task *task, const gchar *selector,
                        }
                }
                else {
-                       rh = rspamd_message_get_header_array (task, dh->name);
+                       rh = rspamd_message_get_header_array(task, dh->name, FALSE);
 
                        if (rh) {
                                if (hstat.s.count > 0) {
index d8e6d56d20fd42b8afca08641580c169eeb6cbf3..b3326bcff87e79f6bac9fbf778ac15a8110a60ec 100644 (file)
@@ -1154,8 +1154,8 @@ rspamd_re_cache_exec_re (struct rspamd_task *task,
        case RSPAMD_RE_HEADER:
        case RSPAMD_RE_RAWHEADER:
                /* Get list of specified headers */
-               rh = rspamd_message_get_header_array (task,
-                               re_class->type_data);
+               rh = rspamd_message_get_header_array(task,
+                               re_class->type_data, FALSE);
 
                if (rh) {
                        ret = rspamd_re_cache_process_headers_list (task, rt, re,
@@ -1177,8 +1177,8 @@ rspamd_re_cache_exec_re (struct rspamd_task *task,
                break;
        case RSPAMD_RE_MIMEHEADER:
                PTR_ARRAY_FOREACH (MESSAGE_FIELD (task, parts), i, mime_part) {
-                       rh = rspamd_message_get_header_from_hash (mime_part->raw_headers,
-                                       re_class->type_data);
+                       rh = rspamd_message_get_header_from_hash(mime_part->raw_headers,
+                                       re_class->type_data, FALSE);
 
                        if (rh) {
                                ret += rspamd_re_cache_process_headers_list (task, rt, re,
@@ -1345,7 +1345,7 @@ rspamd_re_cache_exec_re (struct rspamd_task *task,
                 * of the body content.
                 */
 
-               rh = rspamd_message_get_header_array (task, "Subject");
+               rh = rspamd_message_get_header_array(task, "Subject", FALSE);
 
                if (rh) {
                        scvec[0] = (guchar *)rh->decoded;
index c12485ecbb5dcb8c82c43be028b0f1f1349dd38f..fe8bb424613f5405de5aab5ae49790175b8be4e5 100644 (file)
@@ -1645,7 +1645,7 @@ lua_mimepart_get_header_common (lua_State *L, enum rspamd_lua_task_header_type h
 
                return rspamd_lua_push_header_array (L,
                                name,
-                               rspamd_message_get_header_from_hash (part->raw_headers, name),
+                               rspamd_message_get_header_from_hash(part->raw_headers, name, FALSE),
                                how,
                                strong);
        }
@@ -1782,8 +1782,8 @@ lua_mimepart_is_attachment (lua_State * L)
                /* if has_name and not (image and Content-ID_header_present) */
                if (part->cd && part->cd->filename.len > 0) {
                        if (part->part_type != RSPAMD_MIME_PART_IMAGE &&
-                               rspamd_message_get_header_from_hash (part->raw_headers,
-                                               "Content-Id") == NULL) {
+                                       rspamd_message_get_header_from_hash(part->raw_headers,
+                                                       "Content-Id", FALSE) == NULL) {
                                /* Filename is presented but no content id and not image */
                                lua_pushboolean (L, true);
                        }
index 1095d1a60d215f6111e97f5f6cd1b8e78d57eaae..f4dee6d656687386781dff071f5e80099931b2ee 100644 (file)
@@ -2934,7 +2934,7 @@ lua_task_get_header_common (lua_State *L, enum rspamd_lua_task_header_type how)
                        strong = lua_toboolean (L, 3);
                }
 
-               rh = rspamd_message_get_header_array (task, name);
+               rh = rspamd_message_get_header_array(task, name, FALSE);
 
                return rspamd_lua_push_header_array (L, name, rh, how, strong);
        }
@@ -3988,7 +3988,7 @@ lua_task_get_reply_sender (lua_State *L)
 
        if (task) {
 
-               rh = rspamd_message_get_header_array (task, "Reply-To");
+               rh = rspamd_message_get_header_array(task, "Reply-To", FALSE);
 
                if (rh) {
                        lua_pushstring (L, rh->decoded);
@@ -5082,7 +5082,7 @@ lua_task_get_date (lua_State *L)
                        }
                }
                else {
-                       h = rspamd_message_get_header_array (task, "Date");
+                       h = rspamd_message_get_header_array(task, "Date", FALSE);
 
                        if (h) {
                                time_t tt;
index c00b87b1113d06ba70489208e2296399db0d153f..ab5d6ec4342d04512c6f6f3cb2d06a2b529d2506 100644 (file)
@@ -1174,7 +1174,7 @@ dkim_symbol_callback (struct rspamd_task *task,
        rspamd_symcache_item_async_inc (task, item, M);
 
        /* Now check if a message has its signature */
-       rh = rspamd_message_get_header_array (task, RSPAMD_DKIM_SIGNHEADER);
+       rh = rspamd_message_get_header_array(task, RSPAMD_DKIM_SIGNHEADER, FALSE);
        if (rh) {
                msg_debug_task ("dkim signature found");