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

Add checks to protect the internal claims used by MIW. Ref: issue #2968 #3131

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

DOMZE
Copy link

@DOMZE DOMZE commented Nov 6, 2024

Add checks to protect the users to not use internal claims used by the library

Summary of the changes (Less than 80 chars)

Description

Introduce InternalClaimDetectedException in Microsoft.Identity.Web and Microsoft.Identity.Web.OWIN namespaces. This exception is thrown when internal ID Token claims (UniqueTenantIdentifier, UniqueObjectIdentifier) are detected in the user's ID Token. Updated AppBuilderExtension.cs and MicrosoftIdentityWebAppAuthenticationBuilder.cs to check for these claims and throw the exception if found. The new exception class includes a property to store the invalid claim

Fixes #2968 (in this specific format)

Introduce InternalClaimDetectedException in Microsoft.Identity.Web and Microsoft.Identity.Web.OWIN namespaces. This exception is thrown when internal ID Token claims (UniqueTenantIdentifier, UniqueObjectIdentifier) are detected in the user's ID Token. Updated AppBuilderExtension.cs and MicrosoftIdentityWebAppAuthenticationBuilder.cs to check for these claims and throw the exception if found. The new exception class includes a property to store the invalid claim
@DOMZE DOMZE requested a review from a team as a code owner November 6, 2024 17:07
@DOMZE
Copy link
Author

DOMZE commented Nov 6, 2024

@bgavrilMS / @jennyf19 / @jmprieur please review.

Thank you!

@DOMZE
Copy link
Author

DOMZE commented Nov 6, 2024

@microsoft-github-policy-service agree

Copy link

@dstamand-msft dstamand-msft left a comment

Choose a reason for hiding this comment

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

Changes were made in regards to feedback

@jennyf19 jennyf19 added this to the 3.3.2 milestone Nov 8, 2024
@bgavrilMS
Copy link
Member

LGTM, I only have minor comments and resolving @msbw2 's comments

@DOMZE
Copy link
Author

DOMZE commented Nov 12, 2024

@bgavrilMS @jmprieur @jennyf19 @pmaytak to be able to go forward, we just need your input on claims casing match (type+value/value only) and the changes are done.

Thank you!

@bgavrilMS
Copy link
Member

@bgavrilMS @jmprieur @jennyf19 @pmaytak to be able to go forward, we just need your input on claims casing match (type+value/value only) and the changes are done.

Thank you!

I am fine with case insensitive.

@jennyf19
Copy link
Collaborator

@bgavrilMS @jmprieur @jennyf19 @pmaytak to be able to go forward, we just need your input on claims casing match (type+value/value only) and the changes are done.

Thank you!

ClaimsType should never be treated as case insensitive

@pmaytak
Copy link
Contributor

pmaytak commented Nov 13, 2024

I think claim names should be case sensitive at least as per JWT spec. IdentityModel also treats them as case sensstive.

@pmaytak
Copy link
Contributor

pmaytak commented Nov 14, 2024

Read the issue, thought about this more. When we search for a claim to validate or use, then we should be case sensitive. This way we get the exact claim we're looking for. However, if our intent is to throw an exception if the user specified any variation of utid or uid, then we should use case insensitive approach to broaden the search. This way we ensure that the token only ever has utid and uid variations of claim names.

/// <param name="claimType">The claim type</param>
/// <param name="claimValue">The claim value</param>
/// <exception cref="AuthenticationException">The <see cref="ClaimsIdentity"/> contains internal claims that are used internal use by this library</exception>
private static void RejectInternalClaims(ClaimsIdentity identity, string claimType, string claimValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

@DOMZE I am not clear on the intent, can you please clarify? Why are we comparing the claim value? My understanding is that we should just call FindFirst and if it returns something, then throw an exception because that claim is user-provided.

Copy link
Author

Choose a reason for hiding this comment

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

It was suggested by @bgavrilMS that if an internal claim is found in the ID Token and the value of the claim in the ID Token matches what's in the client_info, then we shouldn't throw an exception as they match.

But happy to revisit if the team is not aligned.

Copy link
Member

Choose a reason for hiding this comment

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

@pmaytak - thanks for reviewing! That ClaimsPrincipal lifecycle is controlled by ASP.NET Core. I was thinking to avoid a situation where Id.Web injects the claims in an already modified ClaimsPrincipal and @DOMZE 's new code throws exception.

Copy link
Contributor

@pmaytak pmaytak left a comment

Choose a reason for hiding this comment

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

Not clear on the intent.

@pmaytak pmaytak removed this from the 3.3.2 milestone Nov 14, 2024
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.

[BUG] GetAuthenticationResultForUserAsync throws an exception when user is authenticated
6 participants