From: Vsevolod Stakhov Date: Tue, 6 Oct 2020 14:36:23 +0000 (+0100) Subject: [Minor] Add erroring to smtp parse date X-Git-Tag: 2.7~260 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b6085d45f1a31b4bf19abf91d5926e1ef87d39ca;p=rspamd.git [Minor] Add erroring to smtp parse date --- diff --git a/src/libmime/mime_headers.c b/src/libmime/mime_headers.c index 34cafd8cb..bbf1dc31d 100644 --- a/src/libmime/mime_headers.c +++ b/src/libmime/mime_headers.c @@ -1655,7 +1655,7 @@ rspamd_smtp_received_parse (struct rspamd_task *task, if (date_pos > 0 && date_pos < len) { rh->timestamp = rspamd_parse_smtp_date (data + date_pos, - len - date_pos); + len - date_pos, NULL); } return 0; diff --git a/src/libmime/smtp_parsers.h b/src/libmime/smtp_parsers.h index 3a52f1dd6..067d84424 100644 --- a/src/libmime/smtp_parsers.h +++ b/src/libmime/smtp_parsers.h @@ -46,7 +46,7 @@ rspamd_rfc2047_parser (const gchar *in, gsize len, gint *pencoding, rspamd_inet_addr_t *rspamd_parse_smtp_ip (const char *data, size_t len, rspamd_mempool_t *pool); -guint64 rspamd_parse_smtp_date (const char *data, size_t len); +guint64 rspamd_parse_smtp_date (const char *data, size_t len, GError **err); #ifdef __cplusplus } diff --git a/src/lua/lua_task.c b/src/lua/lua_task.c index 21780cb9f..6a904352f 100644 --- a/src/lua/lua_task.c +++ b/src/lua/lua_task.c @@ -5042,19 +5042,27 @@ lua_task_get_date (lua_State *L) if (h) { time_t tt; struct tm t; + GError *err = NULL; - tt = rspamd_parse_smtp_date (h->decoded, strlen (h->decoded)); + tt = rspamd_parse_smtp_date (h->decoded, strlen (h->decoded), + &err); - if (!gmt) { - rspamd_localtime (tt, &t); + if (err == NULL) { + if (!gmt) { + rspamd_localtime (tt, &t); #if !defined(__sun) - t.tm_gmtoff = 0; + t.tm_gmtoff = 0; #endif - t.tm_isdst = 0; - tim = mktime (&t); + t.tm_isdst = 0; + tim = mktime (&t); + } + else { + tim = tt; + } } else { - tim = tt; + g_error_free (err); + tim = 0.0; } } else { diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 5509fc435..1a2b52f80 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -3968,25 +3968,35 @@ lua_util_parse_smtp_date (lua_State *L) { gsize slen; const gchar *str = lua_tolstring (L, 1, &slen); + GError *err = NULL; if (str == NULL) { return luaL_argerror (L, 1, "invalid argument"); } - time_t tt = rspamd_parse_smtp_date (str, slen); + time_t tt = rspamd_parse_smtp_date (str, slen, &err); - if (lua_isboolean (L, 2) && !!lua_toboolean (L, 2)) { - struct tm t; + if (err == NULL) { + if (lua_isboolean (L, 2) && !!lua_toboolean (L, 2)) { + struct tm t; - rspamd_localtime (tt, &t); + rspamd_localtime (tt, &t); #if !defined(__sun) - t.tm_gmtoff = 0; + t.tm_gmtoff = 0; #endif - t.tm_isdst = 0; - tt = mktime (&t); + t.tm_isdst = 0; + tt = mktime (&t); + } + + lua_pushnumber (L, tt); } + else { + lua_pushnil (L); + lua_pushstring (L, err->message); + g_error_free (err); - lua_pushnumber (L, tt); + return 2; + } return 1; } diff --git a/src/ragel/smtp_date_parser.rl b/src/ragel/smtp_date_parser.rl index f0d49c23a..48a1cbcbf 100644 --- a/src/ragel/smtp_date_parser.rl +++ b/src/ragel/smtp_date_parser.rl @@ -13,7 +13,7 @@ %% write data; guint64 -rspamd_parse_smtp_date (const char *data, size_t len) +rspamd_parse_smtp_date (const char *data, size_t len, GError **err) { const gchar *p = data, *pe = data + len, *eof = data + len, *tmp = data; struct tm tm; @@ -25,5 +25,11 @@ rspamd_parse_smtp_date (const char *data, size_t len) %% write init; %% write exec; + if ( cs < %%{ write first_final; }%% ) { + g_set_error (err, g_quark_from_static_string ("smtp_date"), cs, "invalid date at offset %d (%c), state %d", + p - data, *p, cs); + return (guint64)(-1); + } + return rspamd_tm_to_time (&tm, tz); } \ No newline at end of file