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 malformed packet - v3.x #58

Merged
merged 1 commit into from
Apr 17, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ Parser.prototype._parseSubscribe = function() {
if (topic === null)
return this._emitError(new Error('Parse error - cannot parse topic'))

if (this._pos >= packet.length) return this._emitError(new Error('Malformed Subscribe Payload'))
qos = this._list.readUInt8(this._pos++)

// Push pair to subscriptions
Expand Down
14 changes: 13 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ function testParseError(expected, fixture) {
t.equal(err.message, expected, 'expected error message')
})

parser.on('packet', function() {
parser.on('packet', function(p) {
console.log(p)
t.fail('parse errors should not be followed by packet events')
})

Expand Down Expand Up @@ -993,3 +994,14 @@ test('stops parsing after first error', function(t) {
224, 0, // Header
]))
})

// When a Subscribe packet contains a topic_filter and the given
// length is topic_filter.length + 1 then the last byte (requested QoS) is interpreted as topic_filter
// reading the requested_qos at the end causes 'Index out of range' read
testParseError('Malformed Subscribe Payload', Buffer.from([
130, 14, // subscribe header and remaining length
0, 123, // packet ID
0, 10, // topic filter length
104, 105, 106, 107, 108, 47, 109, 110, 111, // topic filter with length of 9 bytes
0 // requested QoS
]))