]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] Improve body rewriting support in rspamc and proxy
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 10 Oct 2025 12:37:32 +0000 (13:37 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 10 Oct 2025 12:37:32 +0000 (13:37 +0100)
- Add --output-body option to rspamc for saving rewritten message body to file
  instead of printing to stdout
- Enable body_block protocol flag in proxy for non-milter mode to ensure
  message body is always available for rewriting operations
- This ensures consistent body rewriting capability across all protocol modes
  (rspamc, milter, and proxy)

src/client/rspamc.cxx
src/rspamd_proxy.c

index c42d301429f59ee0b9b3683f1d8315dc795a975c..7d20ac759ab8c7e1ed7b50e45831681e5d12398c 100644 (file)
@@ -94,6 +94,7 @@ static const char *user_agent = "rspamc";
 static const char *files_list = nullptr;
 static const char *queue_id = nullptr;
 static const char *log_tag = nullptr;
+static const char *output_body = nullptr;
 static std::string settings;
 static std::string neural_train;
 static std::string neural_rule;
@@ -200,6 +201,8 @@ static GOptionEntry entries[] =
                 "Set Queue-ID header for the request", nullptr},
                {"log-tag", '\0', 0, G_OPTION_ARG_STRING, &log_tag,
                 "Set Log-Tag header for the request", nullptr},
+               {"output-body", '\0', 0, G_OPTION_ARG_FILENAME, &output_body,
+                "Save rewritten message body to file", nullptr},
                {"settings", '\0', 0, G_OPTION_ARG_CALLBACK, (void *) &rspamc_settings_callback,
                 "Set Settings header as JSON/UCL for the request", nullptr},
                {nullptr, 0, 0, G_OPTION_ARG_NONE, nullptr, nullptr, nullptr}};
@@ -2102,8 +2105,29 @@ rspamc_client_cb(struct rspamd_client_connection *conn,
                                }
 
                                if (body) {
-                                       rspamc_print(out, "\nNew body:\n{}\n",
-                                                                std::string_view{body, bodylen});
+                                       if (output_body) {
+                                               /* Save body to file */
+                                               FILE *body_file = fopen(output_body, "wb");
+                                               if (body_file) {
+                                                       if (fwrite(body, 1, bodylen, body_file) != bodylen) {
+                                                               rspamc_print(stderr, "Failed to write body to file {}: {}\n",
+                                                                                        output_body, strerror(errno));
+                                                       }
+                                                       else {
+                                                               rspamc_print(out, "\nNew body saved to: {}\n", output_body);
+                                                       }
+                                                       fclose(body_file);
+                                               }
+                                               else {
+                                                       rspamc_print(stderr, "Failed to open output file {}: {}\n",
+                                                                                output_body, strerror(errno));
+                                               }
+                                       }
+                                       else {
+                                               /* Print to stdout */
+                                               rspamc_print(out, "\nNew body:\n{}\n",
+                                                                        std::string_view{body, bodylen});
+                                       }
                                }
 
                                ucl_object_unref(result);
index 82a43d3d27e4020d90b02a3c01716dd735aa4497..5321c8677236c92b4762d418dc878baf015a5878 100644 (file)
@@ -2470,6 +2470,10 @@ rspamd_proxy_self_scan(struct rspamd_proxy_session *session)
                        rspamd_task_set_rcpt_esmtp_args(task, session->rcpt_esmtp_args);
                }
        }
+       else {
+               /* Always request body block for rewriting support */
+               task->protocol_flags |= RSPAMD_TASK_PROTOCOL_FLAG_BODY_BLOCK;
+       }
 
        task->sock = -1;