Fix bug in bad validation of topic names and topic filters, they should be at least 1 char wide (issue #67)

This commit is contained in:
andrea
2015-08-01 16:13:24 +02:00
parent fd1df47fec
commit 8b48a82968
7 changed files with 86 additions and 27 deletions
@@ -67,7 +67,11 @@ class PublishDecoder extends DemuxDecoder {
return;
}
if (topic.contains("+") || topic.contains("#")) {
throw new CorruptedFrameException("Received a PUBLISH with topic containting wild card chars, topic: " + topic);
throw new CorruptedFrameException("Received a PUBLISH with topic containing wild card chars, topic: " + topic);
}
//check topic is at least one char [MQTT-4.7.3-1]
if (topic.length() == 0) {
throw new CorruptedFrameException("Received a PUBLISH with topic without any character");
}
message.setTopicName(topic);
@@ -41,16 +41,16 @@ class SubscribeDecoder extends DemuxDecoder {
//check qos level
if (message.getQos() != QOSType.LEAST_ONE) {
throw new CorruptedFrameException("Received Subscribe message with QoS other than LEAST_ONE, was: " + message.getQos());
throw new CorruptedFrameException("Received SUBSCRIBE message with QoS other than LEAST_ONE, was: " + message.getQos());
}
int start = in.readerIndex();
//read messageIDs
message.setMessageID(in.readUnsignedShort());
int readed = in.readerIndex() - start;
while (readed < message.getRemainingLength()) {
int read = in.readerIndex() - start;
while (read < message.getRemainingLength()) {
decodeSubscription(in, message);
readed = in.readerIndex()- start;
read = in.readerIndex() - start;
}
if (message.subscriptions().isEmpty()) {
@@ -65,6 +65,10 @@ class SubscribeDecoder extends DemuxDecoder {
*/
private void decodeSubscription(ByteBuf in, SubscribeMessage message) throws UnsupportedEncodingException {
String topic = Utils.decodeString(in);
//check topic is at least one char [MQTT-4.7.3-1]
if (topic.length() == 0) {
throw new CorruptedFrameException("Received a SUBSCRIBE with empty topic filter");
}
byte qosByte = in.readByte();
if ((qosByte & 0xFC) > 0) { //the first 6 bits is reserved => has to be 0
throw new CorruptedFrameException("subscribe MUST have QoS byte with reserved buts to 0, found " + Integer.toHexString(qosByte));
@@ -46,10 +46,15 @@ class UnsubscribeDecoder extends DemuxDecoder {
int start = in.readerIndex();
//read messageIDs
message.setMessageID(in.readUnsignedShort());
int readed = in.readerIndex()- start;
while (readed < message.getRemainingLength()) {
message.addTopicFilter(Utils.decodeString(in));
readed = in.readerIndex()- start;
int read = in.readerIndex() - start;
while (read < message.getRemainingLength()) {
String topicFilter = Utils.decodeString(in);
//check topic is at least one char [MQTT-4.7.3-1]
if (topicFilter.length() == 0) {
throw new CorruptedFrameException("Received an UNSUBSCRIBE with empty topic filter");
}
message.addTopicFilter(topicFilter);
read = in.readerIndex() - start;
}
if (message.topicFilters().isEmpty()) {
throw new CorruptedFrameException("unsubscribe MUST have got at least 1 topic");