]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Fix folding and folding tests
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Mar 2018 16:20:57 +0000 (17:20 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 29 Mar 2018 16:20:57 +0000 (17:20 +0100)
src/libutil/str_util.c
test/lua/unit/folding.lua

index 238f95c87fdf5867be3ab980def4319f9d107189..29df7e744c8535276a8c01c374c4d8651449b3c4 100644 (file)
@@ -955,72 +955,7 @@ rspamd_header_value_fold (const gchar *name,
        while (*p) {
                switch (state) {
                case read_token:
-                       if (*p == ',' || *p == ';') {
-                               /* We have something similar to the token's end, so check len */
-                               if (cur_len > fold_max * 0.8 && cur_len < fold_max) {
-                                       /* We want fold */
-                                       fold_type = fold_after;
-                                       state = fold_token;
-                                       next_state = read_token;
-                               }
-                               else if (cur_len > fold_max && !first_token) {
-                                       fold_type = fold_before;
-                                       state = fold_token;
-                                       next_state = read_token;
-                               }
-                               else {
-                                       g_string_append_len (res, c, p - c + 1);
-                                       c = p + 1;
-                                       first_token = FALSE;
-                               }
-                               p ++;
-                       }
-                       else if (*p == '"') {
-                               /* Fold before quoted tokens */
-                               g_string_append_len (res, c, p - c);
-                               c = p;
-                               state = read_quoted;
-                       }
-                       else if (*p == '\r' || *p == '\n') {
-                               if (cur_len > fold_max && !first_token) {
-                                       fold_type = fold_before;
-                                       state = fold_token;
-                                       next_state = read_token;
-                               }
-                               else {
-                                       /* Reset line length */
-                                       cur_len = 0;
-
-                                       while (g_ascii_isspace (*p)) {
-                                               p ++;
-                                       }
-
-                                       g_string_append_len (res, c, p - c);
-                                       c = p;
-                                       first_token = TRUE;
-                               }
-                       }
-                       else if (g_ascii_isspace (*p)) {
-                               if (cur_len > fold_max * 0.8 && cur_len < fold_max) {
-                                       /* We want fold */
-                                       fold_type = fold_after;
-                                       state = fold_token;
-                                       next_state = read_token;
-                               }
-                               else if (cur_len > fold_max && !first_token) {
-                                       fold_type = fold_before;
-                                       state = fold_token;
-                                       next_state = read_token;
-                               }
-                               else {
-                                       g_string_append_len (res, c, p - c);
-                                       c = p;
-                                       first_token = FALSE;
-                                       p ++;
-                                       cur_len ++;
-                               }
-                       }
-                       else if (fold_on_chars && strchr (fold_on_chars, *p) != NULL) {
+                       if (fold_on_chars && strchr (fold_on_chars, *p) != NULL) {
                                fold_type = fold_after;
                                state = fold_token;
                                next_state = read_token;
@@ -1028,8 +963,66 @@ rspamd_header_value_fold (const gchar *name,
                                p ++;
                        }
                        else {
-                               p ++;
-                               cur_len ++;
+                               if (*p == ',' || *p == ';') {
+                                       /* We have something similar to the token's end, so check len */
+                                       if (cur_len > fold_max * 0.8 && cur_len < fold_max) {
+                                               /* We want fold */
+                                               fold_type = fold_after;
+                                               state = fold_token;
+                                               next_state = read_token;
+                                       } else if (cur_len > fold_max && !first_token) {
+                                               fold_type = fold_before;
+                                               state = fold_token;
+                                               next_state = read_token;
+                                       } else {
+                                               g_string_append_len (res, c, p - c + 1);
+                                               c = p + 1;
+                                               first_token = FALSE;
+                                       }
+                                       p++;
+                               } else if (*p == '"') {
+                                       /* Fold before quoted tokens */
+                                       g_string_append_len (res, c, p - c);
+                                       c = p;
+                                       state = read_quoted;
+                               } else if (*p == '\r' || *p == '\n') {
+                                       if (cur_len > fold_max && !first_token) {
+                                               fold_type = fold_before;
+                                               state = fold_token;
+                                               next_state = read_token;
+                                       } else {
+                                               /* Reset line length */
+                                               cur_len = 0;
+
+                                               while (g_ascii_isspace (*p)) {
+                                                       p++;
+                                               }
+
+                                               g_string_append_len (res, c, p - c);
+                                               c = p;
+                                               first_token = TRUE;
+                                       }
+                               } else if (g_ascii_isspace (*p)) {
+                                       if (cur_len > fold_max * 0.8 && cur_len < fold_max) {
+                                               /* We want fold */
+                                               fold_type = fold_after;
+                                               state = fold_token;
+                                               next_state = read_token;
+                                       } else if (cur_len > fold_max && !first_token) {
+                                               fold_type = fold_before;
+                                               state = fold_token;
+                                               next_state = read_token;
+                                       } else {
+                                               g_string_append_len (res, c, p - c);
+                                               c = p;
+                                               first_token = FALSE;
+                                               p++;
+                                               cur_len++;
+                                       }
+                               } else {
+                                       p++;
+                                       cur_len++;
+                               }
                        }
                        break;
                case fold_token:
index fb36e0073d4a7949595858bcf469c6cde4ae99a5..b4904f797ecec56d5298b000f326e6eabdb3aebf 100644 (file)
@@ -26,37 +26,35 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 context("Headers folding unit test", function()
   local util = require("rspamd_util")
-
-  test("Headers folding", function()
     -- {header, value}, "expected result"
-    local cases = {
-      {{"test", "test"}, "test"},
-      {{"test1", "_abc _def _ghi _fdjhfd _fhdjkfh _dkhkjd _fdjkf _dshfdks _fhdjfdkhfk _dshfds _fdsjk _fdkhfdks _fdsjf _dkf"},
-        "_abc _def _ghi _fdjhfd _fhdjkfh _dkhkjd _fdjkf _dshfdks _fhdjfdkhfk\r\n\t_dshfds _fdsjk _fdkhfdks _fdsjf _dkf"
-      },
-      {{"Test1", "_abc _def _ghi _fdjhfd _fhdjkfh _dkhaaaaaaaaaaakjdfdjkfdshfdksfhdjfdkhfkdshfdsfdsjkfdkhfdksfdsjf _dkf"},
-        "_abc _def _ghi _fdjhfd _fhdjkfh\r\n\t_dkhaaaaaaaaaaakjdfdjkfdshfdksfhdjfdkhfkdshfdsfdsjkfdkhfdksfdsjf _dkf"
-      },
-      {{"Content-Type", "multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hhhhhhhhhhhhhhhhhhhhhhhhh fjsdhfkjsd fhdjsfhkj"},
-        "multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hhhhhhhhhhhhhhhhhhhhhhhhh\r\n\tfjsdhfkjsd fhdjsfhkj"
-      },
-      {{"Content-Type", "multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hkjhgkfhgfhgf\"hfkjdhf fhjf fghjghf fdshjfhdsj\" hgjhgfjk"},
-        "multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hkjhgkfhgfhgf\"hfkjdhf fhjf fghjghf fdshjfhdsj\" hgjhgfjk"
-      },
-      {{"Content-Type", "Content-Type: multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\" abc def ghfdgfdsgj fdshfgfsdgfdsg hfsdgjfsdg fgsfgjsg"},
-        "Content-Type: multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\" abc def ghfdgfdsgj\r\n\tfdshfgfsdgfdsg hfsdgjfsdg fgsfgjsg"
-      },
-      {{"X-Spam-Symbols", "Returnpath_BL2,HFILTER_FROM_BOUNCE,R_PARTS_DIFFER,R_IP_PBL,R_ONE_RCPT,R_googleredir,R_TO_SEEMS_AUTO,R_SPF_NEUTRAL,R_PRIORITY_3,RBL_SPAMHAUS_PBL,HFILTER_MID_NOT_FQDN,MISSING_CTE,R_HAS_URL,RBL_SPAMHAUS_CSS,RBL_SPAMHAUS_XBL,BAYES_SPAM,RECEIVED_RBL10"},
-      "Returnpath_BL2,HFILTER_FROM_BOUNCE,R_PARTS_DIFFER,\r\n\tR_IP_PBL,R_ONE_RCPT,R_googleredir,R_TO_SEEMS_AUTO,R_SPF_NEUTRAL,R_PRIORITY_3,\r\n\tRBL_SPAMHAUS_PBL,HFILTER_MID_NOT_FQDN,MISSING_CTE,R_HAS_URL,RBL_SPAMHAUS_CSS,\r\n\tRBL_SPAMHAUS_XBL,BAYES_SPAM,RECEIVED_RBL10"
-      },
-    }
-
-    for _,c in ipairs(cases) do
-      local fv = util.fold_header(c[1][1], c[1][2])
+  local cases = {
+    {{"test", "test"}, "test"},
+    {{"test1", "_abc _def _ghi _fdjhfd _fhdjkfh _dkhkjd _fdjkf _dshfdks _fhdjfdkhfk _dshfds _fdsjk _fdkhfdks _fdsjf _dkf"},
+     "_abc _def _ghi _fdjhfd _fhdjkfh _dkhkjd _fdjkf _dshfdks\r\n\t_fhdjfdkhfk _dshfds _fdsjk _fdkhfdks _fdsjf _dkf"
+    },
+    {{"Test1", "_abc _def _ghi _fdjhfd _fhdjkfh _dkhaaaaaaaaaaakjdfdjkfdshfdksfhdjfdkhfkdshfdsfdsjkfdkhfdksfdsjf _dkf"},
+     "_abc _def _ghi _fdjhfd _fhdjkfh\r\n\t_dkhaaaaaaaaaaakjdfdjkfdshfdksfhdjfdkhfkdshfdsfdsjkfdkhfdksfdsjf\r\n\t_dkf"
+    },
+    {{"Content-Type", "multipart/mixed; boundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hhhhhhhhhhhhhhhhhhhhhhhhh fjsdhfkjsd fhdjsfhkj"},
+     "multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hhhhhhhhhhhhhhhhhhhhhhhhh\r\n\tfjsdhfkjsd fhdjsfhkj"
+    },
+    {{"Content-Type", "multipart/mixed; boundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hkjhgkfhgfhgf\"hfkjdhf fhjf fghjghf fdshjfhdsj\" hgjhgfjk"},
+     "multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\"hkjhgkfhgfhgf\"hfkjdhf fhjf fghjghf fdshjfhdsj\" hgjhgfjk"
+    },
+    {{"Content-Type", "Content-Type: multipart/mixed; boundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\" abc def ghfdgfdsgj fdshfgfsdgfdsg hfsdgjfsdg fgsfgjsg"},
+     "Content-Type: multipart/mixed;\r\n\tboundary=\"---- =_NextPart_000_01BDBF1F.DA8F77EE\" abc def ghfdgfdsgj\r\n\tfdshfgfsdgfdsg hfsdgjfsdg fgsfgjsg"
+    },
+    {{"X-Spam-Symbols", "Returnpath_BL2,HFILTER_FROM_BOUNCE,R_PARTS_DIFFER,R_IP_PBL,R_ONE_RCPT,R_googleredir,R_TO_SEEMS_AUTO,R_SPF_NEUTRAL,R_PRIORITY_3,RBL_SPAMHAUS_PBL,HFILTER_MID_NOT_FQDN,MISSING_CTE,R_HAS_URL,RBL_SPAMHAUS_CSS,RBL_SPAMHAUS_XBL,BAYES_SPAM,RECEIVED_RBL10"},
+     "Returnpath_BL2,HFILTER_FROM_BOUNCE,R_PARTS_DIFFER,\r\n\tR_IP_PBL,R_ONE_RCPT,R_googleredir,R_TO_SEEMS_AUTO,R_SPF_NEUTRAL,R_PRIORITY_3,\r\n\tRBL_SPAMHAUS_PBL,HFILTER_MID_NOT_FQDN,MISSING_CTE,R_HAS_URL,RBL_SPAMHAUS_CSS,\r\n\tRBL_SPAMHAUS_XBL,BAYES_SPAM,RECEIVED_RBL10"
+    },
+  }
+
+  for i,c in ipairs(cases) do
+    test("Headers folding: " .. i, function()
+      local fv = util.fold_header(c[1][1], c[1][2], 'crlf', ';')
       assert_not_nil(fv)
       assert_equal(fv, c[2], string.format("'%s' doesn't match with '%s'",
-        c[2], fv))
-    end
-
-  end)
+              c[2], fv))
+    end)
+  end
 end)