]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-mail: Fix rfc822_parse_dot_atom() to reject if dot isn't followed by atom
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 4 May 2018 16:21:52 +0000 (19:21 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Thu, 30 Aug 2018 08:23:55 +0000 (11:23 +0300)
src/lib-mail/rfc822-parser.c
src/lib-mail/test-rfc822-parser.c

index 57a8171a9395d77c1210b9c0553679e540682e66..8817b6696267b5b5098fbf59eb98f95a3252fb44 100644 (file)
@@ -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;
 }
index 6ac4fad5ebdffff3a5e0b2b1c527cd4719009ef6..55018b99aa6596de6751655ebdddf9662d4f8ef1 100644 (file)
@@ -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