forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'w2p-101573_CSRF-endpoint' into w2p-111801_CSRF-bugfix-7.6
- Loading branch information
Showing
5 changed files
with
185 additions
and
5 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
dspace-server-webapp/src/main/java/org/dspace/app/rest/SecurityRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.app.rest; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.dspace.app.rest.security.DSpaceCsrfTokenRepository; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* Rest controller handling security related requests | ||
*/ | ||
@RequestMapping(value = "/api/security") | ||
@RestController | ||
public class SecurityRestController { | ||
DSpaceCsrfTokenRepository csrfTokenRepository = new DSpaceCsrfTokenRepository(); | ||
|
||
/** | ||
* The csrf endpoint checks the current csrf cookie and header, resetting it if any of the two are missing, or | ||
* they don't match anymore | ||
*/ | ||
@RequestMapping(value = "/csrf", method = RequestMethod.POST) | ||
public ResponseEntity csrf(HttpServletRequest request, HttpServletResponse response) { | ||
csrfTokenRepository.saveNewTokenWhenCookieAndHeaderDontMatch(request, response); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
dspace-server-webapp/src/test/java/org/dspace/app/rest/SecurityRestControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* The contents of this file are subject to the license and copyright | ||
* detailed in the LICENSE and NOTICE files at the root of the source | ||
* tree and available online at | ||
* | ||
* http://www.dspace.org/license/ | ||
*/ | ||
package org.dspace.app.rest; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import javax.servlet.http.Cookie; | ||
import javax.ws.rs.core.HttpHeaders; | ||
|
||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest; | ||
import org.hamcrest.core.StringContains; | ||
import org.junit.Test; | ||
|
||
public class SecurityRestControllerIT extends AbstractControllerIntegrationTest { | ||
|
||
@Test | ||
public void testCsrfEndpointWithoutCookieOrHeader() throws Exception { | ||
String token = getAuthToken(admin.getEmail(), password); | ||
|
||
getClient(token).perform(post("/api/security/csrf")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string( | ||
HttpHeaders.SET_COOKIE, | ||
StringContains.containsString("DSPACE-XSRF-COOKIE") | ||
)); | ||
} | ||
|
||
@Test | ||
public void testCsrfEndpointWithoutHeader() throws Exception { | ||
String token = getAuthToken(admin.getEmail(), password); | ||
|
||
getClient(token).perform(post("/api/security/csrf").cookie( | ||
new Cookie("DSPACE-XSRF-COOKIE", "5f12f98b-5de3-4ee1-bd5f-95b784bd17cf"))) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string( | ||
HttpHeaders.SET_COOKIE, | ||
StringContains.containsString("DSPACE-XSRF-COOKIE") | ||
)); | ||
} | ||
|
||
@Test | ||
public void testCsrfEndpointWithoutCookie() throws Exception { | ||
String token = getAuthToken(admin.getEmail(), password); | ||
|
||
getClient(token).perform(post("/api/security/csrf").header("X-XSRF-TOKEN", | ||
"5f12f98b-5de3-4ee1-bd5f-95b784bd17cf")) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string( | ||
HttpHeaders.SET_COOKIE, | ||
StringContains.containsString("DSPACE-XSRF-COOKIE") | ||
)); | ||
} | ||
|
||
@Test | ||
public void testCsrfEndpointMismatchingCookieAndHeader() throws Exception { | ||
String token = getAuthToken(admin.getEmail(), password); | ||
|
||
getClient(token).perform(post("/api/security/csrf").header("X-XSRF-TOKEN", | ||
"5f12f98b-5de3-4ee1-bd5f-95b784bd17cf").cookie( | ||
new Cookie("DSPACE-XSRF-COOKIE", "1427c36d-6d4c-423a-939d-8ddf0115b5c6"))) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string( | ||
HttpHeaders.SET_COOKIE, | ||
StringContains.containsString("DSPACE-XSRF-COOKIE") | ||
)); | ||
} | ||
|
||
@Test | ||
public void testCsrfEndpointMatchingCookieAndHeader() throws Exception { | ||
String token = getAuthToken(admin.getEmail(), password); | ||
|
||
getClient(token).perform(post("/api/security/csrf").header("X-XSRF-TOKEN", | ||
"5f12f98b-5de3-4ee1-bd5f-95b784bd17cf").cookie( | ||
new Cookie("DSPACE-XSRF-COOKIE", "5f12f98b-5de3-4ee1-bd5f-95b784bd17cf"))) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().doesNotExist(HttpHeaders.SET_COOKIE)); | ||
} | ||
|
||
} |