Skip to content

Commit 04d23ce

Browse files
fix: check the format of the index of each attachment
A specially crafted packet could be incorrectly decoded. Example: ```js const decoder = new Decoder(); decoder.on("decoded", (packet) => { console.log(packet.data); // prints [ 'hello', [Function: splice] ] }) decoder.add('51-["hello",{"_placeholder":true,"num":"splice"}]'); decoder.add(Buffer.from("world")); ``` As usual, please remember not to trust user input. Backported from b5d0cb7
1 parent 6a59237 commit 04d23ce

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

binary.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,16 @@ exports.reconstructPacket = function(packet, buffers) {
7070
function _reconstructPacket(data, buffers) {
7171
if (!data) return data;
7272

73-
if (data && data._placeholder) {
74-
return buffers[data.num]; // appropriate buffer (should be natural order anyway)
73+
if (data && data._placeholder === true) {
74+
var isIndexValid =
75+
typeof data.num === "number" &&
76+
data.num >= 0 &&
77+
data.num < buffers.length;
78+
if (isIndexValid) {
79+
return buffers[data.num]; // appropriate buffer (should be natural order anyway)
80+
} else {
81+
throw new Error("illegal attachments");
82+
}
7583
} else if (isArray(data)) {
7684
for (var i = 0; i < data.length; i++) {
7785
data[i] = _reconstructPacket(data[i], buffers);

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ Emitter(Decoder.prototype);
239239
Decoder.prototype.add = function(obj) {
240240
var packet;
241241
if (typeof obj === 'string') {
242+
if (this.reconstructor) {
243+
throw new Error("got plaintext data when reconstructing a packet");
244+
}
242245
packet = decodeString(obj);
243246
if (exports.BINARY_EVENT === packet.type || exports.BINARY_ACK === packet.type) { // binary packet's json
244247
this.reconstructor = new BinaryReconstructor(packet);

test/buffer.js

+45-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
var parser = require('../index.js');
22
var expect = require('expect.js');
33
var helpers = require('./helpers.js');
4-
var encode = parser.encode;
5-
var decode = parser.decode;
4+
var Decoder = parser.Decoder;
65

76
describe('parser', function() {
87
it('encodes a Buffer', function() {
@@ -14,6 +13,15 @@ describe('parser', function() {
1413
});
1514
});
1615

16+
it("encodes a nested Buffer", function() {
17+
helpers.test_bin({
18+
type: parser.BINARY_EVENT,
19+
data: ["a", { b: ["c", Buffer.from("abc", "utf8")] }],
20+
id: 23,
21+
nsp: "/cool",
22+
});
23+
});
24+
1725
it('encodes a binary ack with Buffer', function() {
1826
helpers.test_bin({
1927
type: parser.BINARY_ACK,
@@ -22,4 +30,39 @@ describe('parser', function() {
2230
nsp: '/back'
2331
})
2432
});
33+
34+
it("throws an error when adding an attachment with an invalid 'num' attribute (string)", function() {
35+
var decoder = new Decoder();
36+
37+
expect(function() {
38+
decoder.add('51-["hello",{"_placeholder":true,"num":"splice"}]');
39+
decoder.add(Buffer.from("world"));
40+
}).to.throwException(/^illegal attachments$/);
41+
});
42+
43+
it("throws an error when adding an attachment with an invalid 'num' attribute (out-of-bound)", function() {
44+
var decoder = new Decoder();
45+
46+
expect(function() {
47+
decoder.add('51-["hello",{"_placeholder":true,"num":1}]');
48+
decoder.add(Buffer.from("world"));
49+
}).to.throwException(/^illegal attachments$/);
50+
});
51+
52+
it("throws an error when adding an attachment without header", function() {
53+
var decoder = new Decoder();
54+
55+
expect(function() {
56+
decoder.add(Buffer.from("world"));
57+
}).to.throwException(/^got binary data when not reconstructing a packet$/);
58+
});
59+
60+
it("throws an error when decoding a binary event without attachments", function() {
61+
var decoder = new Decoder();
62+
63+
expect(function() {
64+
decoder.add('51-["hello",{"_placeholder":true,"num":0}]');
65+
decoder.add('2["hello"]');
66+
}).to.throwException(/^got plaintext data when reconstructing a packet$/);
67+
});
2568
});

0 commit comments

Comments
 (0)