Skip to content

Commit

Permalink
[Minor] Fix parsing of some misformed email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Jul 21, 2021
1 parent 23b52c3 commit 276d5a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
19 changes: 18 additions & 1 deletion src/libmime/email_addr.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 4 additions & 4 deletions test/lua/unit/smtp_addr.lua
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -81,7 +81,7 @@ context("SMTP address check functions", function()
'<a@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)

Expand Down

0 comments on commit 276d5a9

Please sign in to comment.