mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
fix(parser): add a limit to the number of binary attachments
Backported from main: b25738c416
When a packet contains binary elements, the built-in parser does not modify them and simply sends them in their own WebSocket frame.
Example: `socket.emit("some event", Buffer.of(1,2,3))`
is encoded and transferred as:
- 1st frame: 51-["some event",{"_placeholder":true,"num":0}]
- 2nd frame: <buffer 01 02 03>
where:
- `5` is the type of the packet (binary message)
- `1` is the number of binary attachments
- `-` is the separator
- `["some event",{"_placeholder":true,"num":0}]` is the payload (including the placeholder)
On the receiving end, the parser reads the number of attachments and buffers them until they are all received.
Before this change, the built-in parser accepted any number of binary attachments, which could be exploited to make the server run out of memory.
The number of attachments is now limited to 10, which should be sufficient for most use cases.
The limit can be increased with a custom `parser`:
```js
import { Encoder, Decoder } from "socket.io-parser";
const io = new Server({
parser: {
Encoder,
Decoder: class extends Decoder {
constructor() {
super({
maxAttachments: 20
});
}
}
}
});
```
This commit is contained in:
29
index.js
29
index.js
@@ -218,8 +218,12 @@ function encodeAsBinary(obj, callback) {
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Decoder() {
|
||||
function Decoder(opts) {
|
||||
this.reconstructor = null;
|
||||
opts = opts || {};
|
||||
this.opts = {
|
||||
maxAttachments: opts.maxAttachments || 10,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -242,7 +246,7 @@ Decoder.prototype.add = function(obj) {
|
||||
if (this.reconstructor) {
|
||||
throw new Error("got plaintext data when reconstructing a packet");
|
||||
}
|
||||
packet = decodeString(obj);
|
||||
packet = decodeString(obj, this.opts.maxAttachments);
|
||||
if (exports.BINARY_EVENT === packet.type || exports.BINARY_ACK === packet.type) { // binary packet's json
|
||||
this.reconstructor = new BinaryReconstructor(packet);
|
||||
|
||||
@@ -272,11 +276,12 @@ Decoder.prototype.add = function(obj) {
|
||||
* Decode a packet String (JSON data)
|
||||
*
|
||||
* @param {String} str
|
||||
* @param {Number} maxAttachments - the maximum number of binary attachments
|
||||
* @return {Object} packet
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function decodeString(str) {
|
||||
function decodeString(str, maxAttachments) {
|
||||
var i = 0;
|
||||
// look up type
|
||||
var p = {
|
||||
@@ -295,7 +300,13 @@ function decodeString(str) {
|
||||
if (buf != Number(buf) || str.charAt(i) !== '-') {
|
||||
throw new Error('Illegal attachments');
|
||||
}
|
||||
p.attachments = Number(buf);
|
||||
var n = Number(buf);
|
||||
if (!isInteger(n) || n < 0) {
|
||||
throw new Error("Illegal attachments");
|
||||
} else if (n > maxAttachments) {
|
||||
throw new Error("too many attachments");
|
||||
}
|
||||
p.attachments = n;
|
||||
}
|
||||
|
||||
// look up namespace (if any)
|
||||
@@ -432,3 +443,13 @@ function error(msg) {
|
||||
data: 'parser error: ' + msg
|
||||
};
|
||||
}
|
||||
|
||||
var isInteger =
|
||||
Number.isInteger ||
|
||||
function (value) {
|
||||
return (
|
||||
typeof value === "number" &&
|
||||
isFinite(value) &&
|
||||
Math.floor(value) === value
|
||||
);
|
||||
};
|
||||
|
||||
@@ -101,5 +101,26 @@ describe('parser', function(){
|
||||
isInvalidPayload('2[{"toString":"foo"}]');
|
||||
isInvalidPayload('2[true,"foo"]');
|
||||
isInvalidPayload('2[null,"bar"]');
|
||||
|
||||
function isInvalidAttachmentCount (str) {
|
||||
expect(() => new parser.Decoder().add(str)).to.throwException(
|
||||
/^Illegal attachments$/,
|
||||
);
|
||||
}
|
||||
|
||||
isInvalidAttachmentCount("5");
|
||||
isInvalidAttachmentCount("51");
|
||||
isInvalidAttachmentCount("5a-");
|
||||
isInvalidAttachmentCount("51.23-");
|
||||
});
|
||||
|
||||
it("throws an error when receiving too many attachments", () => {
|
||||
const decoder = new parser.Decoder({ maxAttachments: 2 });
|
||||
|
||||
expect(() => {
|
||||
decoder.add(
|
||||
'53-["hello",{"_placeholder":true,"num":0},{"_placeholder":true,"num":1},{"_placeholder":true,"num":2}]',
|
||||
);
|
||||
}).to.throwException(/^too many attachments$/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user