Close what the security review found

A session alone could change the password, switch the second factor on or
off, send the key of the language model to another address and remove the
rules that stop purchases. Each now needs the password and the code, with
one limit on wrong attempts that a restart of the services does not clear,
and the other sessions end when the password or the factor changes.

Requests to the brokers pass a list of allowed calls, never follow a
redirect with a key and are signed by one client only. Pages are closed by
default, nothing is cached, requests are limited to 1 MB, secrets are
masked in logs and notifications, exported tables cannot run as formulas,
archives carry no sessions and the container refuses to run as root.

refs #68

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 19:11:11 +01:00
1 parent 4897594b87
commit 4d92a39fef
35 files changed
+907 -118

No files matched your search

+63 -12
View File
@@ -144,7 +144,7 @@ internal static partial class Forms
return bare + (bare.Contains('?', StringComparison.Ordinal) ? "&" : "?") + key + "=" + Uri.EscapeDataString(value) + anchor;
}
private static async Task<IResult> SignInAsync(IFormCollection form, AnubisHost host, LoginLimiter limiter, HttpContext context, CancellationToken ct)
private static async Task<IResult> SignInAsync(IFormCollection form, AnubisHost host, LoginLimiter limiter, ILogger<AnubisHost> logger, HttpContext context, CancellationToken ct)
{
var services = host.Services;
if (services is null)
@@ -158,12 +158,18 @@ internal static partial class Forms
if (wait > TimeSpan.Zero)
{
var minutes = Math.Max(1, (int)Math.Ceiling(wait.TotalMinutes));
LogSignInRefused(logger, address, "Locked");
return Results.Redirect(With(With("/accesso", "errore", "attesa"), "minuti", minutes.ToString(System.Globalization.CultureInfo.InvariantCulture)));
}
var code = form["codice"].ToString();
var (result, token) = await services.Access.SignInAsync(form["password"].ToString(), code, context.Request.Headers.UserAgent.ToString(), address, ct)
.ConfigureAwait(false);
if (result is not (LoginResult.Ok or LoginResult.SecondFactorNeeded))
{
LogSignInRefused(logger, address, result.ToString());
}
switch (result)
{
case LoginResult.Ok:
@@ -182,6 +188,9 @@ internal static partial class Forms
}
}
[LoggerMessage(Level = LogLevel.Warning, Message = "Sign-in refused from {Address}: {Reason}")]
private static partial void LogSignInRefused(ILogger logger, string address, string reason);
private static async Task<IResult> SignOutAsync(IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
{
if (context.Request.Cookies.TryGetValue(SessionAuthentication.Cookie, out var token) && host.Services is { } services)
@@ -211,9 +220,15 @@ internal static partial class Forms
return Results.Redirect(With(page, "errore", replacement.Length < Passwords.MinimumLength ? "corta" : "uguale"));
}
var ok = await host.Services!.Access.ChangePasswordAsync(CurrentUser.Id(context.User), form["attuale"].ToString(), replacement, CurrentUser.Session(context.User), ct)
var (outcome, token) = await host.Services!.Access.ChangePasswordAsync(CurrentUser.Id(context.User), form["attuale"].ToString(), replacement, CurrentUser.Session(context.User), ct)
.ConfigureAwait(false);
return Results.Redirect(ok ? With(done, "ok", "password") : With(page, "errore", "attuale"));
if (outcome != AccessChange.Done)
{
return Results.Redirect(With(page, "errore", outcome == AccessChange.Locked ? "attesa" : "attuale"));
}
SessionAuthentication.WriteCookie(context, token!);
return Results.Redirect(With(done, "ok", "password"));
}
private static async Task<IResult> EnableSecondFactorAsync(IFormCollection form, AnubisHost host, SecondFactorDrafts drafts, HttpContext context, CancellationToken ct)
@@ -225,22 +240,35 @@ internal static partial class Forms
return Results.Redirect("/impostazioni?errore=segreto#sicurezza");
}
var ok = await host.Services!.Access.EnableSecondFactorAsync(CurrentUser.Id(context.User), secret, form["codice"].ToString(), ct).ConfigureAwait(false);
if (ok)
var outcome = await host.Services!.Access.EnableSecondFactorAsync(CurrentUser.Id(context.User), secret, form["password"].ToString(), form["codice"].ToString(), session, ct)
.ConfigureAwait(false);
if (outcome is AccessChange.Done or AccessChange.AlreadyOn)
{
drafts.Remove(session);
}
return Results.Redirect(ok ? "/impostazioni?ok=due-fattori#sicurezza" : "/impostazioni?errore=codice#sicurezza");
return Results.Redirect(outcome switch
{
AccessChange.Done => "/impostazioni?ok=due-fattori#sicurezza",
AccessChange.Locked => "/impostazioni?errore=attesa#sicurezza",
AccessChange.AlreadyOn => "/impostazioni?errore=gia-attivo#sicurezza",
_ => "/impostazioni?errore=password-o-codice#sicurezza",
});
}
private static async Task<IResult> DisableSecondFactorAsync(IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
{
var ok = !host.IsDemo && await host.Services!.Access.DisableSecondFactorAsync(CurrentUser.Id(context.User), form["password"].ToString(), form["codice"].ToString(), ct).ConfigureAwait(false);
return Results.Redirect(ok ? "/impostazioni?ok=due-fattori-tolti#sicurezza" : "/impostazioni?errore=attuale#sicurezza");
var outcome = host.IsDemo ? AccessChange.Wrong : await host.Services!.Access.DisableSecondFactorAsync(CurrentUser.Id(context.User), form["password"].ToString(),
form["codice"].ToString(), CurrentUser.Session(context.User), ct).ConfigureAwait(false);
return Results.Redirect(outcome switch
{
AccessChange.Done => "/impostazioni?ok=due-fattori-tolti#sicurezza",
AccessChange.Locked => "/impostazioni?errore=attesa#sicurezza",
_ => "/impostazioni?errore=password-o-codice#sicurezza",
});
}
private static async Task<IResult> SetSettingAsync(string key, IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
private static async Task<IResult> SetSettingAsync(string key, IFormCollection form, AnubisHost host, IClock clock, HttpContext context, CancellationToken ct)
{
var back = Back(form, "/impostazioni");
var news = Anubis.News.NewsRuleSettings.All.Any(d => d.Key == key);
@@ -271,10 +299,26 @@ internal static partial class Forms
return Results.Redirect(With(back, "errore", key == Anubis.News.NewsModelSettings.DailyCap.Key ? "tetto" : "modello"));
}
// Who gets the key of the model is decided with the password: with only a session it could be sent anywhere.
if ((key == Anubis.News.NewsModelSettings.Address.Key || key == Anubis.News.NewsModelSettings.Provider.Key) && host.Services is { } services &&
value != (await services.Settings.GetAsync(key, ct).ConfigureAwait(false)).Value &&
!await services.Access.ReauthenticateAsync(form["password"].ToString(), form["codice"].ToString().Trim(), ct).ConfigureAwait(false))
{
return Results.Redirect(With(back, "errore", "password-rischio"));
}
var saved = await Endpoints.SetSettingAsync(host, key, value, context, ct).ConfigureAwait(false);
return Results.Redirect(saved is IStatusCodeHttpResult { StatusCode: StatusCodes.Status204NoContent } ? back : With(back, "errore", "impostazione"));
}
// Without a rule of SPEC 10.5 purchases pass that it would stop: with real money in play that is more risk (SPEC 12.4).
if (news && form["valore"].ToString() == Anubis.News.NewsRuleSettings.Off && host.Services is { } app &&
(await app.Settings.GetAsync(key, ct).ConfigureAwait(false)).Value != Anubis.News.NewsRuleSettings.Off &&
await RaiseRefusalAsync(app, clock, null, form, ct).ConfigureAwait(false) is { } refusal)
{
return Results.Redirect(With(back, "errore", refusal));
}
if (key == SettingsStore.CentralBankMeetings.Key && Anubis.Data.CentralBanks.Check(form["valore"].ToString()) is not null)
{
return Results.Redirect(With(back, "errore", "riunioni"));
@@ -633,9 +677,10 @@ internal static partial class Forms
var next = new Dictionary<string, string>(StringComparer.Ordinal);
foreach (var definition in Anubis.Risk.RiskSettings.Limits)
{
current[definition.Key] = (await services.Settings.GetAsync(definition.Key, ct).ConfigureAwait(false)).Value;
var value = await services.Settings.GetAsync(definition.Key, ct).ConfigureAwait(false);
current[definition.Key] = value.Value;
var typed = form[definition.Key].ToString().Trim().Replace(',', '.');
if (typed.Length == 0)
if (typed.Length == 0 || value.ReadOnly)
{
next[definition.Key] = current[definition.Key];
continue;
@@ -775,13 +820,19 @@ internal static partial class Forms
}
/// <summary>A lower slippage estimate waits for the user (SPEC 11.6). The form is bound for the antiforgery check.</summary>
private static async Task<IResult> DecideCostAsync(Guid id, string choice, IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
private static async Task<IResult> DecideCostAsync(Guid id, string choice, IFormCollection form, AnubisHost host, IClock clock, HttpContext context, CancellationToken ct)
{
if (host.IsDemo || host.Services is not { } services || choice is not ("conferma" or "rifiuta"))
{
return Results.Redirect("/laboratorio?errore=motore#stime");
}
// A lower cost lets through operations a higher one would stop: with real money in play it asks like more risk (SPEC 12.4).
if (choice == "conferma" && await RaiseRefusalAsync(services, clock, null, form, ct).ConfigureAwait(false) is { } refusal)
{
return Results.Redirect("/laboratorio?errore=" + refusal + "#stime");
}
var book = new Anubis.Learning.CostBook(services.Db, host.Clock);
var done = await book.DecideAsync(id, choice == "conferma", context.User.Identity?.Name ?? "utente", ct).ConfigureAwait(false);
if (done && services.Trading is { } trading)