Skip to content

Commit e06cec3

Browse files
committed
Fix obfuscation for zerodepjson
1 parent c0d1385 commit e06cec3

File tree

1 file changed

+73
-7
lines changed

1 file changed

+73
-7
lines changed

Runtime/Client/LootLockerBaseServerAPI.cs

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
using System;
66
using System.Text;
77
using System.Net;
8-
#if !LOOTLOCKER_USE_ZERODEPJSON && !LOOTLOCKER_USE_NEWTONSOFTJSON
9-
using LLlibs.Newtonsoft.Json;
10-
using LLlibs.Newtonsoft.Json.Linq;
8+
#if LOOTLOCKER_USE_ZERODEPJSON
9+
using LootLocker.ZeroDepJson;
1110
#elif LOOTLOCKER_USE_NEWTONSOFTJSON
1211
using Newtonsoft.Json;
1312
using Newtonsoft.Json.Linq;
13+
#else
14+
using LLlibs.Newtonsoft.Json;
15+
using LLlibs.Newtonsoft.Json.Linq;
1416
#endif
1517
using LootLocker.Requests;
1618

@@ -172,12 +174,12 @@ IEnumerator coroutine()
172174
else
173175
{
174176
tries = 0;
175-
response.Error += " " + webRequest.downloadHandler.text;
177+
response.Error += " " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text);
176178
response.statusCode = (int)webRequest.responseCode;
177179
response.success = false;
178180
response.hasError = true;
179181
response.text = webRequest.downloadHandler.text;
180-
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(ObfuscateJsonStringForLogging(response.Error));
182+
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(response.Error);
181183
OnServerResponse?.Invoke(response);
182184
}
183185

@@ -372,8 +374,72 @@ public ObfuscationDetails(string key, char replacementChar = '*', int visibleCha
372374
private static string ObfuscateJsonStringForLogging(string json)
373375
{
374376
#if LOOTLOCKER_USE_ZERODEPJSON
375-
//TODO: Fix this json usage
376-
return json;
377+
if (string.IsNullOrEmpty(json) || json.Equals("{}"))
378+
{
379+
return json;
380+
}
381+
382+
Dictionary<string, object> jsonObject = null;
383+
try
384+
{
385+
jsonObject = Json.Deserialize(json) as Dictionary<string, object>;
386+
}
387+
catch (JsonException)
388+
{
389+
return json;
390+
}
391+
392+
if (jsonObject != null && jsonObject.Count > 0)
393+
{
394+
foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate)
395+
{
396+
string valueToObfuscate;
397+
try
398+
{
399+
if (!jsonObject.ContainsKey(obfuscationInfo.key))
400+
{
401+
continue;
402+
}
403+
404+
valueToObfuscate = Json.Serialize(jsonObject[obfuscationInfo.key]);
405+
}
406+
catch (KeyNotFoundException)
407+
{
408+
continue;
409+
}
410+
411+
if (string.IsNullOrEmpty(valueToObfuscate))
412+
continue;
413+
414+
if (valueToObfuscate.Equals("null", StringComparison.Ordinal))
415+
continue;
416+
417+
int replaceFrom = 0;
418+
int replaceTo = valueToObfuscate.Length;
419+
420+
// Deal with short strings
421+
if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd)
422+
{
423+
if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything
424+
continue;
425+
}
426+
// Replace in
427+
else
428+
{
429+
replaceFrom += obfuscationInfo.visibleCharsFromBeginning;
430+
replaceTo -= obfuscationInfo.visibleCharsFromEnd;
431+
}
432+
433+
StringBuilder replacement = new StringBuilder();
434+
replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom);
435+
StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate);
436+
obfuscatedValue.Remove(replaceFrom, replacement.Length);
437+
obfuscatedValue.Insert(replaceFrom, replacement.ToString());
438+
jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString();
439+
}
440+
}
441+
442+
return LootLockerJson.SerializeObject(jsonObject);
377443
#else
378444
if (string.IsNullOrEmpty(json))
379445
{

0 commit comments

Comments
 (0)