Skip to content

Commit

Permalink
Fix topic handling after altering topic in plugin
Browse files Browse the repository at this point in the history
The subtopics weren't regenerated, so ACL and subscriber lookup was
still done with the unaltered topic.
  • Loading branch information
halfgaar committed Mar 21, 2024
1 parent 6486c06 commit e28e8ca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions FlashMQTests/plugintests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ void MainTests::testChangePublish()
receiver.connectClient(version, false, 100);
receiver.subscribe("#", 2);

FlashMQTestClient receiver_of_pattern;
receiver_of_pattern.start();
receiver_of_pattern.connectClient(version, false, 100);
receiver_of_pattern.subscribe("changed", 2);

sender.publish("changeme", "hello", 1);

receiver.waitForMessageCount(1);
Expand All @@ -672,6 +677,13 @@ void MainTests::testChangePublish()
MYCASTCOMPARE(receiver.receivedPublishes.front().getTopic(), "changed");
MYCASTCOMPARE(receiver.receivedPublishes.front().getPayloadCopy(), "hello");
MYCASTCOMPARE(receiver.receivedPublishes.front().getQos(), 2);

receiver_of_pattern.waitForMessageCount(1);

MYCASTCOMPARE(receiver_of_pattern.receivedPublishes.size(), 1);
MYCASTCOMPARE(receiver_of_pattern.receivedPublishes.front().getTopic(), "changed");
MYCASTCOMPARE(receiver_of_pattern.receivedPublishes.front().getPayloadCopy(), "hello");
MYCASTCOMPARE(receiver_of_pattern.receivedPublishes.front().getQos(), 2);
}
}

Expand Down
5 changes: 5 additions & 0 deletions mqttpacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,11 @@ void MqttPacket::handlePublish()
this->alteredByPlugin = authentication.alterPublish(this->publishData.client_id, this->publishData.topic, this->publishData.getSubtopics(),
getPayloadView(), this->publishData.qos, this->publishData.retain, this->publishData.getUserProperties());

if (this->alteredByPlugin)
{
this->publishData.resplitTopic();
}

if (authentication.aclCheck(this->publishData, getPayloadView()) == AuthResult::success)
{
if (publishData.retain && settings->retainedMessagesMode == RetainedMessagesMode::Enabled)
Expand Down
5 changes: 5 additions & 0 deletions types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,11 @@ const std::vector<std::string> &Publish::getSubtopics()
return subtopics;
}

void Publish::resplitTopic()
{
subtopics = splitTopic(this->topic);
}

WillPublish::WillPublish(const Publish &other) :
Publish(other)
{
Expand Down
1 change: 1 addition & 0 deletions types.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class Publish : public PublishBase
Publish& operator=(const Publish &other);

const std::vector<std::string> &getSubtopics();
void resplitTopic();
};

class WillPublish : public Publish
Expand Down

0 comments on commit e28e8ca

Please sign in to comment.