fix bugs 4373, 'attach mailto URI double free' and 4374, ' insert mailto URI misses...
[claws.git] / src / common / utils.c
index c4364c4bd59c47518052c8cbc7c684c57c66ee7f..848f0ce8690c27bde1bd1f035c6241552da0fb6d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2016 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 1999-2020 The Claws Mail Team and Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -300,6 +300,7 @@ gchar *strcrchomp(gchar *str)
        return str;
 }
 
+#ifndef HAVE_STRCASESTR
 /* Similar to `strstr' but this function ignores the case of both strings.  */
 gchar *strcasestr(const gchar *haystack, const gchar *needle)
 {
@@ -307,6 +308,7 @@ gchar *strcasestr(const gchar *haystack, const gchar *needle)
 
        return strncasestr(haystack, haystack_len, needle);
 }
+#endif /* HAVE_STRCASESTR */
 
 gchar *strncasestr(const gchar *haystack, gint haystack_len, const gchar *needle)
 {
@@ -1411,7 +1413,8 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                                          "../",
                                          NULL };
        gint num_attach = 0;
-       gchar **my_att = NULL;
+
+       cm_return_val_if_fail(mailto != NULL, -1);
 
        Xstrdup_a(tmp_mailto, mailto, return -1);
 
@@ -1427,9 +1430,6 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
        if (to && !*to)
                *to = decode_uri_gdup(tmp_mailto);
 
-       my_att = g_malloc(sizeof(char *));
-       my_att[0] = NULL;
-
        while (p) {
                gchar *field, *value;
 
@@ -1456,6 +1456,7 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                        } else {
                                gchar *tmp = decode_uri_gdup(value);
                                gchar *new_from = g_strdup_printf("%s, %s", *from, tmp);
+                               g_free(tmp);
                                g_free(*from);
                                *from = new_from;
                        }
@@ -1465,6 +1466,7 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                        } else {
                                gchar *tmp = decode_uri_gdup(value);
                                gchar *new_cc = g_strdup_printf("%s, %s", *cc, tmp);
+                               g_free(tmp);
                                g_free(*cc);
                                *cc = new_cc;
                        }
@@ -1474,6 +1476,7 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                        } else {
                                gchar *tmp = decode_uri_gdup(value);
                                gchar *new_bcc = g_strdup_printf("%s, %s", *bcc, tmp);
+                               g_free(tmp);
                                g_free(*bcc);
                                *bcc = new_bcc;
                        }
@@ -1483,19 +1486,42 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                } else if (body && !*body && !g_ascii_strcasecmp(field, "body")) {
                        *body = decode_uri_gdup(value);
                } else if (body && !*body && !g_ascii_strcasecmp(field, "insert")) {
+                       int i = 0;
                        gchar *tmp = decode_uri_gdup(value);
-                       if (!g_file_get_contents(tmp, body, NULL, NULL)) {
-                               g_warning("couldn't set insert file '%s' in body", value);
+
+                       for (; forbidden_uris[i]; i++) {
+                               if (strstr(tmp, forbidden_uris[i])) {
+                                       g_print("Refusing to insert '%s', potential private data leak\n",
+                                                       tmp);
+                                       g_free(tmp);
+                                       tmp = NULL;
+                                       break;
+                               }
+                       }
+
+                       if (tmp) {
+                               if (!is_file_entry_regular(tmp)) {
+                                       g_warning("Refusing to insert '%s', not a regular file\n", tmp);
+                               } else if (!g_file_get_contents(tmp, body, NULL, NULL)) {
+                                       g_warning("couldn't set insert file '%s' in body", value);
+                               }
+
+                               g_free(tmp);
                        }
-                       g_free(tmp);
                } else if (attach && !g_ascii_strcasecmp(field, "attach")) {
                        int i = 0;
                        gchar *tmp = decode_uri_gdup(value);
+                       gchar **my_att = g_malloc(sizeof(char *));
+
+                       my_att[0] = NULL;
+
                        for (; forbidden_uris[i]; i++) {
                                if (strstr(tmp, forbidden_uris[i])) {
                                        g_print("Refusing to attach '%s', potential private data leak\n",
                                                        tmp);
                                        g_free(tmp);
+                                       g_free(my_att);
+                                       tmp = NULL;
                                        break;
                                }
                        }
@@ -1505,6 +1531,7 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                                my_att = g_realloc(my_att, (sizeof(char *))*(num_attach+1));
                                my_att[num_attach-1] = tmp;
                                my_att[num_attach] = NULL;
+                               *attach = my_att;
                        }
                } else if (inreplyto && !*inreplyto &&
                           !g_ascii_strcasecmp(field, "in-reply-to")) {
@@ -1512,8 +1539,6 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                }
        }
 
-       if (attach)
-               *attach = my_att;
        return 0;
 }
 
@@ -2037,6 +2062,14 @@ gboolean is_file_entry_exist(const gchar *file)
        return g_file_test(file, G_FILE_TEST_EXISTS);
 }
 
+gboolean is_file_entry_regular(const gchar *file)
+{
+       if (file == NULL)
+               return FALSE;
+
+       return g_file_test(file, G_FILE_TEST_IS_REGULAR);
+}
+
 gboolean dirent_is_regular_file(struct dirent *d)
 {
 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
@@ -3481,12 +3514,11 @@ static GHashTable *create_domain_tab(void)
 
 static gboolean is_toplvl_domain(GHashTable *tab, const gchar *first, const gchar *last)
 {
-       const gint MAX_LVL_DOM_NAME_LEN = 6;
-       gchar buf[MAX_LVL_DOM_NAME_LEN + 1];
-       const gchar *m = buf + MAX_LVL_DOM_NAME_LEN + 1;
+       gchar buf[BUFFSIZE + 1];
+       const gchar *m = buf + BUFFSIZE + 1;
        register gchar *p;
 
-       if (last - first > MAX_LVL_DOM_NAME_LEN || first > last)
+       if (last - first > BUFFSIZE || first > last)
                return FALSE;
 
        for (p = buf; p < m &&  first < last; *p++ = *first++)
@@ -4547,7 +4579,7 @@ guchar *g_base64_decode_zero(const gchar *text, gsize *out_len)
        g_free(tmp);
 
        if (strlen(out) != *out_len) {
-               g_warning ("strlen(out) %zd != *out_len %" G_GSIZE_FORMAT, strlen(out), *out_len);
+               g_warning ("strlen(out) %"G_GSIZE_FORMAT" != *out_len %"G_GSIZE_FORMAT, strlen(out), *out_len);
        }
 
        return out;
@@ -4617,7 +4649,7 @@ get_random_bytes(void *buf, size_t count)
        /* Read data from the source into buf. */
 #if defined G_OS_WIN32
        if (!CryptGenRandom(rnd, count, buf)) {
-               debug_print("Could not read %zd random bytes.\n", count);
+               debug_print("Could not read %"G_GSIZE_FORMAT" random bytes.\n", count);
                CryptReleaseContext(rnd, 0);
                return FALSE;
        }
@@ -4707,3 +4739,10 @@ gboolean get_serverportfp_from_filename(const gchar *str, gchar **server, gchar
                return TRUE;
 }
 
+#ifdef G_OS_WIN32
+gchar *win32_debug_log_path(void)
+{
+       return g_strconcat(g_get_tmp_dir(), G_DIR_SEPARATOR_S,
+                       "claws-win32.log", NULL);
+}
+#endif