Skip to content

Commit

Permalink
Merged in task/dspace-cris-2023_02_x/DSC-1766_csrf_fix (pull request D…
Browse files Browse the repository at this point in the history
…Space#2947)

[DSC-1766] Add security csrf endpoint to user-agreement allow list

Approved-by: Vincenzo Mecca
  • Loading branch information
atarix83 authored and vins01-4science committed Oct 25, 2024
2 parents 5bf6c6e + b172a17 commit 0163d5c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -236,6 +238,76 @@ public void tryToAccessToItemRestEndpointUserThatCanIgnoreTermsTest() throws Exc
configurationService.setProperty("user-agreement.enabled", "false");
}


@Test
public void shouldBeAbleToAccessCSRFTokenEndpoint() throws Exception {

context.turnOffAuthorisationSystem();

configurationService.setProperty("user-agreement.enabled", "true");

EPerson user =
EPersonBuilder.createEPerson(context)
.withEmail("[email protected]")
.withNameInMetadata("Vins", "1st")
.withCanLogin(true)
.withPassword(password)
.build();

context.restoreAuthSystemState();

String userToken = getAuthToken(user.getEmail(), password);
getClient(userToken).perform(get("/api/security/csrf"))
.andExpect(status().isNoContent())
.andExpect(cookie().exists(DSpaceCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME))
.andExpect(header().exists(DSpaceCsrfTokenRepository.DSPACE_CSRF_HEADER_NAME));

context.turnOffAuthorisationSystem();

user = context.reloadEntity(user);
// ignore user agreement
ePersonService.addMetadata(context, user, "dspace", "agreements", "ignore", "en", "true");
context.commit();

context.restoreAuthSystemState();

getClient(userToken).perform(get("/api/security/csrf"))
.andExpect(status().isNoContent())
.andExpect(cookie().exists(DSpaceCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME))
.andExpect(header().exists(DSpaceCsrfTokenRepository.DSPACE_CSRF_HEADER_NAME));

context.turnOffAuthorisationSystem();

user = context.reloadEntity(user);
// refuse user agreement
ePersonService.setMetadataSingleValue(context, user, "dspace", "agreements", "ignore", "en", "false");
ePersonService.addMetadata(context, user, "dspace", "agreements", "end-user", "en", "false");
context.commit();

context.restoreAuthSystemState();

getClient(userToken).perform(get("/api/security/csrf"))
.andExpect(status().isNoContent())
.andExpect(cookie().exists(DSpaceCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME))
.andExpect(header().exists(DSpaceCsrfTokenRepository.DSPACE_CSRF_HEADER_NAME));

context.turnOffAuthorisationSystem();

user = context.reloadEntity(user);
// accept user agreement
ePersonService.setMetadataSingleValue(context, user, "dspace", "agreements", "end-user", "en", "true");
context.commit();

context.restoreAuthSystemState();

getClient(userToken).perform(get("/api/security/csrf"))
.andExpect(status().isNoContent())
.andExpect(cookie().exists(DSpaceCsrfTokenRepository.DEFAULT_CSRF_COOKIE_NAME))
.andExpect(header().exists(DSpaceCsrfTokenRepository.DSPACE_CSRF_HEADER_NAME));

}


private void resetOpenPathConfigurations(String[] values) {
configurationService.getConfiguration().clearProperty("user-agreement.open-path-patterns");
if (values != null) {
Expand Down
1 change: 1 addition & 0 deletions dspace/config/dspace.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,7 @@ user-agreement.open-path-patterns = /api/core/sites/**
user-agreement.open-path-patterns = /api/eperson/epersons/**
user-agreement.open-path-patterns = /api/eperson/groups/**
user-agreement.open-path-patterns = /api/eperson/registrations/**
user-agreement.open-path-patterns = /api/security/csrf

################################################
context-menu-entry.audit.enabled = true
Expand Down

0 comments on commit 0163d5c

Please sign in to comment.