Skip to content

Commit

Permalink
lib-mail: Make sure istream-attachment-connector detects wrong mail s…
Browse files Browse the repository at this point in the history
…ize.
  • Loading branch information
sirainen committed Feb 23, 2017
1 parent a427bf4 commit 83ba28b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
15 changes: 9 additions & 6 deletions src/lib-mail/istream-attachment-connector.c
Expand Up @@ -65,9 +65,10 @@ int istream_attachment_connector_add(struct istream_attachment_connector *conn,

if (base_prefix_size > 0) {
/* add a part of the base message before the attachment */
input = i_stream_create_range(conn->base_input,
conn->base_input_offset,
base_prefix_size);
input = i_stream_create_min_sized_range(conn->base_input,
conn->base_input_offset, base_prefix_size);
i_stream_set_name(input, t_strdup_printf("%s middle",
i_stream_get_name(conn->base_input)));
array_append(&conn->streams, &input, 1);
conn->base_input_offset += base_prefix_size;
conn->encoded_offset += base_prefix_size;
Expand Down Expand Up @@ -119,9 +120,11 @@ istream_attachment_connector_finish(struct istream_attachment_connector **_conn)
i_assert(conn->base_input_offset < conn->msg_size);

trailer_size = conn->msg_size - conn->encoded_offset;
input = i_stream_create_range(conn->base_input,
conn->base_input_offset,
trailer_size);
input = i_stream_create_sized_range(conn->base_input,
conn->base_input_offset,
trailer_size);
i_stream_set_name(input, t_strdup_printf(
"%s trailer", i_stream_get_name(conn->base_input)));
array_append(&conn->streams, &input, 1);
}
array_append_zero(&conn->streams);
Expand Down
49 changes: 38 additions & 11 deletions src/lib-mail/test-istream-attachment.c
Expand Up @@ -279,21 +279,36 @@ static int test_input_stream(struct istream *file_input)
i_stream_unref(&input2);

/* rebuild the original stream and see if the hash matches */
input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
input = test_build_original_istream(input2, msg_size);
i_stream_unref(&input2);
{
input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
input = test_build_original_istream(input2, msg_size);
i_stream_unref(&input2);

sha1_init(&hash);
while (i_stream_read_more(input, &data, &size) > 0) {
sha1_loop(&hash, data, size);
i_stream_skip(input, size);
}
test_assert(input->eof && input->stream_errno == 0);
sha1_result(&hash, hash_attached);
i_stream_unref(&input);

sha1_init(&hash);
while (i_stream_read_data(input, &data, &size, 0) > 0) {
sha1_loop(&hash, data, size);
i_stream_skip(input, size);
if (memcmp(hash_file, hash_attached, SHA1_RESULTLEN) != 0)
ret = -1;
}
sha1_result(&hash, hash_attached);
i_stream_unref(&input);

ret = memcmp(hash_file, hash_attached, SHA1_RESULTLEN) == 0 ? 0 : -1;
/* try with a wrong message size */
for (int i = 0; i < 2; i++) {
input2 = i_stream_create_from_data(base_buf->data, base_buf->used);
input = test_build_original_istream(input2, msg_size +
(i == 0 ? 1 : -1));
i_stream_unref(&input2);
while (i_stream_read_more(input, &data, &size) > 0)
i_stream_skip(input, size);
test_assert(input->stream_errno == (i == 0 ? EPIPE : EINVAL));
i_stream_unref(&input);
}

i_stream_unref(&file_input);
buffer_free(&base_buf);
if (attachment_data != NULL)
buffer_free(&attachment_data);
Expand Down Expand Up @@ -425,6 +440,17 @@ static void test_istream_attachment_extractor_error(void)
test_end();
}

static void test_istream_attachment_connector(void)
{
struct istream *input;

test_begin("istream attachment connector");
input = i_stream_create_from_data(mail_input, sizeof(mail_input));
test_assert(test_input_stream(input) == 0);
i_stream_unref(&input);
test_end();
}

static int test_input_file(const char *path)
{
struct istream *file_input;
Expand All @@ -450,6 +476,7 @@ int main(int argc, char *argv[])
test_istream_attachment,
test_istream_attachment_extractor,
test_istream_attachment_extractor_error,
test_istream_attachment_connector,
NULL
};
if (argc > 1)
Expand Down

0 comments on commit 83ba28b

Please sign in to comment.