Files
Chronos/src/Titano.Core/Diagnostics/SecretScrubber.cs
T
Claude 03da77277c fix(diagnostics): scrub whole passwords; unique bundle names; no temporary files left behind
- A secret value runs to the next ';' or the end of the line unless quoted
  (backslash and doubled-quote escapes), so passwords with spaces, commas or
  quotes never leak in part; URL passwords run to the last '@' before the path.
- Desktop bundles get " (2)", " (3)"… instead of overwriting one made in the
  same minute, and a bundle that fails half-way deletes its .tmp file.
- The server builds its bundle in a temporary file that no crash leaves behind
  (DeleteOnClose on Windows, unlinked at once elsewhere).
- Tests: passwords with spaces, commas, quotes and '@'; a DbConnectionString in
  the server bundle; bundle name collisions and failure cleanup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-03 10:05:45 +00:00

141 lines
6.8 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
namespace Titano.Core.Diagnostics;
/// <summary>
/// Removes secrets from text and JSON before they leave the machine in a diagnostics bundle: passwords, tokens, keys,
/// cookies, authorisation headers, credentials in URLs and connection strings, private-key blocks. It errs towards
/// removing too much: a lost hint costs less than a leaked secret.
/// </summary>
public static partial class SecretScrubber
{
public const string Removed = "[removed]";
/// <summary>Fragments of property names whose string values are secret (compared without case, '_', '-' or '.').</summary>
private static readonly string[] SecretNames =
["password", "passwd", "pwd", "secret", "token", "apikey", "accesskey", "privatekey", "signingkey", "credential", "cookie", "authorization", "pfx"];
/// <summary>True when a setting or header with this name holds a secret.</summary>
public static bool IsSecretName(string name)
{
var normalized = string.Concat(name.Where(c => c is not ('_' or '-' or '.'))).ToLowerInvariant();
return SecretNames.Any(normalized.Contains);
}
/// <summary>
/// The JSON with every secret-named string value replaced and every other string scrubbed as text; numbers and
/// booleans stay (a "TokenLifetimeDays" is no secret). Text that is not JSON is scrubbed as text.
/// </summary>
public static string ScrubJson(string json)
{
JsonNode? root;
try
{
root = JsonNode.Parse(json);
}
catch (JsonException)
{
return ScrubText(json);
}
if (root is null) return json;
root = ScrubNode(root, secret: false);
return root.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
}
/// <summary>Scrubs a block of text (several lines; private-key blocks are removed whole).</summary>
public static string ScrubText(string text) =>
string.Join('\n', ScrubLines(text.Replace("\r\n", "\n", StringComparison.Ordinal).Split('\n')));
/// <summary>Scrubs lines one by one, dropping the body of any PEM private-key block.</summary>
public static IEnumerable<string> ScrubLines(IEnumerable<string> lines)
{
var inKey = false;
foreach (var line in lines)
{
if (!inKey && line.Contains("-----BEGIN", StringComparison.Ordinal) && line.Contains("PRIVATE KEY", StringComparison.Ordinal))
{
inKey = !line.Contains("-----END", StringComparison.Ordinal);
yield return "[removed private key]";
continue;
}
if (inKey)
{
if (line.Contains("-----END", StringComparison.Ordinal)) inKey = false;
continue;
}
yield return ScrubLine(line);
}
}
/// <summary>Scrubs one line of text.</summary>
public static string ScrubLine(string line)
{
if (line.Length == 0) return line;
var s = HeaderLine().Replace(line, m => m.Groups["name"].Value + ": " + Removed);
s = AuthScheme().Replace(s, m => m.Groups["scheme"].Value + " " + Removed);
s = UrlCredentials().Replace(s, m => m.Groups["prefix"].Value + Removed + "@");
s = QueryParameter().Replace(s, m => m.Groups["name"].Value + Removed);
s = NamedValue().Replace(s, m => m.Groups["name"].Value + Removed);
return KnownTokenFormat().Replace(s, Removed);
}
private static JsonNode ScrubNode(JsonNode node, bool secret)
{
switch (node)
{
case JsonObject obj:
foreach (var name in obj.Select(p => p.Key).ToList())
{
if (obj[name] is not { } child) continue;
var scrubbed = ScrubNode(child, secret || IsSecretName(name));
if (!ReferenceEquals(scrubbed, child)) obj[name] = scrubbed;
}
return obj;
case JsonArray array:
for (var i = 0; i < array.Count; i++)
{
if (array[i] is not { } item) continue;
var scrubbed = ScrubNode(item, secret);
if (!ReferenceEquals(scrubbed, item)) array[i] = scrubbed;
}
return array;
case JsonValue value when value.GetValueKind() == JsonValueKind.String:
return JsonValue.Create(secret ? Removed : ScrubText(value.GetValue<string>()));
default:
return node;
}
}
/// <summary>"Authorization: …", "Cookie: …", "Set-Cookie: …": the whole value goes.</summary>
[GeneratedRegex(@"(?<name>\b(?:proxy-)?authorization|\bset-cookie|\bcookie|\bx-csrf-token)\s*:\s*\S.*$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex HeaderLine();
/// <summary>"Bearer abc…", "Basic dXNl…".</summary>
[GeneratedRegex(@"(?<scheme>\b(?:bearer|basic))\s+[A-Za-z0-9._~+/=-]{6,}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex AuthScheme();
/// <summary>"https://user:secret@host" keeps the user name; the password runs to the last '@' before the path ("p@ss@db").</summary>
[GeneratedRegex(@"(?<prefix>\b[a-z][a-z0-9+.-]*://[^/\s:@]+:)[^/\s]+@", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex UrlCredentials();
/// <summary>"?token=…", "&amp;access_token=…", "&amp;sig=…".</summary>
[GeneratedRegex(@"(?<name>[?&](?:token|access_token|refresh_token|api_key|apikey|key|sig|signature|password|code)=)[^&\s#""']+", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex QueryParameter();
/// <summary>
/// "Password=…", "DbPassword: …", "\"token\": \"…\"", "client_secret=…" (connection strings, environment and settings
/// dumps, JSON in log messages). A quoted value goes up to its closing quote (a backslash or a doubled quote escapes
/// one); any other value up to the next ';' (the connection-string separator) or the end of the line, because
/// passwords may hold spaces, commas or quotes. A value an earlier rule already removed is left alone.
/// </summary>
[GeneratedRegex(@"(?<name>[A-Za-z0-9_-]*(?:password|passwd|pwd|secret|token|api[_-]?key|access[_-]?key|private[_-]?key)[A-Za-z0-9_-]*[""']?\s*[=:]\s*)(?!\[removed\])(?:""(?:[^""\\]|\\.|"""")*""|'(?:[^']|'')*'|[^;\r\n]+)",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex NamedValue();
/// <summary>Self-identifying credentials (GitHub tokens) wherever they appear.</summary>
[GeneratedRegex(@"\b(?:gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})\b", RegexOptions.CultureInvariant)]
private static partial Regex KnownTokenFormat();
}