From b14d8b5c171e208b649bfe9e19ee12439f06c966 Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:22:45 +0100 Subject: [PATCH 01/16] Variable name change to be more explicit --- .../Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml index fbfbeb8f7..d07d62384 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml @@ -6,7 +6,7 @@ var analyticsConsent = Context.Request.Cookies.ContainsKey(".ManageAnAcademyConversion.Consent") && bool.Parse(Context.Request.Cookies[".ManageAnAcademyConversion.Consent"] ?? string.Empty); - var showAnalytics = Configuration["GoogleAnalytics:Enable"] == "Yes" && analyticsConsent; + var enableGoogleAnalytics = Configuration["GoogleAnalytics:Enable"] == "Yes" && analyticsConsent; var titleDescription = Context.Request.Path == "/project-type" ? string.Empty @@ -38,7 +38,7 @@ - @if (showAnalytics) + @if (enableGoogleAnalytics) { - + })(window, document, 'script', 'dataLayer', 'GTM-5H6G773'); + + } - @if (analyticsConsent) + @if (enableAppInsightsAnalytics) { - - - } @@ -71,6 +67,15 @@
+ @if (enableGoogleAnalytics) + { + + + + } + From e5b99f54dc67c729894e5e727c5cc3beb68065ed Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:10:34 +0100 Subject: [PATCH 06/16] Added App Insights Browser JS --- .../Pages/Shared/_Layout.cshtml | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml index ef9a83f8f..52711e810 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml @@ -60,6 +60,29 @@ @if (enableAppInsightsAnalytics) { + + + + } From 630364b172a9dca0ae03dc29c6545802d83ac7b0 Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:22:25 +0100 Subject: [PATCH 07/16] Use identity name as email claim is not set --- .../Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml index 52711e810..6371273c5 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_Layout.cshtml @@ -18,7 +18,7 @@ var notificationBannerMessage = Configuration["notificationBannerMessage"] ?? string.Empty; - var authenticatedUserId = User.Identity is not null && User.Identity.IsAuthenticated ? User.FindFirst(ClaimTypes.Email)?.Value ?? "Unknown" : "Anonymous"; + var authenticatedUserId = User.Identity is not null && User.Identity.IsAuthenticated ? User.Identity.Name ?? "Unknown" : "Anonymous"; } From c639ef83cb3b28310e435b16c62b7ded6638102e Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:23:16 +0100 Subject: [PATCH 08/16] Corrected EoL feed --- .../Security/SecureHeadersDefinitions.cs | 142 +++++++++--------- 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs index 4b5a8e2c6..9a85d066f 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs @@ -1,71 +1,71 @@ -using Microsoft.AspNetCore.Builder; - -namespace Dfe.PrepareConversions.Security; - -public static class SecurityHeadersDefinitions -{ - private static string GoogleTagManagerUri => "https://www.googletagmanager.com"; - private static string GoogleAnalyticsUri => "https://www.google-analytics.com/"; - - public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev) - { - HeaderPolicyCollection policy = new HeaderPolicyCollection() - .AddFrameOptionsDeny() - .AddXssProtectionBlock() - .AddContentTypeOptionsNoSniff() - .AddReferrerPolicyStrictOriginWhenCrossOrigin() - .RemoveServerHeader() - .AddCrossOriginOpenerPolicy(builder => - { - builder.SameOrigin(); - }) - .AddCrossOriginEmbedderPolicy(builder => - { - builder.RequireCorp(); - }) - .AddCrossOriginResourcePolicy(builder => - { - builder.SameOrigin(); - }) - .AddContentSecurityPolicy(builder => - { - builder.AddObjectSrc().None(); - builder.AddBlockAllMixedContent(); - builder.AddImgSrc().Self().From("data:").From(GoogleAnalyticsUri) - .From(GoogleTagManagerUri); - builder.AddFormAction().Self(); - builder.AddFormAction().OverHttps(); - builder.AddFontSrc().Self(); - builder.AddStyleSrc().Self(); - builder.AddBaseUri().Self(); - builder.AddScriptSrc().From(GoogleTagManagerUri).UnsafeInline().WithNonce(); - builder.AddFrameAncestors().None(); - }) - .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(); - }); - - if (!isDev) - { - // max age = one year in seconds - policy.AddStrictTransportSecurityMaxAgeIncludeSubDomains(maxAgeInSeconds: 60 * 60 * 24 * 365); - } - - return policy; - } -} +using Microsoft.AspNetCore.Builder; + +namespace Dfe.PrepareConversions.Security; + +public static class SecurityHeadersDefinitions +{ + private static string GoogleTagManagerUri => "https://www.googletagmanager.com"; + private static string GoogleAnalyticsUri => "https://www.google-analytics.com/"; + + public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev) + { + HeaderPolicyCollection policy = new HeaderPolicyCollection() + .AddFrameOptionsDeny() + .AddXssProtectionBlock() + .AddContentTypeOptionsNoSniff() + .AddReferrerPolicyStrictOriginWhenCrossOrigin() + .RemoveServerHeader() + .AddCrossOriginOpenerPolicy(builder => + { + builder.SameOrigin(); + }) + .AddCrossOriginEmbedderPolicy(builder => + { + builder.RequireCorp(); + }) + .AddCrossOriginResourcePolicy(builder => + { + builder.SameOrigin(); + }) + .AddContentSecurityPolicy(builder => + { + builder.AddObjectSrc().None(); + builder.AddBlockAllMixedContent(); + builder.AddImgSrc().Self().From("data:").From(GoogleAnalyticsUri) + .From(GoogleTagManagerUri); + builder.AddFormAction().Self(); + builder.AddFormAction().OverHttps(); + builder.AddFontSrc().Self(); + builder.AddStyleSrc().Self(); + builder.AddBaseUri().Self(); + builder.AddScriptSrc().From(GoogleTagManagerUri).UnsafeInline().WithNonce(); + builder.AddFrameAncestors().None(); + }) + .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(); + }); + + if (!isDev) + { + // max age = one year in seconds + policy.AddStrictTransportSecurityMaxAgeIncludeSubDomains(maxAgeInSeconds: 60 * 60 * 24 * 365); + } + + return policy; + } +} From 2d94429962336b239f546f356289437d3e9587fb Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:23:33 +0100 Subject: [PATCH 09/16] Added App Insights to CSP --- .../Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs b/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs index 9a85d066f..1bd9fa8f9 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Security/SecureHeadersDefinitions.cs @@ -6,6 +6,7 @@ public static class SecurityHeadersDefinitions { private static string GoogleTagManagerUri => "https://www.googletagmanager.com"; private static string GoogleAnalyticsUri => "https://www.google-analytics.com/"; + private static string ApplicationInsightsUri => "https://js.monitor.azure.com/"; public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev) { @@ -39,6 +40,7 @@ public static HeaderPolicyCollection GetHeaderPolicyCollection(bool isDev) builder.AddStyleSrc().Self(); builder.AddBaseUri().Self(); builder.AddScriptSrc().From(GoogleTagManagerUri).UnsafeInline().WithNonce(); + builder.AddScriptSrc().From(ApplicationInsightsUri).UnsafeInline().WithNonce(); builder.AddFrameAncestors().None(); }) .RemoveServerHeader() From acd3b9ae48d7a02528d7fd89b4f53a9fedecf895 Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Tue, 11 Jun 2024 17:29:48 +0100 Subject: [PATCH 10/16] Updated GA script to match shared Layout.cshtml --- .../Shared/_LayoutNoHeaderAndFooter.cshtml | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_LayoutNoHeaderAndFooter.cshtml b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_LayoutNoHeaderAndFooter.cshtml index d9152915a..127412dc9 100644 --- a/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_LayoutNoHeaderAndFooter.cshtml +++ b/Dfe.PrepareConversions/Dfe.PrepareConversions/Pages/Shared/_LayoutNoHeaderAndFooter.cshtml @@ -6,7 +6,7 @@ var analyticsConsent = Context.Request.Cookies.ContainsKey(".ManageAnAcademyConversion.Consent") && bool.Parse(Context.Request.Cookies[".ManageAnAcademyConversion.Consent"] ?? string.Empty); - var showAnalytics = Configuration["GoogleAnalytics:Enable"] == "Yes" && analyticsConsent; + var enableGoogleAnalytics = Configuration["GoogleAnalytics:Enable"] == "Yes" && analyticsConsent; } @@ -28,31 +28,38 @@