From 8e5027e1700976241ee30e0ffc9e8d8ed08d1a2a Mon Sep 17 00:00:00 2001 From: milanmajchrak <90026355+milanmajchrak@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:09:23 +0200 Subject: [PATCH 1/2] CLARIN-DSpace v9/Hide the magic handle token and make /api/core/handles admin-only Port of dtq-dev PR 1508 (commit 1a22aac076) to the v9 base. Only the javax import in the IT changed to jakarta. Co-Authored-By: Claude Fable 5.1 --- .../handle/HandleClarinServiceImpl.java | 2 +- .../org/dspace/handle/external/Handle.java | 74 ++++++++++++++++- .../app/rest/converter/HandleConverter.java | 4 +- .../ExternalHandleRestRepository.java | 5 +- .../rest/repository/HandleRestRepository.java | 8 +- .../rest/ExternalHandleRestRepositoryIT.java | 83 +++++++++++++++++++ .../app/rest/HandleRestRepositoryIT.java | 47 ++++++++++- 7 files changed, 208 insertions(+), 15 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java b/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java index 3ae43e547053..44b6232f21f7 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java @@ -483,7 +483,7 @@ public Handle findByHandleAndMagicToken(Context context, String handle, String t } org.dspace.handle.external.Handle magicHandle = new org.dspace.handle.external.Handle(h.getHandle(), h.getUrl()); - if (magicHandle.token.equals(token)) { + if (StringUtils.equals(magicHandle.getToken(), token)) { return h; } else { return null; diff --git a/dspace-api/src/main/java/org/dspace/handle/external/Handle.java b/dspace-api/src/main/java/org/dspace/handle/external/Handle.java index fbd67c8f5cc2..4e250545281a 100644 --- a/dspace-api/src/main/java/org/dspace/handle/external/Handle.java +++ b/dspace-api/src/main/java/org/dspace/handle/external/Handle.java @@ -14,6 +14,8 @@ import java.util.Objects; import java.util.UUID; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.dspace.handle.HandlePlugin; /** @@ -26,6 +28,11 @@ */ public class Handle { + private static final Logger log = LogManager.getLogger(Handle.class); + + private static final int MAGIC_URL_PARTS = 10; + private static final int TOKEN_INDEX = 8; + private String handle; public String url; public String title; @@ -36,7 +43,7 @@ public class Handle { public String datasetName; public String datasetVersion; public String query; - public String token; + private String token; public Handle() { @@ -65,7 +72,7 @@ public Handle(String handle, String url, String title, String repository, String public Handle(String handle, String magicURL) { this.handle = handle; //similar to HandlePlugin - String[] splits = magicURL.split(MAGIC_BEAN,10); + String[] splits = magicURL.split(MAGIC_BEAN, MAGIC_URL_PARTS); this.url = splits[splits.length - 1]; this.title = splits[1]; this.repository = splits[2]; @@ -80,12 +87,55 @@ public Handle(String handle, String magicURL) { if (isNotBlank(splits[7])) { this.query = splits[7]; } - if (isNotBlank(splits[8])) { - this.token = splits[8]; + if (isNotBlank(splits[TOKEN_INDEX])) { + this.token = splits[TOKEN_INDEX]; } this.subprefix = handle.split("/",2)[1].split("-",2)[0]; } + /** + * Return the url with an empty token, so a magic URL can be shown without the secret that allows updating it. + * @param url handle url, magic or not + * @return the url with the token part left empty, or the url as it is when it is not a magic URL + */ + public static String hideToken(String url) { + String[] splits = splitMagicUrl(url); + if (Objects.isNull(splits)) { + return url; + } + splits[TOKEN_INDEX] = ""; + return String.join(MAGIC_BEAN, splits); + } + + /** + * Put the stored token back into a magic URL whose token is empty, e.g. one loaded with a hidden token. + * @param url new handle url + * @param storedUrl handle url currently stored in the database + * @return the url with the stored token, or the url as it is when it has its own token or is not a magic URL + */ + public static String restoreToken(String url, String storedUrl) { + String[] splits = splitMagicUrl(url); + String[] storedSplits = splitMagicUrl(storedUrl); + if (Objects.isNull(splits) || Objects.isNull(storedSplits) || isNotBlank(splits[TOKEN_INDEX])) { + return url; + } + splits[TOKEN_INDEX] = storedSplits[TOKEN_INDEX]; + return String.join(MAGIC_BEAN, splits); + } + + private static String[] splitMagicUrl(String url) { + if (isBlank(url) || !url.contains(MAGIC_BEAN)) { + return null; + } + String[] splits = url.split(MAGIC_BEAN, MAGIC_URL_PARTS); + if (splits.length != MAGIC_URL_PARTS) { + // the url is not logged, it may contain the token + log.warn("Magic URL has {} parts instead of {}, it is used unchanged.", splits.length, MAGIC_URL_PARTS); + return null; + } + return splits; + } + /** * Generate new token and combine the properties into the url with `@magicLindat` string * @return url with the `@magicLindat` string @@ -130,4 +180,20 @@ public String getHandle() { public void setHandle(String handle) { this.handle = handle.replace(HandlePlugin.getCanonicalHandlePrefix(),""); } + + /** + * The token is needed to update the handle. + * @return the update token, or null when there is none + */ + public String getToken() { + return token; + } + + /** + * Set the token, e.g. from the update request. + * @param token + */ + public void setToken(String token) { + this.token = token; + } } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/HandleConverter.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/HandleConverter.java index ceb6409429a1..d2ca5266aa83 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/HandleConverter.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/HandleConverter.java @@ -7,6 +7,8 @@ */ package org.dspace.app.rest.converter; +import static org.dspace.handle.external.Handle.hideToken; + import java.util.Objects; import org.dspace.app.rest.model.HandleRest; @@ -36,7 +38,7 @@ public HandleRest convert(Handle modelObject, Projection projection) { handleRest.setId(modelObject.getID()); handleRest.setHandle(modelObject.getHandle()); handleRest.setResourceTypeID(modelObject.getResourceTypeId()); - handleRest.setUrl(modelObject.getUrl()); + handleRest.setUrl(hideToken(modelObject.getUrl())); if (Objects.nonNull(modelObject.getDSpaceObject())) { handleRest.setResourceId(modelObject.getDSpaceObject().getID()); } else { diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ExternalHandleRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ExternalHandleRestRepository.java index 62c4202fcf54..16641eecd270 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ExternalHandleRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/ExternalHandleRestRepository.java @@ -143,7 +143,7 @@ public ResponseEntity updateHandle(@RequestBody Handle updatedHandle, Ht } if (validHandleUrl(updatedHandle.url) - && isNotBlank(updatedHandle.getHandle()) && isNotBlank(updatedHandle.token)) { + && isNotBlank(updatedHandle.getHandle()) && isNotBlank(updatedHandle.getToken())) { try { // remove canonical prefix from handle string because the handle is stored in the DB without // the canonical prefix @@ -153,7 +153,8 @@ && isNotBlank(updatedHandle.getHandle()) && isNotBlank(updatedHandle.token)) { // load Handle object from the DB org.dspace.handle.Handle handleEntity = - this.handleClarinService.findByHandleAndMagicToken(context, oldHandleStr, updatedHandle.token); + this.handleClarinService.findByHandleAndMagicToken(context, oldHandleStr, + updatedHandle.getToken()); if (Objects.isNull(handleEntity)) { return new ResponseEntity<>("Cannot find the handle in the database.", diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/HandleRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/HandleRestRepository.java index 73197cd604c3..866ab40451d4 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/HandleRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/HandleRestRepository.java @@ -7,6 +7,8 @@ */ package org.dspace.app.rest.repository; +import static org.dspace.handle.external.Handle.restoreToken; + import java.io.IOException; import java.lang.invoke.WrongMethodTypeException; import java.sql.SQLException; @@ -73,7 +75,7 @@ public class HandleRestRepository extends DSpaceRestRepository findAll(Context context, Pageable pageable) { try { // Get sorting request into the string variable @@ -380,7 +382,7 @@ private void fetchHandleAndUpdate(Context context, Operation operation, Integer // Update handle based on obtained values from json nodes this.updateHandle(context, handleObject, jsonNodeHandle.asText(), - handleObject.getDSpaceObject(), jsonNodeUrl.asText(), + handleObject.getDSpaceObject(), restoreToken(jsonNodeUrl.asText(), handleObject.getUrl()), jsonNodeArchive.asBoolean()); } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java index 8c8a8775b63a..28e40afa098c 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java @@ -8,10 +8,14 @@ package org.dspace.app.rest; import static org.hamcrest.Matchers.anyOf; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; @@ -25,6 +29,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.dspace.app.rest.matcher.ExternalHandleMatcher; +import org.dspace.app.rest.model.patch.ReplaceOperation; import org.dspace.app.rest.repository.RandomStringGenerator; import org.dspace.app.rest.test.AbstractControllerIntegrationTest; import org.dspace.authorize.AuthorizeException; @@ -68,6 +73,7 @@ public class ExternalHandleRestRepositoryIT extends AbstractControllerIntegratio private final String urlBlacklistConfig = "shortener.post.url.blacklist.regexps"; private final String hostBlacklistConfig = "shortener.post.host.blacklist.regexps"; + private static final String TEST_URL = "https://example.org/target"; private String[] urlBlacklist; private String[] hostBlacklist; @@ -297,6 +303,63 @@ public void noTokenInGetResponse() throws Exception { Assert.assertFalse("Token should not be in the response", content.contains("token0")); } + @Test + public void coreHandlesEndpointHidesToken() throws Exception { + org.dspace.handle.Handle handle = handlesWithMagicURLs.get(0); + String hiddenUrl = handle.getUrl().replace("token0", ""); + String adminToken = getAuthToken(admin.getEmail(), password); + + getClient(adminToken).perform(get("/api/core/handles/" + handle.getID())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.url", is(hiddenUrl))); + + // the "sort" param is a filter: only handles with the shortener prefix, whatever else is in the DB + getClient(adminToken).perform(get("/api/core/handles").param("sort", "handle:" + prefix)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.page.totalElements", is(handlesWithMagicURLs.size()))) + .andExpect(jsonPath("$._embedded.handles[*].url", hasItem(hiddenUrl))) + .andExpect(content().string(not(containsString(ExternalHandleConstants.MAGIC_BEAN + "token")))); + } + + @Test + public void tokenSetByAdminIsHiddenAndWorksForUpdate() throws Exception { + org.dspace.handle.Handle handle = handlesWithMagicURLs.get(0); + patchUrlAndCheckToken(handle, handle.getUrl().replace("token0", "adminToken"), "adminToken"); + + allowAnyUrl(); + updateHandle(null, Map.of("handle", handle.getHandle(), "token", "adminToken", "url", TEST_URL)); + } + + @Test + public void adminSaveOfHiddenUrlKeepsToken() throws Exception { + org.dspace.handle.Handle handle = handlesWithMagicURLs.get(0); + // admin saves the url exactly as the REST API shows it + patchUrlAndCheckToken(handle, handle.getUrl().replace("token0", ""), "token0"); + + allowAnyUrl(); + updateHandle(null, Map.of("handle", handle.getHandle(), "token", "token0", "url", TEST_URL)); + } + + @Test + public void updateOfHandleWithoutTokenIsNotFound() throws Exception { + String urlWithoutToken = handlesWithMagicURLs.get(0).getUrl().replace("token0", ""); + context.turnOffAuthorisationSystem(); + org.dspace.handle.Handle handle = ClarinHandleBuilder + .createHandle(context, prefix + "notoken", urlWithoutToken) + .build(); + context.commit(); + context.restoreAuthSystemState(); + getClient(getAuthToken(admin.getEmail(), password)).perform(get("/api/core/handles/" + handle.getID())) + .andExpect(status().isOk()); + + allowAnyUrl(); + Map update = Map.of("handle", handle.getHandle(), "token", "token0", "url", TEST_URL); + getClient().perform(put("/api/services/handles") + .content(mapper.writeValueAsBytes(update)) + .contentType(contentType)) + .andExpect(status().isNotFound()); + } + @Test public void noMagicInCreateResponse() throws Exception { String url = "https://lindat.mff.cuni.cz/#!/services/pmltq/"; @@ -381,6 +444,26 @@ private MvcResult updateHandle(String authToken, Map updatedHand ; } + /** + * Update the url as admin, then check that the token is stored but is not in the response. + */ + private void patchUrlAndCheckToken(org.dspace.handle.Handle handle, String url, String token) throws Exception { + Map values = Map.of("handle", handle.getHandle(), "url", url, "archive", "false"); + getClient(getAuthToken(admin.getEmail(), password)) + .perform(patch("/api/core/handles/" + handle.getID()) + .content(getPatchContent(List.of(new ReplaceOperation("/updateHandle", values)))) + .contentType(jakarta.ws.rs.core.MediaType.APPLICATION_JSON_PATCH_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(not(containsString(token)))); + Assert.assertTrue("Token should be stored in the database", + handleClarinService.findByHandle(context, handle.getHandle()).getUrl().contains(token)); + } + + private void allowAnyUrl() { + configurationService.setProperty(urlBlacklistConfig, null); + configurationService.setProperty(hostBlacklistConfig, null); + } + private MvcResult createHandle(String authToken, Map handleJson) throws Exception { return getClient(authToken).perform(post("/api/services/handles") .content(mapper.writeValueAsBytes(handleJson)) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/HandleRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/HandleRestRepositoryIT.java index e963f037d5b2..af426758cb46 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/HandleRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/HandleRestRepositoryIT.java @@ -126,7 +126,9 @@ public void findAll() throws Exception { public void findOne() throws Exception { Handle handle = publicItem.getHandles().get(0); - getClient().perform(get(HANDLES_ENDPOINT + handle.getID())) + String adminToken = getAuthToken(admin.getEmail(), password); + + getClient(adminToken).perform(get(HANDLES_ENDPOINT + handle.getID())) .andExpect(status().isOk()) .andExpect(content().contentType(contentType)) .andExpect(jsonPath("$", Matchers.is( @@ -139,16 +141,53 @@ public void findOne() throws Exception { } @Test - public void deleteSuccess() throws Exception { + public void findAllUnauthorized() throws Exception { + getClient().perform(get("/api/core/handles")) + .andExpect(status().isUnauthorized()); + this.cleanHandles(); + } + + @Test + public void findAllForbidden() throws Exception { + String epersonToken = getAuthToken(eperson.getEmail(), password); + + getClient(epersonToken).perform(get("/api/core/handles")) + .andExpect(status().isForbidden()); + this.cleanHandles(); + } + + @Test + public void findOneUnauthorized() throws Exception { Handle handle = publicItem.getHandles().get(0); getClient().perform(get(HANDLES_ENDPOINT + handle.getID())) + .andExpect(status().isUnauthorized()); + this.cleanHandles(); + } + + @Test + public void findOneForbidden() throws Exception { + Handle handle = publicItem.getHandles().get(0); + String epersonToken = getAuthToken(eperson.getEmail(), password); + + getClient(epersonToken).perform(get(HANDLES_ENDPOINT + handle.getID())) + .andExpect(status().isForbidden()); + this.cleanHandles(); + } + + @Test + public void deleteSuccess() throws Exception { + Handle handle = publicItem.getHandles().get(0); + + String adminToken = getAuthToken(admin.getEmail(), password); + + getClient(adminToken).perform(get(HANDLES_ENDPOINT + handle.getID())) .andExpect(status().isOk()); - getClient(getAuthToken(admin.getEmail(), password)) + getClient(adminToken) .perform(delete(HANDLES_ENDPOINT + handle.getID())) .andExpect(status().isNoContent()); - getClient().perform(get(HANDLES_ENDPOINT + handle.getID())) + getClient(adminToken).perform(get(HANDLES_ENDPOINT + handle.getID())) .andExpect(status().isNotFound()); //Item handle was deleted in test From 62437c341aa72e6af4ffd49fc41ccf9c79cebf39 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 19:32:45 +0100 Subject: [PATCH 2/2] Read the magic handle token only from a complete magic URL The constructor uses the same format check as hideToken, a magic URL must start with the marker as in HandlePlugin, and a blank token never matches. Co-Authored-By: Claude Opus 5.5 --- .../handle/HandleClarinServiceImpl.java | 5 +- .../org/dspace/handle/external/Handle.java | 11 ++-- .../rest/ExternalHandleRestRepositoryIT.java | 50 +++++++++++++++++-- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java b/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java index 44b6232f21f7..873221b5f0a0 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandleClarinServiceImpl.java @@ -477,8 +477,11 @@ public Handle createHandle(Context context, String handleStr) throws SQLExceptio @Override public Handle findByHandleAndMagicToken(Context context, String handle, String token) throws SQLException { + if (StringUtils.isBlank(token)) { + return null; + } Handle h = findByHandle(context, handle); - if (Objects.isNull(h) || Objects.isNull(h.getUrl()) || !h.getUrl().contains(MAGIC_BEAN)) { + if (Objects.isNull(h) || Objects.isNull(h.getUrl()) || !h.getUrl().startsWith(MAGIC_BEAN)) { return null; } org.dspace.handle.external.Handle magicHandle = diff --git a/dspace-api/src/main/java/org/dspace/handle/external/Handle.java b/dspace-api/src/main/java/org/dspace/handle/external/Handle.java index 4e250545281a..d35ac7ffe2f3 100644 --- a/dspace-api/src/main/java/org/dspace/handle/external/Handle.java +++ b/dspace-api/src/main/java/org/dspace/handle/external/Handle.java @@ -87,8 +87,10 @@ public Handle(String handle, String magicURL) { if (isNotBlank(splits[7])) { this.query = splits[7]; } - if (isNotBlank(splits[TOKEN_INDEX])) { - this.token = splits[TOKEN_INDEX]; + // same check as hideToken: in an incomplete magic URL the last part is the target url, not a token + String[] complete = splitMagicUrl(magicURL); + if (Objects.nonNull(complete) && isNotBlank(complete[TOKEN_INDEX])) { + this.token = complete[TOKEN_INDEX]; } this.subprefix = handle.split("/",2)[1].split("-",2)[0]; } @@ -124,13 +126,14 @@ public static String restoreToken(String url, String storedUrl) { } private static String[] splitMagicUrl(String url) { - if (isBlank(url) || !url.contains(MAGIC_BEAN)) { + // as HandlePlugin: only a url that starts with the marker is a magic URL + if (isBlank(url) || !url.startsWith(MAGIC_BEAN)) { return null; } String[] splits = url.split(MAGIC_BEAN, MAGIC_URL_PARTS); if (splits.length != MAGIC_URL_PARTS) { // the url is not logged, it may contain the token - log.warn("Magic URL has {} parts instead of {}, it is used unchanged.", splits.length, MAGIC_URL_PARTS); + log.warn("Magic URL has {} parts instead of {}, it has no token.", splits.length, MAGIC_URL_PARTS); return null; } return splits; diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java index 28e40afa098c..f5b5c8a362e6 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/ExternalHandleRestRepositoryIT.java @@ -327,6 +327,8 @@ public void tokenSetByAdminIsHiddenAndWorksForUpdate() throws Exception { patchUrlAndCheckToken(handle, handle.getUrl().replace("token0", "adminToken"), "adminToken"); allowAnyUrl(); + // a new token from the admin is how the old one is withdrawn + updateHandleIsNotFound(handle.getHandle(), "token0"); updateHandle(null, Map.of("handle", handle.getHandle(), "token", "adminToken", "url", TEST_URL)); } @@ -353,11 +355,41 @@ public void updateOfHandleWithoutTokenIsNotFound() throws Exception { .andExpect(status().isOk()); allowAnyUrl(); - Map update = Map.of("handle", handle.getHandle(), "token", "token0", "url", TEST_URL); - getClient().perform(put("/api/services/handles") - .content(mapper.writeValueAsBytes(update)) - .contentType(contentType)) - .andExpect(status().isNotFound()); + updateHandleIsNotFound(handle.getHandle(), "token0"); + Assert.assertNull("A missing token must not match an empty token slot", + handleClarinService.findByHandleAndMagicToken(context, handle.getHandle(), null)); + } + + @Test + public void updateOfHandleWithIncompleteMagicUrlIsNotFound() throws Exception { + // no token part: the last part is the target url, as HandlePlugin reads it + String targetUrl = "https://example.org/old"; + String incompleteUrl = handlesWithMagicURLs.get(0).getUrl() + .replace("token0" + ExternalHandleConstants.MAGIC_BEAN + "url0", targetUrl); + context.turnOffAuthorisationSystem(); + org.dspace.handle.Handle handle = ClarinHandleBuilder + .createHandle(context, prefix + "incomplete", incompleteUrl) + .build(); + context.commit(); + context.restoreAuthSystemState(); + + allowAnyUrl(); + updateHandleIsNotFound(handle.getHandle(), targetUrl); + } + + @Test + public void updateOfHandleWithMagicMarkerInsideUrlIsNotFound() throws Exception { + // HandlePlugin resolves this as a plain url, so it is not an external handle with a token + String url = TEST_URL + handlesWithMagicURLs.get(0).getUrl(); + context.turnOffAuthorisationSystem(); + org.dspace.handle.Handle handle = ClarinHandleBuilder + .createHandle(context, prefix + "inside", url) + .build(); + context.commit(); + context.restoreAuthSystemState(); + + allowAnyUrl(); + updateHandleIsNotFound(handle.getHandle(), "token0"); } @Test @@ -459,6 +491,14 @@ private void patchUrlAndCheckToken(org.dspace.handle.Handle handle, String url, handleClarinService.findByHandle(context, handle.getHandle()).getUrl().contains(token)); } + private void updateHandleIsNotFound(String handle, String token) throws Exception { + Map update = Map.of("handle", handle, "token", token, "url", TEST_URL); + getClient().perform(put("/api/services/handles") + .content(mapper.writeValueAsBytes(update)) + .contentType(contentType)) + .andExpect(status().isNotFound()); + } + private void allowAnyUrl() { configurationService.setProperty(urlBlacklistConfig, null); configurationService.setProperty(hostBlacklistConfig, null);