socket.io-parser
4.2.44.2.6
build/cjs/index.js~
build/cjs/index.jsModified+62−16
Index: package/build/cjs/index.js
===================================================================
--- package/build/cjs/index.js
+++ package/build/cjs/index.js
@@ -1,7 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Decoder = exports.Encoder = exports.PacketType = exports.protocol = void 0;
+exports.isPacketValid = isPacketValid;
const component_emitter_1 = require("@socket.io/component-emitter");
const binary_js_1 = require("./binary.js");
const is_binary_js_1 = require("./is-binary.js");
const debug_1 = require("debug"); // debug()
@@ -9,13 +10,13 @@
/**
* These strings must not be used as event names, as they have a special meaning.
*/
const RESERVED_EVENTS = [
- "connect",
- "connect_error",
- "disconnect",
- "disconnecting",
- "newListener",
+ "connect", // used on the client side
+ "connect_error", // used on the client side
+ "disconnect", // used on both sides
+ "disconnecting", // used on the server side
+ "newListener", // used by the Node.js EventEmitter
"removeListener", // used by the Node.js EventEmitter
];
/**
* Protocol version.
@@ -31,9 +32,9 @@
PacketType[PacketType["ACK"] = 3] = "ACK";
PacketType[PacketType["CONNECT_ERROR"] = 4] = "CONNECT_ERROR";
PacketType[PacketType["BINARY_EVENT"] = 5] = "BINARY_EVENT";
PacketType[PacketType["BINARY_ACK"] = 6] = "BINARY_ACK";
-})(PacketType = exports.PacketType || (exports.PacketType = {}));
+})(PacketType || (exports.PacketType = PacketType = {}));
/**
* A socket.io Encoder instance
*/
class Encoder {
@@ -107,26 +108,23 @@
return buffers; // write all the buffers
}
}
exports.Encoder = Encoder;
-// see https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
-function isObject(value) {
- return Object.prototype.toString.call(value) === "[object Object]";
-}
/**
* A socket.io Decoder instance
*
* @return {Object} decoder
*/
class Decoder extends component_emitter_1.Emitter {
/**
* Decoder constructor
- *
- * @param {function} reviver - custom reviver to pass down to JSON.stringify
*/
- constructor(reviver) {
+ constructor(opts) {
super();
- this.reviver = reviver;
+ this.opts = Object.assign({
+ reviver: undefined,
+ maxAttachments: 10,
+ }, typeof opts === "function" ? { reviver: opts } : opts);
}
/**
* Decodes an encoded packet string into packet JSON.
*
@@ -195,9 +193,16 @@
const buf = str.substring(start, i);
if (buf != Number(buf) || str.charAt(i) !== "-") {
throw new Error("Illegal attachments");
}
- p.attachments = Number(buf);
+ const n = Number(buf);
+ if (!isInteger(n) || n < 0) {
+ throw new Error("Illegal attachments");
+ }
+ else if (n > this.opts.maxAttachments) {
+ throw new Error("too many attachments");
+ }
+ p.attachments = n;
}
// look up namespace (if any)
if ("/" === str.charAt(i + 1)) {
const start = i + 1;
@@ -242,9 +247,9 @@
return p;
}
tryParse(str) {
try {
- return JSON.parse(str, this.reviver);
+ return JSON.parse(str, this.opts.reviver);
}
catch (e) {
return false;
}
@@ -318,4 +323,45 @@
this.reconPack = null;
this.buffers = [];
}
}
+function isNamespaceValid(nsp) {
+ return typeof nsp === "string";
+}
+// see https://caniuse.com/mdn-javascript_builtins_number_isinteger
+const isInteger = Number.isInteger ||
+ function (value) {
+ return (typeof value === "number" &&
+ isFinite(value) &&
+ Math.floor(value) === value);
+ };
+function isAckIdValid(id) {
+ return id === undefined || isInteger(id);
+}
+// see https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
+function isObject(value) {
+ return Object.prototype.toString.call(value) === "[object Object]";
+}
+function isDataValid(type, payload) {
+ switch (type) {
+ case PacketType.CONNECT:
+ return payload === undefined || isObject(payload);
+ case PacketType.DISCONNECT:
+ return payload === undefined;
+ case PacketType.EVENT:
+ return (Array.isArray(payload) &&
+ (typeof payload[0] === "number" ||
+ (typeof payload[0] === "string" &&
+ RESERVED_EVENTS.indexOf(payload[0]) === -1)));
+ case PacketType.ACK:
+ return Array.isArray(payload);
+ case PacketType.CONNECT_ERROR:
+ return typeof payload === "string" || isObject(payload);
+ default:
+ return false;
+ }
+}
+function isPacketValid(packet) {
+ return (isNamespaceValid(packet.nsp) &&
+ isAckIdValid(packet.id) &&
+ isDataValid(packet.type, packet.data));
+}