diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-29 17:20:57 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-29 17:20:57 +0100 |
commit | d85fd8877d691aab88a18c6d54cd72e1198a6844 (patch) | |
tree | e6c456cb3878005ce5cc5750b43f295e8278c9df | |
parent | e487db32bc47aadb69753cffbb6c554a3153dc00 (diff) | |
download | rspamd-d85fd8877d691aab88a18c6d54cd72e1198a6844.tar.gz rspamd-d85fd8877d691aab88a18c6d54cd72e1198a6844.zip |
[Fix] Fix folding and folding tests
-rw-r--r-- | src/libutil/str_util.c | 129 | ||||
-rw-r--r-- | test/lua/unit/folding.lua | 58 |
2 files changed, 89 insertions, 98 deletions
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 238f95c87..29df7e744 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -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: diff --git a/test/lua/unit/folding.lua b/test/lua/unit/folding.lua index fb36e0073..b4904f797 100644 --- a/test/lua/unit/folding.lua +++ b/test/lua/unit/folding.lua @@ -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) |