From: Timo Sirainen Date: Fri, 4 May 2018 16:21:52 +0000 (+0300) Subject: lib-mail: Fix rfc822_parse_dot_atom() to reject if dot isn't followed by atom X-Git-Tag: 2.3.4~240 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2f2d9930ddef7de198eca2347a3e181fa833ad57;p=thirdparty%2Fdovecot%2Fcore.git lib-mail: Fix rfc822_parse_dot_atom() to reject if dot isn't followed by atom --- diff --git a/src/lib-mail/rfc822-parser.c b/src/lib-mail/rfc822-parser.c index 57a8171a93..8817b66962 100644 --- a/src/lib-mail/rfc822-parser.c +++ b/src/lib-mail/rfc822-parser.c @@ -199,6 +199,8 @@ int rfc822_parse_dot_atom(struct rfc822_parser_context *ctx, string_t *str) continue; } + if (start == ctx->data) + return -1; str_append_data(str, start, ctx->data - start); if ((ret = rfc822_skip_lwsp(ctx)) <= 0) @@ -211,10 +213,11 @@ int rfc822_parse_dot_atom(struct rfc822_parser_context *ctx, string_t *str) str_append_c(str, '.'); if ((ret = rfc822_skip_lwsp(ctx)) <= 0) - return ret; + return -1; start = ctx->data; } + i_assert(start != ctx->data); str_append_data(str, start, ctx->data - start); return 0; } diff --git a/src/lib-mail/test-rfc822-parser.c b/src/lib-mail/test-rfc822-parser.c index 6ac4fad5eb..55018b99aa 100644 --- a/src/lib-mail/test-rfc822-parser.c +++ b/src/lib-mail/test-rfc822-parser.c @@ -112,6 +112,59 @@ static void test_rfc822_parse_quoted_string(void) test_end(); } +static void test_rfc822_parse_dot_atom(void) +{ + static const struct { + const char *input, *output; + int ret; + } tests[] = { + { "foo", "foo", 0 }, + { "foo.bar", "foo.bar", 0 }, + { "foo.bar.baz", "foo.bar.baz", 0 }, + { "foo . \tbar (comments) . (...) baz\t ", "foo.bar.baz", 0 }, + + { ".", "", -1 }, + { "..", "", -1 }, + { ".foo", "", -1 }, + { "foo.", "foo.", -1 }, + { "foo..bar", "foo.", -1 }, + { "foo. .bar", "foo.", -1 }, + { "foo.(middle).bar", "foo.", -1 }, + { "foo. ", "foo.", -1 }, + { "foo.\t", "foo.", -1 }, + { "foo.(ending)", "foo.", -1 }, + }; + struct rfc822_parser_context parser; + string_t *str = t_str_new(64); + string_t *input2 = t_str_new(64); + unsigned int i = 0; + + test_begin("rfc822 parse dot-atom"); + for (i = 0; i < N_ELEMENTS(tests); i++) { + rfc822_parser_init(&parser, (const void *)tests[i].input, + strlen(tests[i].input), NULL); + test_assert_idx(rfc822_parse_dot_atom(&parser, str) == tests[i].ret, i); + test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i); + rfc822_parser_deinit(&parser); + str_truncate(str, 0); + + /* same input but with "," appended should return 1 on success, + and -1 still on error. */ + int expected_ret = tests[i].ret == -1 ? -1 : 1; + str_append(input2, tests[i].input); + str_append_c(input2, ','); + rfc822_parser_init(&parser, str_data(input2), + str_len(input2), NULL); + test_assert_idx(rfc822_parse_dot_atom(&parser, str) == expected_ret, i); + test_assert_idx(strcmp(tests[i].output, str_c(str)) == 0, i); + rfc822_parser_deinit(&parser); + + str_truncate(str, 0); + str_truncate(input2, 0); + } + test_end(); +} + static void test_rfc822_parse_domain_literal(void) { static const struct { @@ -182,6 +235,7 @@ int main(void) test_rfc822_parse_comment, test_rfc822_parse_comment_nuls, test_rfc822_parse_quoted_string, + test_rfc822_parse_dot_atom, test_rfc822_parse_domain_literal, test_rfc822_parse_content_param, NULL