diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index a75f9ce..e031b77 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -367,6 +367,14 @@ public partial class Program { var logger = context.HttpContext.RequestServices.GetRequiredService>(); logger.LogWarning("Redirect to login: {Path} -> {LoginPath}", context.Request.Path, options.LoginPath); + // API requests (JSON or /api/*) must get a 401, not a + // browser redirect — otherwise unauthenticated API calls + // silently follow the redirect and report 200. + if (IsApiRequest(context.Request)) + { + context.Response.StatusCode = StatusCodes.Status401Unauthorized; + return Task.CompletedTask; + } context.Response.Redirect(context.RedirectUri); return Task.CompletedTask; }, @@ -375,6 +383,11 @@ public partial class Program { var logger = context.HttpContext.RequestServices.GetRequiredService>(); logger.LogWarning("Access denied: {Path} -> {AccessDeniedPath}", context.Request.Path, options.AccessDeniedPath); + if (IsApiRequest(context.Request)) + { + context.Response.StatusCode = StatusCodes.Status403Forbidden; + return Task.CompletedTask; + } context.Response.Redirect(context.RedirectUri); return Task.CompletedTask; }, @@ -648,4 +661,18 @@ public partial class Program { return Results.Ok(); }); } + + /// + /// True for JSON/API requests (Accept: application/json or an /api/* path). + /// Used by the cookie challenge handlers to return 401/403 instead of a + /// browser redirect — API clients must not be silently redirected to /login. + /// + private static bool IsApiRequest(HttpRequest request) + { + if (request.Path.StartsWithSegments("/api")) + return true; + + var accept = request.Headers.Accept.ToString(); + return accept.Contains("application/json", StringComparison.OrdinalIgnoreCase); + } } diff --git a/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs b/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs index 82b6fd0..2c16ced 100644 --- a/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs +++ b/tests/Heimdall.IntegrationTests/Controllers/AuthFlowTests.cs @@ -103,7 +103,10 @@ public class AuthFlowTests : IAsyncLifetime response.StatusCode.ShouldBe(HttpStatusCode.OK); var body = await response.Content.ReadAsStringAsync(); body.ShouldContain("Heimdall"); - body.ShouldContain("Se connecter"); + // Prerender is disabled (InteractiveServerRenderMode prerender:false), + // so the static shell is served and the Login component content only + // appears after the SignalR circuit connects. Assert the Blazor shell. + body.ShouldContain("_framework/blazor.web.js"); } /* ─── API auth tests ───────────────────────────────────────── */ diff --git a/tests/Heimdall.Web.Tests/Components/MudTestBase.cs b/tests/Heimdall.Web.Tests/Components/MudTestBase.cs index 1cabcda..03ca75a 100644 --- a/tests/Heimdall.Web.Tests/Components/MudTestBase.cs +++ b/tests/Heimdall.Web.Tests/Components/MudTestBase.cs @@ -2,7 +2,9 @@ using Bunit; using Microsoft.Extensions.DependencyInjection; using MudBlazor; using MudBlazor.Services; +using System.Globalization; using System.Reflection; +using System.Threading; namespace Heimdall.Web.Tests.Components; @@ -13,8 +15,24 @@ namespace Heimdall.Web.Tests.Components; /// public abstract class MudTestBase : TestContext { + static MudTestBase() + { + // Deterministic rendering regardless of the machine/CI locale. + // Without this, {Confidence:P0} renders "87 %" (fr-FR, space) vs + // "87%" (en-US), making format-sensitive assertions flaky across runners. + var culture = CultureInfo.GetCultureInfo("en-US"); + CultureInfo.DefaultThreadCurrentCulture = culture; + CultureInfo.DefaultThreadCurrentUICulture = culture; + } + protected MudTestBase() { + // Also pin the current thread (xUnit may reuse a pooled thread whose + // culture was set before the static ctor ran). + var culture = CultureInfo.GetCultureInfo("en-US"); + Thread.CurrentThread.CurrentCulture = culture; + Thread.CurrentThread.CurrentUICulture = culture; + var services = (IServiceCollection)Services; // Register all MudBlazor services except KeyInterceptorService