From ac3d2549b938e5814885505f8e8997f832e205be Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Sat, 17 Oct 2026 11:50:26 +0200 Subject: [PATCH 1/4] Derive the temporary auth cookie path from the request context path The cookie was always issued with Path=/server/api/authn, so under another context path (e.g. /repository/server) the browser never sent it to /api/authn/login and the Shibboleth/OIDC/ORCID/SAML login ended as anonymous. The removal cookie now carries the same path. Co-Authored-By: Claude Fable 5.1 --- ...JWTTokenRestAuthenticationServiceImpl.java | 16 +++++++- .../rest/AuthenticationRestControllerIT.java | 38 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java index 7fe1b1747cd8..b6aed03bdbd9 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java @@ -180,7 +180,8 @@ public void invalidateAuthenticationData(HttpServletRequest request, HttpServlet public void invalidateAuthenticationCookie(HttpServletRequest request, HttpServletResponse response) { // Re-send the same cookie (as addTokenToResponse()) with no value and a Max-Age of 0 seconds ResponseCookie cookie = ResponseCookie.from(AUTHORIZATION_COOKIE, "") - .maxAge(0).httpOnly(true).secure(true).sameSite("None").build(); + .maxAge(0).httpOnly(true).secure(true).sameSite("None") + .path(getAuthorizationCookiePath(request)).build(); // Write the cookie to the Set-Cookie header in order to send it response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString()); @@ -265,7 +266,7 @@ private void addTokenToResponse(final HttpServletRequest request, final HttpServ .httpOnly(true) .secure(true) .sameSite("None") - .path("/server/api/authn") + .path(getAuthorizationCookiePath(request)) .build(); // Write the cookie to the Set-Cookie header in order to send it @@ -312,6 +313,17 @@ private String getShortLivedToken(HttpServletRequest request) { return tokenValue; } + /** + * Path of the temporary authorization cookie: the /api/authn endpoints under the current context path. + * The path must match the URL the browser calls, otherwise the browser never sends the cookie back + * (e.g. when the REST API runs under /repository/server instead of /server). + * @param request current request + * @return cookie path + */ + private String getAuthorizationCookiePath(HttpServletRequest request) { + return request.getContextPath() + "/api/authn"; + } + /** * Get the value of the (temporary) authorization cookie, if exists. * @param request current request diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java index 576864d4f137..460bed829fe4 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java @@ -357,6 +357,42 @@ public void testStatusNotAuthenticated() throws Exception { .andExpect(jsonPath("$._embedded").doesNotExist()); } + /** + * The temporary auth cookie and its removal carry the /api/authn path under the real context path, + * otherwise a browser never sends the cookie back when the REST API runs behind a path prefix. + */ + @Test + public void testShibAuthCookiePathFollowsContextPath() throws Exception { + setAuthenticationMethodSequence(SHIB_ONLY); + + String uiURL = configurationService.getProperty("dspace.ui.url"); + String contextPath = "/repository/server"; + + // The temporary auth cookie is scoped to /api/authn under the context path the browser sees. + Cookie authCookie = getClient().perform(get(contextPath + "/api/authn/shibboleth") + .contextPath(contextPath) + .header("Referer", "https://myshib.example.com") + .param("redirectUrl", uiURL) + .requestAttr("SHIB-MAIL", eperson.getEmail()) + .requestAttr("SHIB-SCOPED-AFFILIATION", "faculty;staff")) + .andExpect(status().is3xxRedirection()) + .andExpect(cookie().exists(AUTHORIZATION_COOKIE)) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, contextPath + "/api/authn")) + .andReturn().getResponse().getCookie(AUTHORIZATION_COOKIE); + + // /login destroys the cookie with the same path, otherwise the browser would keep the old one. + getClient().perform(post(contextPath + "/api/authn/login") + .contextPath(contextPath) + .header("Origin", uiURL) + .secure(true) + .cookie(authCookie)) + .andExpect(status().isOk()) + .andExpect(cookie().value(AUTHORIZATION_COOKIE, "")) + .andExpect(cookie().maxAge(AUTHORIZATION_COOKIE, 0)) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, contextPath + "/api/authn")) + .andExpect(header().exists(AUTHORIZATION_HEADER)); + } + @Test public void testStatusShibAuthenticatedWithCookie() throws Exception { //Enable Shibboleth login only @@ -387,6 +423,7 @@ public void testStatusShibAuthenticatedWithCookie() throws Exception { .andExpect(cookie().doesNotExist("DSPACE-XSRF-COOKIE")) .andExpect(header().doesNotExist("DSPACE-XSRF-TOKEN")) .andExpect(cookie().exists(AUTHORIZATION_COOKIE)) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, "/api/authn")) .andReturn().getResponse().getCookie(AUTHORIZATION_COOKIE); // Verify the temporary cookie now exists & obtain its token for use below @@ -433,6 +470,7 @@ public void testStatusShibAuthenticatedWithCookie() throws Exception { .andExpect(status().isOk()) // Verify the Auth cookie has been destroyed .andExpect(cookie().value(AUTHORIZATION_COOKIE, "")) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, "/api/authn")) // Verify Authorization header is returned .andExpect(header().exists(AUTHORIZATION_HEADER)) // Verify that the CSRF token has been changed From 2cfd947e7cb7518e5a2bafd958385a3aced8d280 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 19:27:24 +0100 Subject: [PATCH 2/4] Take the temporary auth cookie path from dspace.server.url The login callbacks and the UI use dspace.server.url, so the cookie path follows it also when the servlet context path is different, e.g. behind a proxy. Co-Authored-By: Claude Opus 5.5 --- ...JWTTokenRestAuthenticationServiceImpl.java | 20 +++++++++++-------- .../rest/AuthenticationRestControllerIT.java | 20 ++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java index b6aed03bdbd9..d85536c74814 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java @@ -8,6 +8,7 @@ package org.dspace.app.rest.security.jwt; import java.io.IOException; +import java.net.URI; import java.sql.SQLException; import java.text.ParseException; import java.util.Iterator; @@ -33,6 +34,7 @@ import org.dspace.core.Context; import org.dspace.eperson.EPerson; import org.dspace.eperson.service.EPersonService; +import org.dspace.services.ConfigurationService; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; @@ -70,6 +72,9 @@ public class JWTTokenRestAuthenticationServiceImpl implements RestAuthentication @Autowired ClarinTokenService clarinTokenService; + @Autowired + private ConfigurationService configurationService; + @Lazy @Autowired private DSpaceCsrfAuthenticationStrategy dspaceCsrfAuthenticationStrategy; @@ -181,7 +186,7 @@ public void invalidateAuthenticationCookie(HttpServletRequest request, HttpServl // Re-send the same cookie (as addTokenToResponse()) with no value and a Max-Age of 0 seconds ResponseCookie cookie = ResponseCookie.from(AUTHORIZATION_COOKIE, "") .maxAge(0).httpOnly(true).secure(true).sameSite("None") - .path(getAuthorizationCookiePath(request)).build(); + .path(getAuthorizationCookiePath()).build(); // Write the cookie to the Set-Cookie header in order to send it response.addHeader(HttpHeaders.SET_COOKIE, cookie.toString()); @@ -266,7 +271,7 @@ private void addTokenToResponse(final HttpServletRequest request, final HttpServ .httpOnly(true) .secure(true) .sameSite("None") - .path(getAuthorizationCookiePath(request)) + .path(getAuthorizationCookiePath()) .build(); // Write the cookie to the Set-Cookie header in order to send it @@ -314,14 +319,13 @@ private String getShortLivedToken(HttpServletRequest request) { } /** - * Path of the temporary authorization cookie: the /api/authn endpoints under the current context path. - * The path must match the URL the browser calls, otherwise the browser never sends the cookie back - * (e.g. when the REST API runs under /repository/server instead of /server). - * @param request current request + * Path of the temporary authorization cookie: /api/authn under the path of dspace.server.url. + * The login callbacks and the UI use that public URL, so the browser sends the cookie back to /login + * even when the REST API runs under another path than /server (e.g. /repository/server). * @return cookie path */ - private String getAuthorizationCookiePath(HttpServletRequest request) { - return request.getContextPath() + "/api/authn"; + private String getAuthorizationCookiePath() { + return URI.create(configurationService.getProperty("dspace.server.url")).getPath() + "/api/authn"; } /** diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java index 460bed829fe4..65e8f02fd646 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java @@ -358,17 +358,23 @@ public void testStatusNotAuthenticated() throws Exception { } /** - * The temporary auth cookie and its removal carry the /api/authn path under the real context path, - * otherwise a browser never sends the cookie back when the REST API runs behind a path prefix. + * The temporary auth cookie and its removal carry /api/authn under the path of dspace.server.url, also when + * the servlet context path differs from it (a proxy in front of the REST API). Otherwise a browser never + * sends the cookie back to /login. */ @Test - public void testShibAuthCookiePathFollowsContextPath() throws Exception { + public void testShibAuthCookiePathFollowsServerUrl() throws Exception { setAuthenticationMethodSequence(SHIB_ONLY); + assertShibAuthCookiePath("https://dspace.example.com/server", "", "/server/api/authn"); + assertShibAuthCookiePath("https://dspace.example.com/repository/server", "/server", + "/repository/server/api/authn"); + } + + private void assertShibAuthCookiePath(String serverUrl, String contextPath, String cookiePath) throws Exception { + configurationService.setProperty("dspace.server.url", serverUrl); String uiURL = configurationService.getProperty("dspace.ui.url"); - String contextPath = "/repository/server"; - // The temporary auth cookie is scoped to /api/authn under the context path the browser sees. Cookie authCookie = getClient().perform(get(contextPath + "/api/authn/shibboleth") .contextPath(contextPath) .header("Referer", "https://myshib.example.com") @@ -377,7 +383,7 @@ public void testShibAuthCookiePathFollowsContextPath() throws Exception { .requestAttr("SHIB-SCOPED-AFFILIATION", "faculty;staff")) .andExpect(status().is3xxRedirection()) .andExpect(cookie().exists(AUTHORIZATION_COOKIE)) - .andExpect(cookie().path(AUTHORIZATION_COOKIE, contextPath + "/api/authn")) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, cookiePath)) .andReturn().getResponse().getCookie(AUTHORIZATION_COOKIE); // /login destroys the cookie with the same path, otherwise the browser would keep the old one. @@ -389,7 +395,7 @@ public void testShibAuthCookiePathFollowsContextPath() throws Exception { .andExpect(status().isOk()) .andExpect(cookie().value(AUTHORIZATION_COOKIE, "")) .andExpect(cookie().maxAge(AUTHORIZATION_COOKIE, 0)) - .andExpect(cookie().path(AUTHORIZATION_COOKIE, contextPath + "/api/authn")) + .andExpect(cookie().path(AUTHORIZATION_COOKIE, cookiePath)) .andExpect(header().exists(AUTHORIZATION_HEADER)); } From 0421563d0d6216a067b61525349b85090b145e53 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 19:39:56 +0100 Subject: [PATCH 3/4] Ignore a trailing slash of dspace.server.url in the auth cookie path Co-Authored-By: Claude Opus 5.5 --- .../jwt/JWTTokenRestAuthenticationServiceImpl.java | 3 ++- .../app/rest/AuthenticationRestControllerIT.java | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java index d85536c74814..8062972d12b0 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java @@ -325,7 +325,8 @@ private String getShortLivedToken(HttpServletRequest request) { * @return cookie path */ private String getAuthorizationCookiePath() { - return URI.create(configurationService.getProperty("dspace.server.url")).getPath() + "/api/authn"; + String serverPath = URI.create(configurationService.getProperty("dspace.server.url")).getPath(); + return StringUtils.removeEnd(serverPath, "/") + "/api/authn"; } /** diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java index 65e8f02fd646..74986eece764 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java @@ -366,9 +366,15 @@ public void testStatusNotAuthenticated() throws Exception { public void testShibAuthCookiePathFollowsServerUrl() throws Exception { setAuthenticationMethodSequence(SHIB_ONLY); - assertShibAuthCookiePath("https://dspace.example.com/server", "", "/server/api/authn"); - assertShibAuthCookiePath("https://dspace.example.com/repository/server", "/server", - "/repository/server/api/authn"); + String serverUrl = configurationService.getProperty("dspace.server.url"); + try { + assertShibAuthCookiePath("https://dspace.example.com/server", "", "/server/api/authn"); + assertShibAuthCookiePath("https://dspace.example.com/server/", "", "/server/api/authn"); + assertShibAuthCookiePath("https://dspace.example.com/repository/server", "/server", + "/repository/server/api/authn"); + } finally { + configurationService.setProperty("dspace.server.url", serverUrl); + } } private void assertShibAuthCookiePath(String serverUrl, String contextPath, String cookiePath) throws Exception { From 569cfe1114597db6547c51386c7d5d1e5189c536 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 26 Nov 2026 19:48:05 +0100 Subject: [PATCH 4/4] Keep percent escapes of dspace.server.url in the auth cookie path Co-Authored-By: Claude Opus 5.5 --- .../security/jwt/JWTTokenRestAuthenticationServiceImpl.java | 2 +- .../org/dspace/app/rest/AuthenticationRestControllerIT.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java index 8062972d12b0..9c55231d25d5 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenRestAuthenticationServiceImpl.java @@ -325,7 +325,7 @@ private String getShortLivedToken(HttpServletRequest request) { * @return cookie path */ private String getAuthorizationCookiePath() { - String serverPath = URI.create(configurationService.getProperty("dspace.server.url")).getPath(); + String serverPath = URI.create(configurationService.getProperty("dspace.server.url")).getRawPath(); return StringUtils.removeEnd(serverPath, "/") + "/api/authn"; } diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java index 74986eece764..7d8a1844b843 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java @@ -372,6 +372,8 @@ public void testShibAuthCookiePathFollowsServerUrl() throws Exception { assertShibAuthCookiePath("https://dspace.example.com/server/", "", "/server/api/authn"); assertShibAuthCookiePath("https://dspace.example.com/repository/server", "/server", "/repository/server/api/authn"); + assertShibAuthCookiePath("https://dspace.example.com/caf%C3%A9/server", "/server", + "/caf%C3%A9/server/api/authn"); } finally { configurationService.setProperty("dspace.server.url", serverUrl); }