Files
Chronos/tests/Titano.Server.Tests/Api/DiagnosticsTests.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

71 lines
3.9 KiB
C#

using System.IO.Compression;
using System.Net;
using System.Text.Json;
using Titano.Core.Model;
using Titano.Testing;
namespace Titano.Server.Tests.Api;
[Category("server")]
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, "rejected: token=" + DbPassword + " for db");
using var response = await admin.GetAsync("/api/v1/server/diagnostics");
await response.ExpectAsync(HttpStatusCode.OK);
Assert.Equal("application/zip", response.Content.Headers.ContentType?.MediaType);
await using var body = await response.Content.ReadAsStreamAsync();
using var zip = new ZipArchive(body, ZipArchiveMode.Read);
var texts = zip.Entries.ToDictionary(e => e.FullName, Read);
Assert.Contains("environment.txt", texts.Keys);
Assert.Contains("encoders.txt", texts.Keys);
Assert.Contains("logs/titano-server-test.log", texts.Keys);
foreach (var (name, text) in texts)
{
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", "rejected: token=[removed]"), (entry.GetProperty("kind").GetString(), entry.GetProperty("error").GetString()));
}
var (viewer, _, _) = await server.UserAsync("vera", "viewer");
using var refused = await viewer.GetAsync("/api/v1/server/diagnostics");
await refused.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
Assert.NotEmpty((await server.Database.Audit.ListAsync(null, "server_diagnostics", 10, null)).Items);
}
private static string Read(ZipArchiveEntry entry)
{
using var reader = new StreamReader(entry.Open());
return reader.ReadToEnd();
}
}