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

Graceful handling of unauthorized requests to the Management API #16836

Merged
merged 1 commit into from
Jul 31, 2024
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 @@ -17,8 +17,8 @@ public AllowedApplicationHandler(IAuthorizationHelper authorizationHelper)

protected override Task<bool> IsAuthorized(AuthorizationHandlerContext context, AllowedApplicationRequirement requirement)
{
IUser user = _authorizationHelper.GetUmbracoUser(context.User);
var allowed = user.AllowedSections.ContainsAny(requirement.Applications);
var allowed = _authorizationHelper.TryGetUmbracoUser(context.User, out IUser? user)
&& user.AllowedSections.ContainsAny(requirement.Applications);
return Task.FromResult(allowed);
}
}
17 changes: 9 additions & 8 deletions src/Umbraco.Core/Security/Authorization/AuthorizationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Claims;
using System.Security.Principal;
using Umbraco.Cms.Core.Models.Membership;
Expand All @@ -20,8 +21,14 @@ public AuthorizationHelper(IUserService userService)

/// <inheritdoc/>
public IUser GetUmbracoUser(IPrincipal currentUser)
=> TryGetUmbracoUser(currentUser, out IUser? user)
? user
: throw new InvalidOperationException($"Could not obtain an {nameof(IUser)} instance from {nameof(IPrincipal)}");

/// <inheritdoc/>
public bool TryGetUmbracoUser(IPrincipal currentUser, [NotNullWhen(true)] out IUser? user)
{
IUser? user = null;
user = null;
ClaimsIdentity? umbIdentity = currentUser.GetUmbracoIdentity();
Guid? currentUserKey = umbIdentity?.GetUserKey();

Expand All @@ -38,12 +45,6 @@ public IUser GetUmbracoUser(IPrincipal currentUser)
user = _userService.GetAsync(currentUserKey.Value).GetAwaiter().GetResult();
}

if (user is null)
{
throw new InvalidOperationException(
$"Could not obtain an {nameof(IUser)} instance from {nameof(IPrincipal)}");
}

return user;
return user is not null;
}
}
11 changes: 10 additions & 1 deletion src/Umbraco.Core/Security/Authorization/IAuthorizationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Principal;
using Umbraco.Cms.Core.Models.Membership;

Expand All @@ -9,11 +10,19 @@ namespace Umbraco.Cms.Core.Security.Authorization;
public interface IAuthorizationHelper
{
/// <summary>
/// Converts an <see cref="IUser" /> into <see cref="IPrincipal" />.
/// Converts an <see cref="IPrincipal" /> into an <see cref="IUser" />.
/// </summary>
/// <param name="currentUser">The current user's principal.</param>
/// <returns>
/// <see cref="IUser" />.
/// </returns>
IUser GetUmbracoUser(IPrincipal currentUser);

/// <summary>
/// Attempts to convert an <see cref="IPrincipal" /> into an <see cref="IUser" />.
/// </summary>
/// <param name="currentUser">The current user's principal.</param>
/// <param name="user">The resulting <see cref="IUser" />, if the conversion is successful.</param>
/// <returns>True if the conversion is successful, false otherwise</returns>
bool TryGetUmbracoUser(IPrincipal currentUser, [NotNullWhen(true)] out IUser? user);
Copy link
Member

Choose a reason for hiding this comment

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

How is this not breaking. ?

Do we mis something to check for breaking changes?

I think we should make an interface implementation just catching the exception from GetUmbracoUser

@kjac @Zeegaan

Copy link
Member

Choose a reason for hiding this comment

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

You are right, seems some package validation is missing if it's not catching these things automatically 😢

Copy link
Member

Choose a reason for hiding this comment

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

Fix here: #16899

}
Loading