]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Fix issue with broken HTTP message to learn endpoints (#5106)
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 17 Aug 2024 19:25:53 +0000 (01:25 +0600)
committerGitHub <noreply@github.com>
Sat, 17 Aug 2024 19:25:53 +0000 (20:25 +0100)
* [Fix] Check message before trying to dereference pointer

Issue: #5089

* [Minor] Fix stripping of the last characters

src/client/rspamc.cxx
src/controller.c

index 1e083049373dd35d53f950b5644b30409bda7ee0..1c67e4167d047ce3684edc7d58808a48518e57e3 100644 (file)
@@ -524,16 +524,13 @@ rspamc_password_callback(const char *option_name,
                        else {
                                /* Strip trailing spaces */
                                auto *map = (char *) locked_mmap.value().get_map();
-                               auto *end = map + locked_mmap.value().get_size() - 1;
-
-                               while (g_ascii_isspace(*end) && end > map) {
-                                       end--;
-                               }
-
-                               end++;
-                               value_view = std::string_view{map, static_cast<std::size_t>(end - map + 1)};
-                               processed_passwd.assign(std::begin(value_view), std::end(value_view));
-                               processed_passwd.push_back('\0');
+                               value_view = std::string_view{map, locked_mmap->get_size()};
+                               auto right = value_view.end() - 1;
+                               for (; right > value_view.cbegin() && g_ascii_isspace(*right); --right)
+                                       ;
+                               std::string_view str{value_view.begin(), static_cast<size_t>(right - value_view.begin()) + 1};
+                               processed_passwd.assign(std::begin(str), std::end(str));
+                               processed_passwd.push_back('\0'); /* Null-terminate for C part */
                        }
                }
                else {
index d91f99098338857c594b43ff10d1a1ff37c60707..386448f93c21ed4be56b4b721ac4e9cddaa3fef9 100644 (file)
@@ -1945,7 +1945,7 @@ rspamd_controller_learn_fin_task(void *ud)
 
        if (task->err != NULL) {
                msg_info_session("cannot learn <%s>: %e",
-                                                MESSAGE_FIELD(task, message_id), task->err);
+                                                MESSAGE_FIELD_CHECK(task, message_id), task->err);
                rspamd_controller_send_error(conn_ent, task->err->code, "%s",
                                                                         task->err->message);
 
@@ -1957,14 +1957,14 @@ rspamd_controller_learn_fin_task(void *ud)
                msg_info_task("<%s> learned message as %s: %s",
                                          rspamd_inet_address_to_string(session->from_addr),
                                          session->is_spam ? "spam" : "ham",
-                                         MESSAGE_FIELD(task, message_id));
+                                         MESSAGE_FIELD_CHECK(task, message_id));
                rspamd_controller_send_string(conn_ent, "{\"success\":true}");
                return TRUE;
        }
 
        if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_LEARN)) {
                msg_info_task("cannot learn <%s>: %e",
-                                         MESSAGE_FIELD(task, message_id), task->err);
+                                         MESSAGE_FIELD_CHECK(task, message_id), task->err);
 
                if (task->err) {
                        rspamd_controller_send_error(conn_ent, task->err->code, "%s",
@@ -1985,7 +1985,7 @@ rspamd_controller_learn_fin_task(void *ud)
                        msg_info_task("<%s> learned message as %s: %s",
                                                  rspamd_inet_address_to_string(session->from_addr),
                                                  session->is_spam ? "spam" : "ham",
-                                                 MESSAGE_FIELD(task, message_id));
+                                                 MESSAGE_FIELD_CHECK(task, message_id));
                        rspamd_controller_send_string(conn_ent, "{\"success\":true}");
                }
 
@@ -2114,7 +2114,7 @@ rspamd_controller_handle_learn_common(
 
        if (!rspamd_task_process(task, RSPAMD_TASK_PROCESS_LEARN)) {
                msg_warn_session("<%s> message cannot be processed",
-                                                MESSAGE_FIELD(task, message_id));
+                                                MESSAGE_FIELD_CHECK(task, message_id));
                goto end;
        }