[SERVER-8497] Javascript shell does not escape quotes in field names Created: 11/Feb/13  Updated: 26/Oct/21  Resolved: 26/Oct/21

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

Type: Bug Priority: Trivial - P5
Reporter: Shaun Verch Assignee: Geert Bosch
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

> x = {}
{ }
> x['":false,"'] = '"'
"
> x
{ "":false,"" : "\"" }
> interpreterVersion()
SpiderMonkey 1.7

> x = {}
{ }
> x['":false,"'] = '"'
"
> x
{ "":false,"" : "\"" }
> interpreterVersion()
V8 3.12.19



 Comments   
Comment by Geert Bosch [ 26/Oct/21 ]

Looks like this now works with mongosh:

test> x={}
{}
test> x['":false,"'] = '"'
"
test> x
{ '":false,"': '"' }
test> 

Comment by Tad Marshall [ 11/Feb/13 ]

Agreed. See src/mongo/shell/types.js line 596:

s += indent + "\"" + k + "\" : " + tojson(val, indent, nolint);

This line adds quotes around the field name (k) without making any effort to quote special characters in it.

MongoDB shell version: 2.4.0-rc1-pre-
> x = {}
{ }
> x['":false,"'] = '"'
"
> x
{ "":false,"" : "\"" }
>

Seems like changing it to this fixes it:

s += indent + tojson(k, indent, nolint) + " : " + tojson(val, indent, nolint);

MongoDB shell version: 2.4.0-rc1-pre-
> x = {}
{ }
> x['":false,"'] = '"'
"
> x
{ "\":false,\"" : "\"" }
>

Comment by Shaun Verch [ 11/Feb/13 ]

I think this is actually a separate issue from what Scott mentioned and SERVER-5809. Those are both issues with how we input things with quotes into the shell, while this one is a bug in how field names with quotes are output. My example above is valid and the shell correctly allows it as input.

Comment by Tad Marshall [ 11/Feb/13 ]

Re: scotthernandez's comment: Is this the same issue as SERVER-5809? This is related to SERVER-3994 in that it's the same routine (isBalanced()) that is not doing a good job of figuring out when to enter multi-line mode.

Comment by Scott Hernandez (Inactive) [ 11/Feb/13 ]

It seems like quoting/escaping is a bit worse that that:

> x["\""] = 1
... 
... 
> x = {"\"":1}
... 
... 

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