Browse Source

Require upload/update permission when upserting an existing asset

pull/1368/head
github-actions[bot] 3 weeks ago
committed by GitHub
parent
commit
f502c8a63c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 12
      backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/AssetDomainObject.cs
  2. 32
      backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/Guards/SecurityExtensions.cs
  3. 57
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/DomainObject/AssetDomainObjectTests.cs

12
backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/AssetDomainObject.cs

@ -16,6 +16,7 @@ using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Reflection;
using Squidex.Infrastructure.States;
using Squidex.Shared;
#pragma warning disable MA0022 // Return Task.FromResult instead of returning null
@ -68,8 +69,12 @@ public partial class AssetDomainObject(
{
var operation = await AssetOperation.CreateAsync(serviceProvider, c, () => Snapshot);
if (Version > EtagVersion.Empty && !IsDeleted(Snapshot))
var isUpdate = Version > EtagVersion.Empty && !IsDeleted(Snapshot);
if (isUpdate)
{
// The API only requires the create permission, which must not allow to replace existing assets.
operation.MustHavePermission(PermissionIds.AppAssetsUpload);
await UpdateCore(c.AsUpdate(), operation, ct);
}
else
@ -79,6 +84,11 @@ public partial class AssetDomainObject(
if (c.ParentId != null && c.ParentId != Snapshot.ParentId)
{
if (isUpdate)
{
operation.MustHavePermission(PermissionIds.AppAssetsUpdate);
}
await MoveCore(c.AsMove(c.ParentId.Value), operation, ct);
}

32
backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/Guards/SecurityExtensions.cs

@ -0,0 +1,32 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Squidex.Infrastructure;
using Squidex.Infrastructure.Translations;
using Squidex.Shared;
using Squidex.Shared.Identity;
namespace Squidex.Domain.Apps.Entities.Assets.DomainObject.Guards;
public static class SecurityExtensions
{
public static void MustHavePermission(this AssetOperation operation, string permissionId)
{
// Commands without a user are created by the system itself and are not restricted.
if (operation.User == null)
{
return;
}
var permissions = operation.User.Claims.Permissions();
if (!permissions.Allows(permissionId, operation.App.Name))
{
throw new DomainForbiddenException(T.Get("common.errorNoPermission"));
}
}
}

57
backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/DomainObject/AssetDomainObjectTests.cs

@ -5,6 +5,7 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Security.Claims;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Squidex.Assets;
@ -17,6 +18,7 @@ using Squidex.Domain.Apps.Entities.Contents.Repositories;
using Squidex.Domain.Apps.Entities.TestHelpers;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands;
using Squidex.Shared;
namespace Squidex.Domain.Apps.Entities.Assets.DomainObject;
@ -133,7 +135,7 @@ public class AssetDomainObjectTests : HandlerTestBase<Asset>
[Fact]
public async Task Upsert_should_create_events_and_update_file_state_if_found()
{
var command = new UpsertAsset { File = file, FileHash = "NewHash" };
var command = new UpsertAsset { File = file, FileHash = "NewHash", User = CreateUser(PermissionIds.AppAssetsUpload) };
await ExecuteCreateAsync();
@ -145,6 +147,54 @@ public class AssetDomainObjectTests : HandlerTestBase<Asset>
.MustHaveHappened();
}
[Fact]
public async Task Upsert_should_create_asset_with_create_permission_only_if_not_found()
{
var command = new UpsertAsset { File = file, FileHash = "NewHash", User = CreateUser(PermissionIds.AppAssetsCreate) };
await PublishAsync(sut, command);
Assert.Equal(0, sut.Snapshot.Version);
}
[Fact]
public async Task Upsert_should_throw_exception_if_found_and_user_has_no_upload_permission()
{
var command = new UpsertAsset { File = file, FileHash = "NewHash", User = CreateUser(PermissionIds.AppAssetsCreate) };
await ExecuteCreateAsync();
await Assert.ThrowsAsync<DomainForbiddenException>(() => PublishAsync(sut, command));
A.CallTo(() => scriptEngine.ExecuteAsync(A<ScriptVars>._, "<update-script>", ScriptOptions(), CancellationToken))
.MustNotHaveHappened();
}
[Fact]
public async Task Upsert_should_throw_exception_if_found_and_moved_and_user_has_no_update_permission()
{
var command = new UpsertAsset { File = file, FileHash = "NewHash", ParentId = parentId, User = CreateUser(PermissionIds.AppAssetsUpload) };
await ExecuteCreateAsync();
await Assert.ThrowsAsync<DomainForbiddenException>(() => PublishAsync(sut, command));
A.CallTo(() => scriptEngine.ExecuteAsync(A<ScriptVars>._, "<move-script>", ScriptOptions(), CancellationToken))
.MustNotHaveHappened();
}
[Fact]
public async Task Upsert_should_update_and_move_asset_if_found_and_user_has_permissions()
{
var command = new UpsertAsset { File = file, FileHash = "NewHash", ParentId = parentId, User = CreateUser(PermissionIds.AppAssetsUpload, PermissionIds.AppAssetsUpdate) };
await ExecuteCreateAsync();
await PublishAsync(sut, command);
Assert.Equal(parentId, sut.Snapshot.ParentId);
}
[Fact]
public async Task Update_should_create_events_and_update_file_state()
{
@ -316,6 +366,11 @@ public class AssetDomainObjectTests : HandlerTestBase<Asset>
.MustHaveHappened();
}
private ClaimsPrincipal CreateUser(params string[] permissionIds)
{
return Mocks.ApiUser(null, permissionIds.Select(x => PermissionIds.ForApp(x, AppId.Name).Id).ToArray());
}
private Task ExecuteCreateAsync()
{
return PublishAsync(sut, new CreateAsset { File = file, FileHash = "123" });

Loading…
Cancel
Save