]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add erroring to smtp parse date
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 6 Oct 2020 14:36:23 +0000 (15:36 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 6 Oct 2020 14:36:23 +0000 (15:36 +0100)
src/libmime/mime_headers.c
src/libmime/smtp_parsers.h
src/lua/lua_task.c
src/lua/lua_util.c
src/ragel/smtp_date_parser.rl

index 34cafd8cbc933414d2b83d75da75cc3060cdd203..bbf1dc31da5e40ed8771015225ce05cfe66823ff 100644 (file)
@@ -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;
index 3a52f1dd6950729dc7e452ecc730d2d7c85f1626..067d84424ad9518cffbfce122c339ebafc49bac2 100644 (file)
@@ -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
 }
index 21780cb9fe151039c104d6c21f9bf47a7eeb409f..6a904352fc64dfd73d34bd4441532671f8d76d20 100644 (file)
@@ -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 {
index 5509fc435b97896fe47f8cb1c573a023a744e167..1a2b52f808d7f9247e02593c5fc8ba0db84e27bf 100644 (file)
@@ -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;
 }
index f0d49c23ae99784cb70c81d44f1cc35f33096fa1..48a1cbcbfc1aa78a99ae061354e0c8d8f2f6ea6e 100644 (file)
@@ -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