Skip to content

Commit 4b800ae

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 29aa89c commit 4b800ae

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lib/objectid.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ObjectId {
8585
} else if (id != null && id.length === 12) {
8686
// assume 12 byte string
8787
this.id = id;
88-
} else if (id != null && id.toHexString) {
88+
} else if (id != null && typeof id.toHexString === 'function') {
8989
// Duck-typing to support ObjectId from different npm packages
9090
return ObjectId.createFromHexString(id.toHexString());
9191
} else {
@@ -338,7 +338,10 @@ class ObjectId {
338338
}
339339

340340
// Duck-Typing detection of ObjectId like objects
341-
if (id.toHexString) {
341+
if (
342+
typeof id.toHexString === 'function' &&
343+
(id.id instanceof _Buffer || typeof id.id === 'string')
344+
) {
342345
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id));
343346
}
344347

0 commit comments

Comments
 (0)