Skip to content

Commit

Permalink
Update packages
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Dec 11, 2024
1 parent 09545f3 commit c484386
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions EmployeePaycheck/EmployeePaycheck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageReference Include="Microsoft.Identity.Web.UI" Version="3.5.0" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="9.0.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="0.24.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="0.24.0" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders" Version="1.0.0-preview.2" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="1.0.0-preview.2" />
</ItemGroup>

</Project>
10 changes: 6 additions & 4 deletions EmployeePaycheck/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSecurityHeaderPolicies()
.SetPolicySelector(ctx => SecurityHeadersDefinitions
.GetHeaderPolicyCollection(builder.Environment.IsDevelopment()));

builder.Services.AddScoped<VerifierService>();
builder.Services.AddScoped<ValidateUserAndPaycheckIdService>();

Expand All @@ -29,9 +33,7 @@ public static void Main(string[] args)

var app = builder.Build();

app.UseSecurityHeaders(
SecurityHeadersDefinitions.GetHeaderPolicyCollection(
app.Environment.IsDevelopment()));
app.UseSecurityHeaders();

if (!app.Environment.IsDevelopment())
{
Expand All @@ -40,7 +42,7 @@ public static void Main(string[] args)
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.MapStaticAssets();

app.UseRouting();

Expand Down
29 changes: 11 additions & 18 deletions EmployeePaycheck/SecurityHeadersDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ namespace EmployeePaycheck;

public static class SecurityHeadersDefinitions
{
private static HeaderPolicyCollection? policy;

public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev)
{
var policy = new HeaderPolicyCollection()
// Avoid building a new HeaderPolicyCollection on every request for performance reasons.
// Where possible, cache and reuse HeaderPolicyCollection instances.
if (policy != null)
{
return policy;
}

policy = new HeaderPolicyCollection()
.AddFrameOptionsDeny()
.AddContentTypeOptionsNoSniff()
.AddReferrerPolicyStrictOriginWhenCrossOrigin()
Expand All @@ -23,23 +32,7 @@ public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev)
builder.AddScriptSrc().UnsafeInline().WithNonce();
builder.AddFrameAncestors().None();
})
.AddPermissionsPolicy(builder =>
{
builder.AddAccelerometer().None();
builder.AddAutoplay().None();
builder.AddCamera().None();
builder.AddEncryptedMedia().None();
builder.AddFullscreen().All();
builder.AddGeolocation().None();
builder.AddGyroscope().None();
builder.AddMagnetometer().None();
builder.AddMicrophone().None();
builder.AddMidi().None();
builder.AddPayment().None();
builder.AddPictureInPicture().None();
builder.AddSyncXHR().None();
builder.AddUsb().None();
});
.AddPermissionsPolicyWithDefaultSecureDirectives();

if (!isDev)
{
Expand Down

0 comments on commit c484386

Please sign in to comment.