Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b9bd81b
add new option to serializeoptions type
baileympearson Dec 1, 2021
f66ced3
Misc changes - commit to switch branches
baileympearson Dec 2, 2021
11de2a4
Added unit tests for BinMsg.parse with the utf8 validation option
baileympearson Dec 2, 2021
50f7289
Merge branch 'main' of github.com:mongodb/node-mongodb-native into NO…
baileympearson Dec 2, 2021
3770ed7
Fix null check linting error
baileympearson Dec 3, 2021
a482a7f
staing files to switch branches
baileympearson Dec 3, 2021
b48a336
Add Validation Tests
baileympearson Dec 3, 2021
6a1366d
Merge branch 'main' of github.com:mongodb/node-mongodb-native into NO…
baileympearson Dec 3, 2021
1f14bf3
Add functional tests
baileympearson Dec 3, 2021
c3f0ed4
Remove ucomment in command options interface
baileympearson Dec 3, 2021
fa801b1
Merge branch 'main' of github.com:mongodb/node-mongodb-native into NO…
baileympearson Dec 6, 2021
3da9e68
Misc Fixes
baileympearson Dec 6, 2021
a9b2a66
Add comment to new option
baileympearson Dec 7, 2021
eeaec96
Merge branch 'main' of github.com:mongodb/node-mongodb-native into NO…
baileympearson Dec 7, 2021
492e653
Fix import error && make utility public
baileympearson Dec 7, 2021
f37464a
Remove commented out import
baileympearson Dec 7, 2021
c0daa54
Flip boolean logic in BinMsg class
baileympearson Dec 8, 2021
860c486
Fix test description
baileympearson Dec 8, 2021
1685c27
Remove commented out code
baileympearson Dec 8, 2021
e04ceb8
re-word test description
baileympearson Dec 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export interface BSONSerializeOptions
> {
/** Return BSON filled buffers from operations */
raw?: boolean;

/** Enable utf8 validation when deserializing BSON documents. Defaults to true. */
enableUtf8Validation?: boolean;
}

export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSerializeOptions {
Expand All @@ -66,7 +69,8 @@ export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSe
serializeFunctions,
ignoreUndefined,
bsonRegExp,
raw
raw,
enableUtf8Validation
} = options;
return {
fieldsAsRaw,
Expand All @@ -76,7 +80,8 @@ export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSe
serializeFunctions,
ignoreUndefined,
bsonRegExp,
raw
raw,
enableUtf8Validation
};
}

