Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove closure from JsonWebTokenHandler.ValidateSignature #2169

Merged
merged 1 commit into from
Jul 26, 2023

Conversation

stephentoub
Copy link
Collaborator

The Any call on an exceptional path is closing over the jwtToken parameter, which means a display class gets allocated at the beginning of the method, whether that code path is taken or not.

The Any call on an exceptional path is closing over the jwtToken parameter, which means a display class gets allocated at the beginning of the method, whether that code path is taken or not.
@@ -1760,7 +1760,8 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
{
if (kidMatched)
{
var isKidInTVP = keysInTokenValidationParameters.Any(x => x.KeyId.Equals(jwtToken.Kid));
JsonWebToken localJwtToken = jwtToken; // avoid closure on non-exceptional path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so tricky to catch both when coding and in review. Is there a way a static analyzer could catch this? It would probably be super noisy.

I assume the best way to find these today is to look at the allocations in a trace?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the best way to find these today is to look at the allocations in a trace?

They certainly jump out at you there. "What is <>__DisplayClass_52 and why is it being allocated 18,000 times?"

In other projects, every now and then I'll also load up all the relevant assemblies in ILSpy, search for DisplayClass, and just flip through all of them to see whether they make sense.

Of course, the real solution is to avoid the patterns that lead to this in the first place, e.g. don't use Any(x => ...) and you won't close over anything, or use APIs that allow the TState to be passed in. And if you can avoid closing over stuff, protect it by using static on the lambda so they don't then regress.

@keegan-caruso keegan-caruso merged commit 4d1e7b0 into AzureAD:dev7x Jul 26, 2023
3 checks passed
@jennyf19 jennyf19 added this to the 7.0.0-preview2 milestone Jul 27, 2023
brentschmaltz pushed a commit that referenced this pull request Jul 28, 2023
The Any call on an exceptional path is closing over the jwtToken parameter, which means a display class gets allocated at the beginning of the method, whether that code path is taken or not.
brentschmaltz pushed a commit that referenced this pull request Sep 6, 2023
The Any call on an exceptional path is closing over the jwtToken parameter, which means a display class gets allocated at the beginning of the method, whether that code path is taken or not.
brentschmaltz pushed a commit that referenced this pull request Sep 7, 2023
The Any call on an exceptional path is closing over the jwtToken parameter, which means a display class gets allocated at the beginning of the method, whether that code path is taken or not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants