diff --git a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentQueryService.cs b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentQueryService.cs index cde787702..179b5d946 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentQueryService.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Contents/Queries/ContentQueryService.cs @@ -90,6 +90,12 @@ public sealed class ContentQueryService( return null; } + // The API only checks for read.own permission, so the content must have been created by the user. + if (!HasPermission(context, schema, PermissionIds.AppContentsRead) && !IsCreatedByUser(context, content)) + { + return null; + } + return await TransformAsync(context, content, ct); } } @@ -161,6 +167,14 @@ public sealed class ContentQueryService( } var contents = await QueryCoreAsync(context, parsedQuery, schemas, ct); + + // The API only checks for read.own permission, so we have to remove the contents of other users. + var ownSchemaIds = schemas.Where(x => !HasPermission(context, x, PermissionIds.AppContentsRead)).Select(x => x.Id).ToHashSet(); + if (ownSchemaIds.Count > 0) + { + contents = FilterOwnContents(context, contents, ownSchemaIds); + } + if (parsedQuery.Ids is { Count: > 0 }) { contents = contents.Sorted(x => x.Id, parsedQuery.Ids); @@ -286,6 +300,23 @@ public sealed class ContentQueryService( } } + private static IResultList FilterOwnContents(Context context, IResultList contents, HashSet ownSchemaIds) + { + var filtered = contents.Where(x => !ownSchemaIds.Contains(x.SchemaId.Id) || IsCreatedByUser(context, x)).ToList(); + + // A negative total means that the total has not been calculated. + var total = contents.Total < 0 ? contents.Total : contents.Total - (contents.Count - filtered.Count); + + return ResultList.Create(total, filtered); + } + + private static bool IsCreatedByUser(Context context, Content content) + { + var actor = context.UserPrincipal.Token(); + + return actor != null && Equals(content.CreatedBy, actor); + } + private static bool IsAccessible(Schema schema) { return schema.IsPublished; diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentQueryServiceTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentQueryServiceTests.cs index faf6ec79b..90171a243 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentQueryServiceTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/Queries/ContentQueryServiceTests.cs @@ -5,6 +5,7 @@ // All rights reserved. Licensed under the MIT license. // ========================================================================== +using System.Security.Claims; using Microsoft.Extensions.Options; using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.Schemas; @@ -157,6 +158,69 @@ public class ContentQueryServiceTests : GivenContext AssertContent(content, actual); } + [Fact] + public async Task Should_return_content_by_id_if_user_has_only_own_permission_and_created_content() + { + var requestContext = SetupOwnContext(); + + var content = CreateContent() with { CreatedBy = requestContext.UserPrincipal.Token()! }; + A.CallTo(() => contentRepository.FindContentAsync(App, Schema, content.Id, A>._, A._, A._)) + .Returns(content); + + var actual = await sut.FindAsync(requestContext, SchemaId.Name, content.Id, ct: CancellationToken); + + AssertContent(content, actual); + } + + [Fact] + public async Task Should_not_return_content_by_id_if_user_has_only_own_permission_and_content_from_other_user() + { + var requestContext = SetupOwnContext(); + + var content = CreateContent() with { CreatedBy = RefToken.User("other") }; + A.CallTo(() => contentRepository.FindContentAsync(App, Schema, content.Id, A>._, A._, A._)) + .Returns(content); + + var actual = await sut.FindAsync(requestContext, SchemaId.Name, content.Id, ct: CancellationToken); + + Assert.Null(actual); + } + + [Fact] + public async Task Should_not_return_content_by_id_and_version_if_user_has_only_own_permission_and_content_from_other_user() + { + var requestContext = SetupOwnContext(); + + var content = CreateContent() with { CreatedBy = RefToken.User("other") }; + A.CallTo(() => contentVersionLoader.GetAsync(AppId.Id, content.Id, 13, A._)) + .Returns(content); + + var actual = await sut.FindAsync(requestContext, SchemaId.Name, content.Id, 13, CancellationToken); + + Assert.Null(actual); + } + + [Fact] + public async Task Should_only_query_own_contents_by_ids_if_user_has_only_own_permission() + { + var requestContext = SetupOwnContext(); + + var content1 = CreateContent() with { CreatedBy = requestContext.UserPrincipal.Token()! }; + var content2 = CreateContent() with { CreatedBy = RefToken.User("other") }; + + var q = Q.Empty.WithIds([content1.Id, content2.Id]); + + A.CallTo(() => contentRepository.QueryAsync(App, + A>.That.Matches(x => x.Count == 1), q, SearchScope.Published, + A._)) + .Returns(ResultList.CreateFrom(2, content1, content2)); + + var actual = await sut.QueryAsync(requestContext, q, CancellationToken); + + Assert.Equal(1, actual.Total); + AssertContent(content1, Assert.Single(actual)); + } + [Fact] public async Task Should_throw_exception_if_user_has_no_permission_to_query_content() { @@ -322,6 +386,18 @@ public class ContentQueryServiceTests : GivenContext return CreateContext(isFrontend == 1, permissions.ToArray()).Clone(b => b.WithUnpublished(isUnpublished == 1)); } + private Context SetupOwnContext() + { + var permission = PermissionIds.ForApp(PermissionIds.AppContentsReadOwn, AppId.Name, SchemaId.Name).Id; + + var principal = Mocks.CreateUser(false, null, permission); + + // The actor is needed to compare it with the creator of the content. + principal.Identities.First().AddClaim(new Claim(OpenIdClaims.ClientId, "my-client")); + + return new Context(principal, App); + } + private static void AssertContent(EnrichedContent source, EnrichedContent? actual) { Assert.NotNull(actual);