[SERVER-66841] LOGV2: invalid JSON when truncation happens at a backslash character Created: 27/May/22  Updated: 29/Oct/23  Resolved: 02/Aug/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 4.4.17, 5.0.11, 6.0.2, 6.1.0-rc0

Type: Bug Priority: Major - P3
Reporter: Andrew Witten (Inactive) Assignee: Erwin Pe
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File invalid-logged-json.json    
Issue Links:
Backports
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v6.0, v5.0, v4.4
Sprint: Security 2022-06-27, Security 2022-07-11, Security 2022-08-08
Participants:

 Description   

I put the following code in ShardLocal::_runCommand:

std::string s;
StringStackTraceSink sink{s};
printStackTrace(sink);
LOGV2(1, "In ShardLocal::_runCommand", "cmd"_attr=cmdObj, "dbName"_attr=dbName, "stacktrace"_attr=s); 

The actual location and contents of that log line is probably irrelevant (other than that it contains a stacktrace).

 

And I have a python script that parses each of those log lines.

But sometimes, the logged message is invalid JSON.  There is an issue with the "stacktrace" field of the JSON string.  Each time there is an error with it, the JSON object has a field that is 

"truncated":{"stacktrace":{"type":"string","size":109042}}

I have attached an example of an invalid log line

invalid-logged-json.json.

 

Discussion on it here



 Comments   
Comment by Githook User [ 09/Aug/22 ]

Author:

{'name': 'Erwin Pe', 'email': 'erwin.pe@mongodb.com', 'username': 'erwee'}

Message: SERVER-66841 Fix LOGV2 invalid JSON when truncation happens at a backslash character

(cherry picked from commit b795776ac3c1599528a5772825f16f88ecb5b1c9)
Branch: v5.0
https://github.com/mongodb/mongo/commit/d56d5295aed750c2d9d585d78d1d224955b40a62

Comment by Githook User [ 09/Aug/22 ]

Author:

{'name': 'Erwin Pe', 'email': 'erwin.pe@mongodb.com', 'username': 'erwee'}

Message: SERVER-66841 Fix LOGV2 invalid JSON when truncation happens at a backslash character

(cherry picked from commit b795776ac3c1599528a5772825f16f88ecb5b1c9)
Branch: v6.0
https://github.com/mongodb/mongo/commit/315375bbda07e56c7ccbb703b0c5dd3e544ed48a

Comment by Billy Donahue [ 28/May/22 ]

I figured it out. The truncation of your string happened to occur at a backslash character, which accidentally escaped the close-quote and left the backtrace string unterminated, so the enclosing JSON object became invalid.

That's why it feels kind of random. The truncation point just so happens to line up with a dangling backslash only occasionally.

The string was truncated at this point, leaving a backslash.

... AACCA8C\",\

Then this suffix was appended to truncate and characterize the truncation.

"},"truncated":{"stacktrace":{"type":"string","size":110891}}}

But the backslash ended up escaping what should be the close quote.

... AACCA8C\",\"},"truncated":{"stacktrace":{"type":"string","size":110891}}}

Generated at Thu Feb 08 06:06:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.