Skip to content

Commit f1d5727

Browse files
Merge commit from fork
* fix: limit depth of recursion in Reader.prototype.skipType * fix: use Reader.skipTypeMaxDepth as a limit
1 parent 9dd7e8c commit f1d5727

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/reader.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,19 @@ Reader.prototype.skip = function skip(length) {
352352
return this;
353353
};
354354

355+
// Recursion depth limit in skipType
356+
Reader.skipTypeMaxDepth = 100;
357+
355358
/**
356359
* Skips the next element of the specified wire type.
357360
* @param {number} wireType Wire type received
361+
* @param {number} [depth] Depth of recursion to control nested calls; 0 if omitted
358362
* @returns {Reader} `this`
359363
*/
360-
Reader.prototype.skipType = function(wireType) {
364+
Reader.prototype.skipType = function(wireType, depth) {
365+
if (depth === undefined) depth = 0;
366+
if (depth > Reader.skipTypeMaxDepth)
367+
throw Error("maximum nesting depth exceeded");
361368
switch (wireType) {
362369
case 0:
363370
this.skip();
@@ -370,7 +377,7 @@ Reader.prototype.skipType = function(wireType) {
370377
break;
371378
case 3:
372379
while ((wireType = this.uint32() & 7) !== 4) {
373-
this.skipType(wireType);
380+
this.skipType(wireType, depth + 1);
374381
}
375382
break;
376383
case 5:

tests/api_writer-reader.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ tape.test("writer & reader", function(test) {
129129
test.end();
130130
});
131131

132+
test.throws(function() {
133+
const root = protobuf.Root.fromJSON({
134+
nested: {
135+
MyMessage: {
136+
fields: {
137+
name: { type: "string", id: 1 }
138+
}
139+
}
140+
}
141+
});
142+
const MyMessage = root.lookupType("MyMessage");
143+
// 0x7B (field 15, wire type 3 = start group)
144+
const payload = Buffer.alloc(50000, 0x7B);
145+
MyMessage.decode(payload);
146+
}, /maximum nesting depth exceeded/, "limits recursion in reader");
147+
132148
test.end();
133149
});
134150

@@ -178,4 +194,4 @@ function expect(type, value, expected, WriterToTest) {
178194
}
179195
}
180196
return true;
181-
}
197+
}

0 commit comments

Comments
 (0)