Repository navigation
COG-1159: W1 · Add GET /api/csrf token-bootstrap endpoint (204) #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
patrickbradley-cog
wants to merge
1
commit into
master
Choose a base branch
from
devin/cog-1159-w1-add-get-apicsrf-token-bootstrap-endpoint-204
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
UserFront/src/main/java/com/userFront/resource/CsrfResource.java
This file contains hidden or 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,23 @@ | ||
| package com.userFront.resource; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.web.csrf.CsrfToken; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| /** | ||
| * Safe, unauthenticated request a client can issue before its first state-changing call | ||
| * so that the CSRF token repository writes the XSRF-TOKEN cookie. While CSRF protection | ||
| * is disabled in SecurityConfig no token is bound to the request and no cookie is written. | ||
| */ | ||
| @RestController | ||
| public class CsrfResource { | ||
|
|
||
| @GetMapping("/api/csrf") | ||
| public ResponseEntity<Void> csrf(CsrfToken token) { | ||
| if (token != null) { | ||
| token.getToken(); | ||
| } | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
UserFront/src/test/java/com/userFront/resource/CsrfResourceTest.java
This file contains hidden or 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,38 @@ | ||
| package com.userFront.resource; | ||
|
|
||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.test.context.junit4.SpringRunner; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
|
|
||
| import com.userFront.config.SecurityConfig; | ||
| import com.userFront.service.UserServiceImpl.UserSecurityService; | ||
|
|
||
| @RunWith(SpringRunner.class) | ||
| @WebMvcTest(CsrfResource.class) | ||
| @Import(SecurityConfig.class) | ||
| public class CsrfResourceTest { | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @MockBean | ||
| private UserSecurityService userSecurityService; | ||
|
|
||
| /** | ||
| * Only the status is asserted: the XSRF-TOKEN cookie is written once CSRF protection | ||
| * is enabled with a cookie-based token repository, and is absent while it is disabled. | ||
| */ | ||
| @Test | ||
| public void csrfEndpointReturnsNoContentForAnonymousClient() throws Exception { | ||
| mockMvc.perform(get("/api/csrf")) | ||
| .andExpect(status().isNoContent()); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔍 Bootstrap cookie remains untested
The status-only test passes without issuing a token. When CSRF protection is enabled, a separate test must verify that an anonymous request receives the XSRF-TOKEN cookie.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional, per COG-1159. CSRF is still disabled on Boot 1.5.4, so the ticket scopes this test to the 204 only. A cookie assertion would fail today. Asserting that
XSRF-TOKENis set belongs with W2, whereSecurityConfigswitches toCookieCsrfTokenRepository. I checked that flow manually on a throwaway local build:GET /api/csrfreturned 204 withSet-Cookie: XSRF-TOKEN=…. The details are in the PR description.