feat(diagnostics): scrubbed diagnostics bundles on the desktop and the server
SecretScrubber removes passwords, tokens, keys, cookies, authorisation headers, URL credentials and private-key blocks from text and JSON; DiagnosticsArchive zips reports, settings, failed jobs and log tails through it. The desktop bundle (Settings > Advanced, About) now adds the codec and encoder inventory, the settings and recent failed jobs; the server serves the same as GET /api/v1/server/diagnostics (ManageArchive, audited), linked from admin > Logs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
16 files changed
+757
-40
No files matched your search
@@ -0,0 +1,84 @@
|
||||
using System.Text.Json;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Diagnostics;
|
||||
|
||||
public sealed class SecretScrubberTests
|
||||
{
|
||||
private const string Secret = "hunter2-S3cr3t!";
|
||||
|
||||
[Theory]
|
||||
[Case("Host=db;Port=5432;Username=titano;Password=" + Secret + ";SslMode=prefer", "Host=db;Port=5432;Username=titano;Password=[removed];SslMode=prefer")]
|
||||
[Case("TITANO_DB_PASSWORD=" + Secret, "TITANO_DB_PASSWORD=[removed]")]
|
||||
[Case("DbPassword: " + Secret, "DbPassword: [removed]")]
|
||||
[Case("login body {\"username\":\"alice\",\"password\":\"" + Secret + "\"}", "login body {\"username\":\"alice\",\"password\":[removed]}")]
|
||||
[Case("Authorization: Bearer " + Secret, "Authorization: [removed]")]
|
||||
[Case("calling with bearer abcdefghijklmnop", "calling with bearer [removed]")]
|
||||
[Case("Cookie: titano_session=" + Secret + "; titano_csrf=x", "Cookie: [removed]")]
|
||||
[Case("git clone https://x-access-token:" + Secret + "@github.com/o/r.git", "git clone https://x-access-token:[removed]@github.com/o/r.git")]
|
||||
[Case("GET /api/v1/events?token=" + Secret + "&x=1", "GET /api/v1/events?token=[removed]&x=1")]
|
||||
[Case("client_secret=" + Secret + " refresh_token='" + Secret + "'", "client_secret=[removed] refresh_token=[removed]")]
|
||||
[Case("pushed with ghp_0123456789abcdefghijABCDEFGHIJ012345", "pushed with [removed]")]
|
||||
public void SecretsInTextAreRemoved(string line, string expected) => Assert.Equal(expected, SecretScrubber.ScrubLine(line));
|
||||
|
||||
[Theory]
|
||||
[Case("2026-10-02T09:00:00Z Info archive: Scan of /archive found 1,204 frames in 12 sequences")]
|
||||
[Case("Index open (schema 3) at C:\\Users\\alice\\AppData\\Local\\Titano\\titano.db")]
|
||||
[Case("frame 0193 sha256 4f2a9c0e1b7d3a5f6e8c9b0a1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7b8c9d0e verified")]
|
||||
[Case("Token revoked for user alice")]
|
||||
[Case("")]
|
||||
public void OrdinaryLinesAreKept(string line) => Assert.Equal(line, SecretScrubber.ScrubLine(line));
|
||||
|
||||
[Test]
|
||||
public void PrivateKeyBlocksAreDroppedWhole()
|
||||
{
|
||||
var text = "before\n-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC\nBKcwggSjAgEAAoIBAQC7\n-----END PRIVATE KEY-----\nafter";
|
||||
Assert.Equal("before\n[removed private key]\nafter", SecretScrubber.ScrubText(text));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SecretNamedJsonValuesAreRemovedAndOtherStringsScrubbed()
|
||||
{
|
||||
var json = $$"""
|
||||
{
|
||||
"ServerName": "NAS",
|
||||
"AdminPassword": "{{Secret}}",
|
||||
"DbPassword": null,
|
||||
"DbConnectionString": "Host=db;Password={{Secret}}",
|
||||
"TokenLifetimeDays": 30,
|
||||
"ProtectedToken": "AQAAANCMnd8BFdERjHoAwE",
|
||||
"TrustedCertificates": [ { "Fingerprint": "AB:CD:EF", "Server": "nas" } ],
|
||||
"Nested": { "api_key": "{{Secret}}", "SigningKeys": [ "{{Secret}}" ], "Credentials": { "User": "alice", "Value": "{{Secret}}" } }
|
||||
}
|
||||
""";
|
||||
var scrubbed = SecretScrubber.ScrubJson(json);
|
||||
Assert.False(scrubbed.Contains(Secret, StringComparison.Ordinal), scrubbed);
|
||||
Assert.False(scrubbed.Contains("AQAAANCMnd8BFdERjHoAwE", StringComparison.Ordinal), scrubbed);
|
||||
|
||||
using var doc = JsonDocument.Parse(scrubbed);
|
||||
var root = doc.RootElement;
|
||||
Assert.Equal("NAS", root.GetProperty("ServerName").GetString());
|
||||
Assert.Equal(SecretScrubber.Removed, root.GetProperty("AdminPassword").GetString());
|
||||
Assert.Equal(JsonValueKind.Null, root.GetProperty("DbPassword").ValueKind);
|
||||
Assert.Equal("Host=db;Password=[removed]", root.GetProperty("DbConnectionString").GetString());
|
||||
Assert.Equal(30, root.GetProperty("TokenLifetimeDays").GetInt32());
|
||||
Assert.Equal("AB:CD:EF", root.GetProperty("TrustedCertificates")[0].GetProperty("Fingerprint").GetString());
|
||||
Assert.Equal(SecretScrubber.Removed, root.GetProperty("Nested").GetProperty("Credentials").GetProperty("User").GetString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TextThatIsNotJsonIsScrubbedAsText() =>
|
||||
Assert.Equal("password=[removed] {", SecretScrubber.ScrubJson("password=" + Secret + " {"));
|
||||
|
||||
[Theory]
|
||||
[Case("DbPassword", true)]
|
||||
[Case("SIGN_PFX_BASE64", true)]
|
||||
[Case("X-Titano-Token", true)]
|
||||
[Case("set-cookie", true)]
|
||||
[Case("privateKey", true)]
|
||||
[Case("ServerName", false)]
|
||||
[Case("TlsCert", false)]
|
||||
[Case("SessionKey", false)]
|
||||
public void SecretNamesAreRecognised(string name, bool secret) => Assert.Equal(secret, SecretScrubber.IsSecretName(name));
|
||||
}
|
||||
Reference in new issue
Block a user