Skip to content

Commit 06f6bca

Browse files
committed
fix: prevent js objects that are typically values
1 parent 2a5ed28 commit 06f6bca

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/parser/serializer.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { ObjectId } from '../objectid';
1414
import type { BSONRegExp } from '../regexp';
1515
import { ByteUtils } from '../utils/byte_utils';
1616
import {
17+
isAnyArrayBuffer,
1718
isBigInt64Array,
1819
isBigUInt64Array,
1920
isDate,
@@ -648,6 +649,13 @@ export function serializeInto(
648649
throw new BSONError('serialize does not support non-object as the root input');
649650
} else if ('_bsontype' in object && typeof object._bsontype === 'string') {
650651
throw new BSONError(`BSON types cannot be serialized as a document`);
652+
} else if (
653+
isDate(object) ||
654+
isRegExp(object) ||
655+
isUint8Array(object) ||
656+
isAnyArrayBuffer(object)
657+
) {
658+
throw new BSONError(`date, regexp, typedarray, and arraybuffer cannot be BSON documents`);
651659
}
652660

653661
path = new Set();

test/node/parser/serializer.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,13 @@ describe('serialize()', () => {
8080
})
8181
).to.throw(/does not support non-object/);
8282
});
83+
84+
it('does not permit certain objects that are typically values as the root input', () => {
85+
expect(() => BSON.serialize(new Date())).to.throw(/cannot be BSON documents/);
86+
expect(() => BSON.serialize(/a/)).to.throw(/cannot be BSON documents/);
87+
expect(() => BSON.serialize(new ArrayBuffer(2))).to.throw(/cannot be BSON documents/);
88+
expect(() => BSON.serialize(Buffer.alloc(2))).to.throw(/cannot be BSON documents/);
89+
expect(() => BSON.serialize(new Uint8Array(3))).to.throw(/cannot be BSON documents/);
90+
});
8391
});
8492
});

0 commit comments

Comments
 (0)