commit 13a13b6: [Minor] Add `max_recipients` config knob

Vsevolod Stakhov vsevolod at highsecure.ru
Tue May 19 16:21:07 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-05-19 17:18:43 +0100
URL: https://github.com/rspamd/rspamd/commit/13a13b60f155603393c5575ccfdeaa877cb613b6 (HEAD -> master)

[Minor] Add `max_recipients` config knob

---
 conf/options.inc           |  1 +
 src/libmime/email_addr.c   |  2 +-
 src/libmime/email_addr.h   |  4 +---
 src/libmime/mime_headers.c | 11 ++++++-----
 src/libserver/cfg_file.h   |  1 +
 src/libserver/cfg_rcl.c    |  6 ++++++
 src/libserver/cfg_utils.c  |  1 +
 src/libserver/milter.c     |  8 ++++----
 src/libserver/protocol.c   |  6 +++---
 9 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/conf/options.inc b/conf/options.inc
index 8338a89cc..f305bcfcb 100644
--- a/conf/options.inc
+++ b/conf/options.inc
@@ -29,6 +29,7 @@ check_all_filters = false;
 dns_max_requests = 64;
 max_lua_urls = 1024;
 max_urls = 10240;
+max_recipients = 1024;
 
 dns {
     timeout = 1s;
diff --git a/src/libmime/email_addr.c b/src/libmime/email_addr.c
index 1987c58c3..eaf44fc91 100644
--- a/src/libmime/email_addr.c
+++ b/src/libmime/email_addr.c
@@ -48,7 +48,7 @@ rspamd_email_address_unescape (struct rspamd_email_address *addr)
 }
 
 struct rspamd_email_address *
