Skip to content

Commit 06af813

Browse files
clarkttfudaprahamian
authored andcommitted
fix(ObjectID): ObjectId.isValid should check buffer length
1 parent a767fa1 commit 06af813

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/objectid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class ObjectId {
328328
return true;
329329
}
330330

331-
if (id instanceof _Buffer) {
331+
if (id instanceof _Buffer && id.length === 12) {
332332
return true;
333333
}
334334

test/node/object_id_tests.js

+14
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ describe('ObjectId', function() {
8080

8181
done();
8282
});
83+
84+
/**
85+
* @ignore
86+
*/
87+
it('should isValid check input Buffer length', function(done) {
88+
var buffTooShort = new Buffer ([]);
89+
var buffTooLong = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
90+
var buff12Bytes = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
91+
92+
expect(ObjectId.isValid(buffTooShort)).to.be.false;
93+
expect(ObjectId.isValid(buffTooLong)).to.be.false;
94+
expect(ObjectId.isValid(buff12Bytes)).to.be.true;
95+
done();
96+
});
8397
});

0 commit comments

Comments
 (0)