[CDRIVER-1879] libbson truncates code_with_scope with embedded nulls in javascript Created: 23/Oct/16  Updated: 03/May/17  Resolved: 09/Feb/17

Status: Closed
Project: C Driver
Component/s: libbson
Affects Version/s: None
Fix Version/s: 1.7.0

Type: New Feature Priority: Minor - P4
Reporter: David Golden Assignee: A. Jesse Jiryu Davis
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to CDRIVER-1364 BSON type "JavaScript code w/ scope" ... Closed
related to PHPC-929 Skip BSON corpus test for embedded nu... Closed
related to PHPC-714 Implement BSON corpus test suite Closed
is related to CDRIVER-1335 libbson does not properly serialize t... Closed
is related to CDRIVER-1878 bson_append_code_with_scope drops emp... Closed
Epic Link: libbson corpus

 Description   

The spec for code_with_scope says that the javascript is a "string", which is allowed to have embedded nulls. However the API for bson_append_code_with_scope has no parameter for the javascript length, so computes it with strlen in bson.c line 963. This truncates any input with embedded nulls.

In order to fully support the spec and pass the BSON Corpus tests, we need a new function:

 
/**
 * bson_append_code_with_scope_and_length:
 * @bson: A bson_t.
 * @key: The key for the document.
 * @javascript: JavaScript code to be executed.
 * @scope: A bson_t containing the scope for @javascript.
 *
 * Appends a field of type BSON_TYPE_CODE to the BSON document
 * if @scope is NULL. Appends BSON_TYPE_CODEWSCOPE if @scope
 * is not NULL (even if it is an empty document). Unlike 
 * bson_append_code_with_scope, this function makes it possible
 * to embed literal NULL bytes in the Javascript text.
 *
 * Returns: true if successful; false if append would overflow max size.
 */
BSON_API
bool
bson_append_code_with_scope_and_length (bson_t       *bson,
                                        const char   *key,
                                        int           key_length,
                                        const char   *javascript,
                                        int           js_length,
                                        const bson_t *scope);



 Comments   
Comment by Githook User [ 13/Mar/17 ]

Author:

{u'username': u'jmikola', u'name': u'Jeremy Mikola', u'email': u'jmikola@gmail.com'}

Message: PHPC-929: Skip BSON corpus test for embedded null in code string

Per discussion in CDRIVER-1879, there are no plans to support this in libbson.
Branch: master
https://github.com/mongodb/mongo-php-driver/commit/7277da001817a6445b63330fca0f07bc35132f2a

Comment by David Golden [ 09/Feb/17 ]

I disagree without a change to the BSON spec itself. End users may use libbson for other purposes than communicating with a MongoDB server. (Not a great choice, but I've seen it done.) The "official" library should support the spec as written.

Comment by A. Jesse Jiryu Davis [ 09/Feb/17 ]

Since the MongoDB server as currently implemented only reads Code objects to the first NIL, there's no point adding an API that allows us to embed NILs in larger Code objects.

Generated at Wed Feb 07 21:13:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.