Skip to content

Commit

Permalink
fix(object-id): harden the duck-typing
Browse files Browse the repository at this point in the history
The insufficient validation may otherwise lead to type confusions.

REF: NODE-2618
Signed-off-by: Jakob Ackermann <jakob.ackermann@overleaf.com>
  • Loading branch information
das7pad committed Aug 9, 2020
1 parent 29aa89c commit 4b800ae
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class ObjectId {
} else if (id != null && id.length === 12) {
// assume 12 byte string
this.id = id;
} else if (id != null && id.toHexString) {
} else if (id != null && typeof id.toHexString === 'function') {
// Duck-typing to support ObjectId from different npm packages
return ObjectId.createFromHexString(id.toHexString());
} else {
Expand Down Expand Up @@ -338,7 +338,10 @@ class ObjectId {
}

// Duck-Typing detection of ObjectId like objects
if (id.toHexString) {
if (
typeof id.toHexString === 'function' &&
(id.id instanceof _Buffer || typeof id.id === 'string')
) {
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id));
}

Expand Down

0 comments on commit 4b800ae

Please sign in to comment.