Interfaccia web, parte 1: ospite, sicurezza, API, modalità dimostrativa, libreria, visualizzatore e account

Febe.Web: AddFebeWeb/UseFebe/FebeWebHost per le due vesti, interfaccia incorporata con IFileProvider proprio, indirizzi italiani, API JSON con contesto generato, eventi SSE, controlli di Host/Origin, gettone del desktop e password con blocco (§13.1), INativeShell e capacità per veste. Modalità --demo con 38 account e circa 18.000 elementi finti e scene del motore. Pagine Libreria, Visualizzatore, Account, Aggiungi account e Scheda account (contenuti, impostazioni con piano, cronologia); schermate in docs/schermate con le differenze dai mockup; prove delle API, della sicurezza, dell'esportazione CSV e dei dati dimostrativi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-13 19:14:35 +00:00
1 parent 6ff11ac8e3
commit e33540246e
76 files changed
+12503 -17

No files matched your search

+158
View File
@@ -0,0 +1,158 @@
using Febe.Web.Api;
using Microsoft.AspNetCore.Http;
namespace Febe.Web.Security;
/// <summary>
/// The pluggable access points of spec §13.1, in order: Host, Origin, then the access mode (none for the demo, token
/// for the desktop, password for Docker). Also sets the security headers of every response. Requests that fail get a
/// short Italian answer and are never logged with their query (the token travels there once).
/// </summary>
public sealed class SecurityMiddleware(RequestDelegate next, FebeWebOptions options, PasswordAccess? passwords = null)
{
/// <summary>Paths reachable without a session in password mode: the login page and what it needs.</summary>
public static readonly IReadOnlyList<string> PublicPrefixes =
["/accesso", "/api/accesso", "/css/", "/caratteri/", "/img/", "/testi/", "/js/", "/favicon.ico"];
public async Task InvokeAsync(HttpContext context)
{
ArgumentNullException.ThrowIfNull(context);
var request = context.Request;
var response = context.Response;
SetHeaders(response);
if (!HostOriginGuard.HostAllowed(request, options))
{
await RefuseAsync(context, StatusCodes.Status400BadRequest, "Indirizzo non ammesso: apri Febe dal suo indirizzo.").ConfigureAwait(false);
return;
}
if (HostOriginGuard.NeedsOriginCheck(request) && !HostOriginGuard.OriginAllowed(request, options))
{
await RefuseAsync(context, StatusCodes.Status403Forbidden, "Richiesta rifiutata: non arriva dall'interfaccia di Febe.").ConfigureAwait(false);
return;
}
switch (options.Access)
{
case AccessMode.Token:
if (await TokenAsync(context).ConfigureAwait(false))
{
await next(context).ConfigureAwait(false);
}
return;
case AccessMode.Password:
if (await PasswordAsync(context).ConfigureAwait(false))
{
await next(context).ConfigureAwait(false);
}
return;
default:
await next(context).ConfigureAwait(false);
return;
}
}
private async Task<bool> TokenAsync(HttpContext context)
{
var request = context.Request;
var query = request.Query[AccessToken.QueryName].ToString();
if (query.Length > 0)
{
if (!AccessToken.Matches(query, options.AccessToken))
{
await RefuseAsync(context, StatusCodes.Status401Unauthorized, "Gettone non valido: apri Febe dalla sua finestra.").ConfigureAwait(false);
return false;
}
context.Response.Cookies.Append(AccessToken.CookieName, query, new CookieOptions
{
HttpOnly = true,
SameSite = SameSiteMode.Strict,
Secure = request.IsHttps,
Path = "/",
IsEssential = true,
});
// The token leaves the address at once: it must not stay in the history or in a bookmark.
var rest = QueryString.Create(request.Query.Where(q => !string.Equals(q.Key, AccessToken.QueryName, StringComparison.Ordinal)));
context.Response.Redirect(request.PathBase + request.Path + rest);
return false;
}
var presented = request.Headers[AccessToken.HeaderName].ToString();
if (presented.Length == 0)
{
presented = request.Cookies[AccessToken.CookieName] ?? string.Empty;
}
if (AccessToken.Matches(presented, options.AccessToken))
{
return true;
}
await RefuseAsync(context, StatusCodes.Status401Unauthorized, "Questa pagina si apre solo dalla finestra di Febe.").ConfigureAwait(false);
return false;
}
private async Task<bool> PasswordAsync(HttpContext context)
{
if (passwords is null)
{
await RefuseAsync(context, StatusCodes.Status503ServiceUnavailable, "Manca la password dell'interfaccia (FEBE_PASSWORD): Febe non si apre senza.").ConfigureAwait(false);
return false;
}
var path = context.Request.Path.Value ?? "/";
if (PublicPrefixes.Any(p => path.StartsWith(p, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
if (passwords.IsValid(context.Request.Cookies[PasswordAccess.CookieName]))
{
return true;
}
if (IsApi(context.Request) || !HttpMethods.IsGet(context.Request.Method))
{
await RefuseAsync(context, StatusCodes.Status401Unauthorized, "Sessione scaduta: accedi di nuovo.").ConfigureAwait(false);
}
else
{
context.Response.Redirect("/accesso?torna=" + Uri.EscapeDataString(path));
}
return false;
}
private static bool IsApi(HttpRequest request) => request.Path.StartsWithSegments("/api", StringComparison.OrdinalIgnoreCase);
private static async Task RefuseAsync(HttpContext context, int status, string message)
{
context.Response.StatusCode = status;
if (IsApi(context.Request))
{
await context.Response.WriteAsJsonAsync(new ProblemDto(message), FebeJsonContext.Default.ProblemDto).ConfigureAwait(false);
}
else
{
context.Response.ContentType = "text/plain; charset=utf-8";
await context.Response.WriteAsync(message).ConfigureAwait(false);
}
}
private static void SetHeaders(HttpResponse response)
{
var headers = response.Headers;
headers["X-Content-Type-Options"] = "nosniff";
headers["Referrer-Policy"] = "no-referrer";
headers["X-Frame-Options"] = "DENY";
headers["Cross-Origin-Opener-Policy"] = "same-origin";
headers["Content-Security-Policy"] =
"default-src 'self'; img-src 'self' data: blob:; media-src 'self' blob:; style-src 'self' 'unsafe-inline'; "
+ "script-src 'self'; connect-src 'self'; font-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'";
}
}