From f502c8a63c853275b9d9a9bd9b7cfbb8e5db76ad Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:29:29 +0100 Subject: [PATCH] Require upload/update permission when upserting an existing asset --- .../Assets/DomainObject/AssetDomainObject.cs | 12 +++- .../DomainObject/Guards/SecurityExtensions.cs | 32 +++++++++++ .../DomainObject/AssetDomainObjectTests.cs | 57 ++++++++++++++++++- 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/Guards/SecurityExtensions.cs diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/AssetDomainObject.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/AssetDomainObject.cs index 41e0d3493..1d67b7dd6 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/AssetDomainObject.cs +++ b/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); } diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/Guards/SecurityExtensions.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/DomainObject/Guards/SecurityExtensions.cs new file mode 100644 index 000000000..16a7d3e8a --- /dev/null +++ b/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")); + } + } +} diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/DomainObject/AssetDomainObjectTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/DomainObject/AssetDomainObjectTests.cs index e0cc27f50..6793e4f31 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Assets/DomainObject/AssetDomainObjectTests.cs +++ b/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 [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 .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(() => PublishAsync(sut, command)); + + A.CallTo(() => scriptEngine.ExecuteAsync(A._, "", 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(() => PublishAsync(sut, command)); + + A.CallTo(() => scriptEngine.ExecuteAsync(A._, "", 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 .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" });