Skip to content

Commit 7e7e48a

Browse files
lerouxbaddaleax
andauthored
fix: don't crash when using bson-equal to compare against null COMPASS-9924 (#7423)
* don't crash when using bson-equal to compare against null * Update packages/mongodb-query-util/src/bson-equal.ts Co-authored-by: Anna Henningsen <[email protected]> --------- Co-authored-by: Anna Henningsen <[email protected]>
1 parent 538e70e commit 7e7e48a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/mongodb-query-util/src/bson-equal.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,42 @@ describe('#bsonEqual', function () {
8181
expect(bsonEqual(first, second)).to.equal(false);
8282
});
8383
});
84+
85+
context('when the other value is a null', function () {
86+
const first = new Int32(12);
87+
const second = null;
88+
89+
it('returns undefined', function () {
90+
expect(bsonEqual(first, second)).to.equal(undefined);
91+
});
92+
});
93+
94+
context('when the other value is undefined', function () {
95+
const first = new Int32(12);
96+
const second = undefined;
97+
98+
it('returns undefined', function () {
99+
expect(bsonEqual(first, second)).to.equal(undefined);
100+
});
101+
});
102+
103+
context('when the other value has no _bsontype', function () {
104+
const first = new Int32(12);
105+
const second = {};
106+
107+
it('returns undefined', function () {
108+
expect(bsonEqual(first, second)).to.equal(undefined);
109+
});
110+
});
111+
112+
context("when the other value's _bsontype is not a string", function () {
113+
const first = new Int32(12);
114+
const second = { _bsontype: 12 };
115+
116+
it('returns undefined', function () {
117+
expect(bsonEqual(first, second)).to.equal(undefined);
118+
});
119+
});
84120
});
85121

86122
context('when the value is a double', function () {

packages/mongodb-query-util/src/bson-equal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export const bsonEqual = (value: any, other: any): boolean | undefined => {
99
return undefined;
1010
}
1111

12+
if (typeof other?._bsontype !== 'string') {
13+
return undefined;
14+
}
15+
1216
if (bsontype === 'ObjectId') {
1317
return (value as ObjectId).equals(other);
1418
}

0 commit comments

Comments
 (0)