Files
Chronos/tests/Titano.App.Tests/Library/UntrustedTlsEndpoint.cs
T
Claude 4850af2443 fix(app): keep the editor's own save errors and gate an unread archive
- Editor: a failed save showed what the server or the archive layer would say
  for the exception's type, so the editor's own sentences were lost: frames
  changed on the server or settings changed elsewhere read "The server does not
  answer", a read-only account or a gone sequence "Something went wrong". These
  failures are now EditorException (an IOException; ProxiesOutdatedException
  derives from it) and the toast shows their sentence as it is. A project
  written by a newer Titano is reported with the same sentence as a corrupt one
  instead of the parser's English text.
- Library: a job posted to a server whose certificate changed since sign-in
  threw out of the action unreported; the pin's refusal now reaches the error
  toast ("The certificate was not trusted").
- Archive: picking another archive forced it offline until it was read, so the
  rebuild dialog called it offline with the previous archive's count, and
  disabled commands said the archive must be online. What was read is now kept
  apart from availability: until the picked archive is read, Rebuild and the
  online commands wait and say so, health fixes and search results of the
  previous archive are cleared, and a running search is cancelled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-12 11:09:04 +00:00

79 lines
2.8 KiB
C#

using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
namespace Titano.App.Tests.Library;
/// <summary>
/// A TLS endpoint on loopback with a self-signed certificate no client trusted (a server whose certificate changed since
/// sign-in): the client's pinning refuses every handshake, so nothing is ever sent.
/// </summary>
internal sealed class UntrustedTlsEndpoint : IDisposable
{
private readonly TcpListener _listener = new(IPAddress.Loopback, 0);
private readonly X509Certificate2 _certificate;
private readonly CancellationTokenSource _stop = new();
private readonly Task _accepting;
public UntrustedTlsEndpoint()
{
using var key = RSA.Create(2048);
var request = new CertificateRequest("CN=127.0.0.1", key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
using var created = request.CreateSelfSigned(DateTimeOffset.UtcNow.AddDays(-1), DateTimeOffset.UtcNow.AddDays(1));
// Schannel serves only a certificate whose key it can open: a PKCS #12 round trip gives it one.
_certificate = X509CertificateLoader.LoadPkcs12(created.Export(X509ContentType.Pkcs12), null);
_listener.Start();
_accepting = Task.Run(AcceptAsync);
}
public Uri Address => new($"https://127.0.0.1:{((IPEndPoint)_listener.LocalEndpoint).Port}/");
public void Dispose()
{
_stop.Cancel();
_listener.Stop();
try
{
_accepting.Wait(TimeSpan.FromSeconds(5));
}
catch (AggregateException)
{
// The loop ends with the listener; how it ended does not matter.
}
_certificate.Dispose();
_stop.Dispose();
}
private async Task AcceptAsync()
{
while (!_stop.IsCancellationRequested)
{
TcpClient client;
try
{
client = await _listener.AcceptTcpClientAsync(_stop.Token);
}
catch (Exception ex) when (ex is OperationCanceledException or SocketException or ObjectDisposedException)
{
return;
}
using (client)
{
try
{
await using var tls = new SslStream(client.GetStream());
await tls.AuthenticateAsServerAsync(new SslServerAuthenticationOptions { ServerCertificate = _certificate }, _stop.Token);
}
catch (Exception ex) when (ex is AuthenticationException or IOException or OperationCanceledException or SocketException)
{
// The client refused the certificate: the handshake ends here, as intended.
}
}
}
}
}