From 624cfd15675d090da97406a031df6a0fc48016a1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 20:20:13 +0200 Subject: [PATCH] Concept tally export and its CORS policy move out of Program.cs One owner for the route and the policy, so Stryker can mutate them: it flags every mutant of Program.cs as a compile error (safe mode), which left the export untested by mutation. Program.cs keeps UseCors and the two registration calls. Co-Authored-By: Claude Opus 5.5 --- .../ConceptVotes/ConceptTallyExport.cs | 44 +++++++++++++++++++ src/Bifrost.Web/Program.cs | 29 +----------- 2 files changed, 46 insertions(+), 27 deletions(-) create mode 100644 src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs diff --git a/src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs b/src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs new file mode 100644 index 0000000..864bcab --- /dev/null +++ b/src/Bifrost.Web/Features/ConceptVotes/ConceptTallyExport.cs @@ -0,0 +1,44 @@ +using Bifrost.Web.Configuration; +using Microsoft.AspNetCore.Cors.Infrastructure; +using Microsoft.Extensions.Options; + +namespace Bifrost.Web.Features.ConceptVotes; + +// The public tally export and the one cross-origin policy in Bifrost. It owns nothing: counts are derived by +// ConceptAppraisalService, origins come from ConceptVoteOptions. +public static class ConceptTallyExport +{ + public const string CorsPolicy = "ConceptTallies"; + + // GET only, no credentials, and required by the export route alone: every other route carries no CORS + // metadata and gets no CORS headers. + public static IServiceCollection AddConceptTallyCors(this IServiceCollection services) + { + services.AddCors(); + services + .AddOptions() + .Configure>((cors, conceptVotes) => + cors.AddPolicy( + CorsPolicy, + policy => policy.WithOrigins(conceptVotes.Value.AllowedOrigins).WithMethods("GET"))); + return services; + } + + // Counts only, per live subject: ConceptTally carries no voter key, account id or network tag. + public static IEndpointConventionBuilder MapConceptTallyExport(this IEndpointRouteBuilder app) => + app.MapGet("/public/concepts/{collection}/tallies", async ( + string collection, + ConceptAppraisalService appraisals, + HttpResponse response, + CancellationToken cancellationToken) => + { + var tallies = await appraisals.GetTalliesAsync(collection, cancellationToken); + if (tallies.Count == 0) + { + return Results.NotFound(); + } + + response.Headers.CacheControl = "public, max-age=60"; + return Results.Json(tallies); + }).AllowAnonymous().RequireCors(CorsPolicy); +} diff --git a/src/Bifrost.Web/Program.cs b/src/Bifrost.Web/Program.cs index a210489..eac1bb0 100644 --- a/src/Bifrost.Web/Program.cs +++ b/src/Bifrost.Web/Program.cs @@ -17,7 +17,6 @@ using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OAuth; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.HttpLogging; @@ -206,16 +205,7 @@ builder.Services.AddAuthorization(options => }); }); -// The one cross-origin surface: the public tally export, read by browsers on the configured origins only. -// No credentials, GET only; every other route carries no CORS metadata and gets no CORS headers. -const string ConceptTalliesCorsPolicy = "ConceptTallies"; -builder.Services.AddCors(); -builder.Services - .AddOptions() - .Configure>((cors, conceptVotes) => - cors.AddPolicy( - ConceptTalliesCorsPolicy, - policy => policy.WithOrigins(conceptVotes.Value.AllowedOrigins).WithMethods("GET"))); +builder.Services.AddConceptTallyCors(); var app = builder.Build(); var appJsonSerializerOptions = CreateAppJsonSerializerOptions(); @@ -257,22 +247,7 @@ app.MapGet("/readyz", async (ReadinessService readinessService, CancellationToke statusCode: StatusCodes.Status503ServiceUnavailable); }).AllowAnonymous(); -// Counts only, per live subject: ConceptTally carries no voter key, account id or network tag. -app.MapGet("/public/concepts/{collection}/tallies", async ( - string collection, - ConceptAppraisalService appraisals, - HttpResponse response, - CancellationToken cancellationToken) => -{ - var tallies = await appraisals.GetTalliesAsync(collection, cancellationToken); - if (tallies.Count == 0) - { - return Results.NotFound(); - } - - response.Headers.CacheControl = "public, max-age=60"; - return Results.Json(tallies); -}).AllowAnonymous().RequireCors(ConceptTalliesCorsPolicy); +app.MapConceptTallyExport(); app.MapPost("/github/webhooks", async ( HttpRequest request,