Skip to content

Commit ddd6998

Browse files
test (2)
1 parent 11a15d2 commit ddd6998

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

packages/socket.io-parser/lib/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ type JSONReviver = (this: any, key: string, value: any) => any;
139139

140140
export interface DecoderOptions {
141141
/**
142-
* Custom reviver to pass down to JSON.stringify()
142+
* Custom reviver to pass down to JSON.parse()
143143
*/
144144
reviver?: JSONReviver;
145145
/**
@@ -160,15 +160,16 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
160160

161161
/**
162162
* Decoder constructor
163-
*
164-
* @param {function|DecoderOptions} opts - custom reviver to pass down to JSON.stringify or an options object
165163
*/
166164
constructor(opts?: DecoderOptions | JSONReviver) {
167165
super();
168-
this.opts = Object.assign({
169-
reviver: undefined,
170-
maxAttachments: 3
171-
}, typeof opts === "function" ? { reviver: opts } : opts);
166+
this.opts = Object.assign(
167+
{
168+
reviver: undefined,
169+
maxAttachments: 3,
170+
},
171+
typeof opts === "function" ? { reviver: opts } : opts,
172+
);
172173
}
173174

174175
/**
@@ -244,7 +245,9 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
244245
throw new Error("Illegal attachments");
245246
}
246247
p.attachments = Number(buf);
247-
if (p.attachments > this.opts.maxAttachments) {
248+
if (!isInteger(p.attachments) || p.attachments < 0) {
249+
throw new Error("Illegal attachments");
250+
} else if (p.attachments > this.opts.maxAttachments) {
248251
throw new Error("too many attachments");
249252
}
250253
}

packages/socket.io-parser/test/parser.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ describe("socket.io-parser", () => {
171171
isInvalidPayload('2["connect"]');
172172
isInvalidPayload('2["disconnect","123"]');
173173

174+
const isInvalidAttachmentCount = (str) =>
175+
expect(() => new Decoder().add(str)).to.throwException(
176+
/^Illegal attachments$/,
177+
);
178+
179+
isInvalidAttachmentCount("5");
180+
isInvalidAttachmentCount("51");
181+
isInvalidAttachmentCount("5a-");
182+
isInvalidAttachmentCount("51.23-");
183+
174184
expect(() => new Decoder().add("999")).to.throwException(
175185
/^unknown packet type 9$/,
176186
);

0 commit comments

Comments
 (0)