From 2ae1bb7ea11f67217c87fb8392bb3c206aa7bd99 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sat, 27 Mar 2021 15:29:38 +0100 Subject: [PATCH] Integrate valid UTF-8 and publish char checking --- mqttpacket.cpp | 10 ++-------- utils.cpp | 5 ++++- utils.h | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/mqttpacket.cpp b/mqttpacket.cpp index bb7dd5d..6fde0dd 100644 --- a/mqttpacket.cpp +++ b/mqttpacket.cpp @@ -439,15 +439,9 @@ void MqttPacket::handlePublish() topic = std::string(readBytes(variable_header_length), variable_header_length); - if (!isValidUtf8(topic)) + if (!isValidUtf8(topic, true)) { - logger->logf(LOG_WARNING, "Client '%s' published a message with invalid UTF8. Dropping.", sender->repr().c_str()); - return; - } - - if (!isValidPublishPath(topic)) - { - logger->logf(LOG_WARNING, "Client '%s' published a message with invalid publish path '%s'. Dropping.", sender->repr().c_str(), topic.c_str()); + logger->logf(LOG_WARNING, "Client '%s' published a message with invalid UTF8 or +/# in it. Dropping.", sender->repr().c_str()); return; } diff --git a/utils.cpp b/utils.cpp index 2e3b23a..8a1ed76 100644 --- a/utils.cpp +++ b/utils.cpp @@ -77,7 +77,7 @@ bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTo return result; } -bool isValidUtf8(const std::string &s) +bool isValidUtf8(const std::string &s, bool alsoCheckInvalidPublishChars) { int multibyte_remain = 0; int cur_code_point = 0; @@ -86,6 +86,9 @@ bool isValidUtf8(const std::string &s) if (x == 0) return false; + if (alsoCheckInvalidPublishChars && (x == '#' || x == '+')) + return false; + if(!multibyte_remain) { cur_code_point = 0; diff --git a/utils.h b/utils.h index 568ff7e..8ab3af7 100644 --- a/utils.h +++ b/utils.h @@ -49,7 +49,7 @@ std::vector splitToVector(const std::string &input, const char sep, bool topicsMatch(const std::string &subscribeTopic, const std::string &publishTopic); -bool isValidUtf8(const std::string &s); +bool isValidUtf8(const std::string &s, bool alsoCheckInvalidPublishChars = false); bool strContains(const std::string &s, const std::string &needle); -- libgit2 0.21.4