Skip to content

Commit b526145

Browse files
authored
fix(object-id): harden the duck-typing
The insufficient validation may otherwise lead to type confusions. REF: NODE-2618 Signed-off-by: Jakob Ackermann <[email protected]>
1 parent 6e782da commit b526145

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/bson/objectid.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ var ObjectID = function ObjectID(id) {
6666
} else if (id != null && id.length === 12) {
6767
// assume 12 byte string
6868
this.id = id;
69-
} else if (id != null && id.toHexString) {
69+
} else if (id != null && typeof id.toHexString === 'function') {
7070
// Duck-typing to support ObjectId from different npm packages
7171
return id;
7272
} else {
@@ -357,7 +357,10 @@ ObjectID.isValid = function isValid(id) {
357357
}
358358

359359
// Duck-Typing detection of ObjectId like objects
360-
if (id.toHexString) {
360+
if (
361+
typeof id.toHexString === 'function' &&
362+
(id.id instanceof _Buffer || typeof id.id === 'string')
363+
) {
361364
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id));
362365
}
363366

0 commit comments

Comments
 (0)