1111using System . Collections . Generic ;
1212using System . Security . Cryptography . X509Certificates ;
1313using System . Net . Security ;
14- using Newtonsoft . Json ;
1514using System . Threading ;
1615using System . Runtime . CompilerServices ;
17- using Newtonsoft . Json . Linq ;
1816
1917namespace KeyAuth
2018{
@@ -909,22 +907,19 @@ public static void LogEvent(string content)
909907
910908 try
911909 {
912- JObject jsonObject = JsonConvert . DeserializeObject < JObject > ( content ) ;
913-
914910 // 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 ) ;
911+ content = RedactField ( content , "sessionid" ) ;
912+ content = RedactField ( content , "ownerid" ) ;
913+ content = RedactField ( content , "app" ) ;
914+ content = RedactField ( content , "secret" ) ;
915+ content = RedactField ( content , "version" ) ;
916+ content = RedactField ( content , "fileid" ) ;
917+ content = RedactField ( content , "webhooks" ) ;
918+ content = RedactField ( content , "nonce" ) ;
924919
925920 using ( StreamWriter writer = File . AppendText ( logFilePath ) )
926921 {
927- writer . WriteLine ( $ "[{ DateTime . Now } ] [{ AppDomain . CurrentDomain . FriendlyName } ] { redactedContent } ") ;
922+ writer . WriteLine ( $ "[{ DateTime . Now } ] [{ AppDomain . CurrentDomain . FriendlyName } ] { content } ") ;
928923 }
929924 }
930925 catch ( Exception ex )
@@ -933,13 +928,13 @@ public static void LogEvent(string content)
933928 }
934929 }
935930
936- private static void RedactField ( JObject jsonObject , string fieldName )
931+ private static string RedactField ( string content , string fieldName )
937932 {
938- JToken token ;
939- if ( jsonObject . TryGetValue ( fieldName , out token ) )
940- {
941- jsonObject [ fieldName ] = "REDACTED" ;
942- }
933+ // Basic pattern matching to replace values of sensitive fields
934+ string pattern = $ " \" { fieldName } \" : \" [^ \" ]* \" " ;
935+ string replacement = $ " \" { fieldName } \" : \" REDACTED \" " ;
936+
937+ return System . Text . RegularExpressions . Regex . Replace ( content , pattern , replacement ) ;
943938 }
944939
945940 public static void error ( string message )
0 commit comments