MQTT topic validation and lockup fix

closes #3557
This commit is contained in:
Steve-Mcl
2022-04-29 19:56:37 +01:00
parent 4fb8292618
commit be3b5b7fe2
3 changed files with 151 additions and 88 deletions

View File

@@ -452,7 +452,17 @@
}
return defaultContentType || 'none'
}
/**
* Test a topic string is valid for publishing
* @param {string} topic
* @returns `true` if it is a valid topic
*/
function validateMQTTPublishTopic(topic) {
if(!topic || topic == "") {
return true;
}
return !/[\+#\b\f\n\r\t\v\0]/.test(topic);
}
RED.nodes.registerType('mqtt-broker',{
category: 'config',
defaults: {
@@ -487,17 +497,17 @@
label: RED._("node-red:mqtt.label.keepalive"),
validate:RED.validators.number(false)},
cleansession: {value: true},
birthTopic: {value:""},
birthTopic: {value:"", validate:validateMQTTPublishTopic},
birthQos: {value:"0"},
birthRetain: {value:false},
birthPayload: {value:""},
birthMsg: { value: {}},
closeTopic: {value:""},
closeTopic: {value:"", validate:validateMQTTPublishTopic},
closeQos: {value:"0"},
closeRetain: {value:false},
closePayload: {value:""},
closeMsg: { value: {}},
willTopic: {value:""},
willTopic: {value:"", validate:validateMQTTPublishTopic},
willQos: {value:"0"},
willRetain: {value:false},
willPayload: {value:""},
@@ -856,7 +866,7 @@
category: 'network',
defaults: {
name: {value:""},
topic: {value:""},
topic: {value:"", validate:validateMQTTPublishTopic},
qos: {value:""},
retain: {value:""},
respTopic: {value:""},

View File

@@ -91,12 +91,21 @@ module.exports = function(RED) {
}
/**
* Test a topic string is valid
* Test a topic string is valid for subscription
* @param {string} topic
* @returns `true` if it is a valid topic
*/
function isValidSubscriptionTopic(topic) {
return /^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/.test(topic)
return /^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/.test(topic);
}
/**
* Test a topic string is valid for publishing
* @param {string} topic
* @returns `true` if it is a valid topic
*/
function isValidPublishTopic(topic) {
return !/[\+#\b\f\n\r\t\v\0]/.test(topic);
}
/**
@@ -340,38 +349,15 @@ module.exports = function(RED) {
msg.messageExpiryInterval = node.messageExpiryInterval;
}
}
if (msg.userProperties && typeof msg.userProperties !== "object") {
delete msg.userProperties;
}
if (hasProperty(msg, "topicAlias") && !isNaN(msg.topicAlias) && (msg.topicAlias === 0 || bsp.topicAliasMaximum === 0 || msg.topicAlias > bsp.topicAliasMaximum)) {
delete msg.topicAlias;
}
if (hasProperty(msg, "payload")) {
//check & sanitise topic
let topicOK = hasProperty(msg, "topic") && (typeof msg.topic === "string") && (msg.topic !== "");
if (!topicOK && v5) {
//NOTE: A value of 0 (in server props topicAliasMaximum) indicates that the Server does not accept any Topic Aliases on this connection
if (hasProperty(msg, "topicAlias") && !isNaN(msg.topicAlias) && msg.topicAlias >= 0 && bsp.topicAliasMaximum && bsp.topicAliasMaximum >= msg.topicAlias) {
topicOK = true;
msg.topic = ""; //must be empty string
} else if (hasProperty(msg, "responseTopic") && (typeof msg.responseTopic === "string") && (msg.responseTopic !== "")) {
//TODO: if topic is empty but responseTopic has a string value, use that instead. Is this desirable?
topicOK = true;
msg.topic = msg.responseTopic;
//TODO: delete msg.responseTopic - to prevent it being resent?
// send the message
node.brokerConn.publish(msg, function(err) {
if(err && err.warn) {
node.warn(err);
return;
}
}
topicOK = topicOK && !/[\+#\b\f\n\r\t\v\0]/.test(msg.topic);
if (topicOK) {
node.brokerConn.publish(msg, done); // send the message
} else {
node.warn(RED._("mqtt.errors.invalid-topic"));
done();
}
done(err);
});
} else {
done();
}
@@ -827,31 +813,46 @@ module.exports = function(RED) {
}
};
node.disconnect = function (callback) {
const _callback = function (resetNodeConnectedState) {
setStatusDisconnected(node, true);
if(resetNodeConnectedState) {
node.closing = true;
node.connecting = false;
node.connected = false;
const _callback = function () {
if(node.connected || node.connecting) {
setStatusDisconnected(node, true);
}
if(node.client) { node.client.removeAllListeners(); }
node.connecting = false;
node.connected = false;
callback && typeof callback == "function" && callback();
};
if(!node.client) { return _callback(); }
if(node.closing) { return _callback(); }
if(node.closing) {
return _callback(false);
}
var endCallBack = function endCallBack() {
}
let waitEnd = (client, ms) => {
return new Promise( (resolve, reject) => {
node.closing = true;
if(!client) {
resolve();
} else {
const t = setTimeout(reject, ms);
client.end(() => {
clearTimeout(t);
resolve()
});
}
});
};
if(node.connected && node.closeMessage) {
node.publish(node.closeMessage, function (err) {
node.client.end(endCallBack);
_callback(true);
waitEnd(node.client, 2000).then(() => {
_callback();
}).catch((e) => {
_callback();
})
});
} else if(node.connected) {
node.client.end(endCallBack);
_callback(true);
} else {
_callback(false);
waitEnd(node.client, 2000).then(() => {
_callback();
}).catch((e) => {
_callback();
})
}
}
node.subscriptionIds = {};
@@ -933,8 +934,18 @@ module.exports = function(RED) {
qos: msg.qos || 0,
retain: msg.retain || false
};
let topicOK = hasProperty(msg, "topic") && (typeof msg.topic === "string") && (isValidPublishTopic(msg.topic));
//https://github.com/mqttjs/MQTT.js/blob/master/README.md#mqttclientpublishtopic-message-options-callback
if(node.options.protocolVersion == 5) {
const bsp = node.serverProperties || {};
if (msg.userProperties && typeof msg.userProperties !== "object") {
delete msg.userProperties;
}
if (hasProperty(msg, "topicAlias") && !isNaN(Number(msg.topicAlias))) {
msg.topicAlias = parseInt(msg.topicAlias);
} else {
delete msg.topicAlias;
}
options.properties = options.properties || {};
setStrProp(msg, options.properties, "responseTopic");
setBufferProp(msg, options.properties, "correlationData");
@@ -944,29 +955,46 @@ module.exports = function(RED) {
setIntProp(msg, options.properties, "topicAlias", 1, node.serverProperties.topicAliasMaximum || 0);
setBoolProp(msg, options.properties, "payloadFormatIndicator");
//FUTURE setIntProp(msg, options.properties, "subscriptionIdentifier", 1, 268435455);
if (options.properties.topicAlias) {
if (!node.topicAliases.hasOwnProperty(options.properties.topicAlias) && msg.topic == "") {
//check & sanitise topic
if (topicOK && options.properties.topicAlias) {
let aliasValid = (bsp.topicAliasMaximum && bsp.topicAliasMaximum >= options.properties.topicAlias);
if (!aliasValid) {
done("Invalid topicAlias");
return
}
if (node.topicAliases[options.properties.topicAlias] === msg.topic) {
msg.topic = ""
msg.topic = "";
} else {
node.topicAliases[options.properties.topicAlias] = msg.topic
node.topicAliases[options.properties.topicAlias] = msg.topic;
}
} else if (!msg.topic && options.properties.responseTopic) {
msg.topic = msg.responseTopic;
topicOK = isValidPublishTopic(msg.topic);
delete msg.responseTopic; //prevent responseTopic being resent?
}
}
node.client.publish(msg.topic, msg.payload, options, function(err) {
done && done(err);
return
});
if (topicOK) {
node.client.publish(msg.topic, msg.payload, options, function(err) {
done && done(err);
return
});
} else {
const error = new Error(RED._("mqtt.errors.invalid-topic"));
error.warn = true;
done(error);
}
}
};
node.on('close', function(done) {
node.closing = true;
node.disconnect(done);
node.disconnect(function() {
if(node.client) {
node.client.removeAllListeners();
}
done();
}, true);
});
}
@@ -1143,6 +1171,7 @@ module.exports = function(RED) {
node.brokerConn.unsubscribe(node.topic,node.id, removed);
}
node.brokerConn.deregister(node, done);
node.brokerConn = null;
} else {
done();
}
@@ -1207,6 +1236,7 @@ module.exports = function(RED) {
node.on('close', function(done) {
if (node.brokerConn) {
node.brokerConn.deregister(node,done);
node.brokerConn = null;
} else {
done();
}