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

Fixed token validation bug when using a custom IDataProtectionProvider. #1860

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,19 @@ public ValueTask HandleAsync(ValidateTokenContext context)
return default;
}

// Note: ASP.NET Core Data Protection tokens always start with "CfDJ8", that corresponds
// to the base64 representation of the magic "09 F0 C9 F0" header identifying DP payloads.
if (!context.Token.StartsWith("CfDJ8", StringComparison.Ordinal))
// Note: ASP.NET Core Data Protection tokens created by the default implementation always start
// with "CfDJ8", that corresponds to the base64 representation of the "09 F0 C9 F0" value used
// by KeyRingBasedDataProtectionProvider as a Data Protection version identifier/magic header.
//
// Unless a custom provider implementation - that may use a different mechanism - has been
// registered, return immediately if the token doesn't start with the expected magic header.
//
// See https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/authenticated-encryption-details
// for more information.
if (!context.Token.StartsWith("CfDJ8", StringComparison.Ordinal) &&
string.Equals(_options.CurrentValue.DataProtectionProvider.GetType().FullName,
"Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtectionProvider",
StringComparison.Ordinal))
{
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,19 @@ public ValueTask HandleAsync(ValidateTokenContext context)
return default;
}

// Note: ASP.NET Core Data Protection tokens always start with "CfDJ8", that corresponds
// to the base64 representation of the magic "09 F0 C9 F0" header identifying DP payloads.
if (!context.Token.StartsWith("CfDJ8", StringComparison.Ordinal))
// Note: ASP.NET Core Data Protection tokens created by the default implementation always start
// with "CfDJ8", that corresponds to the base64 representation of the "09 F0 C9 F0" value used
// by KeyRingBasedDataProtectionProvider as a Data Protection version identifier/magic header.
//
// Unless a custom provider implementation - that may use a different mechanism - has been
// registered, return immediately if the token doesn't start with the expected magic header.
//
// See https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/authenticated-encryption-details
// for more information.
if (!context.Token.StartsWith("CfDJ8", StringComparison.Ordinal) &&
string.Equals(_options.CurrentValue.DataProtectionProvider.GetType().FullName,
"Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtectionProvider",
StringComparison.Ordinal))
{
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ public ValueTask HandleAsync(ValidateTokenContext context)
return default;
}

// Note: ASP.NET Core Data Protection tokens always start with "CfDJ8", that corresponds
// to the base64 representation of the magic "09 F0 C9 F0" header identifying DP payloads.
if (!context.Token.StartsWith("CfDJ8", StringComparison.Ordinal))
// Note: ASP.NET Core Data Protection tokens created by the default implementation always start
// with "CfDJ8", that corresponds to the base64 representation of the "09 F0 C9 F0" value used
// by KeyRingBasedDataProtectionProvider as a Data Protection version identifier/magic header.
//
// Unless a custom provider implementation - that may use a different mechanism - has been
// registered, return immediately if the token doesn't start with the expected magic header.
//
// See https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/implementation/authenticated-encryption-details
// for more information.
if (!context.Token.StartsWith("CfDJ8", StringComparison.Ordinal) &&
string.Equals(_options.CurrentValue.DataProtectionProvider.GetType().FullName,
"Microsoft.AspNetCore.DataProtection.KeyManagement.KeyRingBasedDataProtectionProvider",
StringComparison.Ordinal))
{
return default;
}
Expand Down