-
Notifications
You must be signed in to change notification settings - Fork 258
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
Merged
+103
−37
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6af24ad
feat!(NODE-4410): only enumerate own properties
nbbeeken 8ed01a8
test: improvement
nbbeeken 6e8c5c4
test: fixup
nbbeeken 6a21f28
test: use json parse
nbbeeken 54c6881
docs: update
nbbeeken a66d037
Merge branch 'main' into NODE-4410-no-proto-keys
nbbeeken 465b16e
Merge branch 'main' into NODE-4410-no-proto-keys
baileympearson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]) | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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
test/node/parser/deserializer.test.ts
. If the global object prototype has been polluted with unexpected keys they can control the behavior of the BSON library without actually needing access to the options object a user passes in.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.
I see. This impacts all other options too, no?
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.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me. I left a comment about the test below