Skip to content

Commit fb21e42

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 3b0a392 commit fb21e42

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

binary.js

Lines changed: 10 additions & 2 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 45 additions & 2 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy