commit 276d5a9: [Minor] Fix parsing of some misformed email addresses

Vsevolod Stakhov vsevolod at highsecure.ru
Wed Jul 21 14:49:04 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-07-21 15:42:57 +0100
URL: https://github.com/rspamd/rspamd/commit/276d5a92efb6f4c630a53398458b7286da048757

[Minor] Fix parsing of some misformed email addresses

---
 src/libmime/email_addr.c    | 19 ++++++++++++++++++-
 test/lua/unit/smtp_addr.lua |  8 ++++----
 2 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/src/libmime/email_addr.c b/src/libmime/email_addr.c
index 6895c1b11..38a4732c5 100644
--- a/src/libmime/email_addr.c
+++ b/src/libmime/email_addr.c
@@ -248,7 +248,7 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr,
 								gint max_elements)
 {
 	GPtrArray *res = src;
-	gboolean seen_at = FALSE;
+	gboolean seen_at = FALSE, seen_obrace = FALSE;
 
 	const gchar *p = hdr, *end = hdr + len, *c = hdr, *t;
 	GString *ns, *cpy;
@@ -434,6 +434,12 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr,
 				state = skip_spaces;
 				next_state = parse_name;
 			}
+			else if (*p == '@' && seen_obrace) {
+				seen_at = TRUE;
+			}
+			else if (*p == '<') {
+				seen_obrace = TRUE;
+			}
 			p ++;
 			break;
 		case parse_addr:
@@ -518,6 +524,17 @@ rspamd_email_address_from_mime (rspamd_mempool_t *pool, const gchar *hdr,
 		break;
 	case parse_quoted:
 		/* Unfinished quoted string or a comment */
+		/* If we have seen obrace + at, then we still can try to resolve address */
+		if (seen_at && seen_obrace) {
+			p = rspamd_memrchr (cpy->str, '<', cpy->len);
+			g_assert (p != NULL);
+			if (rspamd_email_address_check_and_add (p, end - p,
+					res, pool, ns, max_elements) == 0) {
+				if (res->len == 0) {
+					rspamd_email_address_add (pool, res, NULL, ns);
+				}
+			}
+		}
 		break;
 	default:
 		/* Do nothing */
diff --git a/test/lua/unit/smtp_addr.lua b/test/lua/unit/smtp_addr.lua
index 062c509d5..cfce20fcc 100644
--- a/test/lua/unit/smtp_addr.lua
+++ b/test/lua/unit/smtp_addr.lua
@@ -4,7 +4,7 @@ context("SMTP address check functions", function()
   local logger = require("rspamd_logger")
   local ffi = require("ffi")
   local util = require("rspamd_util")
-  require "fun" ()
+  local fun = require "fun"
   ffi.cdef[[
   struct rspamd_email_address {
     const char *raw;
@@ -47,13 +47,13 @@ context("SMTP address check functions", function()
   }
 
 
-  each(function(case)
+  fun.each(function(case)
     test("Parse valid smtp addr: " .. case[1], function()
       local st = ffi.C.rspamd_email_address_from_smtp(case[1], #case[1])
 
       assert_not_nil(st, "should be able to parse " .. case[1])
 
-      each(function(k, ex)
+      fun.each(function(k, ex)
         if k == 'user' then
           local str = ffi.string(st.user, st.user_len)
           assert_equal(str, ex)
@@ -81,7 +81,7 @@ context("SMTP address check functions", function()
       '<a at example.com><>',
     }
 
-  each(function(case)
+  fun.each(function(case)
     test("Parse invalid smtp addr: " .. case, function()
       local st = ffi.C.rspamd_email_address_from_smtp(case, #case)
 


More information about the Commits mailing list