Skip to content

Commit

Permalink
fix(dbPointer): fix utf8 bug for dbPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
daprahamian committed Nov 6, 2018
1 parent 6f30b4e commit 018c769
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/parser/deserializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ function deserializeObject(buffer, index, options, isArray) {
)
throw new Error('bad string length in bson');
// Namespace
if (!validateUtf8(buffer, index, index + stringSize - 1)) {
throw new Error('Invalid UTF-8 string in BSON document');
}
const namespace = buffer.toString('utf8', index, index + stringSize - 1);
// Update parse index position
index = index + stringSize;
Expand All @@ -558,11 +561,6 @@ function deserializeObject(buffer, index, options, isArray) {

// Update the index
index = index + 12;
for (i = 0; i < namespace.length; i++) {
if (namespace.charCodeAt(i) === 0xfffd) {
throw new Error('Invalid UTF-8 string in BSON document');
}
}

// Upgrade to DBRef type
object[name] = new DBRef(namespace, oid);
Expand Down
58 changes: 58 additions & 0 deletions test/node/db_pointer_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';

const BSON = require('../../lib/bson');
const expect = require('chai').expect;

// 0x0C foo\0 \0\0\07 String.fromCharCode(0x41, 0x42, 0xfffd, 0x43, 0x44) 12
const bsonSnippet = Buffer.from([
// Size
34,
0,
0,
0,
// BSON type for DBPointer
0x0c,

// CString Label Foo
0x66,
0x6f,
0x6f,
0,

// Length of UTF8 string "AB�CD"
8,
0,
0,
0,
0x41,
0x42,
0xef,
0xbf,
0xbd,
0x43,
0x44,
0,

// 12-bit pointer
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,

// null terminator
0
]);

describe('dbpointer tests', function() {
it('can serialize and deserialize 0xFFFD in dbpointer name', function() {
expect(() => BSON.deserialize(bsonSnippet)).to.not.throw();
});
});

0 comments on commit 018c769

Please sign in to comment.