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 - 5.x #56

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 @@ -265,6 +265,7 @@ Parser.prototype._parseSubscribe = function () {
topic = this._parseString()
if (topic === null) return this._emitError(new 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
11 changes: 11 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,17 @@ testParseError('Cannot parse protocolId', Buffer.from([
77, 81, 73, 115, 100, 112
]))

// 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
]))

test('stops parsing after first error', function (t) {
t.plan(4)

Expand Down