Skip to content

Commit

Permalink
Merge pull request #107 from redboltz/add_utf8_check
Browse files Browse the repository at this point in the history
Added UTF-8 checking.
  • Loading branch information
redboltz authored Dec 23, 2023
2 parents 0b57691 + 5977b30 commit c7a5326
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 68 deletions.
11 changes: 7 additions & 4 deletions include/async_mqtt/packet/property.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <async_mqtt/util/static_vector.hpp>
#include <async_mqtt/util/endian_convert.hpp>
#include <async_mqtt/util/json_like_out.hpp>
#include <async_mqtt/util/utf8validate.hpp>

#include <async_mqtt/exception.hpp>
#include <async_mqtt/packet/qos.hpp>
Expand Down Expand Up @@ -215,10 +216,12 @@ struct binary_property : private boost::totally_ordered<binary_property> {
struct string_property : binary_property {
string_property(property::id id, buffer buf)
:binary_property{id, force_move(buf)} {
#if 0 // TBD
auto r = utf8string::validate_contents(this->val());
if (r != utf8string::validation::well_formed) throw utf8string_contents_error(r);
#endif
if (!utf8string_check(this->val())) {
throw make_error(
errc::bad_message,
"string property invalid utf8"
);
}
}
};

Expand Down
57 changes: 38 additions & 19 deletions include/async_mqtt/packet/v3_1_1_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <async_mqtt/util/move.hpp>
#include <async_mqtt/util/static_vector.hpp>
#include <async_mqtt/util/endian_convert.hpp>
#include <async_mqtt/util/utf8validate.hpp>

#include <async_mqtt/packet/fixed_header.hpp>
#include <async_mqtt/packet/copy_to_static_vector.hpp>
Expand Down Expand Up @@ -136,14 +137,21 @@ class connect_packet {
endian_store(keep_alive_sec, keep_alive_buf_.data());
endian_store(boost::numeric_cast<std::uint16_t>(client_id_.size()), client_id_length_buf_.data());

#if 0 // TBD
utf8string_check(client_id_);
#endif
if (!utf8string_check(client_id_)) {
throw make_error(
errc::bad_message,
"v3_1_1::connect_packet client_id invalid utf8"
);
}

if (clean_session) connect_flags_ |= connect_flags::mask_clean_session;
if (user_name) {
#if 0 // TBD
utf8string_check(*user_name);
#endif
if (!utf8string_check(*user_name)) {
throw make_error(
errc::bad_message,
"v3_1_1::connect_packet user name invalid utf8"
);
}
connect_flags_ |= connect_flags::mask_user_name_flag;
user_name_ = force_move(*user_name);
user_name_length_buf_ = endian_static_vector(boost::numeric_cast<std::uint16_t>(user_name_.size()));
Expand All @@ -159,10 +167,12 @@ class connect_packet {
connect_flags_ |= connect_flags::mask_will_flag;
if (w->get_retain() == pub::retain::yes) connect_flags_ |= connect_flags::mask_will_retain;
connect_flags::set_will_qos(connect_flags_, w->get_qos());

#if 0 // TBD
utf8string_check(w->topic());
#endif
if (!utf8string_check(w->topic())) {
throw make_error(
errc::bad_message,
"v3_1_1::connect_packet will topic invalid utf8"
);
}
will_topic_ = force_move(w->topic());
will_topic_length_buf_ = endian_static_vector(boost::numeric_cast<std::uint16_t>(will_topic_.size()));
if (w->message().size() > 0xffffL) {
Expand Down Expand Up @@ -270,9 +280,12 @@ class connect_packet {
);
}
client_id_ = buf.substr(0, client_id_length);
#if 0 // TBD
utf8string_check(client_id_);
#endif
if (!utf8string_check(client_id_)) {
throw make_error(
errc::bad_message,
"v3_1_1::connect_packet client_id invalid utf8"
);
}
buf.remove_prefix(client_id_length);

// will
Expand Down Expand Up @@ -303,9 +316,12 @@ class connect_packet {
);
}
will_topic_ = buf.substr(0, will_topic_length);
#if 0 // TBD
utf8string_check(will_topic_);
#endif
if (!utf8string_check(will_topic_)) {
throw make_error(
errc::bad_message,
"v3_1_1::connect_packet will topic invalid utf8"
);
}
buf.remove_prefix(will_topic_length);

// will_message_length
Expand Down Expand Up @@ -362,9 +378,12 @@ class connect_packet {
);
}
user_name_ = buf.substr(0, user_name_length);
#if 0 // TBD
utf8string_check(user_name_);
#endif
if (!utf8string_check(user_name_)) {
throw make_error(
errc::bad_message,
"v3_1_1::connect_packet user name invalid utf8"
);
}
buf.remove_prefix(user_name_length);
}

Expand Down
23 changes: 17 additions & 6 deletions include/async_mqtt/packet/v3_1_1_publish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <async_mqtt/util/move.hpp>
#include <async_mqtt/util/static_vector.hpp>
#include <async_mqtt/util/endian_convert.hpp>
#include <async_mqtt/util/utf8validate.hpp>

#include <async_mqtt/packet/packet_iterator.hpp>
#include <async_mqtt/packet/packet_id_type.hpp>
Expand Down Expand Up @@ -104,9 +105,14 @@ class basic_publish_packet {
remaining_length_ += payload.size();
payloads_.push_back(payload);
}
#if 0 // TBD
utf8string_check(topic_name_);
#endif

if (!utf8string_check(topic_name_)) {
throw make_error(
errc::bad_message,
"v3_1_1::publish_packet topic name invalid utf8"
);
}

auto rb = val_to_variable_bytes(boost::numeric_cast<std::uint32_t>(remaining_length_));
for (auto e : rb) {
remaining_length_buf_.push_back(e);
Expand Down Expand Up @@ -215,9 +221,14 @@ class basic_publish_packet {
);
}
topic_name_ = buf.substr(0, topic_name_length);
#if 0 // TBD
utf8string_check(topic_name_);
#endif

if (!utf8string_check(topic_name_)) {
throw make_error(
errc::bad_message,
"v3_1_1::publish_packet topic name invalid utf8"
);
}

buf.remove_prefix(topic_name_length);

// packet_id
Expand Down
18 changes: 15 additions & 3 deletions include/async_mqtt/packet/v3_1_1_subscribe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <async_mqtt/util/move.hpp>
#include <async_mqtt/util/static_vector.hpp>
#include <async_mqtt/util/endian_convert.hpp>
#include <async_mqtt/util/utf8validate.hpp>

#include <async_mqtt/packet/packet_id_type.hpp>
#include <async_mqtt/packet/fixed_header.hpp>
Expand Down Expand Up @@ -96,9 +97,12 @@ class basic_subscribe_packet {
size + // topic filter
1; // opts

#if 0 // TBD
utf8string_check(e.all_topic());
#endif
if (!utf8string_check(e.all_topic())) {
throw make_error(
errc::bad_message,
"v3_1_1::subscribe_packet topic filter invalid utf8"
);
}
}

remaining_length_buf_ = val_to_variable_bytes(boost::numeric_cast<std::uint32_t>(remaining_length_));
Expand Down Expand Up @@ -165,6 +169,14 @@ class basic_subscribe_packet {
);
}
auto topic = buf.substr(0, topic_length);

