[JAVA-4689] BasicDBObject#parse Treats All Character Sequences Beginning With '\' as Escape Characters Created: 27/Jul/22  Updated: 27/Oct/23  Resolved: 27/Jul/22

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ramasai Tadepalli Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Summary

Trying to parse the following string (unescaped):

{"keyfileWindows" : "%SystemDrive%\KeyfileVersions\versions\keyfile"} 

fails because JsonScanner#scanString recognizes everything beginning with }} as an escape character. {{\M is the above example is not a valid escape character, so there is an exception thrown while parsing this:

Exception in thread "main" org.bson.json.JsonParseException: Invalid escape sequence in JSON string '\K'.
    at org.bson.json.JsonScanner.scanString(JsonScanner.java:520)
    at org.bson.json.JsonScanner.nextToken(JsonScanner.java:92)
    at org.bson.json.JsonReader.popToken(JsonReader.java:504)
    at org.bson.json.JsonReader.readBsonType(JsonReader.java:155)
    at com.mongodb.DBObjectCodec.readDocument(DBObjectCodec.java:400)
    at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:167)
    at com.mongodb.DBObjectCodec.decode(DBObjectCodec.java:67)
    at com.mongodb.BasicDBObject.parse(BasicDBObject.java:94)
    at com.mongodb.BasicDBObject.parse(BasicDBObject.java:81)
    at benchmark.BenchmarkMain.decodeUsingParse(BenchmarkMain.java:41)
    at benchmark.BenchmarkMain.main(BenchmarkMain.java:27) 

I am using driver version 4.7.0.

How to Reproduce

BasicDBObject.parse("{ \"keyfileWindows\" : \"%SystemDrive%\\KeyfileVersions\\versions\\keyfile\" }");

Additional Background

The exception does not get thrown when using the (now deprecated) JSON#parse functionality provided in 3.11.x. The parsing is still incorrect, but that's irrelevant to the point of this ticket.



 Comments   
Comment by Jeffrey Yemin [ 27/Jul/22 ]

The backslash characters need double escaping in this example: one to satisfy the Java parser, the other to satisfy the JSON parser.

Generated at Thu Feb 08 09:02:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.