Search code examples
c++jsonjsoncpp

Best way to strip private data from JSon when using jsoncpp


Problem is simple. Some JSon data are exchanged with the server. Since communication is quite complex I need logs as complex information as possible, to see if:

  • server send new value which is omitted by code
  • JSon with typo was sent
  • ans so on

But at the same time any private data should be shadowed, by dummy data.

So instead see in logs:

{
    "secuityToken" : "asdasdgas234fsdfsaD",
    "message" : "user private message"
}

something like this should be seen:

{
    "secuityToken" : "********",
    "message" : "*******"
}

My code is C++ so jsoncpp is in use. Best arproach I can see is:

bool ProcessServerMessage(const std::string& message)
{
    Json::Value jsonValue;
    Json::Reader reader;
    if (reader.parse(sMessage, jsonValue, false))
    {
        auto logValue =  ShadowPrivateData(jsonValue, listOfKeysWithPrivateData);
        LOG() << " JSOn recived: " << logValue;
        …
    }

Question how ShadowPrivateData should look like, to be most generic?


Solution

  • As to me, a straightforward approach is enough here. Just call ShadowPrivateData recursively for all members of jsonValue. On each recursion step you should determine if jsonValue is array, object or neither of them, and iterate through it properly. Use isArray and isObject for that.

    When iterating through object's or array's field, if the field is not aggregate (object or array), search for it's name in listOfKeysWithPrivateData. If field's name is found in the list, determine field's type (using isString, isDouble, isIntegral and so on) and replace the field with proper value: replace strings with asterisks, numbers with zeros, etc.

    Declare listOfKeysWithPrivateData as std::set<std::string> or something like that to perform logarithmic search instead of linear.

    How to iterate through an aggregate object? Use getMemberNames for objects and size for arrays. In other words, jsoncpp provides a full collection of methods for json object introspection.

    Being properly implemented, this approach should censor all sensitive data in jsonValue disregarding it's complexity.