[SERVER-7150] update() able to create fields which begin with '$' Created: 25/Sep/12  Updated: 11/Jul/16  Resolved: 27/Jul/13

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 2.0.2
Fix Version/s: 2.5.2

Type: Bug Priority: Major - P3
Reporter: Caleb Jones Assignee: Scott Hernandez (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS X 10.6.8


Issue Links:
Depends
is depended on by SERVER-6833 Secondary crashes when replicating a ... Closed
Related
related to SERVER-14244 Additional tests for setting fields w... Closed
is related to SERVER-6399 Refactor update() code Closed
Operating System: ALL
Participants:

 Description   

> db.test.insert({"_id" : 123456, "$push" : "foo"})
Tue Sep 25 08:53:46 uncaught exception: field names cannot start with $ [$push]
> db.test.insert({"_id" : 123456})
> db.test.find()
{ "_id" : 123456 }
> db.test.update({"_id" : 123456}, {"$set" : { "$push" : "foo" }})
> db.test.find()
{ "$push" : "foo", "_id" : 123456 }
> db.test.update({}, {"$rename" : {"$push" : "$"}})
> db.test.find()
{ "$" : "foo", "_id" : 123456 }
> db.test.update({}, {"$rename" : "$" : "$$$"}})
> db.test.find()
{ "$$$" : "foo", "_id" : 123456 }

It seems like the same field name validation rules should be enforced on updates for any modifiers.



 Comments   
Comment by Scott Hernandez (Inactive) [ 27/Jul/13 ]

fixed by SERVER-7557

Comment by Caleb Jones [ 04/Oct/12 ]

There seems to be some more awkward situations you can get yourself into via updates:

> db.test.insert({"_subdoc_" : {"id" : 12345, " " : "space", "" : "empty"}})
> db.test.find()
{ "_id" : ObjectId("506dbb9cdce13c926d098435"), "_subdoc_" : { "id" : 12345, " " : "space", "" : "empty" } }
> db.test.update({"_subdoc_.id" : 12345}, {"$rename" : {"_subdoc_." : "_subdoc_.empty'"}});
Invalid mod field name, may not end in a period
> db.test.update({"_subdoc_.id" : 12345}, {"$unset" : {"_subdoc_." : 1}});
Invalid mod field name, may not end in a period
> db.test.update({"_subdoc_.id" : 12345}, {"$set" : {"_subdoc_." : 1}});
Invalid mod field name, may not end in a period
> giveUp();
Thu Oct  4 09:41:27 ReferenceError: giveUp is not defined (shell):1

Overall, I think both insert and update field name validation is somewhat lacking. I am wrapping MongoDB and am finding all sorts of edge cases which I need to catch to prevent people from getting themselves into situations like these.

Coincidentally, I cannot find a source document which describes exactly what is or isn't allowed for field names.

Comment by Caleb Jones [ 25/Sep/12 ]

Could someone confirm this for 2.1 and/or 2.2?

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