fix(server): client address from the page's port, stricter Chronos identity, Host networking notes
Review fixes on top of the public ports change (owner review 3): - Settings > Network and TLS: opened over HTTPS, the client address uses the port the page came through (a container's 8443 published as 8444 with Public HTTPS port still empty showed the wrong 8443), and a "Use <port>" button fills that port in. Saving a Public HTTPS port other than the page's asks first, since the HTTPS redirect follows it at once and a typo would lock the panel's link out. - GetInfoAsync tells Chronos from other services by stricter rules on both the trusted and the untrusted-certificate path: any redirect, any answer that is not JSON, more than 1 MB, or an error without the X-Chronos-Api header is not Chronos. Another service's certificate is no longer offered for trust after a cross-scheme redirect, an error page or a JSON 401/403/404, and a JSON 401 over HTTP no longer reads as a wrong password. - The HTTP port typed without a scheme (TLS to a plain HTTP port) now gives the not-Chronos error instead of "the server did not answer". - The Uploader's certificate question names the address with its port. - A link-local IPv6 address with a zone index is refused instead of losing it. - In a container, the first save of the settings removes HttpPort/HttpsPort from server.json, so the start-up warning stops; the warning says so. - Guide, README, compose and template: with network type Host the mappings do nothing and CHRONOS_HTTP_PORT / CHRONOS_HTTPS_PORT move the listening ports off ones another service uses; the template overview is back to two sentences. - BRIEF, DECISIONS and CHANGELOG updated; en/it strings added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
369e754bec
commit
0aa2fb8135
19 files changed
+303
-67
No files matched your search
@@ -66,71 +66,112 @@ public sealed class ChronosClient : IDisposable
|
||||
/// <summary>
|
||||
/// Reads server info; throws <see cref="UntrustedCertificateException"/> when the TLS certificate of a Chronos Server
|
||||
/// needs confirmation, and <see cref="NotChronosServerException"/> when the address answers as something else (owner
|
||||
/// review 3). Behind a certificate that is not trusted yet, the info is first read once more without checking the
|
||||
/// certificate and without credentials: only an answer that is Chronos's leads to the fingerprint question, so the
|
||||
/// certificate of another service on the host is never offered for trust. Nothing is trusted here.
|
||||
/// review 3): a redirect, a page that is not JSON, JSON of another shape, or an error Chronos did not write. Behind a
|
||||
/// certificate that is not trusted yet, the info is first read once more without checking the certificate and without
|
||||
/// credentials: only an answer that is Chronos's leads to the fingerprint question, so the certificate of another
|
||||
/// service on the host is never offered for trust. An HTTPS address whose port answers plain HTTP (the HTTP port typed
|
||||
/// without a scheme) is not Chronos either. Nothing is trusted here.
|
||||
/// </summary>
|
||||
public async Task<ServerInfo> GetInfoAsync(CancellationToken ct = default)
|
||||
{
|
||||
ServerInfo info;
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
info = await GetAsync(InfoPath, ProtocolJson.Default.ServerInfo, ct).ConfigureAwait(false);
|
||||
response = await SendRawAsync(new HttpRequestMessage(HttpMethod.Get, InfoPath), ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (UntrustedCertificateException) when (_ownsHandler)
|
||||
{
|
||||
if (await AnswersAsChronosAsync(ct).ConfigureAwait(false) == false) throw new NotChronosServerException(BaseAddress);
|
||||
if (await ProbeAsync(BaseAddress, ct).ConfigureAwait(false) is { Chronos: false }) throw new NotChronosServerException(BaseAddress);
|
||||
throw;
|
||||
}
|
||||
catch (ChronosApiException api) when (!api.IsTransient && (int)api.Status < 500 && !IsChronosError(api.Error))
|
||||
catch (HttpRequestException tls) when (_ownsHandler && tls.HttpRequestError == HttpRequestError.SecureConnectionError
|
||||
&& BaseAddress.Scheme == Uri.UriSchemeHttps)
|
||||
{
|
||||
throw new NotChronosServerException(BaseAddress, api);
|
||||
// The same authority over plain HTTP: any answer means the port is not Chronos's HTTPS port.
|
||||
var plain = new UriBuilder(BaseAddress) { Scheme = Uri.UriSchemeHttp, Port = BaseAddress.Port }.Uri;
|
||||
if (await ProbeAsync(plain, ct).ConfigureAwait(false) is { Answered: true }) throw new NotChronosServerException(BaseAddress, tls);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or NotSupportedException)
|
||||
using (response)
|
||||
{
|
||||
throw new NotChronosServerException(BaseAddress, ex);
|
||||
var answer = await IdentifyAsync(response, ct).ConfigureAwait(false);
|
||||
if (answer.Info is { } info) return info;
|
||||
if (answer.Chronos == false) throw new NotChronosServerException(BaseAddress);
|
||||
// Chronos's own error, or busy: as any other request reports it.
|
||||
await EnsureSuccessAsync(response, ct).ConfigureAwait(false);
|
||||
throw new NotChronosServerException(BaseAddress);
|
||||
}
|
||||
return IsChronos(info) ? info : throw new NotChronosServerException(BaseAddress);
|
||||
}
|
||||
|
||||
private const string InfoPath = "api/v1/server/info";
|
||||
|
||||
/// <summary>More than a server info ever holds: a larger answer is something else.</summary>
|
||||
private const int MaxInfoBytes = 1 << 20;
|
||||
|
||||
/// <summary>What every Chronos Server's info holds; JSON of another shape reads as nulls and zeros.</summary>
|
||||
private static bool IsChronos(ServerInfo? info) =>
|
||||
info is { Name.Length: > 0, Version.Length: > 0, ApiVersion: > 0, Capabilities: not null, Formats: not null, Limits: not null };
|
||||
|
||||
/// <summary>An error body Chronos wrote (<see cref="EnsureSuccessAsync"/> codes any other reply <c>http_<status></c>).</summary>
|
||||
private static bool IsChronosError(ApiError error) => error.Code is { Length: > 0 } code && !code.StartsWith("http_", StringComparison.Ordinal);
|
||||
/// <summary>What a <c>server/info</c> answer is: Chronos's (with its info when it succeeded), something else, or unknown (busy).</summary>
|
||||
private readonly record struct Identity(bool? Chronos, ServerInfo? Info = null, bool Answered = true);
|
||||
|
||||
/// <summary>
|
||||
/// The info read without checking the certificate, by a client of its own that holds no token: true when it is
|
||||
/// Chronos's, false when the address answers as something else, null when it cannot tell (no answer, busy).
|
||||
/// Tells a <c>server/info</c> answer of Chronos from any other. Chronos answers it anonymously with its JSON and never
|
||||
/// redirects; every response of its carries <see cref="ApiVersion.Header"/>, so an error is Chronos's only with that
|
||||
/// header and an error body of its own. A busy answer (408, 429, 502-504) with JSON or no body cannot tell; any answer
|
||||
/// that is not JSON, or is larger than <see cref="MaxInfoBytes"/>, is something else.
|
||||
/// </summary>
|
||||
private async Task<bool?> AnswersAsChronosAsync(CancellationToken ct)
|
||||
private static async Task<Identity> IdentifyAsync(HttpResponseMessage response, CancellationToken ct)
|
||||
{
|
||||
if ((int)response.StatusCode is >= 300 and < 400) return new(false);
|
||||
var type = response.Content.Headers.ContentType?.MediaType;
|
||||
var json = type is not null && (type.Equals("application/json", StringComparison.OrdinalIgnoreCase) || type.EndsWith("+json", StringComparison.OrdinalIgnoreCase));
|
||||
if (ChronosApiException.IsTransientStatus(response.StatusCode)) return new(json || type is null ? null : false);
|
||||
if (!json || response.Content.Headers.ContentLength > MaxInfoBytes) return new(false);
|
||||
try
|
||||
{
|
||||
await response.Content.LoadIntoBufferAsync(MaxInfoBytes, ct).ConfigureAwait(false);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
var info = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ServerInfo, ct).ConfigureAwait(false);
|
||||
return IsChronos(info) ? new(true, info) : new(false);
|
||||
}
|
||||
if (!response.Headers.Contains(ApiVersion.Header)) return new(false);
|
||||
var error = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ApiError, ct).ConfigureAwait(false);
|
||||
return new(error is { Code.Length: > 0 } && !error.Code.StartsWith("http_", StringComparison.Ordinal));
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or NotSupportedException or HttpRequestException)
|
||||
{
|
||||
// Not JSON after all, or more than a server info.
|
||||
return new(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The info at <paramref name="address"/> read without checking the certificate, by a client of its own that holds no
|
||||
/// token and follows no redirect; <see cref="Identity.Answered"/> is false when nothing answered (no connection, TLS
|
||||
/// refused, timeout).
|
||||
/// </summary>
|
||||
private static async Task<Identity> ProbeAsync(Uri address, CancellationToken ct)
|
||||
{
|
||||
using var handler = new SocketsHttpHandler
|
||||
{
|
||||
SslOptions = { RemoteCertificateValidationCallback = static (_, _, _, _) => true },
|
||||
ConnectTimeout = TimeSpan.FromSeconds(10),
|
||||
AutomaticDecompression = DecompressionMethods.None,
|
||||
AllowAutoRedirect = false,
|
||||
UseCookies = false,
|
||||
};
|
||||
using var http = new HttpClient(handler) { BaseAddress = BaseAddress, Timeout = TimeSpan.FromSeconds(20), MaxResponseContentBufferSize = 1 << 20 };
|
||||
using var http = new HttpClient(handler) { BaseAddress = address, Timeout = TimeSpan.FromSeconds(20) };
|
||||
http.DefaultRequestHeaders.Add(ApiVersion.Header, ApiVersion.Current.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
||||
try
|
||||
{
|
||||
using var response = await http.GetAsync(InfoPath, ct).ConfigureAwait(false);
|
||||
if (response.IsSuccessStatusCode) return IsChronos(await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ServerInfo, ct).ConfigureAwait(false));
|
||||
if ((int)response.StatusCode is < 400 or >= 500 || ChronosApiException.IsTransientStatus(response.StatusCode)) return null;
|
||||
var error = await response.Content.ReadFromJsonAsync(ProtocolJson.Default.ApiError, ct).ConfigureAwait(false);
|
||||
return error is not null && IsChronosError(error);
|
||||
}
|
||||
catch (Exception ex) when (ex is JsonException or NotSupportedException)
|
||||
{
|
||||
return false;
|
||||
using var response = await http.GetAsync(InfoPath, HttpCompletionOption.ResponseHeadersRead, ct).ConfigureAwait(false);
|
||||
return await IdentifyAsync(response, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or IOException || (ex is OperationCanceledException && !ct.IsCancellationRequested))
|
||||
{
|
||||
return null;
|
||||
return new(null, Answered: false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user