feat(diagnostics): scrubbed diagnostics bundles on the desktop and the server

SecretScrubber removes passwords, tokens, keys, cookies, authorisation
headers, URL credentials and private-key blocks from text and JSON;
DiagnosticsArchive zips reports, settings, failed jobs and log tails through
it. The desktop bundle (Settings > Advanced, About) now adds the codec and
encoder inventory, the settings and recent failed jobs; the server serves
the same as GET /api/v1/server/diagnostics (ManageArchive, audited), linked
from admin > Logs.

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-02 22:29:26 +00:00
1 parent 85d091d1fe
commit 2650aa31fb
16 files changed
+757 -40

No files matched your search

+3
View File
@@ -175,11 +175,14 @@
<data name="Admin.Logs.Action.SequenceCreated" xml:space="preserve"><value>Sequence created</value></data>
<data name="Admin.Logs.Action.SequenceDeleted" xml:space="preserve"><value>Sequence deleted</value></data>
<data name="Admin.Logs.Action.SequenceUpdated" xml:space="preserve"><value>Sequence edited</value></data>
<data name="Admin.Logs.Action.ServerDiagnostics" xml:space="preserve"><value>Diagnostics exported</value></data>
<data name="Admin.Logs.Action.ServerSettings" xml:space="preserve"><value>Server settings</value></data>
<data name="Admin.Logs.Action.UserCreated" xml:space="preserve"><value>User created</value></data>
<data name="Admin.Logs.Action.UserDeleted" xml:space="preserve"><value>User deleted</value></data>
<data name="Admin.Logs.Action.UserUpdated" xml:space="preserve"><value>User changed</value></data>
<data name="Admin.Logs.Details" xml:space="preserve"><value>Details</value></data>
<data name="Admin.Logs.Diagnostics" xml:space="preserve"><value>Diagnostics</value></data>
<data name="Admin.Logs.Diagnostics.Tip" xml:space="preserve"><value>Download a zip with logs, system report, settings and recent failed jobs; passwords and tokens are left out</value></data>
<data name="Admin.Logs.Download" xml:space="preserve"><value>Download</value></data>
<data name="Admin.Logs.Download.Tip" xml:space="preserve"><value>Save this log file</value></data>
<data name="Admin.Logs.Empty" xml:space="preserve"><value>Nothing recorded yet</value></data>
+2 -2
View File
@@ -5,7 +5,7 @@
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<data name="Tooltips.About.Check" xml:space="preserve"><value>Ask the public releases page for a newer version · a few seconds</value></data>
<data name="Tooltips.About.Diagnostics" xml:space="preserve"><value>Zip the logs and an environment report into a folder you choose; settings and passwords are not included</value></data>
<data name="Tooltips.About.Diagnostics" xml:space="preserve"><value>Zip the logs, a system and codec report, settings and recent failed jobs into a folder you choose; passwords, tokens and photos are left out</value></data>
<data name="Tooltips.About.Download" xml:space="preserve"><value>Download the setup, check it against the release's SHA-256 list and show it in Explorer · tens of MB</value></data>
<data name="Tooltips.About.Licence" xml:space="preserve"><value>Open the licence in the browser</value></data>
<data name="Tooltips.About.OpenData" xml:space="preserve"><value>Open the folder with database, settings and logs</value></data>
@@ -136,7 +136,7 @@
<data name="Tooltips.Settings.ClearCacheAsk" xml:space="preserve"><value>Delete spilled frames and cached thumbnails; asks first</value></data>
<data name="Tooltips.Settings.DataFolder" xml:space="preserve"><value>Database, settings, logs and caches of this PC</value></data>
<data name="Tooltips.Settings.DefaultServer" xml:space="preserve"><value>Server Titano connects to at start; changing it signs out · Enter applies, Esc reverts</value></data>
<data name="Tooltips.Settings.Diagnostics" xml:space="preserve"><value>Zip the logs and an environment report into a folder you choose</value></data>
<data name="Tooltips.Settings.Diagnostics" xml:space="preserve"><value>Zip the logs, a system and codec report, settings and recent failed jobs into a folder you choose; passwords, tokens and photos are left out</value></data>
<data name="Tooltips.Settings.Export" xml:space="preserve"><value>Save every setting as JSON, without the session token</value></data>
<data name="Tooltips.Settings.FileTemplate" xml:space="preserve"><value>File name of each frame, for example {date}_{time}_{n:0000}; the extension is added · Enter applies, Esc reverts</value></data>
<data name="Tooltips.Settings.FolderTemplate" xml:space="preserve"><value>Archive folders, for example {year}/{date} {camera}; a slash starts a subfolder · Enter applies, Esc reverts</value></data>
+3
View File
@@ -175,11 +175,14 @@
<data name="Admin.Logs.Action.SequenceCreated" xml:space="preserve"><value>Sequenza creata</value></data>
<data name="Admin.Logs.Action.SequenceDeleted" xml:space="preserve"><value>Sequenza eliminata</value></data>
<data name="Admin.Logs.Action.SequenceUpdated" xml:space="preserve"><value>Sequenza modificata</value></data>
<data name="Admin.Logs.Action.ServerDiagnostics" xml:space="preserve"><value>Diagnostica esportata</value></data>
<data name="Admin.Logs.Action.ServerSettings" xml:space="preserve"><value>Impostazioni server</value></data>
<data name="Admin.Logs.Action.UserCreated" xml:space="preserve"><value>Utente creato</value></data>
<data name="Admin.Logs.Action.UserDeleted" xml:space="preserve"><value>Utente eliminato</value></data>
<data name="Admin.Logs.Action.UserUpdated" xml:space="preserve"><value>Utente modificato</value></data>
<data name="Admin.Logs.Details" xml:space="preserve"><value>Dettagli</value></data>
<data name="Admin.Logs.Diagnostics" xml:space="preserve"><value>Diagnostica</value></data>
<data name="Admin.Logs.Diagnostics.Tip" xml:space="preserve"><value>Scarica uno zip con log, rapporto di sistema, impostazioni e attività fallite di recente; password e token restano fuori</value></data>
<data name="Admin.Logs.Download" xml:space="preserve"><value>Scarica</value></data>
<data name="Admin.Logs.Download.Tip" xml:space="preserve"><value>Salva questo file di log</value></data>
<data name="Admin.Logs.Empty" xml:space="preserve"><value>Ancora nessuna registrazione</value></data>
+2 -2
View File
@@ -5,7 +5,7 @@
<resheader name="reader"><value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<resheader name="writer"><value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value></resheader>
<data name="Tooltips.About.Check" xml:space="preserve"><value>Chiede alla pagina pubblica delle versioni se ne esiste una più recente · pochi secondi</value></data>
<data name="Tooltips.About.Diagnostics" xml:space="preserve"><value>Comprime i log e un rapporto sull'ambiente in una cartella a tua scelta; impostazioni e password non sono incluse</value></data>
<data name="Tooltips.About.Diagnostics" xml:space="preserve"><value>Comprime log, rapporto su sistema e codec, impostazioni e attività fallite di recente in una cartella a tua scelta; password, token e foto restano fuori</value></data>
<data name="Tooltips.About.Download" xml:space="preserve"><value>Scarica l'installer, lo confronta con l'elenco SHA-256 della versione e lo mostra in Esplora file · decine di MB</value></data>
<data name="Tooltips.About.Licence" xml:space="preserve"><value>Apre la licenza nel browser</value></data>
<data name="Tooltips.About.OpenData" xml:space="preserve"><value>Apri la cartella con database, impostazioni e log</value></data>
@@ -136,7 +136,7 @@
<data name="Tooltips.Settings.ClearCacheAsk" xml:space="preserve"><value>Elimina fotogrammi temporanei e miniature in cache; prima chiede conferma</value></data>
<data name="Tooltips.Settings.DataFolder" xml:space="preserve"><value>Database, impostazioni, log e cache di questo PC</value></data>
<data name="Tooltips.Settings.DefaultServer" xml:space="preserve"><value>Server a cui Titano si connette all'avvio; cambiarlo chiude l'accesso · Invio applica, Esc annulla</value></data>
<data name="Tooltips.Settings.Diagnostics" xml:space="preserve"><value>Comprime i log e un rapporto sull'ambiente in una cartella a tua scelta</value></data>
<data name="Tooltips.Settings.Diagnostics" xml:space="preserve"><value>Comprime log, rapporto su sistema e codec, impostazioni e attività fallite di recente in una cartella a tua scelta; password, token e foto restano fuori</value></data>
<data name="Tooltips.Settings.Export" xml:space="preserve"><value>Salva tutte le impostazioni in JSON, senza il token di sessione</value></data>
<data name="Tooltips.Settings.FileTemplate" xml:space="preserve"><value>Nome del file di ogni fotogramma, ad esempio {date}_{time}_{n:0000}; l'estensione viene aggiunta · Invio applica, Esc annulla</value></data>
<data name="Tooltips.Settings.FolderTemplate" xml:space="preserve"><value>Cartelle dell'archivio, ad esempio {year}/{date} {camera}; la barra crea una sottocartella · Invio applica, Esc annulla</value></data>
@@ -1,58 +1,60 @@
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Text;
using Titano.App.Settings;
using Titano.Core.Diagnostics;
using Titano.Core.Model;
using Titano.Core.Windows.Codecs;
using Titano.Data;
namespace Titano.App.Infrastructure;
/// <summary>
/// The diagnostics bundle of About and Settings › Advanced (BRIEF §12.9): the log files plus an environment report
/// (versions, OS, runtime, culture, folders), zipped into a folder the user chose. Settings and tokens are not included.
/// The diagnostics bundle of About and Settings › Advanced (BRIEF §12.9, §22 Phase 8): an environment report, the codec
/// and encoder inventory, the settings, the latest failed jobs and the log files, zipped into a folder the user chose.
/// Everything passes through <see cref="SecretScrubber"/> (the saved server token, passwords and keys never leave the
/// PC) and no photo or archive file is ever included.
/// </summary>
public static class DiagnosticsBundle
{
/// <summary>Writes "Titano diagnostics yyyy-MM-dd HHmm.zip" into <paramref name="folder"/> and returns its path.</summary>
public static Task<string> CreateAsync(AppEnvironment environment, string folder, IEnumerable<string>? extraLines = null, CancellationToken ct = default) =>
Task.Run(() =>
/// <summary>Failed jobs included, newest first.</summary>
public const int FailedJobs = 50;
/// <summary>
/// Writes "Titano diagnostics yyyy-MM-dd HHmm.zip" into <paramref name="folder"/> and returns its path. Call on the UI
/// thread: the settings are captured there; detection and writing run in the background.
/// </summary>
public static Task<string> CreateAsync(AppServices services, string folder, IEnumerable<string>? extraLines = null, CancellationToken ct = default)
{
var settings = services.Settings.ToJson();
var report = Report(services.Environment, extraLines) + DatabaseLine(services.Database);
return Task.Run(async () =>
{
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 temp = path + ".tmp";
using (var zip = ZipFile.Open(temp, ZipArchiveMode.Create))
await using (var file = new FileStream(temp, FileMode.Create, FileAccess.Write, FileShare.None))
using (var bundle = new DiagnosticsArchive(file))
{
var report = zip.CreateEntry("environment.txt", CompressionLevel.Optimal);
using (var writer = new StreamWriter(report.Open(), new UTF8Encoding(false)))
{
writer.Write(Report(environment, extraLines));
}
if (Directory.Exists(environment.LogDirectory))
{
foreach (var log in Directory.EnumerateFiles(environment.LogDirectory, "*.log").OrderBy(f => f, StringComparer.Ordinal))
{
ct.ThrowIfCancellationRequested();
var entry = zip.CreateEntry("logs/" + Path.GetFileName(log), CompressionLevel.Optimal);
// The current log is open for writing: share the handle instead of copying the file.
using var source = new FileStream(log, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var target = entry.Open();
source.CopyTo(target);
}
}
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, overwrite: true);
return path;
}, ct);
}
/// <summary>Plain-text environment report.</summary>
public static string Report(AppEnvironment environment, IEnumerable<string>? extraLines = null)
{
var sb = new StringBuilder();
sb.AppendLine(CultureInfo.InvariantCulture, $"Titano {AppServices.VersionText}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Created {DateTimeOffset.Now:O}");
sb.AppendLine(CultureInfo.InvariantCulture, $"OS {RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})");
sb.AppendLine(CultureInfo.InvariantCulture, $"Runtime {RuntimeInformation.FrameworkDescription} ({RuntimeInformation.ProcessArchitecture})");
sb.AppendLine(CultureInfo.InvariantCulture, $"Culture {CultureInfo.CurrentCulture.Name} / UI {CultureInfo.CurrentUICulture.Name}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Processors {Environment.ProcessorCount}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Memory {GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / (1024 * 1024)} MB");
sb.Append(DiagnosticsArchive.SystemReport());
sb.AppendLine(CultureInfo.InvariantCulture, $"Data {environment.DataDirectory} (portable: {environment.IsPortable})");
if (extraLines is not null)
{
@@ -60,6 +62,38 @@ public static class DiagnosticsBundle
}
return sb.ToString();
}
private static string DatabaseLine(TitanoDatabase? database) =>
database is null ? "Index not open\n" : string.Create(CultureInfo.InvariantCulture, $"Index schema {database.SchemaVersion}\n");
/// <summary>Graphics adapter, video encoders, WIC decoders per format and missing Store extensions.</summary>
private static string Codecs(IEncoderInventory encoders)
{
var sb = new StringBuilder();
sb.AppendLine(CultureInfo.InvariantCulture, $"Graphics adapter: {encoders.Adapter() ?? "none"}");
sb.AppendLine("Encoders:");
foreach (var e in encoders.Detect())
{
var size = e.MaxWidth > 0 ? string.Create(CultureInfo.InvariantCulture, $" max {e.MaxWidth}x{e.MaxHeight}") : string.Empty;
sb.AppendLine(CultureInfo.InvariantCulture, $" {e.Codec} · {e.DisplayName} ({(e.Hardware ? "hardware" : "software")}{(e.TenBit ? ", 10-bit" : string.Empty)}{size})");
}
try
{
var inventory = CodecInventory.Detect();
sb.AppendLine(CultureInfo.InvariantCulture, $"HEVC decoder: {inventory.HasHevcDecoder}; AV1 decoder: {inventory.HasAv1Decoder}");
sb.AppendLine("Image formats (WIC):");
foreach (var f in inventory.Formats)
{
var missing = f.MissingExtensions.Count == 0 ? string.Empty : "; missing " + string.Join(", ", f.MissingExtensions.Select(x => x.ProductName));
sb.AppendLine(CultureInfo.InvariantCulture, $" {f.Format}: {(f.IsNative ? "native" : f.Decoder is null ? "no decoder" : "preview only")}{(f.Decoder is { } d ? $" ({d.FriendlyName} {d.Version})" : string.Empty)}{missing}");
}
}
catch (Exception ex) when (ex is COMException or DllNotFoundException or EntryPointNotFoundException or InvalidOperationException)
{
sb.AppendLine("WIC decoders could not be listed: " + ex.Message);
}
return sb.ToString();
}
}
/// <summary>
+9 -2
View File
@@ -195,8 +195,15 @@ public sealed class AboutViewModel : SectionViewModel
{
var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Settings.Advanced.DiagnosticsTitle"));
if (folder is null) return;
var path = await DiagnosticsBundle.CreateAsync(Shell.Services.Environment, folder);
Shell.Notify(Loc.L("Settings.Advanced.DiagnosticsDone"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path));
try
{
var path = await DiagnosticsBundle.CreateAsync(Shell.Services, folder);
Shell.Notify(Loc.L("Settings.Advanced.DiagnosticsDone"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or Titano.Data.DatabaseException)
{
Shell.ReportError(ex);
}
}
private void RaiseUpdate()
+10 -3
View File
@@ -647,9 +647,16 @@ public sealed class SettingsViewModel : SectionViewModel
{
var folder = Shell.Services.Dialogs.PickFolder(Loc.L("Settings.Advanced.DiagnosticsTitle"));
if (folder is null) return;
var extra = Encoders.Select(e => $"Encoder {e.Codec} {e.Row.DisplayName} hardware={e.Row.Hardware} selftest={e.ResultText}");
var path = await DiagnosticsBundle.CreateAsync(Shell.Services.Environment, folder, extra.ToList());
Shell.Notify(Loc.L("Settings.Advanced.DiagnosticsDone"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path));
var selfTests = Encoders.Where(e => e.HasResult).Select(e => $"Self-test {e.Codec} {e.Row.DisplayName}: {e.ResultText}").ToList();
try
{
var path = await DiagnosticsBundle.CreateAsync(Shell.Services, folder, selfTests);
Shell.Notify(Loc.L("Settings.Advanced.DiagnosticsDone"), Path.GetFileName(path), Loc.L("Library.Export.Show"), Loc.L("Tooltips.Library.ShowExport"), () => ShellActions.Reveal(path));
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or DatabaseException)
{
Shell.ReportError(ex);
}
}
private async Task BackupDatabaseAsync()
@@ -0,0 +1,127 @@
using System.Globalization;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using Titano.Core.Model;
namespace Titano.Core.Diagnostics;
/// <summary>
/// Writes a diagnostics bundle (Settings › Advanced, admin panel › Logs; BRIEF §22 Phase 8): a zip in which every entry
/// passes through <see cref="SecretScrubber"/>. Only what the caller hands over goes in — reports, settings, job
/// failures and the tail of each log — never photos or other user files.
/// </summary>
public sealed class DiagnosticsArchive : IDisposable
{
/// <summary>Bytes kept from the end of each log file.</summary>
public const long DefaultLogTailBytes = 4L << 20;
/// <summary>Newest log files included.</summary>
public const int DefaultLogFiles = 14;
private readonly ZipArchive _zip;
/// <summary>Writes into <paramref name="output"/> (left open).</summary>
public DiagnosticsArchive(Stream output) => _zip = new ZipArchive(output, ZipArchiveMode.Create, leaveOpen: true);
/// <summary>Operating system, runtime, processor, memory and culture, one fact per line.</summary>
public static string SystemReport()
{
var sb = new StringBuilder();
sb.AppendLine(CultureInfo.InvariantCulture, $"OS {RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})");
sb.AppendLine(CultureInfo.InvariantCulture, $"Runtime {RuntimeInformation.FrameworkDescription} ({RuntimeInformation.ProcessArchitecture})");
sb.AppendLine(CultureInfo.InvariantCulture, $"Processors {Environment.ProcessorCount}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Memory {GC.GetGCMemoryInfo().TotalAvailableMemoryBytes / (1024 * 1024)} MB");
sb.AppendLine(CultureInfo.InvariantCulture, $"Culture {CultureInfo.CurrentCulture.Name} / UI {CultureInfo.CurrentUICulture.Name}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Time zone {TimeZoneInfo.Local.Id} (UTC{TimeZoneInfo.Local.GetUtcOffset(DateTime.UtcNow):hh\\:mm})");
sb.AppendLine(CultureInfo.InvariantCulture, $"Created {DateTimeOffset.Now:O}");
return sb.ToString();
}
/// <summary>Adds a text entry, scrubbed.</summary>
public void AddText(string name, string text) => Write(name, SecretScrubber.ScrubText(text));
/// <summary>Adds a JSON entry with secret-named values removed and every other string scrubbed.</summary>
public void AddJson(string name, string json) => Write(name, SecretScrubber.ScrubJson(json));
/// <summary>Adds failed jobs (kind, times, attempts, node, error and log tail), scrubbed, as a JSON array.</summary>
public void AddJobs(string name, IEnumerable<Job> jobs)
{
using var buffer = new MemoryStream();
using (var w = new Utf8JsonWriter(buffer))
{
w.WriteStartArray();
foreach (var job in jobs)
{
w.WriteStartObject();
w.WriteString("id", job.Id);
w.WriteString("kind", job.Kind.ToString());
w.WriteString("state", job.State.ToString());
if (job.TargetLabel is { } target) w.WriteString("target", target);
w.WriteString("created", job.Created);
if (job.Started is { } started) w.WriteString("started", started);
if (job.Finished is { } finished) w.WriteString("finished", finished);
w.WriteNumber("attempts", job.Attempts);
if (job.NodeId is { } node) w.WriteString("node", node);
if (job.Error is { } error) w.WriteString("error", error);
if (job.LogTail is { } log) w.WriteString("log", log);
w.WriteEndObject();
}
w.WriteEndArray();
}
Write(name, SecretScrubber.ScrubJson(Encoding.UTF8.GetString(buffer.ToArray())));
}
/// <summary>
/// Adds the newest <paramref name="maxFiles"/> files matching <paramref name="pattern"/> under <c>logs/</c>, each cut
/// to its last <paramref name="tailBytes"/> and scrubbed line by line. Files being written are read through a shared
/// handle; a file that cannot be read becomes a note instead of failing the bundle.
/// </summary>
public void AddLogs(string directory, string pattern = "*.log", long tailBytes = DefaultLogTailBytes, int maxFiles = DefaultLogFiles,
CancellationToken cancellationToken = default)
{
if (!Directory.Exists(directory)) return;
var files = new DirectoryInfo(directory).EnumerateFiles(pattern)
.OrderByDescending(f => f.LastWriteTimeUtc).ThenByDescending(f => f.Name, StringComparer.Ordinal)
.Take(maxFiles);
foreach (var file in files)
{
cancellationToken.ThrowIfCancellationRequested();
var entry = _zip.CreateEntry("logs/" + file.Name, CompressionLevel.Optimal);
using var writer = new StreamWriter(entry.Open(), new UTF8Encoding(false));
try
{
using var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
var start = Math.Max(0, stream.Length - tailBytes);
stream.Position = start;
using var reader = new StreamReader(stream, Encoding.UTF8);
// A tail that starts mid-file begins with a partial line.
if (start > 0)
{
reader.ReadLine();
writer.WriteLine(string.Create(CultureInfo.InvariantCulture, $"[first {start:N0} bytes left out]"));
}
foreach (var line in SecretScrubber.ScrubLines(ReadLines(reader))) writer.WriteLine(line);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
writer.WriteLine("[unreadable: " + ex.Message + "]");
}
}
}
public void Dispose() => _zip.Dispose();
private void Write(string name, string text)
{
var entry = _zip.CreateEntry(name, CompressionLevel.Optimal);
using var writer = new StreamWriter(entry.Open(), new UTF8Encoding(false));
writer.Write(text);
}
private static IEnumerable<string> ReadLines(StreamReader reader)
{
while (reader.ReadLine() is { } line) yield return line;
}
}
@@ -0,0 +1,138 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
namespace Titano.Core.Diagnostics;
/// <summary>
/// Removes secrets from text and JSON before they leave the machine in a diagnostics bundle: passwords, tokens, keys,
/// cookies, authorisation headers, credentials in URLs and connection strings, private-key blocks. It errs towards
/// removing too much: a lost hint costs less than a leaked secret.
/// </summary>
public static partial class SecretScrubber
{
public const string Removed = "[removed]";
/// <summary>Fragments of property names whose string values are secret (compared without case, '_', '-' or '.').</summary>
private static readonly string[] SecretNames =
["password", "passwd", "pwd", "secret", "token", "apikey", "accesskey", "privatekey", "signingkey", "credential", "cookie", "authorization", "pfx"];
/// <summary>True when a setting or header with this name holds a secret.</summary>
public static bool IsSecretName(string name)
{
var normalized = string.Concat(name.Where(c => c is not ('_' or '-' or '.'))).ToLowerInvariant();
return SecretNames.Any(normalized.Contains);
}
/// <summary>
/// The JSON with every secret-named string value replaced and every other string scrubbed as text; numbers and
/// booleans stay (a "TokenLifetimeDays" is no secret). Text that is not JSON is scrubbed as text.
/// </summary>
public static string ScrubJson(string json)
{
JsonNode? root;
try
{
root = JsonNode.Parse(json);
}
catch (JsonException)
{
return ScrubText(json);
}
if (root is null) return json;
root = ScrubNode(root, secret: false);
return root.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
}
/// <summary>Scrubs a block of text (several lines; private-key blocks are removed whole).</summary>
public static string ScrubText(string text) =>
string.Join('\n', ScrubLines(text.Replace("\r\n", "\n", StringComparison.Ordinal).Split('\n')));
/// <summary>Scrubs lines one by one, dropping the body of any PEM private-key block.</summary>
public static IEnumerable<string> ScrubLines(IEnumerable<string> lines)
{
var inKey = false;
foreach (var line in lines)
{
if (!inKey && line.Contains("-----BEGIN", StringComparison.Ordinal) && line.Contains("PRIVATE KEY", StringComparison.Ordinal))
{
inKey = !line.Contains("-----END", StringComparison.Ordinal);
yield return "[removed private key]";
continue;
}
if (inKey)
{
if (line.Contains("-----END", StringComparison.Ordinal)) inKey = false;
continue;
}
yield return ScrubLine(line);
}
}
/// <summary>Scrubs one line of text.</summary>
public static string ScrubLine(string line)
{
if (line.Length == 0) return line;
var s = HeaderLine().Replace(line, m => m.Groups["name"].Value + ": " + Removed);
s = AuthScheme().Replace(s, m => m.Groups["scheme"].Value + " " + Removed);
s = UrlCredentials().Replace(s, m => m.Groups["prefix"].Value + Removed + "@");
s = QueryParameter().Replace(s, m => m.Groups["name"].Value + Removed);
s = NamedValue().Replace(s, m => m.Groups["name"].Value + Removed);
return KnownTokenFormat().Replace(s, Removed);
}
private static JsonNode ScrubNode(JsonNode node, bool secret)
{
switch (node)
{
case JsonObject obj:
foreach (var name in obj.Select(p => p.Key).ToList())
{
if (obj[name] is not { } child) continue;
var scrubbed = ScrubNode(child, secret || IsSecretName(name));
if (!ReferenceEquals(scrubbed, child)) obj[name] = scrubbed;
}
return obj;
case JsonArray array:
for (var i = 0; i < array.Count; i++)
{
if (array[i] is not { } item) continue;
var scrubbed = ScrubNode(item, secret);
if (!ReferenceEquals(scrubbed, item)) array[i] = scrubbed;
}
return array;
case JsonValue value when value.GetValueKind() == JsonValueKind.String:
return JsonValue.Create(secret ? Removed : ScrubText(value.GetValue<string>()));
default:
return node;
}
}
/// <summary>"Authorization: …", "Cookie: …", "Set-Cookie: …": the whole value goes.</summary>
[GeneratedRegex(@"(?<name>\b(?:proxy-)?authorization|\bset-cookie|\bcookie|\bx-csrf-token)\s*:\s*\S.*$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex HeaderLine();
/// <summary>"Bearer abc…", "Basic dXNl…".</summary>
[GeneratedRegex(@"(?<scheme>\b(?:bearer|basic))\s+[A-Za-z0-9._~+/=-]{6,}", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex AuthScheme();
/// <summary>"https://user:secret@host" keeps the user name.</summary>
[GeneratedRegex(@"(?<prefix>\b[a-z][a-z0-9+.-]*://[^/\s:@]+:)[^/\s@]+@", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex UrlCredentials();
/// <summary>"?token=…", "&amp;access_token=…", "&amp;sig=…".</summary>
[GeneratedRegex(@"(?<name>[?&](?:token|access_token|refresh_token|api_key|apikey|key|sig|signature|password|code)=)[^&\s#""']+", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex QueryParameter();
/// <summary>
/// "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).
/// </summary>
[GeneratedRegex(@"(?<name>[A-Za-z0-9_-]*(?:password|passwd|pwd|secret|token|api[_-]?key|access[_-]?key|private[_-]?key)[A-Za-z0-9_-]*[""']?\s*[=:]\s*)(?!\[removed\])(?:""[^""]*""|'[^']*'|[^\s;,&""']+)",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex NamedValue();
/// <summary>Self-identifying credentials (GitHub tokens) wherever they appear.</summary>
[GeneratedRegex(@"\b(?:gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})\b", RegexOptions.CultureInvariant)]
private static partial Regex KnownTokenFormat();
}
+2 -1
View File
@@ -1,4 +1,4 @@
// Logs (BRIEF §14.8): server log tail and downloads, sign-in log, audit log.
// Logs (BRIEF §14.8): server log tail and downloads, diagnostics bundle, sign-in log, audit log.
import { t, has, applyStrings } from "../i18n.js";
import { escapeHtml, attr, formatBytes, formatDateTime, toast, errorText, routeParams } from "../ui.js";
import { pascal } from "../jobs.js";
@@ -44,6 +44,7 @@ export async function render(root, { api, meta, actions }) {
<select class="input small" id="lines" data-i18n-title="Admin.Logs.Lines.Tip">${LINES.map((n) => `<option value="${n}" ${n === lines ? "selected" : ""}>${escapeHtml(t("Admin.Logs.LineCount", n))}</option>`).join("")}</select>
<span class="spacer"></span>
<button class="btn small" id="refresh" data-i18n="Admin.Common.Refresh" data-i18n-title="Admin.Logs.Refresh.Tip"></button>
<a class="btn small" href="/api/v1/server/diagnostics" download data-i18n="Admin.Logs.Diagnostics" data-i18n-title="Admin.Logs.Diagnostics.Tip"></a>
</div>
<pre class="log tall">${escapeHtml(tail.lines.join("\n"))}</pre>
</div>
@@ -0,0 +1,108 @@
using System.Globalization;
using System.Text;
using System.Text.Json;
using Titano.Core.Diagnostics;
using Titano.Core.Model;
using Titano.Data;
using Titano.Data.Processing;
using Titano.Server.Configuration;
using Titano.Server.Jobs;
using Titano.Server.Security;
namespace Titano.Server.Api;
/// <summary>
/// <c>GET /api/v1/server/diagnostics</c> (BRIEF §22 Phase 8; ManageArchive): a zip with the environment report, the
/// effective configuration, the encoders, the latest failed jobs and the server logs. Everything passes through
/// <see cref="SecretScrubber"/> (admin and database passwords, tokens and keys are removed); no archive file is included.
/// </summary>
public static class DiagnosticsEndpoints
{
/// <summary>Failed jobs included, newest first.</summary>
public const int FailedJobs = 100;
public static void Map(RouteGroupBuilder api) =>
api.MapGet("/server/diagnostics", DownloadAsync).Requires(Permissions.ManageArchive);
private static async Task<IResult> DownloadAsync(HttpContext context, Caller caller, TitanoDatabase db, ServerOptions options, ServerState state,
JobEngineHost jobs, AuditLog audit, TimeProvider time, IServiceProvider services)
{
var ct = context.RequestAborted;
var failures = (await db.Jobs.ListAsync(JobState.Failed, FailedJobs, null, ct)).Items;
var report = Report(options, state, jobs, time);
var settings = JsonSerializer.Serialize(options, ServerOptionsJson.Default.ServerOptions);
var encoders = Encoders(services.GetService<ProcessingService>());
// 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);
try
{
await Task.Run(() =>
{
using var bundle = new DiagnosticsArchive(file);
bundle.AddText("environment.txt", report);
bundle.AddJson("settings.json", settings);
bundle.AddText("encoders.txt", encoders);
bundle.AddJobs("jobs-failed.json", failures);
bundle.AddLogs(options.LogPath, cancellationToken: ct);
}, ct);
file.Position = 0;
}
catch
{
await file.DisposeAsync();
throw;
}
await audit.WriteAsync(caller, context, "server_diagnostics", "server");
context.Response.Headers.CacheControl = "no-store";
var name = string.Create(CultureInfo.InvariantCulture, $"titano-server-diagnostics-{time.GetUtcNow():yyyyMMdd-HHmm}.zip");
return Results.File(file, "application/zip", name);
}
private static string Report(ServerOptions options, ServerState state, JobEngineHost jobs, TimeProvider time)
{
var sb = new StringBuilder();
sb.AppendLine(CultureInfo.InvariantCulture, $"Titano Server {SystemEndpoints.Version} ({options.ServerName})");
sb.Append(DiagnosticsArchive.SystemReport());
sb.AppendLine(CultureInfo.InvariantCulture, $"Uptime {time.GetUtcNow() - state.StartedAt:d\\.hh\\:mm\\:ss}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Database {state.Database} ({options.Database})");
sb.AppendLine(CultureInfo.InvariantCulture, $"TLS {options.TlsMode} {state.TlsFingerprint}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Workers {jobs.Workers}");
sb.AppendLine(CultureInfo.InvariantCulture, $"Archive {options.ArchivePath} ({Space(options.ArchivePath)})");
sb.AppendLine(CultureInfo.InvariantCulture, $"Data {options.DataPath} ({Space(options.DataPath)})");
sb.AppendLine(CultureInfo.InvariantCulture, $"Container {File.Exists("/.dockerenv")}");
sb.AppendLine("Set by environment: " + string.Join(", ", options.Locked));
return sb.ToString();
}
private static string Encoders(ProcessingService? processing)
{
if (processing is null) return "No processing engine registered.\n";
var sb = new StringBuilder();
foreach (var provider in processing.Options.Encoders)
{
foreach (var c in provider.Capabilities)
{
sb.AppendLine(CultureInfo.InvariantCulture,
$"{provider.Name}: {c.Codec} {c.Name} ({(c.Hardware ? "hardware" : "software")}{(c.SupportsTenBit ? ", 10-bit" : string.Empty)}, max {c.MaxWidth}x{c.MaxHeight})");
}
}
return sb.ToString();
}
private static string Space(string path)
{
try
{
if (!Directory.Exists(path)) return "missing";
var full = Path.GetFullPath(path);
// Windows wants a drive root; Unix accepts any directory and reports the volume it is mounted on.
var drive = new DriveInfo(OperatingSystem.IsWindows() ? Path.GetPathRoot(full)! : full);
return string.Create(CultureInfo.InvariantCulture, $"{drive.AvailableFreeSpace / (1L << 30)} GB free of {drive.TotalSize / (1L << 30)} GB");
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException)
{
return "free space unknown";
}
}
}
+1
View File
@@ -153,6 +153,7 @@ public static class ServerHost
NodeEndpoints.Map(api);
ArchiveEndpoints.Map(api);
AdminEndpoints.Map(api);
DiagnosticsEndpoints.Map(api);
EventEndpoints.Map(api);
app.MapFallback("/api/{**path}", (HttpContext context) => ApiErrors.NotFound(context)).AllowAnonymousCaller();
}
@@ -0,0 +1,65 @@
using System.IO;
using System.IO.Compression;
using Titano.App.Infrastructure;
using Titano.App.Settings;
using Titano.Core.Video;
using Titano.Testing;
namespace Titano.App.Tests.Windows;
/// <summary>The desktop diagnostics bundle: every entry present, the saved server token and passwords never inside.</summary>
[WindowsOnly]
public sealed class DiagnosticsBundleTests(TestContext context)
{
private const string Token = "AQAAANCMnd8BFdERjHoAwE-saved-token";
[Test]
public async Task TheBundleCarriesReportsAndLogsButNoSecrets()
{
var environment = AppEnvironment.ForCapture();
try
{
var settings = new AppSettings { Language = "en", ServerUrl = "https://nas.local:8443", ServerUsername = "alice", ProtectedToken = Token };
var services = new AppServices(environment, settings, () => { }, new NoDialogs()) { Encoders = new OneEncoder() };
await File.WriteAllTextAsync(Path.Combine(environment.LogDirectory, "titano-20261002.log"), $"signed in with password={Token}\n");
var path = await DiagnosticsBundle.CreateAsync(services, context.CreateTempDirectory(), ["Self-test H.264 Test: passed"]);
using var zip = ZipFile.OpenRead(path);
var texts = zip.Entries.ToDictionary(e => e.FullName, e =>
{
using var reader = new StreamReader(e.Open());
return reader.ReadToEnd();
});
Assert.SequenceEqual(new[] { "codecs.txt", "environment.txt", "jobs-failed.json", "logs/titano-20261002.log", "settings.json" },
texts.Keys.Order(StringComparer.Ordinal));
foreach (var (name, text) in texts) Assert.False(text.Contains(Token, StringComparison.Ordinal), $"{name} leaks the token");
Assert.Contains("Self-test H.264 Test: passed", texts["environment.txt"]);
Assert.Contains("Test encoder", texts["codecs.txt"]);
Assert.Contains("\"ServerUsername\": \"alice\"", texts["settings.json"]);
Assert.Contains("\"ProtectedToken\": \"[removed]\"", texts["settings.json"]);
}
finally
{
environment.DeleteCaptureData();
}
}
private sealed class OneEncoder : 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 Task<SelfTestResult> SelfTestAsync(EncoderRow row, CancellationToken ct) => Task.FromResult(new SelfTestResult(true, 100, null));
}
private sealed class NoDialogs : IAppDialogs
{
public string? PickFolder(string title, string? initialFolder = null) => null;
public string? PickJsonFile(string title) => null;
public bool Confirm(ConfirmRequest request) => false;
public bool SignIn(string server, string? username, Func<string, string, Task<string?>> signIn) => false;
}
}
@@ -0,0 +1,74 @@
using System.IO.Compression;
using System.Text;
using Titano.Core.Diagnostics;
using Titano.Core.Model;
using Titano.Testing;
namespace Titano.Core.Tests.Diagnostics;
public sealed class DiagnosticsArchiveTests(TestContext context)
{
private const string Secret = "correct-horse-battery";
[Test]
public void EveryEntryIsScrubbedAndLogsKeepTheirTail()
{
var logs = context.CreateTempDirectory();
File.WriteAllText(Path.Combine(logs, "titano-20261001.log"), "old line\nPassword=" + Secret + "\n");
var big = new StringBuilder();
for (var i = 0; i < 2_000; i++) big.Append("line ").Append(i).Append(" token: ").Append(Secret).Append('\n');
File.WriteAllText(Path.Combine(logs, "titano-20261002.log"), big.ToString());
File.WriteAllText(Path.Combine(logs, "photo.jpg"), "not a log");
File.SetLastWriteTimeUtc(Path.Combine(logs, "titano-20261001.log"), DateTime.UtcNow.AddDays(-1));
using var zip = new MemoryStream();
using (var bundle = new DiagnosticsArchive(zip))
{
bundle.AddText("environment.txt", DiagnosticsArchive.SystemReport() + "Server https://admin:" + Secret + "@nas:8443\n");
bundle.AddJson("settings.json", $$"""{ "ProtectedToken": "{{Secret}}", "Theme": "Dark" }""");
bundle.AddJobs("jobs-failed.json",
[
new Job
{
Id = Guid.CreateVersion7(), Kind = JobKind.Render, State = JobState.Failed, Attempts = 2, Created = DateTimeOffset.UtcNow,
Error = "Upload refused: Authorization: Bearer " + Secret, LogTail = "retry with password=" + Secret,
},
]);
bundle.AddLogs(logs, tailBytes: 4096);
}
zip.Position = 0;
using var archive = new ZipArchive(zip, ZipArchiveMode.Read);
var names = archive.Entries.Select(e => e.FullName).Order(StringComparer.Ordinal).ToArray();
Assert.SequenceEqual(new[] { "environment.txt", "jobs-failed.json", "logs/titano-20261001.log", "logs/titano-20261002.log", "settings.json" }, names);
foreach (var entry in archive.Entries)
{
var text = Read(entry);
Assert.False(text.Contains(Secret, StringComparison.Ordinal), $"{entry.FullName} leaks the secret");
}
Assert.Contains("Runtime ", Read(archive.GetEntry("environment.txt")!));
Assert.Contains("\"Theme\": \"Dark\"", Read(archive.GetEntry("settings.json")!));
Assert.Contains("\"kind\": \"Render\"", Read(archive.GetEntry("jobs-failed.json")!));
var tail = Read(archive.GetEntry("logs/titano-20261002.log")!);
Assert.True(tail.StartsWith("[first ", StringComparison.Ordinal), "a cut log says so");
Assert.Contains("line 1999 token: [removed]", tail);
Assert.Less(tail.Length, 5_000);
}
[Test]
public void AMissingLogFolderAddsNothing()
{
using var zip = new MemoryStream();
using (var bundle = new DiagnosticsArchive(zip)) bundle.AddLogs(Path.Combine(context.CreateTempDirectory(), "missing"));
zip.Position = 0;
using var archive = new ZipArchive(zip, ZipArchiveMode.Read);
Assert.Empty(archive.Entries);
}
private static string Read(ZipArchiveEntry entry)
{
using var reader = new StreamReader(entry.Open(), Encoding.UTF8);
return reader.ReadToEnd();
}
}
@@ -0,0 +1,84 @@
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("client_secret=" + Secret + " refresh_token='" + Secret + "'", "client_secret=[removed] refresh_token=[removed]")]
[Case("pushed with ghp_0123456789abcdefghijABCDEFGHIJ012345", "pushed with [removed]")]
public void SecretsInTextAreRemoved(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;Password={{Secret}}",
"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;Password=[removed]", root.GetProperty("DbConnectionString").GetString());
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] {", SecretScrubber.ScrubJson("password=" + Secret + " {"));
[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));
}
@@ -0,0 +1,65 @@
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";
[Test]
public async Task TheBundleHoldsReportsLogsAndFailuresWithoutSecrets()
{
await using var server = await TestServer.StartAsync(context, o => o.DbPassword = DbPassword);
var admin = await server.AdminAsync();
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");
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.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());
}
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()));
}
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();
}
}