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 security header middleware. #7753

Merged
merged 3 commits into from
Feb 26, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,6 +6,7 @@
using Volo.Abp;
using Volo.Abp.AspNetCore.Auditing;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.Security;
using Volo.Abp.AspNetCore.Security.Claims;
using Volo.Abp.AspNetCore.Tracing;
using Volo.Abp.AspNetCore.Uow;
Expand Down Expand Up @@ -82,5 +83,10 @@ public static IApplicationBuilder UseAbpClaimsMap(this IApplicationBuilder app)
{
return app.UseMiddleware<AbpClaimsMapMiddleware>();
}

public static IApplicationBuilder UseAbpSecurityHeaders(this IApplicationBuilder app)
{
return app.UseMiddleware<AbpSecurityHeadersMiddleware>();
}
}
}
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Volo.Abp.DependencyInjection;

namespace Volo.Abp.AspNetCore.Security
{
public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
/*X-Content-Type-Options header tells the browser to not try and “guess” what a mimetype of a resource might be, and to just take what mimetype the server has returned as fact.*/
AddHeaderIfNotExists(context, "X-Content-Type-Options", "nosniff");

/*X-XSS-Protection is a feature of Internet Explorer, Chrome and Safari that stops pages from loading when they detect reflected cross-site scripting (XSS) attacks*/
AddHeaderIfNotExists(context, "X-XSS-Protection", "1; mode=block");

/*The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame>, <iframe> or <object>. SAMEORIGIN makes it being displayed in a frame on the same origin as the page itself. The spec leaves it up to browser vendors to decide whether this option applies to the top level, the parent, or the whole chain*/
AddHeaderIfNotExists(context, "X-Frame-Options", "SAMEORIGIN");

await next.Invoke(context);
}

protected virtual void AddHeaderIfNotExists(HttpContext context, string key, string value)
{
context.Response.Headers.AddIfNotContains(new KeyValuePair<string, StringValues>(key, value));
}
}
}
Expand Up @@ -110,6 +110,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
app.UseCorrelationId();
app.UseVirtualFiles();
app.UseAbpRequestLocalization();
app.UseAbpSecurityHeaders();
app.UseRouting();
app.UseMiddleware<FakeAuthenticationMiddleware>();
app.UseAbpClaimsMap();
Expand Down
Expand Up @@ -2,21 +2,11 @@
using System.Security.Claims;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.AspNetCore.TestBase;
using Volo.Abp.Autofac;
using Volo.Abp.MemoryDb;
using Volo.Abp.Modularity;
using Volo.Abp.Security.Claims;
using Xunit;

namespace Volo.Abp.AspNetCore.Mvc.Authorization
{
[DependsOn(
typeof(AbpAspNetCoreTestBaseModule),
typeof(AbpMemoryDbTestModule),
typeof(AbpAspNetCoreMvcModule),
typeof(AbpAutofacModule)
)]
public class AuthTestController_Tests : AspNetCoreMvcTestBase
{
private readonly FakeUserClaims _fakeRequiredService;
Expand Down
Expand Up @@ -2,20 +2,10 @@
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.AspNetCore.Mvc.Authorization;
using Volo.Abp.AspNetCore.TestBase;
using Volo.Abp.Autofac;
using Volo.Abp.MemoryDb;
using Volo.Abp.Modularity;
using Xunit;

namespace Volo.Abp.AspNetCore.Mvc.Security.Claims
{
[DependsOn(
typeof(AbpAspNetCoreTestBaseModule),
typeof(AbpMemoryDbTestModule),
typeof(AbpAspNetCoreMvcModule),
typeof(AbpAutofacModule)
)]
public class ClaimsMapTestController_Tests : AspNetCoreMvcTestBase
{
private readonly FakeUserClaims _fakeRequiredService;
Expand Down
@@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Mvc;

namespace Volo.Abp.AspNetCore.Mvc.Security.Headers
{
public class SecurityHeadersTestController : AbpController
{
public ActionResult Get()
{
return Content("OK");
}
}
}
@@ -0,0 +1,19 @@
using System.Linq;
using System.Threading.Tasks;
using Shouldly;
using Xunit;

namespace Volo.Abp.AspNetCore.Mvc.Security.Headers
{
public class SecurityHeadersTestController_Tests : AspNetCoreMvcTestBase
{
[Fact]
public async Task SecurityHeaders_Should_Be_Added()
{
var responseMessage = await GetResponseAsync("/SecurityHeadersTest/Get");
responseMessage.Headers.ShouldContain(x => x.Key == "X-Content-Type-Options" & x.Value.First().ToString() == "nosniff");
responseMessage.Headers.ShouldContain(x => x.Key == "X-XSS-Protection" & x.Value.First().ToString() == "1; mode=block");
responseMessage.Headers.ShouldContain(x => x.Key == "X-Frame-Options" & x.Value.First().ToString() == "SAMEORIGIN");
}
}
}
4 changes: 1 addition & 3 deletions modules/docs/app/VoloDocs.Web/VoloDocsWebModule.cs
Expand Up @@ -152,12 +152,10 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

app.UseVirtualFiles();
app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseAbpRequestLocalization();

app.UseAbpSecurityHeaders();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
Expand Down
Expand Up @@ -188,6 +188,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseAbpRequestLocalization();
app.UseAbpSecurityHeaders();

if (!env.IsDevelopment())
{
Expand Down
Expand Up @@ -197,6 +197,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseAbpRequestLocalization();
app.UseAbpSecurityHeaders();

if (!env.IsDevelopment())
{
Expand Down
Expand Up @@ -165,6 +165,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseAbpRequestLocalization();
app.UseAbpSecurityHeaders();

if (!env.IsDevelopment())
{
Expand Down
Expand Up @@ -240,6 +240,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseAbpRequestLocalization();
app.UseAbpSecurityHeaders();

if (!env.IsDevelopment())
{
Expand Down
Expand Up @@ -215,6 +215,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex
}

app.UseAbpRequestLocalization();
app.UseAbpSecurityHeaders();

if (!env.IsDevelopment())
{
Expand Down