From 746e361c2ee4cfe2b3ea6cbb0af38617069db8ff Mon Sep 17 00:00:00 2001 From: evgeny Date: Tue, 27 Oct 2026 03:41:36 +0100 Subject: [PATCH] Validate complete poll command reads before publication or reaction changes --- src/chat/chat_poll.c | 66 ++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/src/chat/chat_poll.c b/src/chat/chat_poll.c index f993a1fcd..6a8ccaa01 100644 --- a/src/chat/chat_poll.c +++ b/src/chat/chat_poll.c @@ -271,14 +271,20 @@ static int poll_field_text(struct UTUN_INSTANCE* inst, const char* json, const c static int poll_field_ids(struct UTUN_INSTANCE* inst, const char* json, const char* path, uint8_t* ids, size_t capacity) { sqlite3_stmt* field = poll_field(inst, json, path); if (!field) return -1; const char* type = (const char*)sqlite3_column_text(field, 0); int absent = !type; - int array = type && !strcmp(type, "array"); sqlite3_finalize(field); + int array = type && !strcmp(type, "array"); int final = sqlite3_finalize(field); + if (final != SQLITE_OK) { + DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: array type finalize failed field=%s rc=%d", path, final); return -1; + } if (absent) return 0; if (!array) { DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: expected array field=%s", path); return -1; } sqlite3_stmt* st = NULL; - if (sqlite3_prepare_v2(inst->topo_sqlite_db, "SELECT type,value FROM json_each(?1,?2)", -1, &st, NULL) != SQLITE_OK) { - poll_db_error(inst, "command array"); return -1; + int rc = sqlite3_prepare_v2(inst->topo_sqlite_db, "SELECT type,value FROM json_each(?1,?2)", -1, &st, NULL); + if (rc == SQLITE_OK) rc = sqlite3_bind_text(st, 1, json, -1, SQLITE_STATIC); + if (rc == SQLITE_OK) rc = sqlite3_bind_text(st, 2, path, -1, SQLITE_STATIC); + if (rc != SQLITE_OK) { + final = sqlite3_finalize(st); + DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: array query failed field=%s rc=%d finalize=%d", path, rc, final); return -1; } - sqlite3_bind_text(st, 1, json, -1, SQLITE_STATIC); sqlite3_bind_text(st, 2, path, -1, SQLITE_STATIC); int step, count = 0; while ((step = sqlite3_step(st)) == SQLITE_ROW) { const char* element = (const char*)sqlite3_column_text(st, 0); sqlite3_int64 n = sqlite3_column_int64(st, 1); @@ -287,8 +293,10 @@ static int poll_field_ids(struct UTUN_INSTANCE* inst, const char* json, const ch } ids[count++] = (uint8_t)n; } - sqlite3_finalize(st); - if (count >= 0 && step != SQLITE_DONE) { poll_db_error(inst, "command array read"); return -1; } + final = sqlite3_finalize(st); + if (final != SQLITE_OK || (count >= 0 && step != SQLITE_DONE)) { + DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: array read failed field=%s step=%d finalize=%d", path, step, final); return -1; + } return count; } @@ -296,8 +304,9 @@ static int poll_field_bool(struct UTUN_INSTANCE* inst, const char* json, const c sqlite3_stmt* st = poll_field(inst, json, path); if (!st) return -2; const char* type = (const char*)sqlite3_column_text(st, 0); int value = !type ? default_value : !strcmp(type, "true") ? 1 : !strcmp(type, "false") ? 0 : -2; - sqlite3_finalize(st); - if (value == -2) DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: expected boolean field=%s", path); + int final = sqlite3_finalize(st); + if (final != SQLITE_OK) value = -2; + if (value == -2) DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: invalid boolean field=%s finalize=%d", path, final); return value; } @@ -317,10 +326,16 @@ int chat_feedback_command(struct UTUN_INSTANCE* inst, const char* command, char* DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: invalid command arguments"); return -1; } sqlite3_stmt* valid = NULL; - if (strlen(command) > 16384 || sqlite3_prepare_v2(inst->topo_sqlite_db, "SELECT json_valid(?1)", -1, &valid, NULL) != SQLITE_OK) - return poll_command_error("Invalid command size", result); - sqlite3_bind_text(valid, 1, command, -1, SQLITE_STATIC); - int ok = sqlite3_step(valid) == SQLITE_ROW && sqlite3_column_int(valid, 0) == 1; sqlite3_finalize(valid); + if (strlen(command) > 16384) return poll_command_error("Invalid command size", result); + int rc = sqlite3_prepare_v2(inst->topo_sqlite_db, "SELECT json_valid(?1)", -1, &valid, NULL); + if (rc == SQLITE_OK) rc = sqlite3_bind_text(valid, 1, command, -1, SQLITE_STATIC); + if (rc == SQLITE_OK) rc = sqlite3_step(valid); + int ok = rc == SQLITE_ROW && sqlite3_column_int(valid, 0) == 1; + int final = sqlite3_finalize(valid); + if (rc != SQLITE_ROW || final != SQLITE_OK) { + DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: command validation failed step=%d finalize=%d db=%s", rc, final, sqlite3_errmsg(inst->topo_sqlite_db)); + return poll_command_error("Cannot validate command", result); + } if (!ok) return poll_command_error("Malformed JSON", result); char cmd[32], channel[21], hex[65]; uint8_t message_id[32]; if (poll_field_text(inst, command, "$.cmd", cmd, sizeof(cmd), 0) < 0) return poll_command_error("Missing command", result); @@ -344,13 +359,21 @@ int chat_feedback_command(struct UTUN_INSTANCE* inst, const char* command, char* if (custom < 0) return poll_command_error("Invalid custom answer option", result); poll.allow_custom_answer = custom; sqlite3_stmt* field = poll_field(inst, command, "$.options"); if (!field) return poll_command_error("Invalid options", result); - const char* type = (const char*)sqlite3_column_text(field, 0); ok = type && !strcmp(type, "array"); sqlite3_finalize(field); + const char* type = (const char*)sqlite3_column_text(field, 0); ok = type && !strcmp(type, "array"); + final = sqlite3_finalize(field); + if (final != SQLITE_OK) { + DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: options type finalize failed rc=%d", final); + return poll_command_error("Cannot read options", result); + } if (!ok) return poll_command_error("Options must be an array", result); sqlite3_stmt* st = NULL; - if (sqlite3_prepare_v2(inst->topo_sqlite_db, "SELECT type,value FROM json_each(?1,'$.options')", -1, &st, NULL) != SQLITE_OK) { - poll_db_error(inst, "options"); return poll_command_error("Cannot read options", result); + rc = sqlite3_prepare_v2(inst->topo_sqlite_db, "SELECT type,value FROM json_each(?1,'$.options')", -1, &st, NULL); + if (rc == SQLITE_OK) rc = sqlite3_bind_text(st, 1, command, -1, SQLITE_STATIC); + if (rc != SQLITE_OK) { + final = sqlite3_finalize(st); + DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: options query failed rc=%d finalize=%d", rc, final); + return poll_command_error("Cannot read options", result); } - sqlite3_bind_text(st, 1, command, -1, SQLITE_STATIC); int step; while ((step = sqlite3_step(st)) == SQLITE_ROW) { const char* value_type = (const char*)sqlite3_column_text(st, 0), *text = (const char*)sqlite3_column_text(st, 1); @@ -359,8 +382,9 @@ int chat_feedback_command(struct UTUN_INSTANCE* inst, const char* command, char* || len <= 0 || len > CHAT_POLL_OPTION_MAX || strlen(text) != (size_t)len) { ok = 0; break; } memcpy(poll.options[poll.option_count++], text, len + 1); } - sqlite3_finalize(st); - if (!ok || step != SQLITE_DONE || chat_poll_create(inst, channel, &poll, message_id) < 0) + final = sqlite3_finalize(st); + if (final != SQLITE_OK) DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: options finalize failed rc=%d", final); + if (!ok || step != SQLITE_DONE || final != SQLITE_OK || chat_poll_create(inst, channel, &poll, message_id) < 0) return poll_command_error("Invalid poll or publication failed", result); return chat_feedback_snapshot(inst, channel, message_id, result); } @@ -390,11 +414,13 @@ int chat_feedback_command(struct UTUN_INSTANCE* inst, const char* command, char* const char* type = (const char*)sqlite3_column_text(st, 0); sqlite3_int64 emoji = sqlite3_column_int64(st, 1); if (type && !strcmp(type, "text")) { const char* text = (const char*)sqlite3_column_text(st, 1); emoji = -1; - for (unsigned i = 0; text && i < CHAT_REACTION_COUNT; i++) + int bytes = sqlite3_column_bytes(st, 1); + for (unsigned i = 0; text && strlen(text) == (size_t)bytes && i < CHAT_REACTION_COUNT; i++) if (!strcmp(text, chat_reaction_emoji(i))) { emoji = i; break; } } ok = type && (!strcmp(type, "integer") || !strcmp(type, "text")) && emoji >= 0 && emoji < CHAT_REACTION_COUNT; - sqlite3_finalize(st); + final = sqlite3_finalize(st); + if (final != SQLITE_OK) { DEBUG_ERROR(DEBUG_CATEGORY_CHAT_SYNC, "poll: emoji finalize failed rc=%d", final); ok = 0; } if (!ok) return poll_command_error("Invalid emoji", result); int selected = count == 1 && ids[0] == emoji; int on = poll_field_bool(inst, command, "$.on", !selected);