Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,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")
Expand All @@ -105,13 +106,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -64,6 +67,14 @@ public class MetadataBitstreamRestRepository extends DSpaceRestRepository<Metada
@Autowired
ConfigurationService configurationService;

@Autowired
AuthorizeService authorizeService;

/**
* List the bitstreams of the item identified by the handle, with their preview content.
* The caller needs READ on that item (401 for anonymous, 403 otherwise); the check runs before any preview
* content is generated or stored.
*/
@SearchRestMethod(name = "byHandle")
public Page<MetadataBitstreamWrapperRest> findByHandle(@Parameter(value = "handle", required = true) String handle,
@Parameter(value = "fileGrpType") String fileGrpType,
Expand Down Expand Up @@ -93,6 +104,12 @@ public Page<MetadataBitstreamWrapperRest> 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<String> fileGrpTypes = (fileGrpType == null ? List.of() : Arrays.asList(fileGrpType.split(",")));
List<Bundle> bundles = findEnabledBundles(fileGrpTypes, item);
for (Bundle bundle : bundles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -49,6 +51,8 @@ public class MetadataBitstreamControllerIT extends AbstractControllerIntegration

@Autowired
AuthorizeService authorizeService;
@Autowired
GroupService groupService;


@Override
Expand Down Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,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)
Expand Down
Loading