if (!utf8string_check(topic)) {
throw make_error(
errc::bad_message,
"v3_1_1::subscribe_packet topic filter invalid utf8"
);
}

buf.remove_prefix(topic_length);

// opts
Expand Down
17 changes: 14 additions & 3 deletions include/async_mqtt/packet/v3_1_1_unsubscribe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <async_mqtt/util/move.hpp>
#include <async_mqtt/util/static_vector.hpp>
#include <async_mqtt/util/endian_convert.hpp>
#include <async_mqtt/util/utf8validate.hpp>

#include <async_mqtt/packet/packet_id_type.hpp>
#include <async_mqtt/packet/fixed_header.hpp>
Expand Down Expand Up @@ -75,9 +76,13 @@ class basic_unsubscribe_packet {
remaining_length_ +=
2 + // topic filter length
size; // topic filter
#if 0 // TBD
utf8string_check(e.all_topic());
#endif

if (!utf8string_check(e.all_topic())) {
throw make_error(
errc::bad_message,
"v3_1_1::unsubscribe_packet topic filter invalid utf8"
);
}
}

remaining_length_buf_ = val_to_variable_bytes(boost::numeric_cast<std::uint32_t>(remaining_length_));
Expand Down Expand Up @@ -144,6 +149,12 @@ class basic_unsubscribe_packet {
);
}
auto topic = buf.substr(0, topic_length);
if (!utf8string_check(topic)) {
throw make_error(
errc::bad_message,
"v3_1_1::unsubscribe_packet topic filter invalid utf8"
);
}
entries_.emplace_back(force_move(topic));
buf.remove_prefix(topic_length);
}
Expand Down
57 changes: 38 additions & 19 deletions include/async_mqtt/packet/v5_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <async_mqtt/util/move.hpp>
#include <async_mqtt/util/static_vector.hpp>
#include <async_mqtt/util/endian_convert.hpp>
#include <async_mqtt/util/utf8validate.hpp>