Expand All @@ -99,6 +104,8 @@ export function resolveBSONOptions(
ignoreUndefined: options?.ignoreUndefined ?? parentOptions?.ignoreUndefined ?? false,
bsonRegExp: options?.bsonRegExp ?? parentOptions?.bsonRegExp ?? false,
serializeFunctions: options?.serializeFunctions ?? parentOptions?.serializeFunctions ?? false,
fieldsAsRaw: options?.fieldsAsRaw ?? parentOptions?.fieldsAsRaw ?? {}
fieldsAsRaw: options?.fieldsAsRaw ?? parentOptions?.fieldsAsRaw ?? {},
enableUtf8Validation:
options?.enableUtf8Validation ?? parentOptions?.enableUtf8Validation ?? true
};
}
14 changes: 11 additions & 3 deletions src/cmap/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,6 @@ export interface MessageHeader {
export interface OpResponseOptions extends BSONSerializeOptions {
raw?: boolean;
documentsReturnedIn?: string | null;
// For now we use this internally to only prevent writeErrors from crashing the driver
validation?: { utf8: { writeErrors: boolean } };
}

/** @internal */
Expand Down Expand Up @@ -839,7 +837,7 @@ export class BinMsg {
const promoteValues = options.promoteValues ?? this.opts.promoteValues;
const promoteBuffers = options.promoteBuffers ?? this.opts.promoteBuffers;
const bsonRegExp = options.bsonRegExp ?? this.opts.bsonRegExp;
const validation = options.validation ?? { utf8: { writeErrors: false } };
const validation = this.parseBsonSerializationOptions(options);

// Set up the options
const bsonOptions: BSONSerializeOptions = {
Expand Down Expand Up @@ -876,4 +874,14 @@ export class BinMsg {

this.parsed = true;
}

parseBsonSerializationOptions({ enableUtf8Validation }: BSONSerializeOptions): {
utf8: { writeErrors: false } | false;
} {
if (enableUtf8Validation === false) {
return { utf8: false };
}

return { utf8: { writeErrors: false } };
}
}
2 changes: 2 additions & 0 deletions src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,8 @@ function write(
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false,
enableUtf8Validation:
typeof options.enableUtf8Validation === 'boolean' ? options.enableUtf8Validation : true,
raw: typeof options.raw === 'boolean' ? options.raw : false,
started: 0
};
Expand Down
4 changes: 3 additions & 1 deletion src/cmap/wire_protocol/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export function applyCommonQueryOptions(
promoteLongs: typeof options.promoteLongs === 'boolean' ? options.promoteLongs : true,
promoteValues: typeof options.promoteValues === 'boolean' ? options.promoteValues : true,
promoteBuffers: typeof options.promoteBuffers === 'boolean' ? options.promoteBuffers : false,
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false
bsonRegExp: typeof options.bsonRegExp === 'boolean' ? options.bsonRegExp : false,
enableUtf8Validation:
typeof options.enableUtf8Validation === 'boolean' ? options.enableUtf8Validation : true
});

if (options.session) {
Expand Down
1 change: 1 addition & 0 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ export const OPTIONS = {
});
}
},
enableUtf8Validation: { type: 'boolean', default: true },
family: {
transform({ name, values: [value] }): 4 | 6 {
const transformValue = getInt(name, value);
Expand Down
1 change: 1 addition & 0 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const DB_OPTIONS_ALLOW_LIST = [
'promoteBuffers',
'promoteLongs',
'bsonRegExp',
'enableUtf8Validation',
'promoteValues',
'compression',
'retryWrites'
Expand Down
3 changes: 2 additions & 1 deletion src/operations/create_collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const ILLEGAL_COMMAND_FIELDS = new Set([
'promoteBuffers',
'bsonRegExp',
'serializeFunctions',
'ignoreUndefined'
'ignoreUndefined',
'enableUtf8Validation'
]);

/** @public
Expand Down
1 change: 1 addition & 0 deletions src/operations/map_reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const exclusionList = [
'bsonRegExp',
'serializeFunctions',
'ignoreUndefined',
'enableUtf8Validation',
'scope' // this option is reformatted thus exclude the original
];

Expand Down
122 changes: 122 additions & 0 deletions test/functional/cmap/commands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { spy } from 'sinon';
import { expect } from 'chai';
import * as BSON from '../../../src/bson';

const deserializeSpy = spy(BSON, 'deserialize');

const EXPECTED_VALIDATION_DISABLED_ARGUMENT = {
utf8: false
};

const EXPECTED_VALIDATION_ENABLED_ARGUMENT = {
utf8: {
writeErrors: false
}
};

describe('class BinMsg', () => {
beforeEach(() => {
deserializeSpy.resetHistory();
});

describe('enableUtf8Validation option set to false', () => {
let client;
const option = { enableUtf8Validation: false };

for (const passOptionTo of ['client', 'db', 'collection', 'operation']) {
it(`should disable validation with option passed to ${passOptionTo}`, async function () {
try {
client = this.configuration.newClient(passOptionTo === 'client' ? option : undefined);
await client.connect();

const db = client.db(
'bson_utf8Validation_db',
passOptionTo === 'db' ? option : undefined
);
const collection = db.collection(
'bson_utf8Validation_coll',
passOptionTo === 'collection' ? option : undefined
);

await collection.insertOne(
{ name: 'John Doe' },
passOptionTo === 'operation' ? option : {}
);

expect(deserializeSpy.called).to.be.true;
const validationArgument = deserializeSpy.lastCall.lastArg.validation;
expect(validationArgument).to.deep.equal(EXPECTED_VALIDATION_DISABLED_ARGUMENT);
} finally {
await client.close();
}
});
}
});

describe('enableUtf8Validation option set to true', () => {
// define client and option for tests to use
let client;
const option = { enableUtf8Validation: true };
for (const passOptionTo of ['client', 'db', 'collection', 'operation']) {
it(`should enable validation with option passed to ${passOptionTo}`, async function () {
try {
client = this.configuration.newClient(passOptionTo === 'client' ? option : undefined);
await client.connect();

const db = client.db(
'bson_utf8Validation_db',
passOptionTo === 'db' ? option : undefined
);
const collection = db.collection(
'bson_utf8Validation_coll',
passOptionTo === 'collection' ? option : undefined
);

await collection.insertOne(
{ name: 'John Doe' },
passOptionTo === 'operation' ? option : {}
);

expect(deserializeSpy.called).to.be.true;
const validationArgument = deserializeSpy.lastCall.lastArg.validation;
expect(validationArgument).to.deep.equal(EXPECTED_VALIDATION_ENABLED_ARGUMENT);
} finally {
await client.close();
}
});
}
});

describe('enableUtf8Validation option not set', () => {
let client;
const option = { enableUtf8Validation: true };
for (const passOptionTo of ['client', 'db', 'collection', 'operation']) {
it(`should default to enabled with option passed to ${passOptionTo}`, async function () {
try {
client = this.configuration.newClient(passOptionTo === 'client' ? option : undefined);
await client.connect();

const db = client.db(
'bson_utf8Validation_db',
passOptionTo === 'db' ? option : undefined
);
const collection = db.collection(
'bson_utf8Validation_coll',
passOptionTo === 'collection' ? option : undefined
);

await collection.insertOne(
{ name: 'John Doe' },
passOptionTo === 'operation' ? option : {}
);

expect(deserializeSpy.called).to.be.true;
const validationArgument = deserializeSpy.lastCall.lastArg.validation;
expect(validationArgument).to.deep.equal(EXPECTED_VALIDATION_ENABLED_ARGUMENT);
} finally {
await client.close();
}
});
}
});
});
1 change: 1 addition & 0 deletions test/types/bson.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PermittedBSONOptionKeys =
| 'promoteValues'
| 'bsonRegExp'
| 'fieldsAsRaw'
| 'enableUtf8Validation'
| 'raw';

const keys = null as unknown as PermittedBSONOptionKeys;
Expand Down
Loading