Asp net core 内容安全策略实施
Asp net core Content Security Policy implementation
我已经在我的应用程序中实现了管理内容安全策略层的代码。
我的实现基于 ActionFilterAttribute
,它的灵感来自此处可用的代码(为简单起见,我将其包含在问题中)。
public override void OnResultExecuting( ResultExecutingContext context ) {
var result = context.Result;
if ( result is ViewResult ) {
if ( !context.HttpContext.Response.Headers.ContainsKey( "X-Content-Type-Options" ) ) {
context.HttpContext.Response.Headers.Add( "X-Content-Type-Options", "nosniff" );
}
if ( !context.HttpContext.Response.Headers.ContainsKey( "X-Frame-Options" ) ) {
context.HttpContext.Response.Headers.Add( "X-Frame-Options", "SAMEORIGIN" );
}
var csp = "default-src *;";
// once for standards compliant browsers
if ( !context.HttpContext.Response.Headers.ContainsKey( "Content-Security-Policy" ) ) {
context.HttpContext.Response.Headers.Add( "Content-Security-Policy", csp );
}
// and once again for IE
if ( !context.HttpContext.Response.Headers.ContainsKey( "X-Content-Security-Policy" ) ) {
context.HttpContext.Response.Headers.Add( "X-Content-Security-Policy", csp );
}
}
}
但是,正如您从下面的图片中看到的那样,我在浏览器(示例中的 Firefox)中仍然出现错误。这是显示 header 的开发人员控制台:
这些是控制台错误
我做错了什么,特别是控制台中的最后三个错误?
要消除控制台屏幕截图中的 CSP 错误,您必须 header 发生:
Content-Security-Policy:
script-src 'self' https://cdnjs.cloudflare.com;
style-src 'self' https://fonts.googleapis.com;
img-src 'self' data:
(为了便于阅读,上面显示的值被分成多行。)
重点是:
- 你需要
'self'
在那里
- 您需要为要从
加载字体和脚本的 third-party https://cdnjs.cloudflare.com
和 https://fonts.googleapis.com
来源提供来源值
- 您需要在其中添加
data:
才能在标记中使用 data:image/gif
URL
如果文档确实也在从 https://localhost:5000
加载资源,那么您也需要在其中包含它。
并且如果您的后端已经有其他部分正在添加 CSP header,那么请务必了解您添加的任何带有额外 CSP 的策略 header 只会使策略更严格, 不是更自由。
因此,如果在其他地方添加的 CSP header 比您需要的更严格,那么您必须找到正在添加它的系统部分,并使其停止。然后你可以添加你需要的more-liberal CSP header。
我已经在我的应用程序中实现了管理内容安全策略层的代码。
我的实现基于 ActionFilterAttribute
,它的灵感来自此处可用的代码(为简单起见,我将其包含在问题中)。
public override void OnResultExecuting( ResultExecutingContext context ) {
var result = context.Result;
if ( result is ViewResult ) {
if ( !context.HttpContext.Response.Headers.ContainsKey( "X-Content-Type-Options" ) ) {
context.HttpContext.Response.Headers.Add( "X-Content-Type-Options", "nosniff" );
}
if ( !context.HttpContext.Response.Headers.ContainsKey( "X-Frame-Options" ) ) {
context.HttpContext.Response.Headers.Add( "X-Frame-Options", "SAMEORIGIN" );
}
var csp = "default-src *;";
// once for standards compliant browsers
if ( !context.HttpContext.Response.Headers.ContainsKey( "Content-Security-Policy" ) ) {
context.HttpContext.Response.Headers.Add( "Content-Security-Policy", csp );
}
// and once again for IE
if ( !context.HttpContext.Response.Headers.ContainsKey( "X-Content-Security-Policy" ) ) {
context.HttpContext.Response.Headers.Add( "X-Content-Security-Policy", csp );
}
}
}
但是,正如您从下面的图片中看到的那样,我在浏览器(示例中的 Firefox)中仍然出现错误。这是显示 header 的开发人员控制台:
这些是控制台错误
我做错了什么,特别是控制台中的最后三个错误?
要消除控制台屏幕截图中的 CSP 错误,您必须 header 发生:
Content-Security-Policy:
script-src 'self' https://cdnjs.cloudflare.com;
style-src 'self' https://fonts.googleapis.com;
img-src 'self' data:
(为了便于阅读,上面显示的值被分成多行。)
重点是:
- 你需要
'self'
在那里 - 您需要为要从 加载字体和脚本的 third-party
- 您需要在其中添加
data:
才能在标记中使用data:image/gif
URL
https://cdnjs.cloudflare.com
和 https://fonts.googleapis.com
来源提供来源值
如果文档确实也在从 https://localhost:5000
加载资源,那么您也需要在其中包含它。
并且如果您的后端已经有其他部分正在添加 CSP header,那么请务必了解您添加的任何带有额外 CSP 的策略 header 只会使策略更严格, 不是更自由。
因此,如果在其他地方添加的 CSP header 比您需要的更严格,那么您必须找到正在添加它的系统部分,并使其停止。然后你可以添加你需要的more-liberal CSP header。