Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added frontend security testing #948

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="DfE.CoreLibs.Testing" Version="1.1.9" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
Expand All @@ -31,4 +32,10 @@
<ProjectReference Include="..\Dfe.ManageFreeSchoolProjects\Dfe.ManageFreeSchoolProjects.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="ExpectedSecurityConfig.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Endpoints": [
{
"Route": "/Public/AccessibilityStatement",
"ExpectedSecurity": "AllowAnonymous"
},
{
"Route": "/Public/Cookies",
"ExpectedSecurity": "AllowAnonymous"
},
{
"Route": "/Account/AccessDenied",
"ExpectedSecurity": "AllowAnonymous"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Routing;
using DfE.CoreLibs.Testing.Authorization;
using DfE.CoreLibs.Testing.Mocks.WebApplicationFactory;
using Microsoft.Extensions.DependencyInjection;
using DfE.CoreLibs.Testing.Authorization.Helpers;

namespace Dfe.ManageFreeSchoolProjects.Tests.Pages
{
public class PageSecurityTests
{
private readonly AuthorizationTester _validator;
private static readonly Lazy<IEnumerable<RouteEndpoint>> _endpoints = new(InitializeEndpoints);
private const bool _globalAuthorizationEnabled = true;

public PageSecurityTests()
{
_validator = new AuthorizationTester(_globalAuthorizationEnabled);
}

[Theory]
[MemberData(nameof(GetPageSecurityTestData))]
public void ValidatePageSecurity(string route, string expectedSecurity)
{
var result = _validator.ValidatePageSecurity(route, expectedSecurity, _endpoints.Value);
Assert.Null(result.Message);
}

public static IEnumerable<object[]> GetPageSecurityTestData()
{
var configFilePath = "ExpectedSecurityConfig.json";
var pages = EndpointTestDataProvider.GetPageSecurityTestDataFromFile(configFilePath, _endpoints.Value, _globalAuthorizationEnabled);
return pages;
}

private static IEnumerable<RouteEndpoint> InitializeEndpoints()
{
// Using a temporary factory to access the EndpointDataSource for lazy initialization
var factory = new CustomWebApplicationFactory<Startup>();
var endpointDataSource = factory.Services.GetRequiredService<EndpointDataSource>();

var endpoints = endpointDataSource.Endpoints
.OfType<RouteEndpoint>()
.Where(x => !x.RoutePattern.RawText!.Contains("MicrosoftIdentity") &&
!x.RoutePattern.RawText.Equals("/") &&
!x.Metadata.Any(m => m is RouteNameMetadata && ((RouteNameMetadata)m).RouteName == "default"));

return endpoints;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@page "/access-denied"
@using Microsoft.AspNetCore.Authorization
@attribute [AllowAnonymous]
@inject Microsoft.Extensions.Configuration.IConfiguration _configuration
@model Microsoft.Identity.Web.UI.Areas.MicrosoftIdentity.Pages.Account.AccessDeniedModel
@{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@page "/accessibility-statement"

@using Microsoft.AspNetCore.Authorization
@attribute [AllowAnonymous]
@inject Microsoft.Extensions.Configuration.IConfiguration _configuration

@{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@page "/public/cookies"
@using Microsoft.AspNetCore.Authorization
@attribute [AllowAnonymous]
@model Dfe.ManageFreeSchoolProjects.Pages.Public.Cookies

@{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public void ConfigureServices(IServiceCollection services)
.AddRazorPages(options =>
{
options.Conventions.AuthorizeFolder("/");
})
options.Conventions.AllowAnonymousToPage("/Public/AccessibilityStatement");
options.Conventions.AllowAnonymousToPage("/Public/Cookies");
options.Conventions.AllowAnonymousToPage("/Account/AccessDenied");
})
.AddViewOptions(options =>
{
options.HtmlHelperOptions.ClientValidationEnabled = false;
Expand Down
Loading