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
This commit is contained in:
Claude committed 2026-10-03 10:05:45 +00:00
1 parent ffa05d9f69
commit 03da77277c
6 files changed
+95 -27

No files matched your search

@@ -45,12 +45,44 @@ public sealed class DiagnosticsBundleTests(TestContext context)
}
}
private sealed class OneEncoder : IEncoderInventory
[Test]
public async Task BundlesNeverOverwriteEachOtherAndAFailedOneLeavesNothing()
{
var environment = AppEnvironment.ForCapture();
try
{
var services = new AppServices(environment, new AppSettings { Language = "en" }, () => { }, new NoDialogs()) { Encoders = new OneEncoder() };
await File.WriteAllTextAsync(Path.Combine(environment.LogDirectory, "titano-20261002.log"), "started\n");
var folder = context.CreateTempDirectory();
var first = await DiagnosticsBundle.CreateAsync(services, folder);
var second = await DiagnosticsBundle.CreateAsync(services, folder);
Assert.NotEqual(first, second);
Assert.SequenceEqual(new[] { first, second }.Order(StringComparer.Ordinal), Directory.GetFiles(folder).Order(StringComparer.Ordinal));
// The encoder inventory runs after the job query and before the zip is written: cancelling there fails the
// bundle half-way, once its temporary file exists.
using var cts = new CancellationTokenSource();
services.Encoders = new OneEncoder(cts.Cancel);
var empty = context.CreateTempDirectory();
await Assert.ThrowsAsync<OperationCanceledException>(() => DiagnosticsBundle.CreateAsync(services, empty, ct: cts.Token));
Assert.Empty(Directory.GetFiles(empty));
}
finally
{
environment.DeleteCaptureData();
}
}
private sealed class OneEncoder(Action? onDetect = null) : IEncoderInventory
{
public string? Adapter() => "Test adapter";
public IReadOnlyList<EncoderRow> Detect() =>
[new EncoderRow("H.264", "Test encoder", null, EncoderSource.MediaFoundation, true, false, 4096, 2304, VideoCodec.H264)];
public IReadOnlyList<EncoderRow> Detect()
{
onDetect?.Invoke();
return [new EncoderRow("H.264", "Test encoder", null, EncoderSource.MediaFoundation, true, false, 4096, 2304, VideoCodec.H264)];
}
public Task<SelfTestResult> SelfTestAsync(EncoderRow row, CancellationToken ct) => Task.FromResult(new SelfTestResult(true, 100, null));
}
@@ -18,10 +18,23 @@ public sealed class SecretScrubberTests
[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("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")]
@@ -45,7 +58,7 @@ public sealed class SecretScrubberTests
"ServerName": "NAS",
"AdminPassword": "{{Secret}}",
"DbPassword": null,
"DbConnectionString": "Host=db;Password={{Secret}}",
"DbConnectionString": "Host=db;Username=titano;Password=correct horse, battery;SslMode=prefer",
"TokenLifetimeDays": 30,
"ProtectedToken": "AQAAANCMnd8BFdERjHoAwE",
"TrustedCertificates": [ { "Fingerprint": "AB:CD:EF", "Server": "nas" } ],
@@ -61,7 +74,8 @@ public sealed class SecretScrubberTests
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("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());
@@ -69,7 +83,7 @@ public sealed class SecretScrubberTests
[Test]
public void TextThatIsNotJsonIsScrubbedAsText() =>
Assert.Equal("password=[removed] {", SecretScrubber.ScrubJson("password=" + Secret + " {"));
Assert.Equal("password=[removed]\n{ \"kept\": 1", SecretScrubber.ScrubJson("password=" + Secret + "\r\n{ \"kept\": 1"));
[Theory]
[Case("DbPassword", true)]
@@ -10,18 +10,21 @@ namespace Titano.Server.Tests.Api;
public sealed class DiagnosticsTests(TestContext context)
{
private const string DbPassword = "db-secret-pw-42";
private const string ConnectionPassword = "correct horse, \"battery\" staple";
[Test]
public async Task TheBundleHoldsReportsLogsAndFailuresWithoutSecrets()
{
await using var server = await TestServer.StartAsync(context, o => o.DbPassword = DbPassword);
var admin = await server.AdminAsync();
// Only reported, never opened: the test server already runs on its own database.
server.Options.DbConnectionString = $"Host=db;Username=titano;Password={ConnectionPassword};SslMode=prefer";
Directory.CreateDirectory(server.Options.LogPath);
await File.WriteAllTextAsync(Path.Combine(server.Options.LogPath, "titano-server-test.log"),
$"connecting with Password={DbPassword}\nAuthorization: Bearer {TestServer.Password}\n");
var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Render, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
await server.Database.Jobs.EnqueueAsync(job);
await server.Database.Jobs.SetStateAsync(job.Id, JobState.Failed, "token=" + DbPassword + " rejected");
await server.Database.Jobs.SetStateAsync(job.Id, JobState.Failed, "rejected: token=" + DbPassword + " for db");
using var response = await admin.GetAsync("/api/v1/server/diagnostics");
await response.ExpectAsync(HttpStatusCode.OK);
@@ -38,17 +41,19 @@ public sealed class DiagnosticsTests(TestContext context)
Assert.False(text.Contains(DbPassword, StringComparison.Ordinal), $"{name} leaks the database password");
Assert.False(text.Contains(TestServer.AdminPassword, StringComparison.Ordinal), $"{name} leaks the admin password");
Assert.False(text.Contains(TestServer.Password, StringComparison.Ordinal), $"{name} leaks a user password");
Assert.False(text.Contains("horse", StringComparison.Ordinal) || text.Contains("staple", StringComparison.Ordinal), $"{name} leaks the connection-string password");
}
Assert.Contains("Titano Server ", texts["environment.txt"]);
using (var settings = JsonDocument.Parse(texts["settings.json"]))
{
Assert.Equal("[removed]", settings.RootElement.GetProperty("DbPassword").GetString());
Assert.Equal("[removed]", settings.RootElement.GetProperty("AdminPassword").GetString());
Assert.Equal("Host=db;Username=titano;Password=[removed];SslMode=prefer", settings.RootElement.GetProperty("DbConnectionString").GetString());
}
using (var failed = JsonDocument.Parse(texts["jobs-failed.json"]))
{
var entry = failed.RootElement.EnumerateArray().Single();
Assert.Equal(("Render", "token=[removed] rejected"), (entry.GetProperty("kind").GetString(), entry.GetProperty("error").GetString()));
Assert.Equal(("Render", "rejected: token=[removed]"), (entry.GetProperty("kind").GetString(), entry.GetProperty("error").GetString()));
}
var (viewer, _, _) = await server.UserAsync("vera", "viewer");