11using Microsoft . AspNetCore . Builder ;
22using Microsoft . AspNetCore . Hosting ;
33using Microsoft . AspNetCore . HttpOverrides ;
4- using System . Collections . Generic ;
54
65namespace Jp . UI . SSO . Configuration
76{
87 public static class SecurityHeadersConfiguration
98 {
109 public static void UseSecurityHeaders ( this IApplicationBuilder app , IHostingEnvironment env )
1110 {
11+
1212 app . UseForwardedHeaders ( new ForwardedHeadersOptions ( )
1313 {
1414 ForwardedHeaders = ForwardedHeaders . XForwardedFor | ForwardedHeaders . XForwardedProto
@@ -22,22 +22,28 @@ public static void UseSecurityHeaders(this IApplicationBuilder app, IHostingEnvi
2222 app . UseCsp ( options =>
2323 {
2424 options . DefaultSources ( o => o . Self ( ) ) ;
25+ options . ObjectSources ( o => o . None ( ) ) ;
26+ options . FrameAncestors ( o => o . None ( ) ) ;
27+ options . Sandbox ( directive => directive . AllowForms ( ) . AllowSameOrigin ( ) . AllowScripts ( ) ) ;
28+ options . BaseUris ( configuration => configuration . Self ( ) ) ;
2529 options . FrameSources ( o => o . Self ( )
2630 // this custom source can be removed in your build
2731 . CustomSources ( "https://ghbtns.com" ) ) ;
28- options . FrameAncestors ( o => o . CustomSources ( "http:" ) ) ;
29- options . StyleSources ( o => o . Self ( ) ) ;
30- options . ObjectSources ( o => o . None ( ) ) ;
32+
33+ if ( env . IsProduction ( ) )
34+ options . UpgradeInsecureRequests ( ) ;
3135 options . ImageSources ( a =>
3236 {
3337 a . Self ( ) ;
3438 a . CustomSources = new [ ] { "data: https:" } ;
3539 } ) ;
3640 options . FontSources ( configuration => configuration . Self ( ) . CustomSources ( "https://fonts.googleapis.com/" , "https://fonts.gstatic.com/" ) ) ;
37- options . ConnectSources ( s => s . CustomSources ( "https://dc.services.visualstudio.com" ) ) ;
38- options . ScriptSources ( s => s . Self ( ) . CustomSources ( "https://az416426.vo.msecnd.net" , @"sha256-ZT3q7lL9GXNGhPTB1Vvrvds2xw/kOV0zoeok2tiV23I= ") ) ;
41+ options . ConnectSources ( s => s . Self ( ) . CustomSources ( "https://dc.services.visualstudio.com" ) ) ;
42+ options . ScriptSources ( s => s . Self ( ) . UnsafeInline ( ) . CustomSources ( "https://az416426.vo.msecnd.net" ) ) ;
3943
4044 } ) ;
45+
46+
4147 }
4248
4349 }
0 commit comments