[SERVER-9470] shell allows native code to be "saved" to collection and then throws Unexpected identifier when retrieved Created: 25/Apr/13  Updated: 29/Jan/18  Resolved: 05/May/17

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 2.4.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Henry Hicks Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

amazon ami linux (~ CentOS 6), and also os x 10.8.3


Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

~> mongo
MongoDB shell version: 2.4.3
connecting to: test
> db.blah.count()  // new empty table
0
 
// native and non-native functions 
> var native = "".trim
function trim() { [native code] }
> var nonNative = function() {print('hi')}
function () {print('hi')}
> 
> // everything is cool for the nonNative function
> db.blah.insert({f:nonNative})
> db.blah.find()[0]
{
	"_id" : ObjectId("517967ac68f5d75f0efedc26"),
	"f" : function () {print('hi')}
}
> 
 
// lets try and trick it with a native function (it saves fine, bombs on retrieval)
// note: src/mongo/shell/types.js:L592 thrown below is a "for (var k in obj)" construct.  the native function property bombs this, it is not the source of the error
> db.blah.insert({f:native})
> db.blah.find()[1]
Thu Apr 25 17:28:54.386 JavaScript execution failed: SyntaxError: Unexpected identifier
Thu Apr 25 17:28:54.386 JavaScript execution failed: Error: 16722 JavaScript execution failed: SyntaxError: Unexpected identifier at src/mongo/shell/types.js:L592 
 
// record is there, but the property f throws an exception in the shell
> db.blah.find()[1]._id
ObjectId("517967c368f5d75f0efedc27")
> db.blah.find()[1].f
Thu Apr 25 17:29:29.342 JavaScript execution failed: SyntaxError: Unexpected identifier
Thu Apr 25 17:29:29.342 JavaScript execution failed: Error: 16722 JavaScript execution failed: SyntaxError: Unexpected identifier
 
// zero still works
> db.blah.find()[0]
{
	"_id" : ObjectId("517967ac68f5d75f0efedc26"),
	"f" : function () {print('hi')}
}
 

Participants:

 Description   

there is a bson "Code" type allowing us to save functions in a collection.

From the shell, if a native function [like String.prototype.trim used below] is saved to the database, it will throw an Unexpected identifier when retrieved.

I have not been able to identify the exact source of the error. In the nodejs driver -- and i assume other drivers -- it works fine in the sense that it will not throw an error. Obviously the property still has invalid data.



 Comments   
Comment by Spencer Jackson [ 05/May/17 ]

Performing the following repro on 3.4.1 results in:

> var native = "".trim
> db.blah.insert({f:native})
WriteResult({ "nInserted" : 1 })
> db.blah.find()[1]
> db.blah.find()
{ "_id" : ObjectId("590cc80882a166a2c78147e3"), "f" : { "code" : "function trim() {\n    [native code]\n}" } }
> db.blah.find()[0]
{
        "_id" : ObjectId("590cc80882a166a2c78147e3"),
        "f" : {
                "code" : "function trim() {\n    [native code]\n}"
        }
}

Generated at Thu Feb 08 03:20:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.