diff --git a/src/moderationservice/kafka/kafkaclient.cpp b/src/moderationservice/kafka/kafkaclient.cpp index e15e8e5..c174c24 100644 --- a/src/moderationservice/kafka/kafkaclient.cpp +++ b/src/moderationservice/kafka/kafkaclient.cpp @@ -195,10 +195,16 @@ void KafkaConsumer::ProcessMessage(RdKafka::Message* message) { std::string key_str(key_ptr, message->key_len()); + // Validate that key_str contains only digits (and optional leading +/-) + if (key_str.empty() || (!std::isdigit(key_str[0]) && key_str[0] != '-' && key_str[0] != '+')) { + std::cerr << "Invalid message key format (not numeric): '" << key_str << "'\n"; + return; + } + try { request_id = std::stoll(key_str); } catch (const std::exception& e) { - std::cerr << "Failed to parse message key to request ID: " << e.what() << "\n"; + std::cerr << "Failed to parse message key '" << key_str << "' to request ID: " << e.what() << "\n"; return; } } diff --git a/tools/common.just b/tools/common.just index b4bcc16..c4a38e7 100644 --- a/tools/common.just +++ b/tools/common.just @@ -27,8 +27,8 @@ build-windows: fetch-proto kafka-container-up: docker-compose up -d - MSYS_NO_PATHCONV=1 docker exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic moderation-result --replication-factor 1 --partitions 3 - MSYS_NO_PATHCONV=1 docker exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic moderation-request --replication-factor 1 --partitions 3 + MSYS_NO_PATHCONV=1 docker exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic moderation-result --replication-factor 1 --partitions 3 || true + MSYS_NO_PATHCONV=1 docker exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic moderation-request --replication-factor 1 --partitions 3 || true kafka-container-down: MSYS_NO_PATHCONV=1 docker exec kafka /opt/kafka/bin/kafka-topics.sh --bootstrap-server localhost:9092 --delete --topic moderation-request