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

@@ -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");