-rspamd_email_address_from_smtp (const gchar *str, guint len, gint max_elements)
+rspamd_email_address_from_smtp (const gchar *str, guint len)
 {
 	struct rspamd_email_address addr, *ret;
 	gsize nlen;
diff --git a/src/libmime/email_addr.h b/src/libmime/email_addr.h
index 2452fc7aa..fe9fd9e9d 100644
--- a/src/libmime/email_addr.h
+++ b/src/libmime/email_addr.h
@@ -67,9 +67,7 @@ struct rspamd_task;
  * @param len length of string
  * @return
  */
-struct rspamd_email_address *rspamd_email_address_from_smtp (const gchar *str,
-		guint len,
-		gint max_elements);
+struct rspamd_email_address *rspamd_email_address_from_smtp (const gchar *str, guint len);
 
 /**
  * Parses email address from the mime header, decodes names and return the array
diff --git a/src/libmime/mime_headers.c b/src/libmime/mime_headers.c
index 55d04d69a..786d43a6a 100644
--- a/src/libmime/mime_headers.c
+++ b/src/libmime/mime_headers.c
@@ -20,6 +20,7 @@
 #include "contrib/uthash/utlist.h"
 #include "libserver/mempool_vars_internal.h"
 #include "libserver/url.h"
+#include "libserver/cfg_file.h"
 #include "libutil/util.h"
 #include <unicode/utf8.h>
 
@@ -62,25 +63,25 @@ rspamd_mime_header_check_special (struct rspamd_task *task,
 	case 0x76F31A09F4352521ULL:	/* to */
 		MESSAGE_FIELD (task, rcpt_mime) = rspamd_email_address_from_mime (task->task_pool,
 				rh->decoded, strlen (rh->decoded),
-				MESSAGE_FIELD (task, rcpt_mime), -1);
+				MESSAGE_FIELD (task, rcpt_mime), task->cfg->max_recipients);
 		rh->flags |= RSPAMD_HEADER_TO|RSPAMD_HEADER_RCPT|RSPAMD_HEADER_UNIQUE;
 		break;
 	case 0x7EB117C1480B76ULL:	/* cc */
 		MESSAGE_FIELD (task, rcpt_mime) = rspamd_email_address_from_mime (task->task_pool,
 				rh->decoded, strlen (rh->decoded),
-				MESSAGE_FIELD (task, rcpt_mime), -1);
+				MESSAGE_FIELD (task, rcpt_mime), task->cfg->max_recipients);
 		rh->flags |= RSPAMD_HEADER_CC|RSPAMD_HEADER_RCPT|RSPAMD_HEADER_UNIQUE;
 		break;
 	case 0xE4923E11C4989C8DULL:	/* bcc */
 		MESSAGE_FIELD (task, rcpt_mime) = rspamd_email_address_from_mime (task->task_pool,
 				rh->decoded, strlen (rh->decoded),
-				MESSAGE_FIELD (task, rcpt_mime), -1);
+				MESSAGE_FIELD (task, rcpt_mime), task->cfg->max_recipients);
 		rh->flags |= RSPAMD_HEADER_BCC|RSPAMD_HEADER_RCPT|RSPAMD_HEADER_UNIQUE;
 		break;
 	case 0x41E1985EDC1CBDE4ULL:	/* from */
 		MESSAGE_FIELD (task, from_mime) = rspamd_email_address_from_mime (task->task_pool,
 				rh->decoded, strlen (rh->decoded),
-				MESSAGE_FIELD (task, from_mime), -1);
+				MESSAGE_FIELD (task, from_mime), task->cfg->max_recipients);
 		rh->flags |= RSPAMD_HEADER_FROM|RSPAMD_HEADER_SENDER|RSPAMD_HEADER_UNIQUE;
 		break;
 	case 0x43A558FC7C240226ULL:	/* message-id */ {
@@ -129,7 +130,7 @@ rspamd_mime_header_check_special (struct rspamd_task *task,
 	case 0xEE4AA2EAAC61D6F4ULL:	/* return-path */
 		if (task->from_envelope == NULL) {
 			task->from_envelope = rspamd_email_address_from_smtp (rh->decoded,
-					strlen (rh->decoded), -1);
+					strlen (rh->decoded));
 		}
 		rh->flags = RSPAMD_HEADER_RETURN_PATH|RSPAMD_HEADER_UNIQUE;
 		break;
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index 702325f62..5c6c682a1 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -471,6 +471,7 @@ struct rspamd_config {
 	guint full_gc_iters;                            /**< iterations between full gc cycle					*/
 	guint max_lua_urls;                             /**< maximum number of urls to be passed to Lua			*/
 	guint max_urls;                                 /**< maximum number of urls to be processed in general	*/
+	gint max_recipients;                           /**< maximum number of recipients to be processed	*/
 	guint max_blas_threads;                         /**< maximum threads for openblas when learning ANN		*/
 	guint max_opts_len;                             /**< maximum length for all options for a symbol		*/
 
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index dc568297b..885ebad4d 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -2240,6 +2240,12 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
 				G_STRUCT_OFFSET (struct rspamd_config, max_urls),
 				RSPAMD_CL_FLAG_INT_32,
 				"Maximum count of URLs to process to avoid DoS (default: 10240)");
+		rspamd_rcl_add_default_handler (sub,
+				"max_recipients",
+				rspamd_rcl_parse_struct_integer,
+				G_STRUCT_OFFSET (struct rspamd_config, max_recipients),
+				RSPAMD_CL_FLAG_INT_32,
+				"Maximum count of recipients to process to avoid DoS (default: 1024)");
 		rspamd_rcl_add_default_handler (sub,
 				"max_blas_threads",
 				rspamd_rcl_parse_struct_integer,
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index d3ad9e210..6b3f3e15b 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -221,6 +221,7 @@ rspamd_config_new (enum rspamd_config_init_flags flags)
 	cfg->cache_reload_time = 30.0;
 	cfg->max_lua_urls = 1024;
 	cfg->max_urls = cfg->max_lua_urls * 10;
+	cfg->max_recipients = 1024;
 	cfg->max_blas_threads = 1;
 	cfg->max_opts_len = 4096;
 
diff --git a/src/libserver/milter.c b/src/libserver/milter.c
index 4713b7371..dc398912d 100644
--- a/src/libserver/milter.c
+++ b/src/libserver/milter.c
@@ -603,7 +603,7 @@ rspamd_milter_process_command (struct rspamd_milter_session *session,
 				cpy = rspamd_mempool_alloc (priv->pool, zero - pos);
 				memcpy (cpy, pos, zero - pos);
 				msg_debug_milter ("got mail: %*s", (int)(zero - pos), cpy);
-				addr = rspamd_email_address_from_smtp (cpy, zero - pos, -1);
+				addr = rspamd_email_address_from_smtp (cpy, zero - pos);
 
 				if (addr) {
 					session->from = addr;
@@ -618,7 +618,7 @@ rspamd_milter_process_command (struct rspamd_milter_session *session,
 				/* That actually should not happen */
 				cpy = rspamd_mempool_alloc (priv->pool, end - pos);
 				memcpy (cpy, pos, end - pos);
-				addr = rspamd_email_address_from_smtp (cpy, end - pos, -1);
+				addr = rspamd_email_address_from_smtp (cpy, end - pos);
 
 				if (addr) {
 					session->from = addr;
@@ -707,7 +707,7 @@ rspamd_milter_process_command (struct rspamd_milter_session *session,
 				memcpy (cpy, pos, end - pos);
 
 				msg_debug_milter ("got rcpt: %*s", (int)(zero - pos), cpy);
-				addr = rspamd_email_address_from_smtp (cpy, zero - pos, -1);
+				addr = rspamd_email_address_from_smtp (cpy, zero - pos);
 
 				if (addr) {
 					if (!session->rcpts) {
@@ -726,7 +726,7 @@ rspamd_milter_process_command (struct rspamd_milter_session *session,
 				msg_debug_milter ("got weird rcpt: %*s", (int)(end - pos),
 						pos);
 				/* That actually should not happen */
-				addr = rspamd_email_address_from_smtp (cpy, end - pos, -1);
+				addr = rspamd_email_address_from_smtp (cpy, end - pos);
 
 				if (addr) {
 					if (!session->rcpts) {
diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c
index 1d51ff28f..9d1276064 100644
--- a/src/libserver/protocol.c
+++ b/src/libserver/protocol.c
@@ -292,7 +292,7 @@ rspamd_protocol_process_recipients (struct rspamd_task *task,
 			else if (*p == ',' && start_addr != NULL && p > start_addr) {
 				/* We have finished address, check what we have */
 				addr = rspamd_email_address_from_smtp (start_addr,
-						p - start_addr, -1);
+						p - start_addr);
 
 				if (addr) {
 					if (task->rcpt_envelope == NULL) {
@@ -322,7 +322,7 @@ rspamd_protocol_process_recipients (struct rspamd_task *task,
 	if (start_addr && p > start_addr) {
 		switch (state) {
 		case normal_string:
-			addr = rspamd_email_address_from_smtp (start_addr, end - start_addr, -1);
+			addr = rspamd_email_address_from_smtp (start_addr, end - start_addr);
 
 			if (addr) {
 				if (task->rcpt_envelope == NULL) {
@@ -495,7 +495,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task,
 				IF_HEADER (FROM_HEADER) {
 					task->from_envelope = rspamd_email_address_from_smtp (
 							hv_tok->begin,
-							hv_tok->len, -1);
+							hv_tok->len);
 					msg_debug_protocol ("read from header, value: %T", hv_tok);
 
 					if (!task->from_envelope) {


More information about the Commits mailing list