Skip to content

Commit f89591b

Browse files
bouwkastandrewlock
andauthored
Protect against claimsPrincipal being null (#7719)
## Summary of changes We are getting `NullReferenceException` in `HttpContextSetUser`, guessing the `claimsPrincipal` can be `null`. ## Reason for change ``` Error : Exception occurred when calling the CallTarget integration continuation. System.NullReferenceException at Datadog.Trace.ClrProfiler.AutoInstrumentation.AspNetCore.UserEvents.HttpContextSetUser.OnMethodBegin[TTarget](TTarget instance, ClaimsPrincipal& claimsPrincipal) at Microsoft.AspNetCore.Http.DefaultHttpContext.set_User(ClaimsPrincipal value) ``` ## Implementation details Added `claimsPrincipal is not null` ## Test coverage None 🤷 ## Other details <!-- Fixes #{issue} --> <!-- ⚠️ Note: Where possible, please obtain 2 approvals prior to merging. Unless CODEOWNERS specifies otherwise, for external teams it is typically best to have one review from a team member, and one review from apm-dotnet. Trivial changes do not require 2 reviews. MergeQueue is NOT enabled in this repository. If you have write access to the repo, the PR has 1-2 approvals (see above), and all of the required checks have passed, you can use the Squash and Merge button to merge the PR. If you don't have write access, or you need help, reach out in the #apm-dotnet channel in Slack. --> --------- Co-authored-by: Andrew Lock <andrew.lock@datadoghq.com>
1 parent 0ae1382 commit f89591b

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/AspNetCore/UserEvents/HttpContextSetUser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public static class HttpContextSetUser
4343

4444
private const string HttpContextExtensionsTypeName = "Microsoft.AspNetCore.Http.DefaultHttpContext";
4545

46-
internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, ref ClaimsPrincipal claimsPrincipal)
46+
internal static CallTargetState OnMethodBegin<TTarget>(TTarget instance, ref ClaimsPrincipal? claimsPrincipal)
4747
{
4848
if (Security.Instance is { IsTrackUserEventsEnabled: true } security)
4949
{
5050
var tracer = Tracer.Instance;
5151
var scope = tracer.InternalActiveScope;
52-
if (instance is HttpContext httpContext && scope is not null)
52+
if (instance is HttpContext httpContext && scope is not null && claimsPrincipal is not null)
5353
{
5454
var span = scope.Span;
5555
var foundUserId = false;

0 commit comments

Comments
 (0)