From 39ee46e0afbbb4deb4edd883eaaa38a32f7aca2e Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 8 Jul 2019 16:02:47 -0700 Subject: [PATCH 1/4] make JavascriptEncoder optional parameter to prevent crash when no encoder is available. --- .../JavaScriptSnippet.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs b/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs index f394d8f7..cdf253b0 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs @@ -41,7 +41,7 @@ public JavaScriptSnippet( TelemetryConfiguration telemetryConfiguration, IOptions serviceOptions, IHttpContextAccessor httpContextAccessor, - JavaScriptEncoder encoder) + JavaScriptEncoder encoder = null) { this.telemetryConfiguration = telemetryConfiguration; this.httpContextAccessor = httpContextAccessor; @@ -63,6 +63,7 @@ public string FullScript string additionalJS = string.Empty; IIdentity identity = this.httpContextAccessor?.HttpContext?.User?.Identity; if (enableAuthSnippet && + this.encoder != null && identity != null && identity.IsAuthenticated) { From b62b43df6408fb1f0dedf92687bc55f6777b17aa Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 8 Jul 2019 16:13:47 -0700 Subject: [PATCH 2/4] fallback to Default encoder. --- .../JavaScriptSnippet.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs b/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs index cdf253b0..0403ca59 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs @@ -46,7 +46,7 @@ public JavaScriptSnippet( this.telemetryConfiguration = telemetryConfiguration; this.httpContextAccessor = httpContextAccessor; this.enableAuthSnippet = serviceOptions.Value.EnableAuthenticationTrackingJavaScript; - this.encoder = encoder; + this.encoder = (encoder == null) ? JavaScriptEncoder.Default : encoder; } /// @@ -62,11 +62,10 @@ public string FullScript { string additionalJS = string.Empty; IIdentity identity = this.httpContextAccessor?.HttpContext?.User?.Identity; - if (enableAuthSnippet && - this.encoder != null && + if (enableAuthSnippet && identity != null && identity.IsAuthenticated) - { + { string escapedUserName = encoder.Encode(identity.Name); additionalJS = string.Format(CultureInfo.InvariantCulture, AuthSnippet, escapedUserName); } From 3671876ba1f070609ee293d95248af2e462acca4 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 8 Jul 2019 16:14:40 -0700 Subject: [PATCH 3/4] fix spacing --- .../JavaScriptSnippet.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs b/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs index 0403ca59..ee45d207 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/JavaScriptSnippet.cs @@ -62,10 +62,10 @@ public string FullScript { string additionalJS = string.Empty; IIdentity identity = this.httpContextAccessor?.HttpContext?.User?.Identity; - if (enableAuthSnippet && + if (enableAuthSnippet && identity != null && identity.IsAuthenticated) - { + { string escapedUserName = encoder.Encode(identity.Name); additionalJS = string.Format(CultureInfo.InvariantCulture, AuthSnippet, escapedUserName); } From e616e856f460f90569dae8108874368cc896e54b Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Mon, 8 Jul 2019 16:32:22 -0700 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cefd75e..9101b10f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Version 2.8.0-beta1 - [Fix: Add `IJavaScriptSnippet` service interface and update the `IServiceCollection` extension to register it for `JavaScriptSnippet`.](https://github.com/microsoft/ApplicationInsights-aspnetcore/issues/890) +- [Make JavaScriptEncoder optional and Fallback to JavaScriptEncoder.Default.](https://github.com/microsoft/ApplicationInsights-aspnetcore/pull/918) + +## Version 2.7.1 - [Fix - ApplicationInsights StartupFilter should not swallow exceptions from downstream ApplicationBuilder.](https://github.com/microsoft/ApplicationInsights-aspnetcore/issues/897) ## Version 2.7.0