Skip to content

Commit

Permalink
Updated security headers
Browse files Browse the repository at this point in the history
  • Loading branch information
damienbod committed Oct 26, 2024
1 parent b85644b commit 8f1cefd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions AspNetCoreSelectTenant/AspNetCoreSelectTenant.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.10" NoWarn="NU1605" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.2.2" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="3.2.2" />
<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.1" />
<PackageReference Include="NetEscapades.AspNetCore.SecurityHeaders.TagHelpers" Version="1.0.0-preview.1" />

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10" PrivateAssets="All" />
Expand Down
11 changes: 9 additions & 2 deletions AspNetCoreSelectTenant/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using NetEscapades.AspNetCore.SecurityHeaders.Infrastructure;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -17,6 +18,13 @@
var services = builder.Services;
var configuration = builder.Configuration;

services.AddSecurityHeaderPolicies()
.SetPolicySelector((PolicySelectorContext ctx) =>
{
return SecurityHeadersDefinitions
.GetHeaderPolicyCollection(builder.Environment.IsDevelopment());
});

services.AddDistributedMemoryCache();

var connection = configuration.GetConnectionString("DefaultConnection");
Expand Down Expand Up @@ -101,8 +109,7 @@
app.UseHsts();
}

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

app.UseHttpsRedirection();
app.UseStaticFiles();
Expand Down
28 changes: 8 additions & 20 deletions AspNetCoreSelectTenant/SecurityHeadersDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

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 = policy = new HeaderPolicyCollection()
.AddFrameOptionsDeny()
.AddContentTypeOptionsNoSniff()
.AddReferrerPolicyStrictOriginWhenCrossOrigin()
Expand All @@ -25,32 +31,14 @@ public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev)
// builder.AddCustomDirective("require-trusted-types-for", "'script'");
})
.RemoveServerHeader()
.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)
{
// maxage = one year in seconds
policy.AddStrictTransportSecurityMaxAgeIncludeSubDomains(maxAgeInSeconds: 60 * 60 * 24 * 365);
}

policy.ApplyDocumentHeadersToAllResponses();

return policy;
}
}

0 comments on commit 8f1cefd

Please sign in to comment.