-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(deserialize): fix deserialization of 0xFFFD
When deserializing a string, we previously relied on the appearance of 0xFFFD to denote an invalid unicode character. However, 0xFFFD is a valid unicode character if that is what was originally input. Fixes NODE-1718 Fixes #277
- Loading branch information
1 parent
06af813
commit c682ae3
Showing
3 changed files
with
65 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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,48 @@ | ||
'use strict'; | ||
|
||
const FIRST_BIT = 0x80; | ||
const FIRST_TWO_BITS = 0xc0; | ||
const FIRST_THREE_BITS = 0xe0; | ||
const FIRST_FOUR_BITS = 0xf0; | ||
const FIRST_FIVE_BITS = 0xf8; | ||
|
||
const TWO_BIT_CHAR = 0xc0; | ||
const THREE_BIT_CHAR = 0xe0; | ||
const FOUR_BIT_CHAR = 0xf0; | ||
const CONTINUING_CHAR = 0x80; | ||
|
||
/** | ||
* Determines if the passed in bytes are valid utf8 | ||
* @param {Buffer|Uint8Array} bytes An array of 8-bit bytes. Must be indexable and have length property | ||
* @param {Number} start The index to start validating | ||
* @param {Number} end The index to end validating | ||
* @returns {boolean} True if valid utf8 | ||
*/ | ||
function validateUtf8(bytes, start, end) { | ||
let continuation = 0; | ||
|
||
for (let i = start; i < end; i += 1) { | ||
const byte = bytes[i]; | ||
|
||
if (continuation) { | ||
if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) { | ||
return false; | ||
} | ||
continuation -= 1; | ||
} else if (byte & FIRST_BIT) { | ||
if ((byte & FIRST_THREE_BITS) === TWO_BIT_CHAR) { | ||
continuation = 1; | ||
} else if ((byte & FIRST_FOUR_BITS) === THREE_BIT_CHAR) { | ||
continuation = 2; | ||
} else if ((byte & FIRST_FIVE_BITS) === FOUR_BIT_CHAR) { | ||
continuation = 3; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return !continuation; | ||
} | ||
|
||
module.exports.validateUtf8 = validateUtf8; |
This file contains 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,12 @@ | ||
'use strict'; | ||
|
||
const BSON = require('../../lib/bson'); | ||
|
||
describe('string tests', function() { | ||
it('can serialize and deserialize 0xFFFD', function() { | ||
const unicodeString = String.fromCharCode(0x41, 0x42, 0xfffd, 0x43, 0x44); // "AB�CD" | ||
|
||
const serialized = BSON.serialize({ value: unicodeString }); | ||
BSON.deserialize(serialized); | ||
}); | ||
}); |