diff --git a/lib/utils.js b/lib/utils.js index ea7c8dbf..4ebd34f4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -3,12 +3,14 @@ const { Transform, Writable } = require('stream') function validateTopic (topic, message) { + if (!topic || topic.length === 0) { // [MQTT-3.8.3-3] + return new Error('impossible to ' + message + ' to an empty topic') + } + const end = topic.length - 1 const endMinus = end - 1 const slashInPreEnd = endMinus > 0 && topic.charCodeAt(endMinus) !== 47 - if (topic.length === 0) { // [MQTT-3.8.3-3] - return new Error('impossible to ' + message + ' to an empty topic') - } + for (let i = 0; i < topic.length; i++) { switch (topic.charCodeAt(i)) { case 35: { // # diff --git a/test/topics.js b/test/topics.js index 69fa2c5a..7f28cde3 100644 --- a/test/topics.js +++ b/test/topics.js @@ -3,6 +3,14 @@ const { test } = require('tap') const { setup, connect, subscribe } = require('./helper') const aedes = require('../') +const { validateTopic } = require('../lib/utils') + +test('validation of `null` topic', function (t) { + // issue #780 + t.plan(1) + const err = validateTopic(null, 'SUBSCRIBE') + t.equal(err.message, 'impossible to SUBSCRIBE to an empty topic') +}) // [MQTT-4.7.1-3] test('Single-level wildcard should match empty level', function (t) {