diff --git a/CHANGELOG.md b/CHANGELOG.md index 4af97eac90..3cbc9cd1d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - Skip attachment if stream is empty ([#1854](https://github.com/getsentry/sentry-dotnet/pull/1854)) - Allow some mobile options to be modified from defaults ([#1857](https://github.com/getsentry/sentry-dotnet/pull/1857)) - Fix environment name casing issue ([#1861](https://github.com/getsentry/sentry-dotnet/pull/1861)) +- Null check HttpContext in SystemWebVersionLocator ([#1881](https://github.com/getsentry/sentry-dotnet/pull/1881)) ## 3.20.1 diff --git a/src/Sentry.AspNet/Internal/SystemWebVersionLocator.cs b/src/Sentry.AspNet/Internal/SystemWebVersionLocator.cs index a1919a70ac..cad8e2b3f1 100644 --- a/src/Sentry.AspNet/Internal/SystemWebVersionLocator.cs +++ b/src/Sentry.AspNet/Internal/SystemWebVersionLocator.cs @@ -5,7 +5,7 @@ namespace Sentry.AspNet.Internal; internal static class SystemWebVersionLocator { - internal static string? Resolve(string? release, HttpContext context) + internal static string? Resolve(string? release, HttpContext? context) { if (!string.IsNullOrWhiteSpace(release)) { @@ -15,9 +15,9 @@ internal static class SystemWebVersionLocator return Resolve(context); } - internal static string? Resolve(HttpContext context) + internal static string? Resolve(HttpContext? context) { - if (context.ApplicationInstance?.GetType() is { } type) + if (context?.ApplicationInstance?.GetType() is { } type) { // Usually the type is ASP.global_asax and the BaseType is the Web Application. while (type is { Namespace: "ASP" }) diff --git a/test/Sentry.AspNet.Tests/Internal/SystemWebVersionLocatorTests.cs b/test/Sentry.AspNet.Tests/Internal/SystemWebVersionLocatorTests.cs index 743c3b668c..d2f80da091 100644 --- a/test/Sentry.AspNet.Tests/Internal/SystemWebVersionLocatorTests.cs +++ b/test/Sentry.AspNet.Tests/Internal/SystemWebVersionLocatorTests.cs @@ -14,6 +14,14 @@ public void GetCurrent_GetEntryAssemblyNull_HttpApplicationAssembly() Assert.Equal(expected, actual); } + [Fact] + public void Null_HttpContext() + { + var actual = SystemWebVersionLocator.Resolve((string)null, null); + + Assert.Null(actual); + } + [Fact] public void HttpApplicationAssembly_VersionParsing() {