Skip to content

Fix decoding of varints with extra padding bytes #1380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,16 @@ Reader.prototype.uint32 = (function read_uint32_setup() {
value = (value | (this.buf[this.pos] & 127) << 21) >>> 0; if (this.buf[this.pos++] < 128) return value;
value = (value | (this.buf[this.pos] & 15) << 28) >>> 0; if (this.buf[this.pos++] < 128) return value;

if (this.buf[this.pos++] >= 128
&& this.buf[this.pos++] >= 128
&& this.buf[this.pos++] >= 128
&& this.buf[this.pos++] >= 128
&& this.buf[this.pos++] >= 128) {
throw Error("varint too long");
}

/* istanbul ignore if */
if ((this.pos += 5) > this.len) {
if (this.pos > this.len) {
this.pos = this.len;
throw indexOutOfRange(this, 10);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/comp_int32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var tape = require("tape");

var protobuf = require("..");

tape.test("int32 values with padding", function(test) {
// 12345 encoded as a varint with 7 extra 0 bytes on the end
var buf = Uint8Array.from([185, 224, 128, 128, 128, 128, 128, 128, 0]);

var reader = protobuf.Reader.create(buf);

test.equal(reader.int32(), 12345, "should decode padded varint");

test.equal(reader.pos, 9, "should have consumed the entire test buffer");

test.end();
});