Files
Chronos/tests/Titano.Core.Tests/Diagnostics/SecretScrubberTests.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

99 lines
5.7 KiB
C#

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("refresh_token='" + Secret + "' client_secret=" + Secret, "refresh_token=[removed] client_secret=[removed]")]
[Case("pushed with ghp_0123456789abcdefghijABCDEFGHIJ012345", "pushed with [removed]")]
public void SecretsInTextAreRemoved(string line, string expected) => Assert.Equal(expected, SecretScrubber.ScrubLine(line));
[Theory]
[Case("Host=db;Password=correct horse battery;SslMode=prefer", "Host=db;Password=[removed];SslMode=prefer")]
[Case("Host=db;Password=ab,cd;Port=5432", "Host=db;Password=[removed];Port=5432")]
[Case("Server=db;PWD='semi;colon and space';User ID=sa", "Server=db;PWD=[removed];User ID=sa")]
[Case("Host=db;Password=\"quoted, \"\" pw\";Port=5432", "Host=db;Password=[removed];Port=5432")]
[Case("TITANO_ADMIN_PASSWORD=Very Secret phrase", "TITANO_ADMIN_PASSWORD=[removed]")]
[Case("DbPassword: it's a, secret", "DbPassword: [removed]")]
[Case("login body {\"password\":\"a \\\"quoted\\\" pass, word\",\"user\":\"bob\"}", "login body {\"password\":[removed],\"user\":\"bob\"}")]
[Case("postgres://titano:p@ss@db:5432/titano", "postgres://titano:[removed]@db:5432/titano")]
[Case("https://alice:pa:ss@nas.local/api", "https://alice:[removed]@nas.local/api")]
public void PasswordsWithSpacesCommasQuotesOrAtSignsLeaveNothingBehind(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;Username=titano;Password=correct horse, battery;SslMode=prefer",
"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;Username=titano;Password=[removed];SslMode=prefer", root.GetProperty("DbConnectionString").GetString());
Assert.False(scrubbed.Contains("horse", StringComparison.Ordinal), scrubbed);
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]\n{ \"kept\": 1", SecretScrubber.ScrubJson("password=" + Secret + "\r\n{ \"kept\": 1"));
[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));
}