Skip to content

Commit

Permalink
fix(ObjectID): ObjectId.isValid should check buffer length
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkttfu authored and daprahamian committed Nov 2, 2018
1 parent a767fa1 commit 06af813
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class ObjectId {
return true;
}

if (id instanceof _Buffer) {
if (id instanceof _Buffer && id.length === 12) {
return true;
}

Expand Down
14 changes: 14 additions & 0 deletions test/node/object_id_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,18 @@ describe('ObjectId', function() {

done();
});

/**
* @ignore
*/
it('should isValid check input Buffer length', function(done) {
var buffTooShort = new Buffer ([]);
var buffTooLong = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
var buff12Bytes = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);

expect(ObjectId.isValid(buffTooShort)).to.be.false;
expect(ObjectId.isValid(buffTooLong)).to.be.false;
expect(ObjectId.isValid(buff12Bytes)).to.be.true;
done();
});
});

0 comments on commit 06af813

Please sign in to comment.