Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #211 #212

Merged
merged 2 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,20 @@ Client.prototype.close = function (done) {

function finish () {
if (!that.disconnected && that.will) {
that.broker.publish(that.will, that, function (err) {
that.broker.authorizePublish(that, that.will, function (err) {
if (!err) {
that.broker.persistence.delWill({
id: that.id,
brokerId: that.broker.id
}, nop)
that.broker.publish(that.will, that, done)
} else {
done()
}

function done (err) {
if (!err) {
that.broker.persistence.delWill({
id: that.id,
brokerId: that.broker.id
}, nop)
}
}
})
that.will = null // this function might be called twice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huhm! authorizePublish is async function. Server will crash when that.broker.publish(that.will, that, done) called because that.will is null

Expand Down Expand Up @@ -298,4 +306,4 @@ function enqueue (packet) {
handle(this.client, packet, this.client._nextBatch)
}

function nop () {}
function nop () { }
41 changes: 41 additions & 0 deletions test/will.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,44 @@ test('delete the will in the persistence after publish', function (t) {
cb()
}
})

test('delivers a will with authorization', function (t) {
let authorized = false
var opts = {}
// willConnect populates opts with a will
var s = willConnect(setup(aedes({ authorizePublish: (_1, _2, callback) => { authorized = true; callback(null) } })), opts)

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

s.broker.mq.on('mywill', function (packet, cb) {
t.equal(packet.topic, opts.will.topic, 'topic matches')
t.deepEqual(packet.payload, opts.will.payload, 'payload matches')
t.equal(packet.qos, opts.will.qos, 'qos matches')
t.equal(packet.retain, opts.will.retain, 'retain matches')
t.equal(authorized, true, 'authorization called')
cb()
})

s.conn.destroy()
})

test('does not deliver a will without authorization', function (t) {
let authorized = false
var opts = {}
// willConnect populates opts with a will
var s = willConnect(setup(aedes({ authorizePublish: (_1, _2, callback) => { authorized = true; callback(new Error()) } })), opts)

s.broker.on('clientDisconnect', function () {
t.equal(authorized, true, 'authorization called')
t.end()
})

s.broker.mq.on('mywill', function (packet, cb) {
t.fail('received will without authorization')
cb()
})

s.conn.destroy()
})