diff --git a/Dfe.Academies.Academisation.WebApi/Program.cs b/Dfe.Academies.Academisation.WebApi/Program.cs index e1769f384..85b97b044 100644 --- a/Dfe.Academies.Academisation.WebApi/Program.cs +++ b/Dfe.Academies.Academisation.WebApi/Program.cs @@ -1,4 +1,5 @@ using System.Reflection; +using System.Text; using System.Text.Json; using Dfe.Academies.Academisation.Core.Utils; using Dfe.Academies.Academisation.Data; @@ -39,9 +40,12 @@ using FluentValidation; using MediatR; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Options; +using NetEscapades.AspNetCore.SecurityHeaders; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Serialization; +using Swashbuckle.AspNetCore.SwaggerUI; var builder = WebApplication.CreateBuilder(args); @@ -151,6 +155,35 @@ builder.Services.ConfigureOptions(); builder.Services.AddAutoMapper(typeof(AutoMapperProfile)); +builder.Services.AddHttpContextAccessor(); + +builder.Services + .AddOptions() + .Configure((swaggerUiOptions, httpContextAccessor) => + { + // 2. Take a reference of the original Stream factory which reads from Swashbuckle's embedded resources + var originalIndexStreamFactory = swaggerUiOptions.IndexStream; + + // 3. Override the Stream factory + swaggerUiOptions.IndexStream = () => + { + // 4. Read the original index.html file + using var originalStream = originalIndexStreamFactory(); + using var originalStreamReader = new StreamReader(originalStream); + var originalIndexHtmlContents = originalStreamReader.ReadToEnd(); + + // 5. Get the request-specific nonce generated by NetEscapades.AspNetCore.SecurityHeaders + var requestSpecificNonce = httpContextAccessor.HttpContext.GetNonce(); + + // 6. Replace inline `