-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(NODE-3627): Enable flexible BSON validation for server error key containing invalid utf-8 #3054
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bc0368f
feat(NODE-3627) Enable flexible bson validation for server error key …
4591622
chore: use bson 4.6.0
745bf66
refactor: fix misspelled key and add default validation setting and t…
05de41c
test: add test to make sure only writeErrors key has validation setti…
77c83f6
test: limit it statements to one assertion and use contexts for organ…
169ad84
refactor: refactor to keep change internal by removing option from BS…
b692baf
chore: fix type
1eb8584
address comments, but broke tests (wip)
nbbeeken 393b122
fix: tests
nbbeeken 447da06
fix: lint
nbbeeken 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,136 @@ | ||
import { expect } from 'chai'; | ||
import { BinMsg, MessageHeader } from '../../src/cmap/commands'; | ||
import { BSONError } from 'bson'; | ||
import * as BSON from '../../src/bson'; | ||
|
||
const msgHeader: MessageHeader = { | ||
length: 735, | ||
requestId: 14704565, | ||
responseTo: 4, | ||
opCode: 2013 | ||
}; | ||
|
||
// when top-level key writeErrors contains an error message that has invalid utf8 | ||
const invalidUtf8ErrorMsg = | ||
'0000000000ca020000106e00000000000477726974654572726f727300a50200000330009d02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f1000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e2982e2e2e22207d000000016f6b00000000000000f03f00'; | ||
const msgBodyInvalidUtf8WriteErrors = Buffer.from(invalidUtf8ErrorMsg, 'hex'); | ||
const invalidUtf8ErrorMsgDeserializeInput = Buffer.from(invalidUtf8ErrorMsg.substring(10), 'hex'); | ||
const invalidUtf8InWriteErrorsJSON = { | ||
n: 0, | ||
writeErrors: [ | ||
{ | ||
index: 0, | ||
code: 11000, | ||
keyPattern: { | ||
text: 1 | ||
}, | ||
keyValue: { | ||
text: 'd☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃' | ||
}, | ||
errmsg: | ||
'E11000 duplicate key error collection: bigdata.test index: text_1 dup key: { text: "d☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃�..." }' | ||
} | ||
], | ||
ok: 1 | ||
}; | ||
|
||
// when another top-level key besides writeErrors has invalid utf8 | ||
const nKeyWithInvalidUtf8 = | ||
'0000000000cc020000026e0005000000f09f98ff000477726974654572726f727300a60200000330009e02000010696e646578000000000010636f646500f82a0000036b65795061747465726e000f0000001074657874000100000000036b657956616c756500610100000274657874005201000064e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e298830000026572726d736700f2000000453131303030206475706c6963617465206b6579206572726f7220636f6c6c656374696f6e3a20626967646174612e7465737420696e6465783a20746578745f3120647570206b65793a207b20746578743a202264e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883e29883efbfbd2e2e2e22207d000000106f6b000100000000'; | ||
const nKeyWithInvalidUtf8DeserializeInput = Buffer.from(nKeyWithInvalidUtf8.substring(10), 'hex'); | ||
const msgBodyNKeyWithInvalidUtf8 = Buffer.from(nKeyWithInvalidUtf8, 'hex'); | ||
const invalidUtf8InNKeyJSON = { | ||
n: '��', | ||
writeErrors: [ | ||
{ | ||
index: 0, | ||
code: 11000, | ||
keyPattern: { | ||
text: 1 | ||
}, | ||
keyValue: { | ||
text: 'd☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃' | ||
}, | ||
errmsg: | ||
'E11000 duplicate key error collection: bigdata.test index: text_1 dup key: { text: "d☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃�..." }' | ||
} | ||
], | ||
ok: 1 | ||
}; | ||
|
||
describe('BinMsg BSON utf8 validation', () => { | ||
context('when validation is disabled for writeErrors', () => { | ||
const binMsgInvalidUtf8ErrorMsg = new BinMsg( | ||
Buffer.alloc(0), | ||
msgHeader, | ||
msgBodyInvalidUtf8WriteErrors | ||
); | ||
const options = { validation: { utf8: { writeErrors: false } as const } }; | ||
|
||
it('contains replacement characters for invalid utf8 in writeError object', () => { | ||
expect(BSON.deserialize(invalidUtf8ErrorMsgDeserializeInput, options)).to.deep.equals( | ||
invalidUtf8InWriteErrorsJSON | ||
); | ||
}); | ||
|
||
it('should not throw invalid utf8 error', () => { | ||
expect(() => binMsgInvalidUtf8ErrorMsg.parse(options)).to.not.throw(); | ||
}); | ||
}); | ||
|
||
it('should by default disable validation for writeErrors if no validation specified', () => { | ||
const binMsgInvalidUtf8ErrorMsg = new BinMsg( | ||
Buffer.alloc(0), | ||
msgHeader, | ||
msgBodyInvalidUtf8WriteErrors | ||
); | ||
const options = { | ||
bsonRegExp: false, | ||
promoteBuffers: false, | ||
promoteLongs: true, | ||
promoteValues: true | ||
}; | ||
expect(() => binMsgInvalidUtf8ErrorMsg.parse(options)).to.not.throw(); | ||
}); | ||
|
||
context('when another key has invalid utf8 and validation is enabled for writeErrors', () => { | ||
const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg( | ||
Buffer.alloc(0), | ||
msgHeader, | ||
msgBodyNKeyWithInvalidUtf8 | ||
); | ||
const options = { validation: { utf8: { writeErrors: true } as const } }; | ||
|
||
it('should not throw invalid utf8 error', () => { | ||
expect(() => binMsgAnotherKeyWithInvalidUtf8.parse(options)).to.not.throw(); | ||
}); | ||
|
||
it('contains replacement characters for invalid utf8 key', () => { | ||
expect(BSON.deserialize(nKeyWithInvalidUtf8DeserializeInput, options)).to.deep.equals( | ||
invalidUtf8InNKeyJSON | ||
); | ||
}); | ||
}); | ||
|
||
it('should throw invalid utf8 error when validation enabled for writeErrors', () => { | ||
const binMsgInvalidUtf8ErrorMsg = new BinMsg( | ||
Buffer.alloc(0), | ||
msgHeader, | ||
msgBodyInvalidUtf8WriteErrors | ||
); | ||
expect(() => | ||
binMsgInvalidUtf8ErrorMsg.parse({ validation: { utf8: { writeErrors: true } } }) | ||
).to.throw(BSONError, 'Invalid UTF-8 string in BSON document'); | ||
}); | ||
|
||
it('should throw error when another key has invalid utf8 and writeErrors is not validated', () => { | ||
const binMsgAnotherKeyWithInvalidUtf8 = new BinMsg( | ||
Buffer.alloc(0), | ||
msgHeader, | ||
msgBodyNKeyWithInvalidUtf8 | ||
); | ||
expect(() => | ||
binMsgAnotherKeyWithInvalidUtf8.parse({ validation: { utf8: { writeErrors: false } } }) | ||
).to.throw(BSONError, 'Invalid UTF-8 string in BSON document'); | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.