Skip to content

Commit 8c9acbc

Browse files
feat: add sanitized diagnostics log bundle
Port PR #562 onto current main and harden diagnostics redaction so JSONL remains parseable while sensitive values, metadata keys, paths, emails, headers, and compound JSON values are sanitized before export. Co-authored-by: Copilot <[email protected]>
1 parent 8b5ea32 commit 8c9acbc

31 files changed

Lines changed: 2925 additions & 295 deletions

‎src/OpenClaw.Connection/ConnectionDiagnostics.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public ConnectionDiagnostics(int capacity = 500, IClock? clock = null)
2727

2828
public void Record(string category, string message, string? detail = null)
2929
{
30-
var evt = new ConnectionDiagnosticEvent(_clock.UtcNow, category, message, detail);
30+
var evt = new ConnectionDiagnosticEvent(
31+
_clock.UtcNow,
32+
TokenSanitizer.SanitizeLogMessage(category),
33+
TokenSanitizer.SanitizeLogMessage(message),
34+
detail is null ? null : TokenSanitizer.SanitizeLogMessage(detail));
3135
lock (_lock)
3236
{
3337
_buffer[_head] = evt;

‎src/OpenClaw.SetupEngine/SetupLogger.cs‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System.Collections.Concurrent;
2+
using System.Text.Encodings.Web;
23
using System.Text.Json;
4+
using System.Text.Json.Nodes;
35
using System.Text.RegularExpressions;
6+
using OpenClaw.Shared;
47

58
namespace 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

Comments
 (0)