11using System . Collections . Concurrent ;
2+ using System . Text . Encodings . Web ;
23using System . Text . Json ;
4+ using System . Text . Json . Nodes ;
35using System . Text . RegularExpressions ;
6+ using OpenClaw . Shared ;
47
58namespace OpenClaw . SetupEngine ;
69
@@ -84,7 +87,7 @@ private void Write(LogLevel level, string message, object? data = null)
8487 {
8588 if ( level < _minLevel ) return ;
8689
87- var sanitizedMessage = Sanitize ( message ) ;
90+ var sanitizedMessage = NormalizeLogString ( Sanitize ( message ) ) ;
8891 var entry = new LogEntry ( DateTimeOffset . UtcNow , _runId , level , sanitizedMessage , SanitizeData ( data ) ) ;
8992 _recentEntries . Enqueue ( entry ) ;
9093 while ( _recentEntries . Count > MaxRecentEntries )
@@ -125,6 +128,10 @@ private void Write(LogLevel level, string message, object? data = null)
125128 internal static string Sanitize ( string input )
126129 {
127130 if ( string . IsNullOrEmpty ( input ) ) return input ;
131+ input = TokenSanitizer . SanitizeLogMessage ( input ) ;
132+ if ( input == TokenSanitizer . SanitizerTimeoutSentinel )
133+ return input ;
134+
128135 input = PrivateKeyPattern ( ) . Replace ( input , "[REDACTED-PRIVATE-KEY]" ) ;
129136 input = BearerPattern ( ) . Replace ( input , "$1[REDACTED]" ) ;
130137 input = JwtPattern ( ) . Replace ( input , "[REDACTED-JWT]" ) ;
@@ -140,21 +147,65 @@ internal static string Sanitize(string input)
140147 return null ;
141148
142149 if ( data is string value )
143- return Sanitize ( value ) ;
150+ return NormalizeLogString ( Sanitize ( value ) ) ;
144151
145152 try
146153 {
147154 var json = JsonSerializer . Serialize ( data , _jsonOptions ) ;
148155 var sanitized = Sanitize ( json ) ;
149- using var doc = JsonDocument . Parse ( sanitized ) ;
150- return doc . RootElement . Clone ( ) ;
156+ var node = JsonNode . Parse ( sanitized ) ;
157+ return NormalizeJsonNode ( node ) ;
151158 }
152159 catch ( Exception ex ) when ( ex is JsonException or NotSupportedException or ArgumentException )
153160 {
154- return Sanitize ( data . ToString ( ) ?? string . Empty ) ;
161+ return NormalizeLogString ( Sanitize ( data . ToString ( ) ?? string . Empty ) ) ;
155162 }
156163 }
157164
165+ private static JsonNode ? NormalizeJsonNode ( JsonNode ? node )
166+ {
167+ switch ( node )
168+ {
169+ case JsonObject obj :
170+ var normalizedObject = new JsonObject ( ) ;
171+ foreach ( var property in obj )
172+ normalizedObject [ property . Key ] = NormalizeJsonNode ( property . Value ) ;
173+ return normalizedObject ;
174+
175+ case JsonArray array :
176+ var normalizedArray = new JsonArray ( ) ;
177+ for ( var i = 0 ; i < array . Count ; i ++ )
178+ normalizedArray . Add ( NormalizeJsonNode ( array [ i ] ) ) ;
179+ return normalizedArray ;
180+
181+ case JsonValue value when value . TryGetValue < string > ( out var text ) :
182+ var normalized = NormalizeLogString ( text ) ;
183+ var trimmed = normalized . TrimStart ( ) ;
184+ if ( trimmed . StartsWith ( '{' ) || trimmed . StartsWith ( '[' ) )
185+ {
186+ try
187+ {
188+ return NormalizeJsonNode ( JsonNode . Parse ( normalized ) ) ;
189+ }
190+ catch ( JsonException )
191+ {
192+ // Keep malformed JSON-shaped text as a flattened string.
193+ }
194+ }
195+
196+ return JsonValue . Create ( normalized ) ;
197+
198+ default :
199+ return node ? . DeepClone ( ) ;
200+ }
201+ }
202+
203+ private static string NormalizeLogString ( string value ) =>
204+ value
205+ . Replace ( "\r \n " , " " , StringComparison . Ordinal )
206+ . Replace ( '\r ' , ' ' )
207+ . Replace ( '\n ' , ' ' ) ;
208+
158209 private static string Truncate ( string input , int max = 4096 )
159210 => input . Length <= max ? input : input [ .. max ] + $ "... [truncated { input . Length - max } chars]";
160211
@@ -178,7 +229,8 @@ private static string Truncate(string input, int max = 4096)
178229
179230 private static readonly JsonSerializerOptions _jsonOptions = new ( )
180231 {
181- DefaultIgnoreCondition = System . Text . Json . Serialization . JsonIgnoreCondition . WhenWritingNull
232+ DefaultIgnoreCondition = System . Text . Json . Serialization . JsonIgnoreCondition . WhenWritingNull ,
233+ Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
182234 } ;
183235
184236 public void Dispose ( ) => _writer ? . Dispose ( ) ;
0 commit comments