fix author endpoints leaking authors across library boundaries (#3077) (#3096)

This commit is contained in:
ACX
2026-02-27 18:14:37 -07:00
committed by GitHub
parent dc755e9539
commit 251c2c60a8
3 changed files with 48 additions and 2 deletions
@@ -8,6 +8,7 @@ import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@Repository
public interface AuthorRepository extends JpaRepository<AuthorEntity, Long> {
@@ -23,4 +24,10 @@ public interface AuthorRepository extends JpaRepository<AuthorEntity, Long> {
@Query("SELECT a, COUNT(bm) FROM AuthorEntity a LEFT JOIN a.bookMetadataEntityList bm GROUP BY a ORDER BY a.name")
List<Object[]> findAllWithBookCount();
@Query("SELECT a, COUNT(DISTINCT bm) FROM AuthorEntity a LEFT JOIN a.bookMetadataEntityList bm JOIN bm.book b WHERE b.library.id IN :libraryIds GROUP BY a ORDER BY a.name")
List<Object[]> findAllWithBookCountByLibraryIds(@Param("libraryIds") Set<Long> libraryIds);
@Query("SELECT COUNT(b) > 0 FROM AuthorEntity a JOIN a.bookMetadataEntityList bm JOIN bm.book b WHERE a.id = :authorId AND b.library.id IN :libraryIds")
boolean existsByIdAndLibraryIds(@Param("authorId") Long authorId, @Param("libraryIds") Set<Long> libraryIds);
}
@@ -3,10 +3,13 @@ package org.booklore.service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.booklore.exception.ApiError;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.model.dto.AuthorDetails;
import org.booklore.model.dto.AuthorSearchResult;
import org.booklore.model.dto.AuthorSummary;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.CoverImage;
import org.booklore.model.dto.Library;
import org.booklore.model.dto.request.AuthorMatchRequest;
import org.booklore.model.dto.request.AuthorUpdateRequest;
import org.booklore.model.entity.AuthorEntity;
@@ -32,6 +35,8 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
@Service
@@ -43,9 +48,19 @@ public class AuthorMetadataService {
private final AuditService auditService;
private final FileService fileService;
private final DuckDuckGoCoverService duckDuckGoCoverService;
private final AuthenticationService authenticationService;
public List<AuthorSummary> getAllAuthors() {
List<Object[]> results = authorRepository.findAllWithBookCount();
BookLoreUser user = authenticationService.getAuthenticatedUser();
List<Object[]> results;
if (user.getPermissions().isAdmin()) {
results = authorRepository.findAllWithBookCount();
} else {
Set<Long> libraryIds = user.getAssignedLibraries().stream()
.map(Library::getId)
.collect(Collectors.toSet());
results = authorRepository.findAllWithBookCountByLibraryIds(libraryIds);
}
List<AuthorSummary> summaries = new ArrayList<>();
for (Object[] row : results) {
AuthorEntity author = (AuthorEntity) row[0];
@@ -244,12 +259,14 @@ public class AuthorMetadataService {
public AuthorDetails getAuthorByName(String name) {
AuthorEntity author = authorRepository.findByNameIgnoreCase(name)
.orElseThrow(() -> ApiError.AUTHOR_NOT_FOUND.createException(name));
verifyAuthorAccess(author.getId());
return toAuthorDetails(author);
}
public AuthorDetails getAuthorDetails(Long authorId) {
AuthorEntity author = authorRepository.findById(authorId)
.orElseThrow(() -> ApiError.AUTHOR_NOT_FOUND.createException(authorId));
verifyAuthorAccess(authorId);
return toAuthorDetails(author);
}
@@ -277,6 +294,19 @@ public class AuthorMetadataService {
return null;
}
private void verifyAuthorAccess(Long authorId) {
BookLoreUser user = authenticationService.getAuthenticatedUser();
if (user.getPermissions().isAdmin()) {
return;
}
Set<Long> libraryIds = user.getAssignedLibraries().stream()
.map(Library::getId)
.collect(Collectors.toSet());
if (libraryIds.isEmpty() || !authorRepository.existsByIdAndLibraryIds(authorId, libraryIds)) {
throw ApiError.AUTHOR_NOT_FOUND.createException(authorId);
}
}
private void applyMetadataResult(AuthorEntity author, AuthorSearchResult result) {
if (!author.isDescriptionLocked()) {
author.setDescription(result.getDescription());
@@ -1,8 +1,10 @@
package org.booklore.service;
import org.booklore.config.security.service.AuthenticationService;
import org.booklore.exception.APIException;
import org.booklore.model.dto.AuthorDetails;
import org.booklore.model.dto.AuthorSearchResult;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.request.AuthorMatchRequest;
import org.booklore.model.entity.AuthorEntity;
import org.booklore.model.enums.AuditAction;
@@ -28,6 +30,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.lenient;
@ExtendWith(MockitoExtension.class)
class AuthorMetadataServiceTest {
@@ -37,6 +40,7 @@ class AuthorMetadataServiceTest {
@Mock private AuditService auditService;
@Mock private FileService fileService;
@Mock private DuckDuckGoCoverService duckDuckGoCoverService;
@Mock private AuthenticationService authenticationService;
private AuthorMetadataService service;
@@ -45,7 +49,12 @@ class AuthorMetadataServiceTest {
Map<AuthorMetadataSource, AuthorParser> authorParserMap = Map.of(
AuthorMetadataSource.AUDNEXUS, authorParser
);
service = new AuthorMetadataService(authorRepository, authorParserMap, auditService, fileService, duckDuckGoCoverService);
service = new AuthorMetadataService(authorRepository, authorParserMap, auditService, fileService, duckDuckGoCoverService, authenticationService);
BookLoreUser.UserPermissions adminPermissions = new BookLoreUser.UserPermissions();
adminPermissions.setAdmin(true);
BookLoreUser adminUser = BookLoreUser.builder().id(1L).permissions(adminPermissions).build();
lenient().when(authenticationService.getAuthenticatedUser()).thenReturn(adminUser);
}
@Test