|
|
@ -33,7 +33,7 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
|
|
|
|
|
|
|
|
|
|
|
|
var requestAcceptTypeHtml = context.Request.Headers["Accept"].Any(x =>
|
|
|
|
var requestAcceptTypeHtml = context.Request.Headers["Accept"].Any(x =>
|
|
|
|
x!.Contains("text/html") || x.Contains("*/*") || x.Contains("application/xhtml+xml"));
|
|
|
|
x!.Contains("text/html") || x.Contains("*/*") || x.Contains("application/xhtml+xml"));
|
|
|
|
|
|
|
|
|
|
|
|
var endpoint = context.GetEndpoint();
|
|
|
|
var endpoint = context.GetEndpoint();
|
|
|
|
|
|
|
|
|
|
|
|
if (endpoint?.Metadata.GetMetadata<IgnoreAbpSecurityHeaderAttribute>() != null)
|
|
|
|
if (endpoint?.Metadata.GetMetadata<IgnoreAbpSecurityHeaderAttribute>() != null)
|
|
|
@ -42,11 +42,11 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!requestAcceptTypeHtml
|
|
|
|
if (!requestAcceptTypeHtml
|
|
|
|
|| !Options.Value.UseContentSecurityPolicyHeader
|
|
|
|
|| !Options.Value.UseContentSecurityPolicyHeader
|
|
|
|
|| await AlwaysIgnoreContentTypes(context)
|
|
|
|
|| await AlwaysIgnoreContentTypes(context)
|
|
|
|
|| endpoint == null
|
|
|
|
|| endpoint == null
|
|
|
|
|| Options.Value.IgnoredScriptNoncePaths.Any(x => context.Request.Path.StartsWithSegments(x.EnsureStartsWith('/'))))
|
|
|
|
|| Options.Value.IgnoredScriptNoncePaths.Any(x => context.Request.Path.StartsWithSegments(x.EnsureStartsWith('/'), StringComparison.OrdinalIgnoreCase)))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
AddOtherHeaders(context);
|
|
|
|
AddOtherHeaders(context);
|
|
|
|
await next.Invoke(context);
|
|
|
|
await next.Invoke(context);
|
|
|
@ -71,7 +71,7 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (context.Response.StatusCode is < 200 or > 299)
|
|
|
|
if (context.Response.StatusCode is < 200 or > 299)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
return Task.CompletedTask;
|
|
|
@ -85,7 +85,7 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
|
|
|
|
AddOtherHeaders(context);
|
|
|
|
AddOtherHeaders(context);
|
|
|
|
await next.Invoke(context);
|
|
|
|
await next.Invoke(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<bool> AlwaysIgnoreContentTypes(HttpContext context)
|
|
|
|
private async Task<bool> AlwaysIgnoreContentTypes(HttpContext context)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
foreach (var selector in Options.Value.IgnoredScriptNonceSelectors)
|
|
|
|
foreach (var selector in Options.Value.IgnoredScriptNonceSelectors)
|
|
|
@ -95,7 +95,7 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -130,7 +130,7 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
|
|
|
|
var newScriptSrcValue = scriptSrcValue + nonceStr;
|
|
|
|
var newScriptSrcValue = scriptSrcValue + nonceStr;
|
|
|
|
return Options.Value.ContentSecurityPolicyValue!.Replace(scriptSrcValue!, newScriptSrcValue);
|
|
|
|
return Options.Value.ContentSecurityPolicyValue!.Replace(scriptSrcValue!, newScriptSrcValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual void AddHeader(HttpContext context, string key, string value, bool overrideIfExists = false)
|
|
|
|
protected virtual void AddHeader(HttpContext context, string key, string value, bool overrideIfExists = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -142,4 +142,4 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency
|
|
|
|
|
|
|
|
|
|
|
|
context.Response.Headers.AddIfNotContains(new KeyValuePair<string, StringValues>(key, value));
|
|
|
|
context.Response.Headers.AddIfNotContains(new KeyValuePair<string, StringValues>(key, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|