diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java index 4b5985df7716..86374013bfb2 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/MetadataBitstreamController.java @@ -81,6 +81,7 @@ public class MetadataBitstreamController { /** * Download all Item's bitstreams as single ZIP file. + * The {@code handleId} must identify the item {@code uuid}; otherwise the request is refused with 422. */ @PreAuthorize("hasPermission(#uuid, 'ITEM', 'READ')") @RequestMapping( method = {RequestMethod.GET, RequestMethod.HEAD}, value = "allzip") @@ -106,13 +107,12 @@ public void downloadFileZip(@PathVariable UUID uuid, @RequestParam("handleId") S throw new RuntimeException("Cannot resolve handle: " + handleId); } - if (Objects.isNull(dso)) { - log.error("DSO is null"); - throw new UnprocessableEntityException("Retrieved DSO is null, handle: " + handleId); - } - - if (!(dso instanceof Item)) { - log.info("DSO is not instance of Item"); + // @PreAuthorize checks READ on {uuid} only, so the zip must be built from that very item. A handle of + // another object would otherwise stream its files and put its title into Content-Disposition. + // An unknown handle gets the same answer, so the response does not tell whether a handle exists. + if (!(dso instanceof Item) || !Objects.equals(dso.getID(), uuid)) { + log.error("Handle {} does not identify the item {}", handleId, uuid); + throw new UnprocessableEntityException("Handle " + handleId + " does not identify the item " + uuid); } Item item = (Item) dso; diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java index 3b21f7926fdc..752e45403ea3 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/MetadataBitstreamRestRepository.java @@ -19,10 +19,12 @@ import org.dspace.app.rest.SearchRestMethod; import org.dspace.app.rest.converter.MetadataBitstreamWrapperConverter; import org.dspace.app.rest.exception.DSpaceBadRequestException; +import org.dspace.app.rest.exception.RESTAuthorizationException; import org.dspace.app.rest.exception.UnprocessableEntityException; import org.dspace.app.rest.model.MetadataBitstreamWrapperRest; import org.dspace.app.rest.model.wrapper.MetadataBitstreamWrapper; import org.dspace.authorize.AuthorizeException; +import org.dspace.authorize.service.AuthorizeService; import org.dspace.content.Bitstream; import org.dspace.content.Bundle; import org.dspace.content.DSpaceObject; @@ -31,6 +33,7 @@ import org.dspace.content.Thumbnail; import org.dspace.content.service.ItemService; import org.dspace.content.service.PreviewContentService; +import org.dspace.core.Constants; import org.dspace.core.Context; import org.dspace.handle.service.HandleService; import org.dspace.services.ConfigurationService; @@ -64,6 +67,14 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository findByHandle(@Parameter(value = "handle", required = true) String handle, @Parameter(value = "fileGrpType") String fileGrpType, @@ -93,6 +104,12 @@ public Page findByHandle(@Parameter(value = "handl } Item item = (Item) dso; + // The handle is resolved here rather than by a uuid path variable, so no @PreAuthorize covers this item. + try { + authorizeService.authorizeAction(context, item, Constants.READ); + } catch (AuthorizeException e) { + throw new RESTAuthorizationException(e); + } List fileGrpTypes = (fileGrpType == null ? List.of() : Arrays.asList(fileGrpType.split(","))); List bundles = findEnabledBundles(fileGrpTypes, item); for (Bundle bundle : bundles) { diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java index 3dde5b78931d..7c9eff720663 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamControllerIT.java @@ -33,6 +33,8 @@ import org.dspace.content.Bitstream; import org.dspace.content.Collection; import org.dspace.content.Item; +import org.dspace.eperson.Group; +import org.dspace.eperson.service.GroupService; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -49,6 +51,8 @@ public class MetadataBitstreamControllerIT extends AbstractControllerIntegration @Autowired AuthorizeService authorizeService; + @Autowired + GroupService groupService; @Override @@ -163,4 +167,57 @@ public void downloadAllZipWithNonAsciiItemName() throws Exception { + " filename*=UTF-8''P%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD" + "%20k%C5%AF%C5%88.zip")); } + + /** + * READ is checked on {uuid}, so the zip must not be built from an item identified by another handle: + * neither its files nor its title in Content-Disposition may reach the caller. + */ + @Test + public void downloadAllZipRefusesHandleOfAnotherItem() throws Exception { + context.turnOffAuthorisationSystem(); + Item privateItem = ItemBuilder.createItem(context, col) + .withTitle("Private item title") + .withAuthor(AUTHOR) + .withReaderGroup(groupService.findByName(context, Group.ADMIN)) + .build(); + try (InputStream is = IOUtils.toInputStream("PrivateContent", CharEncoding.UTF_8)) { + BitstreamBuilder.createBitstream(context, privateItem, is) + .withName("private.txt") + .withMimeType("text/plain") + .build(); + } + context.restoreAuthSystemState(); + + // uuid of a readable item + handle of a private one + getClient().perform(get(METADATABITSTREAM_ENDPOINT + "/" + publicItem.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, privateItem.getHandle())) + .andExpect(status().isUnprocessableEntity()) + .andExpect(header().doesNotExist("Content-Disposition")); + // an unknown handle gets the same answer + getClient().perform(get(METADATABITSTREAM_ENDPOINT + "/" + publicItem.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, "123456789/does-not-exist")) + .andExpect(status().isUnprocessableEntity()) + .andExpect(header().doesNotExist("Content-Disposition")); + // the reverse is refused by the READ check on {uuid} + getClient().perform(get(METADATABITSTREAM_ENDPOINT + "/" + privateItem.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, publicItem.getHandle())) + .andExpect(status().isUnauthorized()); + // a mismatch is refused for an admin too + String token = getAuthToken(admin.getEmail(), password); + getClient(token).perform(get(METADATABITSTREAM_ENDPOINT + "/" + publicItem.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, privateItem.getHandle())) + .andExpect(status().isUnprocessableEntity()); + + // the matching pair still works, for an anonymous caller on the public item and for an admin on the + // private one + getClient().perform(get(METADATABITSTREAM_ENDPOINT + "/" + publicItem.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, publicItem.getHandle())) + .andExpect(status().isOk()); + getClient(token).perform(get(METADATABITSTREAM_ENDPOINT + "/" + privateItem.getID() + + "/" + ALL_ZIP_PATH).param(HANDLE_PARAM, privateItem.getHandle())) + .andExpect(status().isOk()) + .andExpect(header().string("Content-Disposition", + "attachment; filename=\"Private item title.zip\";" + + " filename*=UTF-8''Private%20item%20title.zip")); + } } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java index fb1eb9b7b4f7..96469ca4567e 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/MetadataBitstreamRestRepositoryIT.java @@ -258,6 +258,47 @@ public void previewingIsDisabledByCfgForHtml() throws Exception { configurationService.setProperty("file.preview.enabled", canPreview); } + /** + * The handle is resolved without @PreAuthorize, so READ on the item has to be checked explicitly, and before + * any preview content is generated for its files. + */ + @Test + public void findByHandleRequiresReadOnItem() throws Exception { + context.turnOffAuthorisationSystem(); + Collection col = CollectionBuilder.createCollection(context, parentCommunity).withName("Collection2").build(); + Item privateItem = ItemBuilder.createItem(context, col) + .withAuthor(AUTHOR) + .withReaderGroup(groupService.findByName(context, Group.ADMIN)) + .build(); + Bitstream privateBitstream; + try (InputStream is = getClass().getResourceAsStream("assetstore/logos.tgz")) { + privateBitstream = BitstreamBuilder. + createBitstream(context, privateItem, is) + .withName("Bitstream") + .withMimeType("application/x-gtar") + .build(); + } + context.restoreAuthSystemState(); + + getClient().perform(get(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT) + .param("handle", privateItem.getHandle()) + .param("fileGrpType", FILE_GRP_TYPE)) + .andExpect(status().isUnauthorized()); + getClient(getAuthToken(ePerson.getEmail(), PASSWORD)).perform(get(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT) + .param("handle", privateItem.getHandle()) + .param("fileGrpType", FILE_GRP_TYPE)) + .andExpect(status().isForbidden()); + assertFalse("Expects no preview content created for a refused caller.", + previewContentService.hasPreview(context, privateBitstream)); + + // an admin can still list the files + getClient(getAuthToken(admin.getEmail(), password)).perform(get(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT) + .param("handle", privateItem.getHandle()) + .param("fileGrpType", FILE_GRP_TYPE)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$._embedded.metadatabitstreams", hasSize(1))); + } + @Test public void findByHandleEmptyFileGrpType() throws Exception { getClient().perform(get(METADATABITSTREAM_SEARCH_BY_HANDLE_ENDPOINT)