diff --git a/src/Titano.App/Infrastructure/DiagnosticsBundle.cs b/src/Titano.App/Infrastructure/DiagnosticsBundle.cs index 903552ca..bb00d67c 100644 --- a/src/Titano.App/Infrastructure/DiagnosticsBundle.cs +++ b/src/Titano.App/Infrastructure/DiagnosticsBundle.cs @@ -4,6 +4,7 @@ using System.Runtime.InteropServices; using System.Text; using Titano.App.Settings; using Titano.Core.Diagnostics; +using Titano.Core.Export; using Titano.Core.Model; using Titano.Core.Windows.Codecs; using Titano.Data; @@ -22,7 +23,8 @@ public static class DiagnosticsBundle public const int FailedJobs = 50; /// - /// Writes "Titano diagnostics yyyy-MM-dd HHmm.zip" into and returns its path. Call on the UI + /// Writes "Titano diagnostics yyyy-MM-dd HHmm.zip" (" (2)", " (3)"… when that name is taken) into + /// and returns its path; a bundle that fails half-way leaves no file behind. Call on the UI /// thread: the settings are captured there; detection and writing run in the background. /// public static Task CreateAsync(AppServices services, string folder, IEnumerable? extraLines = null, CancellationToken ct = default) @@ -33,19 +35,27 @@ public static class DiagnosticsBundle { var failures = services.Database is { } db ? (await db.Jobs.ListAsync(JobState.Failed, FailedJobs, null, ct).ConfigureAwait(false)).Items : []; var codecs = Codecs(services.Encoders); - var path = Path.Combine(folder, $"Titano diagnostics {DateTime.Now.ToString("yyyy-MM-dd HHmm", CultureInfo.InvariantCulture)}.zip"); + var path = ExportNaming.Unique(folder, $"Titano diagnostics {DateTime.Now.ToString("yyyy-MM-dd HHmm", CultureInfo.InvariantCulture)}", ".zip"); var temp = path + ".tmp"; - await using (var file = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.None)) - using (var bundle = new DiagnosticsArchive(file)) + try { - bundle.AddText("environment.txt", report); - bundle.AddText("codecs.txt", codecs); - bundle.AddJson("settings.json", settings); - bundle.AddJobs("jobs-failed.json", failures); - bundle.AddLogs(services.Environment.LogDirectory, cancellationToken: ct); + await using (var file = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.None)) + using (var bundle = new DiagnosticsArchive(file)) + { + bundle.AddText("environment.txt", report); + bundle.AddText("codecs.txt", codecs); + bundle.AddJson("settings.json", settings); + bundle.AddJobs("jobs-failed.json", failures); + bundle.AddLogs(services.Environment.LogDirectory, cancellationToken: ct); + } + File.Move(temp, path); + return path; + } + finally + { + // Gone after a successful move; after a failure a half-written zip helps nobody. + if (File.Exists(temp)) File.Delete(temp); } - File.Move(temp, path, overwrite: true); - return path; }, ct); } diff --git a/src/Titano.Core/Diagnostics/SecretScrubber.cs b/src/Titano.Core/Diagnostics/SecretScrubber.cs index bf614edb..6d0cf943 100644 --- a/src/Titano.Core/Diagnostics/SecretScrubber.cs +++ b/src/Titano.Core/Diagnostics/SecretScrubber.cs @@ -116,8 +116,8 @@ public static partial class SecretScrubber [GeneratedRegex(@"(?\b(?:bearer|basic))\s+[A-Za-z0-9._~+/=-]{6,}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)] private static partial Regex AuthScheme(); - /// "https://user:secret@host" keeps the user name. - [GeneratedRegex(@"(?\b[a-z][a-z0-9+.-]*://[^/\s:@]+:)[^/\s@]+@", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)] + /// "https://user:secret@host" keeps the user name; the password runs to the last '@' before the path ("p@ss@db"). + [GeneratedRegex(@"(?\b[a-z][a-z0-9+.-]*://[^/\s:@]+:)[^/\s]+@", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)] private static partial Regex UrlCredentials(); /// "?token=…", "&access_token=…", "&sig=…". @@ -125,10 +125,12 @@ public static partial class SecretScrubber private static partial Regex QueryParameter(); /// - /// "Password=…", "DbPassword: …", "\"token\": \"…\"", "client_secret=…" (connection strings, settings dumps, JSON in - /// log messages): the value up to the next separator goes (a value an earlier rule already removed is left alone). + /// "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. /// - [GeneratedRegex(@"(?[A-Za-z0-9_-]*(?:password|passwd|pwd|secret|token|api[_-]?key|access[_-]?key|private[_-]?key)[A-Za-z0-9_-]*[""']?\s*[=:]\s*)(?!\[removed\])(?:""[^""]*""|'[^']*'|[^\s;,&""']+)", + [GeneratedRegex(@"(?[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(); diff --git a/src/Titano.Server/Api/DiagnosticsEndpoints.cs b/src/Titano.Server/Api/DiagnosticsEndpoints.cs index 07c7dfe5..802ef10d 100644 --- a/src/Titano.Server/Api/DiagnosticsEndpoints.cs +++ b/src/Titano.Server/Api/DiagnosticsEndpoints.cs @@ -33,8 +33,13 @@ public static class DiagnosticsEndpoints var settings = JsonSerializer.Serialize(options, ServerOptionsJson.Default.ServerOptions); var encoders = Encoders(services.GetService()); - // The bundle is built in a self-deleting temporary file: zip writes are synchronous and logs can be large. - var file = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite, FileShare.None, 1 << 16, FileOptions.DeleteOnClose); + // The bundle is built in a temporary file (zip writes are synchronous and logs can be large) that no crash leaves + // behind: Windows deletes it when the handle closes; elsewhere it is unlinked at once and lives on through the + // open handle (DeleteOnClose there would unlink by name at close, possibly a newer file of the same name). + var temp = Path.GetTempFileName(); + var file = new FileStream(temp, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 1 << 16, + OperatingSystem.IsWindows() ? FileOptions.DeleteOnClose : FileOptions.None); + if (!OperatingSystem.IsWindows()) File.Delete(temp); try { await Task.Run(() => diff --git a/tests/Titano.App.Tests/Windows/DiagnosticsBundleTests.cs b/tests/Titano.App.Tests/Windows/DiagnosticsBundleTests.cs index 6bb5a4ca..6c344b5c 100644 --- a/tests/Titano.App.Tests/Windows/DiagnosticsBundleTests.cs +++ b/tests/Titano.App.Tests/Windows/DiagnosticsBundleTests.cs @@ -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(() => 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 Detect() => - [new EncoderRow("H.264", "Test encoder", null, EncoderSource.MediaFoundation, true, false, 4096, 2304, VideoCodec.H264)]; + public IReadOnlyList Detect() + { + onDetect?.Invoke(); + return [new EncoderRow("H.264", "Test encoder", null, EncoderSource.MediaFoundation, true, false, 4096, 2304, VideoCodec.H264)]; + } public Task SelfTestAsync(EncoderRow row, CancellationToken ct) => Task.FromResult(new SelfTestResult(true, 100, null)); } diff --git a/tests/Titano.Core.Tests/Diagnostics/SecretScrubberTests.cs b/tests/Titano.Core.Tests/Diagnostics/SecretScrubberTests.cs index 0daa141e..f6592f01 100644 --- a/tests/Titano.Core.Tests/Diagnostics/SecretScrubberTests.cs +++ b/tests/Titano.Core.Tests/Diagnostics/SecretScrubberTests.cs @@ -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)] diff --git a/tests/Titano.Server.Tests/Api/DiagnosticsTests.cs b/tests/Titano.Server.Tests/Api/DiagnosticsTests.cs index d3e4063e..1139c28b 100644 --- a/tests/Titano.Server.Tests/Api/DiagnosticsTests.cs +++ b/tests/Titano.Server.Tests/Api/DiagnosticsTests.cs @@ -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");