From aa5d9ec360c7071e06c7229cb3399bb5a4109500 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Mon, 10 Feb 2025 17:24:11 +0200 Subject: [PATCH] imap: LIST - Handle invalid mUTF-7 mailbox names as never matching anything --- src/imap/cmd-list.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/imap/cmd-list.c b/src/imap/cmd-list.c index 2c15e3894f..86d4c3b855 100644 --- a/src/imap/cmd-list.c +++ b/src/imap/cmd-list.c @@ -436,6 +436,7 @@ bool cmd_list_full(struct client_command_context *cmd, bool lsub) ARRAY(const char *) patterns = ARRAY_INIT; const char *ref, *pattern, *const *patterns_strarr; string_t *str; + bool invalid_ref = FALSE; /* [()] |() [RETURN ()] */ @@ -464,6 +465,8 @@ bool cmd_list_full(struct client_command_context *cmd, bool lsub) str = t_str_new(64); if (imap_utf7_to_utf8(ref, str) == 0) ref = p_strdup(cmd->pool, str_c(str)); + else + invalid_ref = TRUE; str_truncate(str, 0); if (imap_arg_get_list_full(&args[1], &list_args, &arg_count)) { @@ -476,9 +479,10 @@ bool cmd_list_full(struct client_command_context *cmd, bool lsub) "Invalid pattern list."); return TRUE; } - if (imap_utf7_to_utf8(pattern, str) == 0) + if (imap_utf7_to_utf8(pattern, str) == 0) { pattern = p_strdup(cmd->pool, str_c(str)); - array_push_back(&patterns, &pattern); + array_push_back(&patterns, &pattern); + } str_truncate(str, 0); } args += 2; @@ -487,11 +491,11 @@ bool cmd_list_full(struct client_command_context *cmd, bool lsub) client_send_command_error(cmd, "Invalid pattern."); return TRUE; } - if (imap_utf7_to_utf8(pattern, str) == 0) - pattern = p_strdup(cmd->pool, str_c(str)); - p_array_init(&patterns, cmd->pool, 1); - array_push_back(&patterns, &pattern); + if (imap_utf7_to_utf8(pattern, str) == 0) { + pattern = p_strdup(cmd->pool, str_c(str)); + array_push_back(&patterns, &pattern); + } args += 2; if (lsub) { @@ -536,8 +540,9 @@ bool cmd_list_full(struct client_command_context *cmd, bool lsub) array_append_zero(&patterns); /* NULL-terminate */ patterns_strarr = array_front(&patterns); - if (!ctx->used_listext && !lsub && *patterns_strarr[0] == '\0') { - /* Only LIST ref "" gets us here */ + if (invalid_ref || patterns_strarr[0] == NULL || + (!ctx->used_listext && !lsub && *patterns_strarr[0] == '\0')) { + /* Only LIST ref "" gets us here, or invalid ref/pattern */ cmd_list_ref_root(client, ref); client_send_tagline(cmd, "OK List completed."); } else { -- 2.47.3