Skip to content

Commit 1af8f37

Browse files
Redacted sensitive information in logs
1 parent a4789df commit 1af8f37

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

Console/KeyAuth.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,9 +908,22 @@ public static void LogEvent(string content)
908908

909909
try
910910
{
911+
JObject jsonObject = JsonConvert.DeserializeObject<JObject>(content);
912+
913+
// Redact sensitive fields - Add more if you would like.
914+
RedactField(jsonObject, "sessionid");
915+
RedactField(jsonObject, "ownerid");
916+
RedactField(jsonObject, "app");
917+
RedactField(jsonObject, "secret");
918+
RedactField(jsonObject, "version");
919+
RedactField(jsonObject, "fileid");
920+
RedactField(jsonObject, "webhooks");
921+
RedactField(jsonObject, "nonce");
922+
string redactedContent = jsonObject.ToString(Newtonsoft.Json.Formatting.None);
923+
911924
using (StreamWriter writer = File.AppendText(logFilePath))
912925
{
913-
writer.WriteLine($"[{DateTime.Now}] [{AppDomain.CurrentDomain.FriendlyName}] {content}");
926+
writer.WriteLine($"[{DateTime.Now}] [{AppDomain.CurrentDomain.FriendlyName}] {redactedContent}");
914927
}
915928
}
916929
catch (Exception ex)
@@ -919,6 +932,15 @@ public static void LogEvent(string content)
919932
}
920933
}
921934

935+
private static void RedactField(JObject jsonObject, string fieldName)
936+
{
937+
JToken token;
938+
if (jsonObject.TryGetValue(fieldName, out token))
939+
{
940+
jsonObject[fieldName] = "REDACTED";
941+
}
942+
}
943+
922944
public static void error(string message)
923945
{
924946
string folder = @"Logs", file = Path.Combine(folder, "ErrorLogs.txt");

Form/KeyAuth.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ public static string checksum(string filename)
894894
return result;
895895
}
896896

897-
public static void LogEvent(string content)
897+
public static void LogEvent(string content)
898898
{
899899
string exeName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location);
900900

@@ -909,9 +909,22 @@ public static void LogEvent(string content)
909909

910910
try
911911
{
912+
JObject jsonObject = JsonConvert.DeserializeObject<JObject>(content);
913+
914+
// Redact sensitive fields - Add more if you would like.
915+
RedactField(jsonObject, "sessionid");
916+
RedactField(jsonObject, "ownerid");
917+
RedactField(jsonObject, "app");
918+
RedactField(jsonObject, "secret");
919+
RedactField(jsonObject, "version");
920+
RedactField(jsonObject, "fileid");
921+
RedactField(jsonObject, "webhooks");
922+
RedactField(jsonObject, "nonce");
923+
string redactedContent = jsonObject.ToString(Newtonsoft.Json.Formatting.None);
924+
912925
using (StreamWriter writer = File.AppendText(logFilePath))
913926
{
914-
writer.WriteLine($"[{DateTime.Now}] [{AppDomain.CurrentDomain.FriendlyName}] {content}");
927+
writer.WriteLine($"[{DateTime.Now}] [{AppDomain.CurrentDomain.FriendlyName}] {redactedContent}");
915928
}
916929
}
917930
catch (Exception ex)
@@ -920,6 +933,15 @@ public static void LogEvent(string content)
920933
}
921934
}
922935

936+
private static void RedactField(JObject jsonObject, string fieldName)
937+
{
938+
JToken token;
939+
if (jsonObject.TryGetValue(fieldName, out token))
940+
{
941+
jsonObject[fieldName] = "REDACTED";
942+
}
943+
}
944+
923945
public static void error(string message)
924946
{
925947
string folder = @"Logs", file = Path.Combine(folder, "ErrorLogs.txt");

0 commit comments

Comments
 (0)