Skip to content

Commit

Permalink
fix: validation of null topics (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando authored Dec 14, 2022
1 parent a9a2279 commit 58c37eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: { // #
Expand Down
8 changes: 8 additions & 0 deletions test/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 58c37eb

Please sign in to comment.