From 0038d1ea109265d5e5aada14d197a2cea9adf4e8 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Mon, 21 Dec 2020 15:34:11 +0100 Subject: [PATCH] Check publish paths --- mqttpacket.cpp | 9 ++++++++- utils.cpp | 14 ++++++++++++++ utils.h | 2 ++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mqttpacket.cpp b/mqttpacket.cpp index 277fd57..4d574ef 100644 --- a/mqttpacket.cpp +++ b/mqttpacket.cpp @@ -4,6 +4,8 @@ #include #include +#include "utils.h" + RemainingLength::RemainingLength() { memset(bytes, 0, 4); @@ -254,9 +256,14 @@ void MqttPacket::handlePublish(std::shared_ptr &subscriptionS if (qos == 3) throw ProtocolError("QoS 3 is a protocol violation."); - // TODO: validate UTF8. std::string topic(readBytes(variable_header_length), variable_header_length); + if (!isValidUtf8(topic) || !isValidPublishPath(topic)) + { + std::cerr << "Client " << sender->repr() << " sent topic with wildcards or invalid UTF8. Ignoring."; + return; + } + if (qos) { throw ProtocolError("Qos not implemented."); diff --git a/utils.cpp b/utils.cpp index 46076bb..dec6f8a 100644 --- a/utils.cpp +++ b/utils.cpp @@ -111,3 +111,17 @@ bool strContains(const std::string &s, const std::string &needle) { return s.find(needle) != std::string::npos; } + +bool isValidPublishPath(const std::string &s) +{ + if (s.empty()) + return false; + + for (const char c : s) + { + if (c == '#' || c == '+') + return false; + } + + return true; +} diff --git a/utils.h b/utils.h index 66b14e0..4a5a8c5 100644 --- a/utils.h +++ b/utils.h @@ -27,4 +27,6 @@ bool isValidUtf8(const std::string &s); bool strContains(const std::string &s, const std::string &needle); +bool isValidPublishPath(const std::string &s); + #endif // UTILS_H -- libgit2 0.21.4