#include <async_mqtt/packet/fixed_header.hpp>
#include <async_mqtt/packet/copy_to_static_vector.hpp>
Expand Down Expand Up @@ -144,14 +145,21 @@ class connect_packet {
endian_store(keep_alive_sec, keep_alive_buf_.data());
endian_store(boost::numeric_cast<std::uint16_t>(client_id_.size()), client_id_length_buf_.data());

#if 0 // TBD
utf8string_check(client_id_);
#endif
if (!utf8string_check(client_id_)) {
throw make_error(
errc::bad_message,
"v5::connect_packet client_id invalid utf8"
);
}

if (clean_start) connect_flags_ |= connect_flags::mask_clean_start;
if (user_name) {
#if 0 // TBD
utf8string_check(*user_name);
#endif
if (!utf8string_check(*user_name)) {
throw make_error(
errc::bad_message,
"v5::connect_packet user name invalid utf8"
);
}
connect_flags_ |= connect_flags::mask_user_name_flag;
user_name_ = force_move(*user_name);
user_name_length_buf_ = endian_static_vector(boost::numeric_cast<std::uint16_t>(user_name_.size()));
Expand Down Expand Up @@ -185,10 +193,12 @@ class connect_packet {
connect_flags_ |= connect_flags::mask_will_flag;
if (w->get_retain() == pub::retain::yes) connect_flags_ |= connect_flags::mask_will_retain;
connect_flags::set_will_qos(connect_flags_, w->get_qos());

#if 0 // TBD
utf8string_check(w->topic());
#endif
if (!utf8string_check(w->topic())) {
throw make_error(
errc::bad_message,
"v5::connect_packet will topic invalid utf8"
);
}
will_topic_ = force_move(w->topic());
will_topic_length_buf_ = endian_static_vector(boost::numeric_cast<std::uint16_t>(will_topic_.size()));
if (w->message().size() > 0xffffL) {
Expand Down Expand Up @@ -338,9 +348,12 @@ class connect_packet {
);
}
client_id_ = buf.substr(0, client_id_length);
#if 0 // TBD
utf8string_check(client_id_);
#endif
if (!utf8string_check(client_id_)) {
throw make_error(
errc::bad_message,
"v5::connect_packet client_id invalid utf8"
);
}
buf.remove_prefix(client_id_length);

// will
Expand Down Expand Up @@ -396,9 +409,12 @@ class connect_packet {
);
}
will_topic_ = buf.substr(0, will_topic_length);
#if 0 // TBD
utf8string_check(will_topic_);
#endif
if (!utf8string_check(will_topic_)) {
throw make_error(
errc::bad_message,
"v5::connect_packet will topic invalid utf8"
);
}
buf.remove_prefix(will_topic_length);

// will_message_length
Expand Down Expand Up @@ -455,9 +471,12 @@ class connect_packet {
);
}
user_name_ = buf.substr(0, user_name_length);
#if 0 // TBD
utf8string_check(user_name_);
#endif
if (!utf8string_check(user_name_)) {
throw make_error(
errc::bad_message,
"v5::connect_packet user name invalid utf8"
);
}
buf.remove_prefix(user_name_length);
}

Expand Down
22 changes: 16 additions & 6 deletions include/async_mqtt/packet/v5_publish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <async_mqtt/util/move.hpp>
#include <async_mqtt/util/static_vector.hpp>
#include <async_mqtt/util/endian_convert.hpp>
#include <async_mqtt/util/utf8validate.hpp>

#include <async_mqtt/packet/packet_iterator.hpp>
#include <async_mqtt/packet/packet_id_type.hpp>
Expand Down Expand Up @@ -80,9 +81,13 @@ class basic_publish_packet {
remaining_length_ += payload.size();
payloads_.push_back(payload);
}
#if 0 // TBD
utf8string_check(topic_name_);
#endif

if (!utf8string_check(topic_name_)) {
throw make_error(
errc::bad_message,
"v5::publish_packet topic name invalid utf8"
);
}

auto pb = val_to_variable_bytes(boost::numeric_cast<std::uint32_t>(property_length_));
for (auto e : pb) {
Expand Down Expand Up @@ -179,9 +184,14 @@ class basic_publish_packet {
);
}
topic_name_ = buf.substr(0, topic_name_length);
#if 0 // TBD
utf8string_check(topic_name_);
#endif

if (!utf8string_check(topic_name_)) {
throw make_error(
errc::bad_message,
"v5::publish_packet topic name invalid utf8"
);
}

buf.remove_prefix(topic_name_length);

// packet_id
Expand Down
Loading

0 comments on commit c7a5326

Please sign in to comment.