Skip to content

Commit be49fe2

Browse files
committed
fix(NODE-7606): restore non-string Map key guard in serializer
Commit a22387a dropped the `typeof key === 'string'` guard from the key validation block on the assumption that keys are always strings. That holds for plain objects and arrays, but Map keys can be non-string. Without the guard, a non-string Map key with checkKeys:true reached `key.includes('.')` and threw a TypeError from a different site than the baseline. Restoring the guard makes behavior byte-identical to the pre-regression implementation: non-string Map keys (unsupported by design, NODE-3027) fail in encodeUTF8Into exactly as before, regardless of checkKeys. The Set lookup optimization is unaffected — it remains gated by `key[0] === '$'`.
1 parent 9983d22 commit be49fe2

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

src/parser/serializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ export function serializeInto(
587587
value = value.toBSON();
588588
}
589589

590-
if (!frame.isArray && !(key[0] === '$' && ignoreKeys.has(key))) {
590+
if (!frame.isArray && typeof key === 'string' && !(key[0] === '$' && ignoreKeys.has(key))) {
591591
if (regexp.test(key)) {
592592
throw new BSONError('key ' + key + ' must not contain null bytes');
593593
}

0 commit comments

Comments
 (0)