11
11
using System . Collections . Generic ;
12
12
using System . Security . Cryptography . X509Certificates ;
13
13
using System . Net . Security ;
14
- using Newtonsoft . Json ;
15
14
using System . Threading ;
16
15
using System . Runtime . CompilerServices ;
17
- using Newtonsoft . Json . Linq ;
18
16
19
17
namespace KeyAuth
20
18
{
@@ -909,22 +907,19 @@ public static void LogEvent(string content)
909
907
910
908
try
911
909
{
912
- JObject jsonObject = JsonConvert . DeserializeObject < JObject > ( content ) ;
913
-
914
910
// 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" ) ;
924
919
925
920
using ( StreamWriter writer = File . AppendText ( logFilePath ) )
926
921
{
927
- writer . WriteLine ( $ "[{ DateTime . Now } ] [{ AppDomain . CurrentDomain . FriendlyName } ] { redactedContent } ") ;
922
+ writer . WriteLine ( $ "[{ DateTime . Now } ] [{ AppDomain . CurrentDomain . FriendlyName } ] { content } ") ;
928
923
}
929
924
}
930
925
catch ( Exception ex )
@@ -933,13 +928,13 @@ public static void LogEvent(string content)
933
928
}
934
929
}
935
930
936
- private static void RedactField ( JObject jsonObject , string fieldName )
931
+ private static string RedactField ( string content , string fieldName )
937
932
{
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 ) ;
943
938
}
944
939
945
940
public static void error ( string message )
0 commit comments