Skip to content

Commit

Permalink
Added topic validations for unsubscribe.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jul 21, 2017
1 parent 79e8854 commit 98cbfee
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
30 changes: 4 additions & 26 deletions lib/handlers/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var write = require('../write')
var fastfall = require('fastfall')
var Packet = require('aedes-packet')
var through = require('through2')
var validateSubscribeTopic = require('./validations').validateSubscribeTopic
var topicActions = fastfall([
authorize,
storeSubscriptions,
Expand Down Expand Up @@ -36,32 +37,9 @@ function doSubscribe (sub, done) {

function authorize (sub, done) {
var client = this.client
var topic = sub.topic
var end = topic.length - 1
var endMinus = end - 1
var err
var slashInPreEnd = endMinus > 0 && topic.charCodeAt(endMinus) !== 47
if (topic.length === 0) {
return done(new Error('impossible to subscribe to an empty topic'))
}
for (var i = 0; i < topic.length; i++) {
switch (topic.charCodeAt(i)) {
case 35:
var notAtTheEnd = i !== end
if (notAtTheEnd || slashInPreEnd) {
err = new Error('# is only allowed in SUBSCRIBE in the last position')
return done(err)
}
break
case 43:
var pastChar = i < end - 1 && topic.charCodeAt(i + 1) !== 47
var preChar = i > 1 && topic.charCodeAt(i - 1) !== 47
if (pastChar || preChar) {
err = new Error('+ is only allowed in SUBSCRIBE between /')
return done(err)
}
break
}
var err = validateSubscribeTopic(sub.topic)
if (err) {
return done(err)
}
client.broker.authorizeSubscribe(client, sub, done)
}
Expand Down
12 changes: 11 additions & 1 deletion lib/handlers/unsubscribe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var write = require('../write')
var validateSubscribeTopic = require('./validations').validateSubscribeTopic

function UnsubscribeState (client, packet, finish, granted) {
this.client = client
Expand All @@ -11,9 +12,18 @@ function UnsubscribeState (client, packet, finish, granted) {

function handleUnsubscribe (client, packet, done) {
var broker = client.broker
var unsubscriptions = packet.unsubscriptions
var err

for (var i = 0; i < unsubscriptions.length; i++) {
err = validateSubscribeTopic(unsubscriptions[i])
if (err) {
return done(err)
}
}

if (packet.messageId) {
broker.persistence.removeSubscriptions(client, packet.unsubscriptions, function (err) {
broker.persistence.removeSubscriptions(client, unsubscriptions, function (err) {
if (err) {
return done(err)
}
Expand Down
29 changes: 29 additions & 0 deletions lib/handlers/validations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

function validateSubscribeTopic (topic) {
var end = topic.length - 1
var endMinus = end - 1
var slashInPreEnd = endMinus > 0 && topic.charCodeAt(endMinus) !== 47
if (topic.length === 0) {
return new Error('impossible to subscribe to an empty topic')
}
for (var i = 0; i < topic.length; i++) {
switch (topic.charCodeAt(i)) {
case 35:
var notAtTheEnd = i !== end
if (notAtTheEnd || slashInPreEnd) {
return new Error('# is only allowed in SUBSCRIBE in the last position')
}
break
case 43:
var pastChar = i < end - 1 && topic.charCodeAt(i + 1) !== 47
var preChar = i > 1 && topic.charCodeAt(i - 1) !== 47
if (pastChar || preChar) {
return new Error('+ is only allowed in SUBSCRIBE between /')
}
break
}
}
}

module.exports.validateSubscribeTopic = validateSubscribeTopic
14 changes: 14 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,18 @@ test('publish invalid topic with +', function (t) {
}]
})
})

test('unsubscribe to invalid topic with "' + topic + '"', function (t) {
var s = connect(setup())

s.broker.on('clientError', function () {
t.end()
})

s.inStream.write({
cmd: 'unsubscribe',
messageId: 24,
unsubscriptions: [topic]
})
})
})

0 comments on commit 98cbfee

Please sign in to comment.