-
Notifications
You must be signed in to change notification settings - Fork 260
feat!(NODE-4410): only enumerate own properties #527
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6af24ad
8ed01a8
6e8c5c4
6a21f28
54c6881
a66d037
465b16e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,23 +314,16 @@ function deserializeObject( | |
(buffer[index + 1] << 8) | | ||
(buffer[index + 2] << 16) | | ||
(buffer[index + 3] << 24); | ||
let arrayOptions = options; | ||
let arrayOptions: DeserializeOptions = options; | ||
|
||
// Stop index | ||
const stopIndex = index + objectSize; | ||
|
||
// All elements of array to be returned as raw bson | ||
if (fieldsAsRaw && fieldsAsRaw[name]) { | ||
arrayOptions = {}; | ||
for (const n in options) { | ||
( | ||
arrayOptions as { | ||
[key: string]: DeserializeOptions[keyof DeserializeOptions]; | ||
} | ||
)[n] = options[n as keyof DeserializeOptions]; | ||
} | ||
arrayOptions['raw'] = true; | ||
arrayOptions = { ...options, raw: true }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a change we want to make? iiuc, this line of code copies the options before recursing (presumably so we're not mutating a shared options object in recursive deserialize calls). The for-in loop here is only used to copy the options object, not the document being deserialized. My understanding of the intention behind this change is that when serializing objects, we only want to serialize own properties on the object so we don't pull in keys from the prototype of the object. This seems unrelated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue is related because it has similar surprising side effects when there are keys that you didn't specify yourself effecting the outcomes of your deserialize calls. I tried demonstrating the possible buggy behavior in the test in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. This impacts all other options too, no? import { deserialize, serialize } from "./src/bson";
const bytes = serialize({ someKey: {
nested: "value"
} });
const opts = {}
Object.setPrototypeOf(opts, { raw: true });
const result = deserialize(bytes, opts);
console.log(result);
// { someKey: <Buffer 17 00 00 00 02 6e 65 73 74 65 64 00 06 00 00 00 76 61 6c 75 65 00 00> } It seems like what we'd want to do is only look for own properties on the options objects too when accessing individual fields (not in scope for this work). My 2-cents would be to remove this from this PR and file a follow up to only consider options that are set as own properties (probably an easy fix by making a shallow clone of the options before we begin any work deserializing / serializing). If you want to leave this in this PR, that's also okay because technically the scope of the ticket is "Use Object.keys to enumerate keys on a JS object passed to BSON", and this is an instance where we're using a for-in loop. If we leave this work in this PR, we should still file a follow up ticket I think. And I'll comment below, but I think we can make the test easier to understand. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep the fix here, but I agree about following up. And yea happy to improve the test, let me know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me. I left a comment about the test below |
||
} | ||
|
||
if (!globalUTFValidation) { | ||
arrayOptions = { ...arrayOptions, validation: { utf8: shouldValidateKey } }; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as BSON from '../../register-bson'; | ||
import { expect } from 'chai'; | ||
|
||
describe('calculateSize()', () => { | ||
it('should only enumerate own property keys from input objects', () => { | ||
const input = { a: 1 }; | ||
Object.setPrototypeOf(input, { b: 2 }); | ||
expect(BSON.calculateObjectSize(input)).to.equal(12); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as BSON from '../../register-bson'; | ||
import { expect } from 'chai'; | ||
|
||
describe('deserializer()', () => { | ||
describe('when the fieldsAsRaw options is present and has a value that corresponds to a key in the object', () => { | ||
it('ignores non-own properties set on the options object', () => { | ||
const bytes = BSON.serialize({ someKey: [1] }); | ||
const options = { fieldsAsRaw: { someKey: true } }; | ||
Object.setPrototypeOf(options, { promoteValues: false }); | ||
const result = BSON.deserialize(bytes, options); | ||
expect(result).to.have.property('someKey').that.is.an('array'); | ||
expect( | ||
result.someKey[0], | ||
'expected promoteValues option set on options object prototype to be ignored, but it was not' | ||
).to.not.have.property('_bsontype', 'Int32'); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as BSON from '../../register-bson'; | ||
import { bufferFromHexArray } from '../tools/utils'; | ||
import { expect } from 'chai'; | ||
|
||
describe('serialize()', () => { | ||
it('should only enumerate own property keys from input objects', () => { | ||
const input = { a: 1 }; | ||
Object.setPrototypeOf(input, { b: 2 }); | ||
const bytes = BSON.serialize(input); | ||
expect(bytes).to.deep.equal( | ||
bufferFromHexArray([ | ||
'106100', // type int32, a\x00 | ||
'01000000' // int32LE = 1 | ||
]) | ||
); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated with just a bit of rephrasing